// JavaScript Document

//==========================================================================
// FUNÇÃO GET ELEMENT BY ID
//==========================================================================
function gE(ID) {return document.getElementById(ID)}

//==========================================================================
// FUNÇÃO LIMPA STRING E DEIXA SOMENTE NUMEROS
//==========================================================================
function limpa_string(S){ 
var Digitos = "0123456789"; 
var temp = ""; 
var digito = ""; 

	for (var i=0; i<S.length; i++) { 
	digito = S.charAt(i); 
		if (Digitos.indexOf(digito)>=0) { 
		 temp=temp+digito
		} 
	} //for 
return temp 
} 

//==========================================================================
// FUNÇÃO ON KEY PRESS DATAS
//==========================================================================
function nData(tipo,campo) {
	campo = gE(campo)

	if (tipo==1){	//DIA
		if (campo.value < 1 || campo.value > 31 || isNaN(campo.value)){
				campo.value = "";
			 	return false;
		}
	}else if (tipo == 2){	//MES
		if (campo.value < 1 || campo.value > 12 || isNaN(campo.value)){
				campo.value = "";
			 	return false;
    }
	}else if (tipo == 3){ //ANO
		if (isNaN(campo.value)){
				campo.value = "";
				return false;
	 	}
	
	}
}

//==========================================================================
// FUNÇÃO ON KEY PRESS EXTRITAMENTE NUMEROS SEM PONTOS OU VIRGULAS
//==========================================================================
function numero(event) {
	var tecla
	if(event.which ) tecla = event.which 
	if(event.keyCode) tecla = event.keyCode
	//alert(tecla);
	if (tecla == 8 || tecla == 9 || tecla == 48) {
			return true;
	} else {		
		if (tecla<49 || tecla>57)
			return false;
	}
	return true;
}
//==========================================================================
// FUNÇÃO ON KEY PRESS SOMENTE NUMEROS COM VIRGULA (DECIMAL)
//==========================================================================
function decimal(event) {
	var tecla
	if(event.which ) tecla = event.which 
	if(event.keyCode) tecla = event.keyCode
	//alert(tecla);
	if (tecla == 8 || tecla == 9 || tecla == 44 || tecla == 46) {
			return true;
	} else {		
		if (tecla<48 || tecla>57)
			return false;
	}
	return true;
}
//==========================================================================
// FUNÇÃO ON KEY PRESS SOMENTE TEXTOS
//==========================================================================
function texto(event) {
	var tecla
	if(event.which ) tecla = event.which 
	if(event.keyCode) tecla = event.keyCode
	//alert(tecla);
	if (tecla == 8 || tecla == 9 || tecla == 46) {
			return true;
	} else {		
		if (tecla<48 || tecla>57)
			return true;
	}
	return false;
}

//==========================================================================
// FUNÇÃO CALCULO CPF
//==========================================================================
function calculoCPF(s) { 
	var i; 
	s = limpa_string(s); 
	var c = s.substr(0,9); 
	var dv = s.substr(9,2); 
	var d1 = 0; 
	
		for (i = 0; i < 9; i++){ 
		 d1 += c.charAt(i)*(10-i); 
		}//for

	if(d1 == 0) return false; 
	d1 = 11 - (d1 % 11); 
	if(d1 > 9) d1 = 0; 
	if(dv.charAt(0) != d1){ 
	return false; 
	} 

d1 *= 2; 

	for (i = 0; i < 9; i++){ 
		d1 += c.charAt(i)*(11-i); 
	}//for

d1 = 11 - (d1 % 11); 

	if(d1 > 9) d1 = 0; 
	if(dv.charAt(1) != d1){ 
	return false; 
	} 
	return true; 
} 

//==========================================================================
// FUNÇÃO CALCULO CNPJ
//==========================================================================
function calculoCNPJ(s) 
{ 
var i; 
s = limpa_string(s); 
var c = s.substr(0,12); 
var dv = s.substr(12,2); 
var d1 = 0; 
for (i = 0; i < 12; i++) 
{ 
d1 += c.charAt(11-i)*(2+(i % 8)); 
} 
if (d1 == 0) return false; 
d1 = 11 - (d1 % 11); 
if (d1 > 9) d1 = 0; 
if (dv.charAt(0) != d1) 
{ 
return false; 
} 

d1 *= 2; 
for (i = 0; i < 12; i++) 
{ 
d1 += c.charAt(11-i)*(2+((i+1) % 8)); 
} 
d1 = 11 - (d1 % 11); 
if (d1 > 9) d1 = 0; 
if (dv.charAt(1) != d1) 
{ 
return false; 
} 
return true; 
} 



//==========================================================================
// FUNÇÃO VALIDA FORM NO SUBMIT
//==========================================================================
function validar(form) {
ConfigStyle = "solid 2px #666666"
ConfigStyle2 = "solid 1px #B5BABF"
var passed = false;
var ok = false;
var campo;

	for (i = 1; i < form.length; i++) {
		campo = form[i].name;
		//alert(campo);
		
		if (form[i].type == "fieldset"){}
			if (form[i].type == "legend"){}

//=================================================
				if (form[i].type == "text" || form[i].type == "select-one" || form[i].type == "password" || form[i].type == "textarea"){//VALIDA CAMPOS DOS TIPOS MENCIONADOS NESTA LINHA
//=================================================				
				  if (form[i].alt!="naovalidar"){ // IF NAO VALIDAR
					  
					if (form[i].value == ""){ // SE VAZIO
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].focus();
						resposta.style.color = '#FF0000';
						resposta.innerHTML = "Campo "+campo+" obrigatório.";
						return false;
					}else{
						if (gE(campo+"erro")){
						gE(campo+"erro").innerHTML='';form[campo].style.border = ConfigStyle2;
						}
					}//if
						
					if(form[i].alt=="email"){	//VALIDA CAMPO EMAIL
						if (form[i].value.indexOf('@', 0) == -1 || form[i].value.indexOf('.com', 0) == -1){ 
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].focus();
						resposta.innerHTML = campo+" Incorreto!";
						return false;
						}else{form[campo].style.border = ConfigStyle2;}
					}//if
	
					if(form[i].alt=="ano"){	//VALIDA CAMPO ANO
						if (form[i].value < 1920 || form[i].value > 2005){
						//if (form[i].value.length < form[i].maxlength){ 
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = "Preencha o Campo "+campo+" corretamente com 4 digitos.";
						return false;
						}else{form[campo].style.border = ConfigStyle2;}
					}//if
	
					if(form[i].alt=="numeroDDD"){	//VALIDA CAMPO DDD
						if (form[i].value.length < form[i].maxLength){ 
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = "Preencha o Campo "+campo+" corretamente";
						return false;
						}else{form[campo].style.border = ConfigStyle2;}
					}//if
	
					if(form[i].alt=="numeroFone"){	//VALIDA CAMPO TELEFONE
						if (form[i].value.length < form[i].maxLength){ 
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = "Preencha o Campo "+campo+" corretamente";
						return false;
						}else{form[campo].style.border = ConfigStyle2;}
					}//if
	
					if(form[i].alt=="senhaCadastro" || form[i].alt=="senhaAcesso"){	//VALIDA CAMPO SENHA
						if (form[i].value.length < form[i].maxLength){ 
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = "O campo "+campo+" deve conter "+form[i].maxLength+" caracteres.";
						return false;
						}else{senha1=form[i].value;}
					}//if
	
					if(form[i].alt=="xsenhaCadastro"){	//VALIDA CAMPO CONFERE SENHA
						if (form[i].value.length < form[i].maxLength){ 
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = "O campo "+campo+" deve conter "+form[i].maxLength+" caracteres.";
						return false;
						}
						else if(form[i].value != senha1){
						form[campo].style.border = ConfigStyle;
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = "A confirmação da senha não confere!";
						return false;
						}
					} //******************
	
	
					if(form[i].alt=="cpf"){	//VALIDA CAMPO CPF
						if(form[i].value.length < form[i].maxLength || form[i].value== "11111111111" || form[i].value== "22222222222" || form[i].value== "33333333333" || form[i].value== "44444444444" || form[i].value== "55555555555" || form[i].value== "66666666666" || form[i].value== "77777777777" || form[i].value== "88888888888"){  //|| form[i].value== "99999999999"
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = campo+" INVÁLIDO!";
						return false;
						
						}else if(calculoCPF(form[campo].value) == false){ // CALCULO CPF RETORNA ERRO
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = campo+" INVÁLIDO!";
						return false;
						}//if
						else{
							resposta = gE(campo+"erro");
							resposta.innerHTML = '';
						}
					}//if
	
	
					if(form[i].alt=="cnpj"){	//VALIDA CAMPO CNPJ
						if(form[i].value.length < form[i].maxLength || form[i].value== "11111111111111" || form[i].value== "22222222222222" || form[i].value== "33333333333333" || form[i].value== "44444444444444" || form[i].value== "55555555555555" || form[i].value== "66666666666666" || form[i].value== "77777777777777" || form[i].value== "88888888888888" || form[i].value== "99999999999999"){ 
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = campo+" INVÁLIDO!";
						return false;
						
						}else if(calculoCNPJ(form[campo].value) == false){ // CALCULO CNPJ RETORNA ERRO
						form[campo].style.border = ConfigStyle;
						resposta = gE(campo+"erro");
						form[campo].value="";
						form[campo].focus();
						resposta.innerHTML = campo+" INVÁLIDO!";
						return false;
						}//if
						else{
							resposta = gE(campo+"erro");
							resposta.innerHTML = '';
						}
						
					}//if
//=================================================				
				  }// if nao validar
//=================================================				
				}//if
//=================================================
	}// For
}//Function

