//<script>
expRegAlfa =/^([0-9]|[a-z]|[ íìáàãâéèêõóòôúùûç])(([0-9]|[a-z]|[ íìáàãâéèêõóòôúùûç])?)*$/i;
aux=0;


function xmlhttpPost(strURL, strSubmit, strResultFunc, div) {

       var xmlHttpReq = false;
       
       // Mozilla/Safari
       if (window.XMLHttpRequest) {
               xmlHttpReq = new XMLHttpRequest();
               if(xmlHttpReq.overrideMimeType) {
               	xmlHttpReq.overrideMimeType('text/html');
               }
       }
       // IE
       else if (window.ActiveXObject) {
               xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
       }

	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
	        if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
	                //alert(xmlHttpReq.responseText);
					eval(strResultFunc + '(xmlHttpReq.responseText,div);');
					
	        }
	}

	xmlHttpReq.send(strSubmit);

}
function muda_resultR(value,div)
{
	
	document.getElementById(div).innerHTML = value;
}
function mudaThumbs(numfotos,valor) 
{
	//alert();
	var data_string = "numfotos=" + numfotos + "&valor=" + valor;
	xmlhttpPost('mudaThumbs.php', data_string, 'muda_resultR','mudaThumbs');
}
function ChSubmenu(onmouse,numRows,valor,menu,lang) 
{
	if (aux!=0)
	{
		
		if (document.getElementById("submenuP").value=="")
		{
			document.getElementById("submenuP").value=aux;
			}
		
	}
	if (document.getElementById("menuP").value==menu)
	{
		document.getElementById("menuP").value="";
		var data_string = "codigo=" + valor + "&menu=" + menu + "&lang=" + lang + "&onmouse=0&mapa=" + aux;
		document.getElementById("hrefMenu"+menu).style.color = 'black';
	}
	else
	{
		document.getElementById("menuP").value=menu;
		var data_string = "codigo=" + valor + "&menu=" + menu + "&lang=" + lang + "&onmouse=" + onmouse + "&rows=" + numRows + "&mapa=" + aux;
		
		//alert("hrefMenu"+menu);
		for (i=1;i<=numRows;i++)
		{
			if (i!=menu)
				document.getElementById("hrefMenu"+i).style.color = 'black';
		}
	
		document.getElementById("hrefMenu"+menu).style.color = 'white';
	}
	aux=0;
	//alert(data_string);
	xmlhttpPost('subMenu.php', data_string, 'muda_resultR','submenu');
	
	
}

function chMain(div,cod,lang,titulo,voltar) 
{
	//alert(cod.substr(1));
	
		var data_string = "codigo=" + cod + "&lang=" + lang + "&voltar=" + voltar;
		xmlhttpPost('chMain.php', data_string, 'muda_resultR',div);
		xmlhttpPost('chTitulo.php', data_string, 'muda_resultR','titulo');
	
	//alert(data_string);
	
}

function chMapaMenuSuperior(div,cod,lang,titulo,voltar) 
{
	ChSubmenuOFF();
	chMain(div,cod,lang,titulo,voltar) 
	
}
function chPaginaMapa(codmae,codfilho,numRows,lang,menu,voltar) 
{
	aux=codfilho;
	ChSubmenu(1,numRows,codmae,menu,lang);
	chMain("main",codfilho,lang,"",voltar) ;
}
function chMainFile(div,cod,file,lang,voltar) 
{
	var data_string = "codigo=" + cod + "&lang=" + lang + "&voltar=" + voltar;
	xmlhttpPost(file, data_string, 'muda_resultR',div);
}

function chAno(div,ano,mes,pagina,lang) 
{
	
	var data_string = "lang=" + lang + "&ano=" + ano + "&pagina=" + pagina;
	xmlhttpPost('chMes.php', data_string, 'muda_resultR',div);
	chMes("main2",ano,mes,pagina,lang);
	
}
function chMes(div,ano,mes,pagina,lang) 
{
	//alert (div + ano + lang);
	var data_string = "lang=" + lang + "&ano=" + ano + "&mes=" + mes + "&pagina=" + pagina;
	//alert(voltar);
	xmlhttpPost('chMainLivro.php', data_string, 'muda_resultR',div);
}

function ChSubmenuOFF()
{
	var data_string = "onmouse=0";
	menu=document.getElementById("menuP").value;
	if (menu!="")
	{
		document.getElementById("menuP").value="";
		document.getElementById("hrefMenu"+menu).style.color = 'black';
		xmlhttpPost('subMenu.php', data_string, 'muda_resultR','submenu');
	}
}
function click(id,img,bloco) {
	if(document.getElementById(id).value != 0) {
		document.getElementById(img).src = "imagens/mais.gif";
		document.getElementById(img).alt = "Expandir";
		document.getElementById(bloco).style.display = "none";
		document.getElementById(id).value = 0;
	} else {
		document.getElementById(img).src = "imagens/menos.gif";
		document.getElementById(img).alt = "Esconder / Hide";
		document.getElementById(bloco).style.display = "Block";
		document.getElementById(id).value = 1;
	}
}
function ValidaForm (lang)
{
	var v_coment = document.getElementById("2").value; 
	var v_nome   = document.getElementById("1").value; 
	var v_coment_test = v_coment.toUpperCase();
	var v_nome_test = v_nome.toUpperCase();
        	
	if (trim(v_nome)=="")
    {
		if (lang=="pt")
			alert ("Tem que preencher o Nome.")
		else
			alert ("Please fill the Name Field.")
		document.getElementById("1").focus();
        return;                           
	}
	if (trim(v_coment)=="")
    {
     	if (lang=="pt")
			alert ("Tem que preencher os Comentários.")
		else
			 alert ("Please fill the Commentaries field.")
		document.getElementById("2").focus();
		return; 
	}
	if (v_coment.length>1500)
	{
		if (lang=="pt")
			alert ("Comentário demasiado extenso. Por favor reduza-o.")
		else
			alert ("The Commentary is too extensive. Please reduce it.")
		document.getElementById("2").focus();
		return;                           
	}
	if (trim(document.getElementById("3").value)!="")
	{
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("3").value)))
		{
			if (lang=="pt")
				alert("Introduza um Email correcto para que possa receber resposta ao seu comentário.")
			else
				alert("Please type your Email so that you can recive a reply to your commentary.")
			document.getElementById("3").focus();
			return;
		}
	}

	passou=1;
	for (var i=0; i < v_nome_test.length; i++) {
		aux=v_nome_test.charAt(i)
		if ((aux == "<") || (aux == ">") || (aux == "@") || (aux == ".")) {
			passou=0; 
			break;
		}
	}
	if (passou == 0)
	{
		if (lang=="pt")
			alert ("ATENÇÃO! Caracteres não permitidos.")
		else
			alert ("Words not allowed.")
		document.getElementById("1").focus();
		return;
	}
	passou=1;
	for (var i=0; i < v_coment_test.length; i++) {
		aux=v_coment_test.charAt(i)
		if ((aux == "<") || (aux == ">") || (aux == "@")) {
			passou=0;   
			break;
		}
	}
	if (passou == 0)
	{
		if (lang=="pt")
			alert ("ATENÇÃO! Caracteres não permitidos.")
		else
			alert ("Words not allowed.")
		document.getElementById("2").focus();
		return;
	}
	passou=1;
	v_cidade=document.getElementById("4").value.toUpperCase()
	for (var i=0; i < v_cidade.length; i++) {
		aux=v_cidade.charAt(i)
		if ((aux == "<") || (aux == ">") || (aux == "@") || (aux == ".")) {
			passou=0;   
			break;
		}
	}
	if (passou == 0)
	{
		if (lang=="pt")
			alert ("ATENÇÃO! Caracteres não permitidos.")
		else
			alert ("Words not allowed.")
		document.getElementById("4").focus();
		return;
	}
	passou=1;
	v_pais=document.getElementById("5").value.toUpperCase()
	for (var i=0; i < v_pais.length; i++) {
		aux=v_pais.charAt(i)
		if ((aux == "<") || (aux == ">") || (aux == "@") || (aux == ".")) {
			passou=0;   
			break;
		}
	}
	if (passou == 0)
	{
		if (lang=="pt")
			alert ("ATENÇÃO! Caracteres não permitidos.")
		else
			alert ("Words not allowed.")
		document.getElementById("5").focus();
		return;
	}
					
	var v_final = "";
	var v_insere = "\r\n";
	var v_espaco = "";
	var v_trab = "";
	while (v_coment.length > 60)
		{
		v_trab = v_coment.indexOf(v_insere);
		if (v_trab > 60 || v_trab == -1)
			{
			v_espaco = v_coment.indexOf(' ',60);
			if (v_espaco == -1)
				{
				v_final = v_final + v_coment;
				v_coment = ' ';
				}
			else
				{
				v_final = v_final + v_coment.substring(0,v_espaco) + v_insere;
				if (v_coment.indexOf(v_insere) == v_espaco + 1)
					{
					v_espaco = v_espaco + 2;
					}
				v_coment = v_coment.substring(v_espaco + 1,v_coment.length);
				}
			}
			else
			{
			v_espaco = v_coment.indexOf(v_insere);
			v_final = v_final + v_coment.substring(0,v_espaco);
			v_coment = v_coment.substring(v_espaco + 1,v_coment.length);
			}
		}
	if (v_final != "")
	{
	v_final = v_final + v_coment;
	document.getElementById("2").value = v_final;
	}
	document.getElementById("FrmLivro").submit();
}

function registar(opcao)
{
	avancar=1;
	if (opcao=="esqueceu" || opcao=="alterar" || opcao=="login")
	{
		var data_string = "opcao="+opcao+"&time="+Math.random();
		//alert(opcao);
		if (opcao=="login")
		{
			var user=trim(document.getElementById("user").value);
			var senha=trim(document.getElementById("pwd").value);
			if (user.length!=0 && avancar==1)
			{
				if (validaCampo(user,"Atenção! Campo (User) Inválido!",expRegAlfa,"user")==0)
					avancar=0;
			}
			else
			{
				if (avancar==1)
				{
					alert("Atenção! Deve preencher o campo User.");
					document.getElementById("user").focus();
					avancar=0;
				}
			}
			if (senha.length==0 && avancar==1)
			{	
				alert("Atenção! Deve preencher a Senha");
				document.getElementById("pwd").focus();
				avancar=0;
			}
			if (avancar==1)
			{
				document.login.action="privado/login.php";
				document.login.submit();
			}
		}
		else
			xmlhttpPost('privado/ResLogin.php', data_string, 'muda_resultR','main');
	}
	if (opcao=="novasenha" || opcao=="alterarsenha")
	{
		var user=trim(document.getElementById("user").value);
		//alert(user);
		if (opcao!="novasenha")
			var senha=trim(document.getElementById("senha").value);
		if (user.length!=0 && avancar==1)
		{
			if (validaCampo(user,"Atenção! Campo (User) Inválido!",expRegAlfa,"user")==0)
				avancar=0;
		}
		else
		{
			if (avancar==1)
			{
				alert("Atenção! Deve preencher o campo User.");
				document.getElementById("user").focus();
				avancar=0;
			}
		}
		if (opcao!="novasenha")
			if (senha.length==0 && avancar==1)
			{	
				alert("Atenção! Deve preencher a Senha");
				document.getElementById("senha").focus();
				avancar=0;
			}
		if (opcao=="alterarsenha")
		{
			var senhanew=trim(document.getElementById("senhanew").value);
			if (senhanew.length==0 && avancar==1)
			{	
				alert("Atenção! Deve preencher a Senha Nova");
				document.getElementById("senhanew").focus();
				avancar=0;
			}
		}
		var data_string = "opcao="+opcao +"&user="+user+"&senha="+senha+"&senhanew="+senhanew;
		if (avancar==1){
			xmlhttpPost('privado/exec_login.php', data_string, 'muda_resultR','main');
			//alert(data_string);
		}
			//micoxUpload("frm_registo","exec_login.php","registo","Aguarde...","Erro na criação");
	}
}


