/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Padrões /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

/*function validaCampos(form, retorno){
	
	elementos = document.getElementById(form).elements;
	
	for (i=0; i<elementos.length; i++) {
		
		// preenchimento ...
		if (elementos[i].hasAttribute("obr")) { 		
			typ = elementos[i].getAttribute("type");
			if ((typ == "text")&&(elementos[i].value == "")) {
				document.getElementById(retorno).innerHTML = elementos[i].getAttribute("obr")+" não preenchido!";
				elementos[i].focus(); return false;
			}
			if ((typ == "textarea")&&(elementos[i].value == "")) {
				document.getElementById(retorno).innerHTML = elementos[i].getAttribute("obr")+" não preenchido!";
				elementos[i].focus(); return false;
			}	
			if ((typ == "file")&&(elementos[i].value == "")) {
				document.getElementById(retorno).innerHTML = elementos[i].getAttribute("obr")+" não preenchido!";
				elementos[i].focus(); return false;
			}
			if ((typ == "select")&&(elementos[i].value == "")){	
				document.getElementById(retorno).innerHTML = elementos[i].getAttribute("obr")+" não preenchido!";				
				elementos[i].focus(); return false;				
			}			
			if (typ == "radio") {
				obj = document.getElementsByName(elementos[i].name);
				flag =false; for(j=0; j < obj.length; j++) if (obj[j].checked) flag = true;
				if (flag == false){	
					document.getElementById(retorno).innerHTML = elementos[i].getAttribute("obr")+" não preenchido!";			
					return false;
				}				
			}
			if (typ == "checkbox") {
				obj = document.getElementsByName(elementos[i].name);
				flag =false; for(j=0; j < obj.length; j++) if (obj[j].checked) flag = true;
				if (flag == false){	
					document.getElementById(retorno).innerHTML = elementos[i].getAttribute("obr")+" não preenchido!";				
					return false;
				}				
			}
		}
		
		// validação ...
		if (elementos[i].hasAttribute("val")) {			
			val = elementos[i].getAttribute("val");				
			if ((val == "mail")&&(elementos[i].value != "")) {
				if (!valida_email(elementos[i])) {
					document.getElementById(retorno).innerHTML = "E-mail Invalido!";	
					elementos[i].focus(); return false;		
				}				
			}			
			if ((val == "data")&&(elementos[i].value != "")) {
				if (!valida_data(elementos[i])) {					
					document.getElementById(retorno).innerHTML = "Data Invalida!";	
					elementos[i].focus(); return false;					
				}				
			}		
		}		
	}
	
	document.getElementById(form).submit();
}
*/
function valida_email(campo) {
	campo = document.getElementById(campo);
	email = campo.value; pos = email.indexOf("@",1);
	if ((email.length > 8) && (pos > 0) && (email.indexOf(".",pos) > 0)) return true; else return false;
}

function valida_data(pObj) {
	pObj = document.getElementById(pObj);
  var expReg = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/(19|20)?\d{2}$/;
  var aRet = true;
  if ((pObj) && (pObj.value.match(expReg)) && (pObj.value != '')) {
    var dia = pObj.value.substring(0,2);
    var mes = pObj.value.substring(3,5);
    var ano = pObj.value.substring(6,10);
    if ((mes == 4 || mes == 6 || mes == 9 || mes == 11 ) && dia > 30)
      aRet = false;
    else
      if ((ano % 4) != 0 && mes == 2 && dia > 28)
        aRet = false;
      else
        if ((ano%4) == 0 && mes == 2 && dia > 29)
          aRet = false;
  }  else
    aRet = false;  
  return aRet;
}

function KeyPressEnter(event){
	if ( !event ) var event = window.event; 	
	if ((event.keyCode == 13)||(event.which == 13)) return true; else return false;	
}

function valida_cpf(campo)
      {
		cpf = document.getElementById(campo).value; 
		
		cpf = cpf.replace('.',"");
		cpf = cpf.replace('.',""); 
		cpf = cpf.replace('-',"");   
		  
      var numeros, digitos, soma, i, resultado, digitos_iguais;
      digitos_iguais = 1;
      if (cpf.length < 11)
            return false;
      for (i = 0; i < cpf.length - 1; i++)
            if (cpf.charAt(i) != cpf.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            numeros = cpf.substring(0,9);
            digitos = cpf.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--)
                  soma += numeros.charAt(10 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            numeros = cpf.substring(0,10);
            soma = 0;
            for (i = 11; i > 1; i--)
                  soma += numeros.charAt(11 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      }

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Ramos ///////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

function visibleTop(val1, val2, val3) {
	for(i=1; i <= val3; i++){
		img = i + 2;
		if(val2 == i){
			 document.getElementById(val1+i).style.display = "";			 
			 document.getElementById("top"+i).style.backgroundImage = "url(images/conteudo."+img+".btn.png)";
			 document.getElementById("top"+i).style.backgroundRepeat = "no-repeat";
		} else {
			 document.getElementById(val1+i).style.display = "none";
			 document.getElementById("top"+i).style.backgroundImage = "url(images/conteudo."+img+".btnI.png)";
			 document.getElementById("top"+i).style.backgroundRepeat = "no-repeat";		 
		}
	}
	/*document.getElementById("top1").style.width = "94px";
	document.getElementById("top2").style.width = "77px";
	document.getElementById("top3").style.width = "80px";
	document.getElementById("top4").style.width = "89px";
	document.getElementById("top5").style.width = "133px";
	document.getElementById("top6").style.width = "109px";	*/
}


function validaCampos(form, retorno){
	
	 var contador = 0;
	 input = document.getElementById("formulario").getElementsByTagName("input"); // Pega os elementos da div com id corpo
		for(a=0;a<input.length;a++){ 
					campo = input.item(a).id;
					nome = input.item(a).title;
			if(input.item(a).lang == "valida"){
				if(input.item(a).value == ""){
					contador = 1;					
					document.getElementById(campo).focus();
					document.getElementById(retorno).innerHTML = nome+" não preenchido!";
					break;
				}else if(nome == "Email") {
					if (!valida_email(campo)) {
						contador = 1;
						document.getElementById(retorno).innerHTML = "E-mail Invalido!";	
						
						return false;
						break;
						document.getElementById(campo).focus();	
					}	
				}else if(nome == "Data") {
					if (!valida_data(campo)) {
						contador = 1;
						document.getElementById(retorno).innerHTML = "Data Inválida!";	
						
						return false;
						break;
						document.getElementById(campo).focus();	
					}	
				}else if(nome == "CPF") {
					if (!valida_cpf(campo)) {
						contador = 1;
						document.getElementById(retorno).innerHTML = "CPF Inválido!";	
						
						return false;
						break;
						document.getElementById(campo).focus();	
					}	
				}
			}
		}
		if(contador==0){
			sel = document.getElementById("formulario").getElementsByTagName("textarea"); // Pega os elementos da div com id corpo
			for(a=0;a<sel.length;a++){ 
				if(sel.item(a).lang == "valida"){
					if(sel.item(a).value == ""){
						contador = 1;
						campo = sel.item(a).id;
						nome = sel.item(a).title;
						document.getElementById(campo).focus();
						document.getElementById(retorno).innerHTML = nome+" não preenchido!";
						break;
					}
				}
			}
		}
		if(contador==0){
			sel = document.getElementById("formulario").getElementsByTagName("select"); // Pega os elementos da div com id corpo
			for(a=0;a<sel.length;a++){ 
				if(sel.item(a).lang == "valida"){
					if(sel.item(a).value == ""){
						contador = 1;
						campo = sel.item(a).id;
						nome = sel.item(a).title;
						document.getElementById(campo).focus();
						document.getElementById(retorno).innerHTML = nome+" não preenchido!";
						break;
					}
				}
			}
		}
		if(contador==0){
			sel = document.getElementById("formulario").getElementsByTagName("radio"); // Pega os elementos da div com id corpo
			for(a=0;a<sel.length;a++){ 
				if(sel.item(a).lang == "valida"){
					alert(sel.item(a).checked);
					if(!sel.item(a).checked){
						contador = 1;
						campo = sel.item(a).id;
						nome = sel.item(a).title;
						document.getElementById(campo).focus();
						document.getElementById(retorno).innerHTML = nome+" não preenchido!";
						break;
					}
				}
			}
		}
		if(contador==0){
			sel = document.getElementById("formulario").getElementsByTagName("checkbox"); // Pega os elementos da div com id corpo
			for(a=0;a<sel.length;a++){ 
				if(sel.item(a).lang == "valida"){
					if(sel.item(a).value == ""){
						contador = 1;
						campo = sel.item(a).id;
						nome = sel.item(a).title;
						document.getElementById(campo).focus();
						document.getElementById(retorno).innerHTML = nome+" não preenchido!";
						break;
					}
				}
			}
		}
		
		
		if(contador == 0){
			return true;
		//document.getElementById(form).submit();
		}else{
			return false;
		}
		
}

