var xmlHttp;
function ajaxFunction()
{
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 // Internet Explorer
	 try
	 {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e)
	  {
	 	 try
	     {
	    	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
}
function checkEmail(email){
	ajaxFunction();
	var url="includes/checkUser.php"
	url=url+"?e="+email
	url=url+"&sid="+Math.random()
	//alert(url);
	xmlHttp.onreadystatechange=function(){
		//alert(url);
		 if(xmlHttp.readyState==4){
	  		//document.getElementById('numchars3').innerHTML =xmlHttp.responseText;
			if(xmlHttp.responseText != ""){
				alert(xmlHttp.responseText);
			}
		}
    }
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

}

function validateFormOnSubmit(theForm) {
var reason = "";
  reason += validateNome(theForm.reg_nome);
  reason += validateEmail(theForm.reg_email);
  reason += validateUsername(theForm.reg_nick);
  reason += validateCondicoes(theForm.reg_condicoes);

  if (reason != "") {
   // alert("Alguns campos estão errados:<br />" + reason);
    //document.getElementById('aviso').style.display= 'block'; 
   //document.getElementById('aviso').innerHTML = "<b>AVISO!</b><br />Alguns campos estão errados:<br />" + reason;
   mostraErro(reason,1)
    return false;
  }

  return true;
}

function validateNome(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Não inseriu o Nome.<br />";
    } else if ((fld.value.length < 6) || (fld.value.length > 60)) {
        fld.style.background = 'Yellow'; 
        error = "O nome é do tamanho errado..<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCondicoes(fld){
	 var error = "";
	 if(fld.checked != true){
	 error = "Para se poder registar, precisa de aceitar os termos e condições.<br />";
	 }
	 return error;
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Não inseriu uma Alcunha.<br />";
    } else if ((fld.value.length < 5) || (fld.value.length > 60)) {
        fld.style.background = 'Yellow'; 
        error = "A Alcunha é do tamanho errado..<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Não inseriu um email.<br />";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Não inseriu um email válido.<br />";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "O email que inseriu tem caracteres inválidos.<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateEmpty(fld,msg) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Não inseriu uma " + msg + ".<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePassword(fld,fld1) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        fld1.style.background = 'Yellow';
        error = "Palavra passe vazia.<br />";
    } else if (fld.value != fld1.value) {
   		error = "As palavras passe não são iguais <br />";
   		fld.style.background = 'Yellow';
   		fld1.style.background = 'Yellow';
    } else if ((fld.value.length < 7) || (fld.value.length > 20)) {
        error = "A palavra passe é do tamanho errado. <br />";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "A palavra passe tem caraceteres ilegais.<br />";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
} 
function validateEmpty(fld,msg) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Campo " + msg + " está vazio.<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function confirmaDelete(ID,AREAID,OPT) {
	var answer = confirm("Tem a certeza que deseja eliminar a entrada número: "+ID+" ?")
	if (answer){
		alert("Entrada eliminada")
		window.location = "index.php?areaid="+AREAID+"&bid="+ID+"&opt="+OPT;
	}
}
function countChars(aForm, campo, campo1, max){
	var tamanho;
	var comptexto;
	var tamanho1;
	var lab;
	lab = document.getElementById(campo1);
	comptexto = document.getElementById(campo);
	tamanho = comptexto.value;
	tamanho1 = tamanho.length;
	//alert(tamanho1);
	lab.innerHTML = tamanho1 + ' caracteres';
	if (tamanho1 > max) {
		document.getElementById(campo).value = tamanho.substring(0, max);
		comptexto = comptexto.substring(60);
		return false;
	}
}

function mostraToC(param){
	if(param == 1){
		document.getElementById('toc').style.visibility = 'visible';
		document.getElementById('toc').style.display = 'block';
	}else{
		document.getElementById('toc').style.visibility = 'hidden;';
		document.getElementById('toc').style.display = 'none';
	}
}