

// ==============================================================================================================
// ==============================================================================================================
// FUNÇÕES PARA FORMATAR CAMPOS GERAL ***************************************************************************
// ==============================================================================================================
// ==============================================================================================================





// PERMITE APENAS NÚMERO *****************************************************************************************

function numero( evento ) {
	
	/************************************************
	backspace = 8
	enter = 13
	tab = 0 ou 9
	*************************************************/
	
	if( navigator.appName == "Netscape" ) {	var tecla = evento.which; } else { var tecla = evento.keyCode; }
	
	if ((tecla > 47 && tecla < 58) || tecla == 8 || tecla == 13 || tecla == 0) {
		return true; 
	}else{
		return false;
	}            
	
}




// FORMATA TELEFONE *****************************************************************************************

function formata_telefone(objeto, evento){
	
	if( navigator.appName == "Netscape" ) {	var tecla = objeto.which; } else { var tecla = objeto.keyCode; }
	var telefone = evento;
	if(tecla != 8 && tecla != 9){
		if(telefone.value.length == 0)  telefone.value += '(';
		if(telefone.value.length == 3)  telefone.value += ') ';
		if(telefone.value.length == 9)  telefone.value += '-';
		
	}
}





// FORMATA CAMPOS DE VALOR *****************************************************************************************

function formata_valor_limpar(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
		result += aux;
		}
	}
	return result;
}

function formata_valor(campo,tammax,teclapres,decimal) {
	
	var tecla = teclapres.keyCode;
	vr = formata_valor_limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	
	if (tecla == 8 )
	{ tam = tam - 1 ; }
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
	
	if ( tam <= dec )
	{ campo.value = vr ; }
	
	if ( (tam > dec) && (tam <= 5) ){
	campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
	campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
	}
	if ( (tam >= 9) && (tam <= 11) ){
	campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
	campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 15) && (tam <= 17) ){
	campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
	}

}




// FORMATA CEP *****************************************************************************************

function formata_cep(objeto, evento){
	
	var tecla = objeto.keyCode;
	var cep = evento;
	if(tecla != 8){
		if(cep.value.length == 5)  cep.value += '-';
		
	}
}









// ==============================================================================================================
// ==============================================================================================================
// FUNÇÕES PARA VALIDAR CAMPOS GERAL ***************************************************************************
// ==============================================================================================================
// ==============================================================================================================






// VALIDA CPF *****************************************************************************************

function valida_cpf (numcpf)
{
	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	numcpf1="";
	len = numcpf.length; x = len -1;
	// var numcpf = "12345678909";
	for (var i=0; i <= len - 3; i++) {
		y = numcpf.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ;
	if (dig1 == 11) dig1=0 ;
	numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (numcpf1.substring(i,i+1) * x);
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0;
	if (dig2 == 11) dig2=0;
	//alert ("Digito Verificador : " + dig1 + "" + dig2);
	if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
		return true;
	}
	return false;
}





// VALIDA CPF *****************************************************************************************

function valida_email (email)
{
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){ 
		return false;
	} 	
	else {
		return true;
	}
}





// ABRE POPUP CPF *************************************************************************************

function MM_openBrWindow(theURL,winName,features) {
	window.open(theURL,winName,features); 
}





// VALIDA CAMPOS *************************************************************************************

function valida_campos(acao){
	
	
	
	if(acao == 'contatar'){
		
		
		if(document.getElementById("contato_nome").value == ''){
			alert("Digite o nome completo.");
			document.getElementById("contato_nome").focus();
			return false;
		}
		
		if(valida_email(document.getElementById("contato_email").value) == false){
			alert("Digite o email corretamente.");
			document.getElementById("contato_email").focus();
			return false;
		};		
		
		if(document.getElementById("contato_telefone").value.length != 14){
			alert("Digite o telefone corretamente.");
			document.getElementById("contato_telefone").focus();			
			return false;
		}
		
		if(document.getElementById("contato_mensagem").value == ''){
			alert("Digite a sua mensagem.");
			document.getElementById("contato_mensagem").focus();			
			return false;
		}
		


		document.getElementById(acao).submit();
		
	}

	
}





// VALIDA CAMPOS *************************************************************************************

function limpa_campos(input){document.getElementById(input).value = '';}








