var vMsg = "";
var vUrl = "";
var isMac = (navigator.cpuClass=='PPC'||navigator.userAgent.substr(36,3)=='Mac'||navigator.userAgent.substr(35,3)=='Mac');
var isIE  = (navigator.userAgent.substr(25,4)=='MSIE');
var isNS = ((navigator.userAgent.substr(0,9)=='Mozilla/5') && (navigator.userAgent.indexOf("Netscape") != -1));
var isFirefox = ((navigator.userAgent.substr(0,9)=='Mozilla/5') && (navigator.userAgent.indexOf("Firefox") != -1));
var tipoBrowser = navigator.appName;
if (isNS)
{
	tipoBrowser=0;
}
else
{
	tipoBrowser=1;
}
String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function fload(){
	try{
		vMsg = document.form1.txtpesquisa.value;
	}catch(e){}
}
function querySearchonfocus(querySearch) {
    if (querySearch.value == vMsg)querySearch.value = '';
}
function querySearchonblur(querySearch) {
    if (querySearch.value.trim() == '')querySearch.value = vMsg;
}
function fTermoPesquisados(vTermo){
	document.form1.txtpesquisa.value = vTermo;
	f_BuscarTermo();
}
function f_BuscarTermo(){	
	with(document.form1){
		if(txtpesquisa.value.trim() == ""){
			alert("Por favor, informe o termo a ser pesquisado!");
			txtpesquisa.focus();
			return;
		}
		var vsPesquisa = txtpesquisa.value.trim();
		if(vsPesquisa.length < 2){
			alert("Por favor, o termo a ser pesquisado deve conter pelo menos 2 caracteres.");
			txtpesquisa.focus();
			return;
		}
		action = vUrl + "search.asp?link=pesquisa&termo=" + f_urlenvode(vsPesquisa);target = "";submit();
	}
}
function f_urlenvode(vtermo){
	var NewTexto = "";
	var texto	 = vtermo;
	var textoArr = texto.split(" ");
	if (texto.indexOf(' ') != -1){
		var textoArr = texto.split(" ");
		var tamtexto = textoArr.length;
		var k = 0;
			
		while ( k < tamtexto )
		{
			NewTexto = NewTexto + textoArr[k];
			 if ( k != (tamtexto-1) )
			 {
				NewTexto = NewTexto + "|";
			 }
			k+=1;
		}
	}else{
		NewTexto = texto;
	}
	return NewTexto;
}
function formataCEP(e, objeto){
	
	var keypress = (isIE) ? event.keyCode : e.which;

	if (((keypress<48)||(keypress>57)) && (keypress!=13) && (keypress!="0") && (keypress!=8))
	{
		if (isIE) event.keyCode=0;
			return false;
	}
	
	campo = eval(objeto);
	caracteres = '01234567890';
	separacoes = 1;
	separacao1 = '-';
	conjuntos = 2;
	conjunto1 = 5;
	conjunto2 = 3;
	
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length <= (conjunto1 + conjunto2 + 1))
		{
		if (campo.value.length == conjunto1) 
		   campo.value = campo.value + separacao1;
		}
	else 
		event.returnValue = false;
	
	return true;
}
function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
        }
}
function fLogin(vTipo){
	with(document.frmlogin){
		if(txtemail.value.trim() == ''){
			alert('Por favor, informe o seu e-mail de cadastro!');
			txtemail.focus();
			return;
		}
		if(txtsenha.value.trim() == ''){
			alert('Por favor, informe a sua senha de acesso!');
			txtsenha.focus();
			return;
		}
		//valida:
		if(! checkMail(txtemail.value)){
			alert('Por favor, informe um e-mail válido!');
			txtemail.focus();
			return;
		}
		txtloginuser.value = vTipo;
		action = "cadastro.dll.asp?login=yes&key=login";
		method = "POST";
		submit();
	}
}
function fAjuda(){
	with(document.frmAjuda){
		if(txtemailesq.value.trim() == ''){
			alert('Por favor, informe o seu e-mail de cadastro!');			
			txtemailesq.focus();
			return;
		}
		//valida:
		if(! checkMail(txtemailesq.value)){
			alert('Por favor, informe um e-mail válido!');
			txtemailesq.focus();
			return;
		}
		txtrecuperar.value = "S";
		action = "cadastro.dll.asp?login=recuperar&key=login";
		method = "POST";
		submit();
	}
}
// Verifica se o caracter do keypress é um dígito de 0 a 9
function isDigit (vpValor){
	var keyNumber = (isIE) ? event.keyCode : e.which;

	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8))
	{
		if (isIE) event.keyCode=0;
			return false;
	}

	return true	
}
//coloca máscara na data digitada ("dd-mm-yyyy")
function f_MaskData(vp, vpChar){
	if (!isDigit(vpChar)) return false;	
	vsAux = "";	
	vsTam = vp.value;
	for (i=0;i<vsTam.length;i++){
		if(i==2 || i==5){
			if (vsTam.substring(i, i+1) != "/") vsAux += "/";
		}	
		vsAux += vsTam.substring(i, i+1);
	}
	vp.value = vsAux;	
}
function onlynum(e){
	var keyNumber = (isIE) ? event.keyCode : e.which;

	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8))
	{
		if (isIE) event.keyCode=0;
			return false;
	}

	return true
}
/*Funcao para checagem de CPF
    Retorno -> True ou False */
             
function f_CheckCPF(pCPF){
     
     pCPF=pCPF.replace(/[.-/-]/g,"");
     pCPF=Right((repeat(11, "0") + pCPF),11);
     
     //Calculo do Primeiro Digito
     var SomaNumeros=0;
     for(var i=0;i<9;i++){
             SomaNumeros+=(parseInt(pCPF.substring(i, i+1),10) * (11 - (i+1)));
	 }	

	 var Resto = 11 - (SomaNumeros - (parseInt(SomaNumeros / 11,10) * 11));
         
	 if(Resto == 10 || Resto == 11) Resto = 0;

         if(Resto != parseInt(pCPF.substring(9,10),10)){
             return false;
         }

     //Calculo do Segundo Digito
     SomaNumeros = 0;
	 for(var i=0;i<10;i++){
             SomaNumeros+=(parseInt(pCPF.substring(i, i+1),10) * (12 - (i+1)));
	 }	
         
	 var Resto = 11 - (SomaNumeros - (parseInt(SomaNumeros / 11,10) * 11));
         
	 if(Resto == 10 || Resto == 11) Resto = 0;

         if(Resto != parseInt(pCPF.substring(10,11),10)){
             return false;
         }

	return true;

}

//Checa se o CNPJ é Valido
//Retorna True ou False
function f_CheckCNPJ(pCNPJ){

	var iAlgarismos = new Array(15) 
	var iSomaAlgarismos=0;
	var iDigito1=0;
	var iDigito2=0;

	pCNPJ=pCNPJ.replace(/[-.\/]/g,"");
	pCNPJ= Right(String("000000000000000")+String(pCNPJ),15);

	for(var i=0;i<pCNPJ.length;i++){
		iAlgarismos[i]=pCNPJ.substring(i,i+1);
	}

	iSomaAlgarismos=  iAlgarismos[0]  * 5;
	iSomaAlgarismos=  iAlgarismos[1]  * 5;  
	iSomaAlgarismos+= iAlgarismos[2]  * 4; 
	iSomaAlgarismos+= iAlgarismos[3]  * 3; 
	iSomaAlgarismos+= iAlgarismos[4]  * 2; 
	iSomaAlgarismos+= iAlgarismos[5]  * 9; 
	iSomaAlgarismos+= iAlgarismos[6]  * 8; 
	iSomaAlgarismos+= iAlgarismos[7]  * 7; 
	iSomaAlgarismos+= iAlgarismos[8]  * 6; 
	iSomaAlgarismos+= iAlgarismos[9]  * 5; 
	iSomaAlgarismos+= iAlgarismos[10] * 4; 
	iSomaAlgarismos+= iAlgarismos[11] * 3; 
	iSomaAlgarismos+= iAlgarismos[12] * 2;

	//Cálculo do Primeiro Digito
	//iSomaAlgarismos = iSomaAlgarismos -(11 * (parseInt(iSomaAlgarismos / 11,10)))
	iSomaAlgarismos = (iSomaAlgarismos % 11);
	iDigito1		= (iSomaAlgarismos > 1 ? 11 - iSomaAlgarismos : 0);

	if(iAlgarismos[13]!=iDigito1) return false;

	iSomaAlgarismos =  iAlgarismos[1]  * 6 
	iSomaAlgarismos+=  iAlgarismos[2]  * 5 
	iSomaAlgarismos+=  iAlgarismos[3]  * 4 
	iSomaAlgarismos+=  iAlgarismos[4]  * 3 
	iSomaAlgarismos+=  iAlgarismos[5]  * 2 
	iSomaAlgarismos+=  iAlgarismos[6]  * 9 
	iSomaAlgarismos+=  iAlgarismos[7]  * 8 
	iSomaAlgarismos+=  iAlgarismos[8]  * 7 
	iSomaAlgarismos+=  iAlgarismos[9]  * 6 
	iSomaAlgarismos+=  iAlgarismos[10] * 5 
	iSomaAlgarismos+=  iAlgarismos[11] * 4 
	iSomaAlgarismos+=  iAlgarismos[12] * 3 
	iSomaAlgarismos+=  iAlgarismos[13] * 2

	//Cálculo do Segundo Digito
	iSomaAlgarismos = (iSomaAlgarismos % 11);
	iDigito2		= (iSomaAlgarismos > 1 ? 11 - iSomaAlgarismos : 0);

	if(iAlgarismos[14]!=iDigito2) return false;
	
	return true;
}
function f_IsNumeric(Objeto, valor)
{
	if(isNumeric(valor)==false)
	{
		alert("Valor incorreto!");
		Objeto.value="";
		Objeto.focus();
	}
}
//A Mesma que do VB e ASP
function Right(sString,Length){
	if(sString.trim()!=''){
		return sString.substring(sString.length,(sString.length-Length))
	}else{
		return sString;
	}
}

//A Mesma que do VB e ASP
function Left(sString,Length){
	if(sString.trim()!=''){
		return sString.substring(0,Length)
	}else{
		return sString;
	}
}
// Faz a mesma coisa que a funcao String do VB e ASP
function repeat(vezes,caracter){
	var resultado=caracter;
	
	for(var i=1;i<vezes;i++){
		resultado=resultado + caracter;
	}
	return resultado;
}
function isDate(s){
	var vsDia;
	var vsMes;
	var vsAno;
	var vsDataAux;
	var vsData;

	if (isEmpty(s)) return true;
	vsData = new Date("1/2/2000");
	vsAux = s.split("/");
	if (vsAux.length != 3) return false;
	vsDia = vsAux[0];
	vsMes = vsAux[1];
	vsAno = vsAux[2];
	
	if (vsAno.length != 4) return false;
	
	vsDataAux =(vsAno + vsMes + vsDia);
	if (isNaN(vsDataAux)) return false;
	if (vsDia == 0) return false;
	if (vsMes == 0) return false;
	if (vsAno == 0) return false;
	if (vsAno < 0001) return false;
	vsDataAux = new Date(vsAno,vsMes-1,vsDia);
	if (vsMes < 1 || vsMes > 12) return false;
	if (vsDia < 1 || vsDia > 31) return false;
	if (vsDia > 30 || (vsDia > 28 && vsMes == 02 )){
		D = vsDataAux.getDate();
		if (D < vsDia) return false; 
	}
	return true;
}

// Verifica se o caracter é vazio
function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}
function fFileds(vopcao){
	if(vopcao == "F"){
		document.all.tbNomeFantasia.style.display = 'none';
		document.all.tbNomeResponsavel.style.display = 'none';
	}else if(vopcao == "J"){
		document.all.tbNomeFantasia.style.display = '';
		document.all.tbNomeResponsavel.style.display = '';		
	}
}
function fAlterCad(){
  with(document.frmCadastro){
	if(vValidade()){
		action = "./cadastro.do.dll.asp?alterar=true&sever=do.dll.alter&https://www.emaster.aewinf.com.br&key=getuser"; target = ""; method = "POST";
		submit();
	}
  }
}
function fEnviaCad(){
  with(document.frmCadastro){
	if(vValidade()){
		txtcadastrar.value = "OK";
		action = "./cadastro.do.dll.asp?link=cadastrar"; target = ""; method = "POST";
		submit();
	}
  }
}
function vValidade(){
  with(document.frmCadastro){
	if(optPessoa[0].checked){
		txtTpPessoa.value = 'F';
	}else{
		txtTpPessoa.value = 'J';
	}
							
	if(Nome.value.trim() == ''){
		alert('Por favor, informe o seu nome!');
		Nome.focus();
		return false;
	}else if(CPFCNPJ.value.trim() == ''){
		alert('Por favor, informe o seu CPF ou CNPJ!');
		CPFCNPJ.focus();
		return false;
	}else if(isNaN(CPFCNPJ.value)){
		alert('Por favor, o CPF ou CNPJ deve ser numérico (sem pontos, vírgula ou traços)!');
		CPFCNPJ.focus();
		return false;
	}
	if(txtTpPessoa.value == 'F'){ //CPF:
		if(! f_CheckCPF(CPFCNPJ.value)){ 
			alert('O CPF informado é inválido!');
			CPFCNPJ.focus();
			return false;
		}
	}else{ //CNPJ:
		//razão Social:
		if(NomeRazao.value.trim() == ''){
			alert('Por favor, informe a razão social da empresa!');
			NomeRazao.focus();
			return false;
		}		
		if(! f_CheckCNPJ(CPFCNPJ.value)){ 
			alert('O CNPJ informado é inválido!');
			CPFCNPJ.focus();
			return false;
		}
	}

	if(datanascimento.value.trim() == ''){
		alert('Por favor, a Data de Nascimento deve ser preenchida!');
		datanascimento.focus();
		return false;	
	}else if(! isDate(datanascimento.value)){
		alert('Por favor, a Data de Nascimento informada é inválida!');
		datanascimento.focus();
		return false;			
	}else if(cbosexo.value == ''){
		alert('Por favor, informe o campo "sexo" !');
		cbosexo.focus();
		return false;
	}else if(emailuser.value == ''){
		alert('Por favor, informe o seu e-mail!');
		emailuser.focus();
		return false;
	}else if(!checkMail(emailuser.value)){
		alert('O e-mail informado é inválido.\nPor Favor informe um e-mail válido, ele será o seu login e nosso canal de comunicação!');
		emailuser.focus();
		return false;/*
	}else if(senha1.value.trim() == ''){
		alert('Por favor, informe a sua senha!');
		senha1.focus();
		return false;		
	}else if(senha2.value.trim() == ''){
		alert('Por favor, informe a confirmação de sua senha!');
		senha2.focus();
		return false;
	}else if(senha1.value.length < 4 || senha1.value.length > 12){
		alert('A senha informada possui a quantidade de caracteres diferente do permitido (De 04 à 12)!');
		senha1.focus();
		return false;		
	}else if(senha1.value != senha2.value){
		alert('Atenção a senha de cofirmação está deferente da senha original!');
		senha2.focus();
		return false;*/
	}else if(CEPlogra.value.trim() == ''){
		alert('Por favor, informe o CEP do seu Endereço !');
		CEPlogra.focus();
		return false;
	}else if(Endereco.value.trim() == ''){
		alert('Por favor, informe o seu Endereço !');
		Endereco.focus();
		return false;
	}else if(Numerologra.value.trim() == ''){
		alert('Por favor, informe o Número do seu Endereço !');
		Numerologra.focus();
		return false;
	}else if(Bairrologra.value.trim() == ''){
		alert('Por favor, informe o Bairro do seu Endereço !');
		Bairrologra.focus();
		return false;
	}else if(Cidadelogra.value.trim() == ''){
		alert('Por favor, informe o Cidade do seu Endereço !');
		Cidadelogra.focus();
		return false;
	}else if(cboEstadologra.value == ''){
		alert('Por favor, informe o Estado do sua Cidade !');		
		cboEstadologra.focus();
		return false;
	}else if(DDD1.value.trim() == ''){
		alert('Por favor, informe o seu DDD do Telefone de contato !');
		DDD1.focus();
		return false;
	}else if(Telefone1User.value.trim() == ''){
		alert('Por favor, informe o seu Telefone de contato !');
		Telefone1User.focus();
		return false;
	}
	return true;
  }
}
function f_pagseguro(){
	document.pagseguro.submit();
}
function fEnviaContatox(){		
	with(document.frmcontato){
		if(txtNomecontato.value.trim() == ''){
			alert('Por favor informe o seu nome de contato!');
			txtNomecontato.focus();
			return false;
		}
		if(txtemailcontato.value.trim() == ''){
			alert('Por favor informe o seu e-mail de contato!');
			txtemailcontato.focus();
			return false;
		}
		if(mensagemcontato.value.trim() == ''){
			alert('Por favor informe a mensagem do contato!');
			mensagemcontato.focus();
			return false;
		}
		if(! checkMail(txtemailcontato.value)){
			alert('O e-mail informado é inválido!');
			txtemailcontato.focus();
			return false;
		}		var _sparameter = "?prod=" + _getParameter("prod") +"&nome="+ _getParameter("nome");			
		txtsubmit.value = 'OK';
		action = "./index.asp" + _sparameter;		submit();		return true;
	}
}function _getParameter(querystring){
	var querystr = new Array();	var queryret = '';
	loc = window.location.search.substr(1).split('&');	
	if ((loc!='')&&(loc!=null)) {
		for (var icnt=0;icnt< loc.length;icnt++){
			var q = loc[icnt].split('=');			if(querystring == q[0]){
				querystr[q[0]] = q[1];
				queryret = q[1];				return unescape(queryret.trim());
			}
		}
		//return unescape(querystr[querystring].trim());
		return unescape(queryret.trim());	
	}else {
		//return(null);		return '';
	}
}
function URLDecode(psEncodeString){  return unescape(psEncodeString);}	
function fopencentral(){
	window.open('./centralatendimento.asp','central','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenx=0,screeny=0,left=300,top=190,width=365px,height=230px');
}
function flimpavisitados(acao){
	var vAction
	if(acao == 1){
		document.getElementById("tbpesquisados").innerHTML = "<center><img src=./imgs/loading.gif></center>";
	}else{
		document.getElementById("tbtopall").innerHTML = "<center><img src=./imgs/loading.gif></center>";		
	}
	vAction = document.forms[0].action;
	document.forms[0].action = "ilimpavisitados.asp?acao=" + acao;
	document.forms[0].target = "ilimpavisitados";
	document.forms[0].submit();
	//
	document.forms[0].action = vAction;
	document.forms[0].target = "";
}
