// Validar E-Mail se esta correto //
function validar_email(campo){
	if (campo.value == "") return;
	var goodEmail = (campo.value).match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (!goodEmail){
		campo.value = "";
		alert("Email inválido!");
		campo.focus();
	}
};
// Validar data se ele colocou uma data com formatação correta
function validar_data(obj, n_ano){
	if (obj.value == "") return;
	if (!ChecaData(obj.value, n_ano)){
        a = '';
        for(z=0;z<n_ano;z++){
            a = a+'a';
           }
		alert("Data inválida (dd/mm/"+a+")!");
		obj.value = "";
		obj.focus();
		return false;
	}
	return true;
}

function ChecaData(strDate, n_ano) {
	if (strDate.length < 1) return true;
	strDateArray = strDate.split("/");
	intday = parseInt(strDateArray[0], 10);
	intMonth = parseInt(strDateArray[1], 10);
	intYear = parseInt(strDateArray[2], 10);
	tama = strDateArray[2].length;
    if(tama < n_ano)
      {
       return false;
      }
	if (isNaN(intYear) || isNaN(intMonth) || isNaN(intYear)) return false;
	if (intMonth>12 || intMonth<1) return false;
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) return false;
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) return false;

	if (intMonth == 2){
		if (intday < 1) return false;
		if (LeapYear(intYear)){
			if (intday > 29) return false;
		}else{
			if (intday > 28) return false;
	   }
	}

	return true;
}
function LeapYear(intYear){
	if (intYear % 100 == 0){
		if (intYear % 400 == 0) return true;
	}else{
		if ((intYear % 4) == 0) return true;
	}
	return false;
}


//// Validação do formulario //
validacao = {
        IsIE: navigator.appName.toLowerCase().indexOf('microsoft')!=-1,
        AZ: /[A-Z]/i,
        Acentos: /[À-ÿ]/i,
        Num: /[0-9]/,
        carregar: function(parte){
                var Tags = ['input','textarea','select'];
                var i = 0;
                if (typeof parte == "undefined") parte = document;
                for(z=0;z<Tags.length;z++){
                        Inputs=parte.getElementsByTagName(Tags[z]);
                        if(Tags[z] == "select")
                          {
                           for(i=0;i<Inputs.length;i++)
                              {
                               if(this.validar_select(Inputs[i]) == 'nao'){return false;}
                              }
                          }
                        else
                          {
                        for(i=0;i<Inputs.length;i++)
                                if(('button,image,hidden,submit,reset').indexOf(Inputs[i].type.toLowerCase())==-1)
                                     if(this.validar(Inputs[i]) == 'nao'){return false;}
                          }
                }
        },
       validar_select: function(campo){
         validar_campo = campo.getAttribute('validar');
         conf_value = campo.getAttribute('conf_value');
         txt = campo.getAttribute('txt');
         if(!conf_value)
           {
            conf_value = '';
           }
         if(validar_campo == 'sim')
           {
            texto = campo.options[campo.selectedIndex].value;
            if(texto == conf_value)
              {
               alert(txt);
               campo.focus();
               return 'nao';
              }
           }
        },
       validar: function(campo){
         validar_campo = campo.getAttribute('validar');
         txt = campo.getAttribute('txt');
         conf_value = campo.getAttribute('conf_value');
         inverter = campo.getAttribute('inverter');
         foco = campo.getAttribute('foco');
         obs = campo.getAttribute('obs');
         n_min = campo.getAttribute('n_min');
         n_max = campo.getAttribute('n_max');
         tipo = campo.getAttribute('tipo');
         mascara = campo.getAttribute('mascara');
         nome_campo = campo.getAttribute('nome_campo');		 
         if(!conf_value)
           {
            conf_value = '';
           }
         if(!inverter)
           {
            inverter = '';
           }
         if(!foco)
           {
            foco = '';
           }
         if(n_min > 0)
           {
            if(n_min > campo.value.length && campo.value != '')
              {
               alert('Você deve digitar no mínimo '+n_min+' caracteres no campo '+nome_campo);
               campo.focus();
               return 'nao';
              }
           }
         if(n_max > 0)
           {
            if(n_max < campo.value.length && campo.value != '')
              {
               alert('Você deve digitar no máximo '+n_max+' caracteres '+nome_campo);
               campo.focus();
               return 'nao';
              }
           }
         if(validar_campo == 'sim')
           {
            if(inverter == 'sim')
              {
               if(campo.value != conf_value)
                 {
                  alert(txt);
                  if(foco == '')
                    {
                     campo.focus();
                    }
                  return 'nao';
                 }
              }
            else
              {
               if(campo.value == conf_value)
                 {
                  alert(txt);
                  if(foco == '')
                    {
                     campo.focus();
                    }
                  return 'nao';
                 }
              }
            if(obs == 'email')
              {
               email = campo.value;
               if(email.indexOf('@')==-1 || email.indexOf('.')==-1)
                 {
                  campo.value = '';
                  campo.focus();
                  alert('E-Mail incorreto');
                  return 'nao';
                 }
              }
            if(obs == 'camparacao')
              {
               n = campo.value;
               obs_txt = campo.getAttribute('obs_txt');
               n2 = campo.getAttribute('obs_campo');
               n3 = document.getElementById(n2).value;
               if(n != n3)
                 {
                  campo.value = '';
                  document.getElementById(n2).value = '';
                  document.getElementById(n2).focus();
                  alert(obs_txt);
                  return 'nao';
                 }
              }
            if(tipo == 'num' && mascara == '')
              {
               n = campo.value;
               n = parseInt(n);
               if(isNaN(n))
                 {
                  campo.value = '';
                  campo.focus();
                  alert('Este campo deve ser númerico');
                  return 'nao';
                 }
              }
             return 'sim';
           }
        },
       pergunta: function(txt){
          if(confirm(txt))
            {
             return true;
            }
          return false;
        }
}
