var caminho_a="";

function carrega(caminho){
	caminho_a=caminho;
}

function vaiver(){
	mail=document.getElementById("mails").value;
	resultado=isEmail(mail);
	if (resultado==true) {

		nome_envia=document.getElementById("nome").value;
		conta="0";
		while (conta=="0"){
			nome_envia=nome_envia.replace(' ' , '');
			testa=nome_envia.search("[ ]");
			if (testa== "-1"){
				nome_envia1=nome_envia;
				conta="1";
			}
		}

		var charpos = nome_envia1.search("[^A-Za-z]");

		erro_nome="0";
		if(nome_envia1.length < 4) { 
			erro_nome="1";
            alert("O Nome deve ter pelo menos 4 Caracteres válidos!"); 
		}
		if(charpos >= 0 && erro_nome=="0") {
			erro_nome="1";
            alert("O Nome contêm caracteres inválidos!"); 
		}
		if (erro_nome=="0"){
			a_enviar=caminho_a+"ajax_newsletter.php?tarefa=guarda&email="+mail+"&nome="+document.getElementById("nome").value;
			$.post(a_enviar,"",function(retorno){$("#newletter_box").html(retorno)});
		}
	}  else {
	        alert("O Email Introduzido é Inválido!"); 
	}
}

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;
}


function clearText(thefield) {
	if (thefield.defaultValue == thefield.value) 
	thefield.value = "" 
	else thefield.value = thefield.defaultValue 
}

function envia_telefone (erro,obrigado){
	nomecon=document.getElementById("x_nome").value;
	telefone=document.getElementById("x_telefone").value;
	quando=document.getElementById("x_quando").value;
	obs=document.getElementById("x_observacoes").value;


	if (nome!="" && telefone!=""){
		a_enviar=caminho_a+"ajax_newsletter.php?tarefa=manda_contacto&nome="+nomecon+"&telefone="+telefone+"&quando="+quando+"&obs="+obs;
		$.post(a_enviar,"",function(retorno){$("#erro_m").html(retorno)});
		alert(obrigado);
		document.getElementById("x_nome").value="";
		document.getElementById("x_telefone").value="";
		document.getElementById("x_quando").value="";
		document.getElementById("x_observacoes").value="";
	} else {
		alert(erro);
	}

}

function mudalingua (lingua){
	

	a_enviar=caminho_a+"ajax_newsletter.php?tarefa=muda_lingua&lingua="+lingua;
	$.post(a_enviar,"",function(retorno){$("#erro_teste").html(retorno)});

	//var data  - para limpara o ajax e refrescar os dados, nos erros de refresh do firefox
	var data = $.ajax( {type: "GET", url: caminho_a+"ajax_newsletter.php?" + (new Date()).getTime(), async: false}); 

	document.open(window.location.href);	
	window.open(window.location.href, "_self");

//	document.close();

}
