var caminho_signup="";

function carrega_signup(caminho){
	caminho_signup=caminho;
}

function fadeIn_signup(txt){
	fadeOutx("erro_signup",500,txt);
}
function fadeOut_signup(){
	$("#erro_signup").fadeOut(2000);
}

function fadeOutx(div,tempo,msg){
	//document.getElementById("erro_signup").style.display="none";
	$("#"+div).fadeOut(tempo);
	$("#"+div).fadeIn(2000);
	document.getElementById("erro_signup").innerHTML=msg;
}
function registo_ok(txt){
	document.getElementById("xsignup_sucesso").style.display="";
	document.getElementById("signup_sucesso").style.backgroundColor="#99cc00";
	document.getElementById("signup_sucesso").style.color="#ffffff";
	document.getElementById("signup_sucesso").innerHTML=txt;
	$("#signup_sucesso").fadeIn(2000);
	//document.getElementById("xsignup").style.display="none";
	//$.post(a_enviar,"",function(retorno){$("#xsignup_sucesso").html(retorno)});
}
function limpar_signup(){
	document.getElementById("xusername").value ="";
	document.getElementById("xpassword").value ="";
	document.getElementById("xnome").value ="";
	document.getElementById("xapelido").value ="";
	document.getElementById("xemail").value ="";
	document.getElementById("xtelefone").value ="";
	document.getElementById("xmorada").value ="";
	document.getElementById("xcod_postal").value ="";
	document.getElementById("xdistrito").value ="";
	document.getElementById("xcidade").value ="";
	document.getElementById("xsecurity_code").value ="";
	fadeOut_signup();
}

function signup(){
		erro="0";

		jcidade=document.getElementById("xcidade").value;
	var vcidade = jcidade.search("[^A-Za-z 徉汊殍觏祛篁豸烂律仁吞斡艺在氽缜]");
		if(jcidade.length < 2){ 
			erro="18";
		}
		if(vcidade >= 0) {
			erro="19";
		}
	
		jdistrito=document.getElementById("xdistrito").value;
	var vdistrito = jdistrito.search("[^A-Za-z 徉汊殍觏祛篁豸烂律仁吞斡艺在氽缜]");
		if(jdistrito.length < 2){ 
			erro="16";
		}
		if(vdistrito >= 0) {
			erro="17";
		}

		jcod_postal=document.getElementById("xcod_postal").value;
	var vcod_postal = jcod_postal.search("[^0-9- ]");
		if(jcod_postal.length < 4){ 
			erro="14";  
		}
		if(vcod_postal >= 0) {
			erro="15";
		}	
	
		jmorada=document.getElementById("xmorada").value;
	var vmorada = jmorada.search("[^0-9A-Za-z _邯.,徉汊殍觏祛篁豸烂律仁吞斡艺在氽缜]");
		if(jmorada.length < 5){ 
			erro="12";
		}
		if(vmorada >= 0) {
			erro="13";
		}
	
	
	jtelefone=document.getElementById("xtelefone").value;
	var vtelefone = jtelefone.search("[^0123456789+ ]");
		if(jtelefone.length < 8){ 
			erro="10";
		}
		if(vtelefone >= 0) {
			erro="11";
		}
		

	
	jemail=document.getElementById("xemail").value;
	resultado=isEmail(jemail);
	if (resultado==false) {
			erro="9";
	}
		
	japelido=document.getElementById("xapelido").value;
	var vapelido = japelido.search("[^A-Za-z 徉汊殍觏祛篁豸烂律仁吞斡艺在氽缜]");
		if(japelido.length < 3){ 
			erro="7";
		}
		if(vapelido >= 0) {
			erro="8";
		}
		
		
		
	jnome=document.getElementById("xnome").value;
	var vnome = jnome.search("[^A-Za-z 徉汊殍觏祛篁豸烂律仁吞斡艺在氽缜]");
		if(jnome.length < 3){ 
			erro="5";
		}
		if(vnome >= 0) {
			erro="6";
		}

		
		
	jpassword=document.getElementById("xpassword").value;
	var vpassword = jpassword.search("[^A-Za-z_0123456789-@]");
		if(jpassword.length < 4){ 
			erro="3"; 
		}
		if(vpassword >= 0) {
			erro="4";
		}	
		
		
		
	jusername=document.getElementById("xusername").value;
	var vusername = jusername.search("[^0-9A-Za-z_]");
		if(jusername.length < 4){ 
			erro="1";
		}
		if(vusername >= 0) {
			erro="2";
		}	
		
	
	jpais=document.getElementById("xpais").value;
	xsec = document.getElementById("xsecurity_code").value;
if(erro != "0"){	
		a_enviar=caminho_signup+"ajax_signup.php?tarefa=regista&username="+escape(jusername)+"&password="+escape(jpassword)+"&nome="+escape(jnome)+"&apelido="+escape(japelido)+"&email="+escape(jemail)+"&telefone="+escape(jtelefone)+"&morada="+escape(jmorada)+"&cod_postal="+escape(jcod_postal)+"&distrito="+escape(jdistrito)+"&cidade="+escape(jcidade)+"&pais="+escape(jpais)+"&erro="+escape(erro)+"&xsecurity_code="+escape(xsec);
		$.post(a_enviar,"",function(retorno){$("#xsignup_output").html(retorno)});
}else{
		a_enviar=caminho_signup+"ajax_signup.php?tarefa=regista&username="+escape(jusername)+"&password="+escape(jpassword)+"&nome="+escape(jnome)+"&apelido="+escape(japelido)+"&email="+escape(jemail)+"&telefone="+escape(jtelefone)+"&morada="+escape(jmorada)+"&cod_postal="+escape(jcod_postal)+"&distrito="+escape(jdistrito)+"&cidade="+escape(jcidade)+"&pais="+escape(jpais)+"&erro="+escape(erro)+"&xsecurity_code="+escape(xsec);
		$.post(a_enviar,"",function(retorno){$("#xsignup_sucesso").html(retorno)});
		
		//Para o Envio de EMAIL
			username=jusername;
			password=jpassword;
			nome=jnome;
			apelido=japelido;
			email=jemail;
			telefone=jtelefone;
			morada=jmorada;
			cod_postal=jcod_postal;
			distrito=jdistrito;
			cidade=jcidade;
			pais=jpais;
			erro=erro;
			xsecurity_code=xsec;
}
}

function envia_email_signup(qual){
	a_enviar=caminho_signup+"ajax_signup.php?tarefa=envia_email_signup&qual="+qual+"&username="+jusername+"&password="+jpassword+"&nome="+jnome+"&apelido="+japelido+"&email="+jemail+"&telefone="+jtelefone+"&morada="+jmorada+"&cod_postal="+jcod_postal+"&distrito="+jdistrito+"&cidade="+jcidade+"&pais="+jpais+"&erro="+erro+"&xsecurity_code="+xsec;
	$.post(a_enviar,"","");
}

function isEmail(text){
   var 	arroba = "@",
		ponto = ".",
		posponto = 0,
		posarroba = 0;
	
	 if (text =="") return false;
	
	 for (var indice = 0; indice < text.length; indice++){
		if (text.charAt(indice) == arroba) {
			posarroba = indice;
			break;
		 }
	 }
	
	for (var indice = posarroba; indice < text.length; indice++){
		if (text.charAt(indice) == ponto) {
			posponto = indice;
			break;
		}
	}
	if (posponto == 0 || posarroba == 0) return false;
	if (posponto == (posarroba + 1)) return false;
	if ((posponto + 1) == text.length) return false;
	return true;
}
