var aok;

function trim(inputString){
	 
		 //Remove espaços precedentes e porsteriores da string transmitida.
		 //Remove também espaços consecutivos e os substitui por um único espaço.
		 // Se algo além de uma string for transmitido ( Objeto nulo, sob medida, etc),
		 //Então retornar a entrada.
		 
		 if(typeof inputString != "String") { return inputString; }
		 
		 valor = inputString;
		 
		 var ch = valor.substring(0, 1);
		 while(ch == " "){
		 	   
			   //VErificar a existencia de espaços no inicio da string.
			   
			   valor = valor.substring(1, valor.length);
			   ch = valor.substring(0, 1);
		 }
		 ch = valor.substring(valor.length-1, valor.length);
		 while(ch == " "){
		 
		 	   //Verificar a existencia de espaçõs no inico da String.
			   
			   valor = valor.substring(0, valor.length-1);
			   ch = valor.substring(valor.length-1, valor.length);
		 }
		 while(valor.indexOf(" ") != -1){
		 	   
			   //Portanto procurar espaçõs multiplos na string.
			   
			   valor = valor.substring(0, valor.indeOf(" ")) +
			   valor.substring(valor.indeOf(" ")+1, valor.length);
			   
			   //Novamente, há dois espaços em cada uma das strings
			   //Observar que há dois espaços na string
			   
		 }
		 return valor; //Retornar a string "trim" ao usuario.
		 
}

//Função para validar o formulário

function validarform(ovalor,onome){
	 
		 var cont = true;
		 document.getElementById("msg").style.display='block';
		 if(onome == "formrevendalogin"){
		 	if(trim(ovalor) == ""){
					document.getElementById("msg").innerHTML = "Acesso Negado, digite um CNPJ.";
					
					document.getElementById("msg").style.display='block';
					document.getElementById("formrevenda").formrevendalogin.focus();
					cont = false;
					aok = false;
			}
		 }
		 if(cont == true){
		 	if(onome == "formrevendasenha"){			
				if(trim(ovalor) == ""){
					document.getElementById("msg").innerHTML = "Acesso Negado, digite sua senha.";
					document.getElementById("msg").style.display='block';
					document.getElementById("formrevenda").formrevendasenha.focus();
					cont = false;
					aok = false;
				}
			}
		 }
} 


//Função para enviar um formulário.

function getformvalues(fobj,valfunc){

		 var str ="";
		 aok = true;
		 var val;
		 
		 //Executa uma lista de todos os objetos contidos dentro do formulário.
		 for(var i = 0; i < fobj.elements.length; i++){		 			  
		 		 if(valfunc){
				 			 if(aok == true){
							 		if(fobj.name == "formrevenda"){			 			 
							 			val = validarform(fobj.elements[i].value,fobj.elements[i].name);
									}else{
										val = true; 
										showRequestContato();
									}
									if(val == false){
										aok = false;
									}
							 }
				 }
				 str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";				 
		 }
		 // Em seguida retornar os valores da string.
		 return str;	 
}

//Função para validar o Formulário

function submitform(theform, serverpage, objID, valfunc){
		 
		 var file = serverpage;
		 var str = getformvalues(theform,valfunc);
		 
		 //Se a validação estiver ok.
		 if(aok == true){
		 		obj = document.getElementById(objID);
				processajax(serverpage, obj, "post", str);
		 }
}

function irpara(url){
	document.ftconosco.action=url;
	document.ftconosco.submit();
}

function validar(url){	
	document._frmPrincipal2.action=url;
	document._frmPrincipal2.submit();
}

function ir(url){
	document.fconosco.action=url;
	document.fconosco.submit();
}

function vhome(url){
	document.vconsulta.action=url;
	document.vconsulta.submit();
}

function vcidatend(url){
	document.vcidatend.action=url;
	document.vcidatend.submit();
}

function vcob(url){
	document.vcob.action=url;
	document.vcob.submit();
}

function vrel(url){
	document.vrel.action=url;
	document.vrel.submit();
}


//--->Função para a formatação dos campos...<---
function Mascara(tipo, campo, teclaPress) {
    if (window.event)
    {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var s = new String(campo.value);
    // Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
    s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

    tam = s.length + 1;

    if ( tecla != 9 && tecla != 8 ) {
        switch (tipo)
        {
        case 'CPF' :
            if (tam > 3 && tam < 7)
                campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
            if (tam >= 7 && tam < 10)
                campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
            if (tam >= 10 && tam < 12)
                campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
        break;

        case 'CNPJ' :

            if (tam > 2 && tam < 6)
                campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
            if (tam >= 6 && tam < 9)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
            if (tam >= 9 && tam < 13)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
            if (tam >= 13 && tam < 15)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
			if(campo.value.length == 18 && campo.name == "cgccpf"){
				document.autentica.senha.focus();
			}else if (campo.value.length == 18 && campo.name == "s_cgccpf"){
				document.esqueceusenha.s_envia.focus();
			}else if (campo.value.length == 18 && campo.name == "f_cgccpf"){
				document.contato.f_email.focus();
			}			
        break;

        case 'TEL' :
            if (tam > 2 && tam < 4)
                campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
            if (tam >= 7 && tam < 11)
                campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
        break;

        case 'DATA' :
            if (tam > 2 && tam < 4)
                campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
            if (tam > 4 && tam < 11)
                campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
        break;
        
        case 'CEP' :
            if (tam > 5 && tam < 7)
                campo.value = s.substr(0,5) + '-' + s.substr(5, tam);
        break;
		
        case 'CNTR' :
            if (tam > 4 && tam < 6)
                campo.value = s.substr(0,4) + ' ' + s.substr(4,tam);
				campo.value = campo.value.toUpperCase();
            if (tam >= 11 && tam < 12)
                campo.value = s.substr(0,4) + ' ' + s.substr(4,11) + '-' + s.substr(11,tam);
				campo.value = campo.value.toUpperCase();
        break;		
        }
    }
}

//--->Função para verificar se o valor digitado é número...<---
function digitos(event){
    if (window.event) {
        // IE
        key = event.keyCode;
    } else if ( event.which ) {
        // netscape
        key = event.which;
    }
    if ( key != 8 || key != 13 || key < 48 || key > 57 )
        return ( ( ( key > 47 ) && ( key < 58 ) ) || ( key == 8 ) || ( key == 13 ) );
    return true;
}
