//declarando as constante
var NOVALINHA="-n-";
var TIC="-`-";
//var TICII="-``-";
var POLEGADA="\'\'";
var POLEGADA1="\'";
var PESQUISANOVALINHA=/\n/gim;
var PESQUISATIC=/\'/gim;
var PESQUISAPOLEGADA=/-``-/gim;
var PESQUISAPOLEGADA1=/-`-/gim;

/***************************************************
* função que envia dados pelo método Post via ajax *
****************************************************/
function enviaAjax(parameters,sUrl)
{
   var  xmlhttp;
   var  objResposta;
   var	sRetorno;
   
    //verifica qual objeto vai ser criado dependendo o tipo de browser 
	try{
		//MOZILLA e AFINS
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
	//trata a quebra de linha
	//parameters=parameters.replace(PESQUISANOVALINHA,NOVALINHA);
	
    //carrega os dados via método post
    xmlhttp.open("post", sUrl,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", parameters.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4){
            //carrega objeto de resposta
			msgTela (xmlhttp.responseText);
		}
		else
			msgTela ("<p>Carregando...</p>");
	}
	
	xmlhttp.send(parameters)
}
/************************************************************************
* Função que valida o e-mail retornando True se for um email válido		*
*	e False caso contrário.												*
* Parâmetros: email: E-mail a ser validado.								*
*************************************************************************/
function Valida_Email(email) 
{
	var teste_arroba = "false";         //VERIFICAR SE FOI DIGITADO PELO MENOS UM ARROBA NO E-MAIL 
	var teste_ponto = "false";         //VERIFICAR SE FOI DIGITADO PELO MENOS UM PONTO NO E-MAIL 
	var tamanho_email = email.length; 
	
	if (tamanho_email > 4) 
	{ 
		for (i=0; i < tamanho_email; i++) 
		{ 
			if (((email.charAt(i) == "@") || (email.charAt(i) == ".")) && ((i == 0) || (i == parseInt(tamanho_email) - 1)))
			{
				//DIGITOU'@' OU '.' NA PRIMEIRA OU ÚLTIMA POSIÇÃO 
				return (false); 
			}
			else
			{
				if (email.charAt(i) == "@") //ACHOU ARROBA 
				{
					if (teste_arroba == "true") //ENCONTROU 2 ARROBAS 
					{ 
						return (false); 
					} 
					else 
					{ 
						teste_arroba = "true"; 
						if ((email.charAt(i + 1) == ".") || (email.charAt(i - 1) == ".") || (email.charAt(i + 1) == "@"))
						{
							//ACHOU "@.", ".@" OU "@@" 
							return (false); 
						} 
					} 
				} 
				else 
				{ 
					if (email.charAt(i) == ".") //ACHOU PONTO 
					{ 
						teste_ponto = "true"; 
						if ((email.charAt(i + 1) == ".") || (email.charAt(i - 1) == "."))
						{
							//ACHOU ".." 
							return (false); 
						}
					}
				}
			} 
		} 
		if (teste_arroba == "false" || teste_ponto == "false") //NÃO ENCONTROU ARROBA OU PONTO NO EMAIL 
		{ 
			return (false); 
		} 
	} 
	else // email com menos de 5 caracteres 
	{ 
		return (false); 
	} 
	return (true); 
}
/************************************************
* Função que Crítica se o campo obrigatório		*
* Parâmetros: parametro: campo a ser testado.	*
*************************************************/
function verifica_branco(parametro)   
{ 
	var	teste_parametro = "false"; // variável para teste de espaços em branco 
	var	tamanho_parametro = parametro.length; 
	if (tamanho_parametro != 0) 
	{ 
		for (i = 0; i < tamanho_parametro; i++) 
		{
			if ( (parametro.charAt(i) != " ") && ( (parametro.charCodeAt(i) != 13) && (parametro.charCodeAt(i) != 10) && (parametro.charCodeAt(i) != 160) ) )
			{ 
				teste_parametro = "true"; // existe caracter diferente de branco 
			} 
		} 
		if (teste_parametro == "false")  // todos os caracteres digitados são brancos 
		{ 
			return false; 
		} 
		else 
		{ 
			return true; 
		} 
	} 
	else 
	{ 
		return false; 
	} 
}

/************************************************************
* Função para mandar e-mail fale conosco e normalizar a tela*
*************************************************************/
function enviaEmailFaleConosco(){
	//verifica se os dados obrigatórios foram prenchidos corretamente
	var strCampo;
	var strMsgNormalizaCampoIn="<span class=\"centro\"><a class=\"saibamais\" ";
	var strMsgNormalizaCampoOut="title=\"Voltar a tela.\">OK</a></span>";
	var sAux;
	
	strCampo=document.getElementById('ipnome').value;
	if (!verifica_branco(strCampo))
	{
		strMsg="<p>Campo \"Nome\" e de prenchimento obrigatório!</p>"+strMsgNormalizaCampoIn+"href=\"javascript:normalizaTela('ipnome');\" "+strMsgNormalizaCampoOut;
		msgTela (strMsg);
		return;
	}
	sParametros="ipnome=" +strCampo;
	
	strCampo=document.getElementById('ipemail').value;
	if (!verifica_branco(strCampo))
	{
		strMsg="<p>Campo \"E-mail\" e de prenchimento obrigatório!</p>"+strMsgNormalizaCampoIn+"href=\"javascript:normalizaTela('ipemail');\" "+strMsgNormalizaCampoOut;
		msgTela (strMsg);
		return;
	}else {
		if (!Valida_Email(strCampo))
		{
			strMsg="<p>Campo \"E-mail\" está prenchimento errado!</p>"+strMsgNormalizaCampoIn+"href=\"javascript:normalizaTela('ipemail');\" "+strMsgNormalizaCampoOut;
			msgTela (strMsg);
			return;
		}
	}
	sParametros=sParametros+"&ipemail=" + strCampo;

	strCampo=document.getElementById('txtmsg').value;
	if (!verifica_branco(strCampo))
	{
		strMsg="<p>Campo \"Mensagem\" e de prenchimento obrigatório!</p>"+strMsgNormalizaCampoIn+"href=\"javascript:normalizaTela('txtmsg');\" "+strMsgNormalizaCampoOut;
		msgTela (strMsg);
		return;
	}
	//prepara a quebra de linha para serem cadastrada no banco
	sAux=(document.getElementById("txtmsg").value).replace(PESQUISANOVALINHA,NOVALINHA);
	sAux=sAux.replace(PESQUISATIC,TIC);
	sParametros=sParametros+"&txtmsg=" + escape(sAux)+"&cbemail_send=" + document.getElementById("cbemail_send").value + "&iptelefone=" + document.getElementById("iptelefone").value + "&ipempresa=" + document.getElementById("ipempresa").value + "&ipendereco=" + escape(document.getElementById("ipendereco").value);
	enviaAjax(sParametros,"email_faleconosco.php");
}
function msgTela (strMsg){
	var objResposta;
	objResposta=document.getElementById("resultado");
	objResposta.innerHTML = strMsg;
	objResposta.style.display="block";
	document.getElementById("travatela").style.display="block";
}
function normalizaTela(strID){
	document.getElementById("resultado").style.display="none";
	document.getElementById("travatela").style.display="none";
	document.getElementById(strID).focus();
}
function normalizaTelaZera(){
	document.getElementById("ipnome").value="";
	document.getElementById("iptelefone").value="";
	document.getElementById("ipemail").value="";
	document.getElementById("ipempresa").value="";
	document.getElementById("ipendereco").value="";
	document.getElementById("cbemail_send").selectedIndex=0;
	document.getElementById("txtmsg").value="";
	document.getElementById("resultado").style.display="none";
	document.getElementById("travatela").style.display="none";
	document.getElementById("ipnome").focus();
}
/*******************************************************************
* Função para Bloquear teclas alfanuméricas, inclusive o ponto "." *
********************************************************************/
function NumericoData(event)
{
   	Tecla = event.which;
	
	if (Tecla == null)
	   Tecla = event.keyCode;
	if ((Tecla >= 48 && Tecla <= 57) || Tecla==8 || Tecla==0)
	   return true;
	return false;
}
