// JavaScript Document

/*
====================================================================
FUNCAO    : verificarCPF()
VERSAO    : 1.0 - 23/09/2005
DESCRICAO : Verifica se o parametro e um CPF valido ou nao.
--------------------------------------------------------------------
PARAMETROS: strCPF - Numero do CPF a ser verificado, com 11 carac-
            teres numericos, sem separadores e nem pontos.
RETORNOS  : False - O parametro nao e um CPF valido.
            True - O parametro e um CPF valido.
====================================================================
*/
function verificarCPF(strCPF)
{
   strCPF = strCPF.replace('.', '');
   strCPF = strCPF.replace('-', '');
   
   if(strCPF == null || strCPF == '' || isNaN(strCPF) || strCPF.length < 11 || strCPF.length > 11) {
		return false;
   }

   arrCPF = new Array(10);
   arrCPF[1] = parseInt(strCPF.substr(0, 1));
   arrCPF[2] = parseInt(strCPF.substr(1, 1));
   arrCPF[3] = parseInt(strCPF.substr(2, 1));
   arrCPF[4] = parseInt(strCPF.substr(3, 1));
   arrCPF[5] = parseInt(strCPF.substr(4, 1));
   arrCPF[6] = parseInt(strCPF.substr(5, 1));
   arrCPF[7] = parseInt(strCPF.substr(6, 1));
   arrCPF[8] = parseInt(strCPF.substr(7, 1));
   arrCPF[9] = parseInt(strCPF.substr(8, 1));
   arrCPF[10] = parseInt(strCPF.substr(9, 1));
   
   intVerificador = 10 * arrCPF[1] + 9 * arrCPF[2] + 8 * arrCPF[3] + 7 * arrCPF[4] + 6 * arrCPF[5] + 5 * arrCPF[6] + 4 * arrCPF[7] + 3 * arrCPF[8] + 2 * arrCPF[9];
   intResto = (parseInt(intVerificador, 10) * 10) % 11;
   
   if(intResto == 10)
      intPrimeiroDigito = 0;
   else
      intPrimeiroDigito = intResto;
   
   intVerificador = 11 * arrCPF[1] + 10 * arrCPF[2] + 9 * arrCPF[3] + 8 * arrCPF[4] + 7 * arrCPF[5] + 6 * arrCPF[6] + 5 * arrCPF[7] + 4 * arrCPF[8] + 3 * arrCPF[9] + 2 * arrCPF[10];
   intResto = (parseInt(intVerificador, 10) * 10) % 11
   
   if(intResto == 10)
      intSegundoDigito = 0;
   else
      intSegundoDigito = intResto;
   
   if(intPrimeiroDigito != strCPF.substr(9, 1) || intSegundoDigito != strCPF.substr(10, 1))
      alert("CPF inválido!");
   else
     return(true);
}

function validarEmail(strEmail)
{
   if(window.RegExp)
   {
      var strRegularString1 = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
      var strRegularString2 = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
      var strRegular1 = new RegExp(strRegularString1);
      var strRegular2 = new RegExp(strRegularString2);
      
      if(!strRegular1.test(strEmail) && strRegular2.test(strEmail))
      {
          return(true);
      }
      window.alert('Endereço de E-Mail Inválido.');
      return(false);
   }
   else
   {
      if(strUsername.indexOf("@") >= 0)
         return(true);
      
      window.alert('Endereço de E-Mail Inválido.');
      return(false);
   }
}

function calcdat(dia,mes,ano) {

	var string = "";
	var temp;
	var err = 0;
	string_dia = dia;
	string_mes = mes;
	string_ano = ano;
	
	string = string_dia + "/" + string_mes + "/" + string_ano;
	
	if (string!="") {
		var valid = "0123456789/";
		var ok = "yes";
		for (var i=0; i< string.length; i++) {
		temp = "" + string.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") err = 1;
		}
	if (string.length != 10) err=1
	d = string.substring(0, 2) // month
	c = string.substring(2, 3)// '/'
	b = string.substring(3, 5) // day
	e = string.substring(5, 6)// '/'
	f = string.substring(6, 10) // year
	if (b<1 || b>12) err = 1
	if (c != '/') err = 1
	if (d<1 || d>31) err = 1
	if (e != '/') err = 1
	if (f<1900 || f>2050) err = 1
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31) err=1
	}
		if (b==2){
			var g=parseInt(f/4)
			if (isNaN(g)) {
				err=1
			}
			if (d>29) err=1
			if (d==29 && ((f/4)!=parseInt(f/4))) err=1
		}
	}
	
		if (err==1) {
			window.alert('Preencha a Data corretamete');
		} else {
			return(true);
		}
				
}

function e_numero(strNumeros, nome_campo) {
	if (strNumeros != ""){
	
	var nome = nome_campo;
	
		if (isNaN(strNumeros)) {
			alert("Favor Utilizar apenas números no " + nome);
		} else {
			return(true);
		}
	}
}
function validaRG(strRG, nome_campo){
	if (strRG != "") {
		
		var nome = nome_campo;

	   strRG = strRG.replace('.', '');
	   strRG = strRG.replace('-', '');
	   
		if (isNaN(strRG)) {
			alert("RG inválido utilize somente números!");
		} else if (strRG.length < 6) {
			alert("RG inválido");
		} else {	
			return true;
		}
	}
}

function clientetopCadastrar()
{
  if((document.frmClienteTop.ds_nome.value.length > 0) &&
 	 (document.frmClienteTop.ds_email.value.length > 0) && 
	 (document.frmClienteTop.ds_empresa.value.length > 0) && 
	 (document.frmClienteTop.ds_cargo.value.length > 0) && 
	 (document.frmClienteTop.ds_rg.value.length > 0) && 
	 (document.frmClienteTop.ds_cpf.value.length > 0) && 
	 (document.frmClienteTop.ds_datanasc_dia.value.length > 0) && 
	 (document.frmClienteTop.ds_datanasc_mes.value.length > 0) && 
	 (document.frmClienteTop.ds_datanasc_ano.value.length > 0) && 
	 (document.frmClienteTop.ds_estcivil.value.length > 0) && 
	 (document.frmClienteTop.ds_endereco.value.length > 0) && 
	 (document.frmClienteTop.ds_cidade.value.length > 0) && 
	 (document.frmClienteTop.ds_ddd.value.length > 0) && 
	 (document.frmClienteTop.ds_telefone.value.length > 0)) {
    
		if(validarEmail(document.frmClienteTop.ds_email.value)) {
			document.frmClienteTop.submit();
		}
		
		if(calcdat(document.frmClienteTop.ds_datanasc_dia.value, document.frmClienteTop.ds_datanasc_mes.value, document.frmClienteTop.ds_datanasc_ano.value)) {
			document.frmClienteTop.submit();
		}
		
		if (e_numero(document.frmClienteTop.ds_ddd.value, 'DDD')) {
			document.frmClienteTop.submit();
		}
		if (e_numero(document.frmClienteTop.ds_telefone.value, 'Telefone')) {
			document.frmClienteTop.submit();
		}
		
		if (verificarCPF(document.frmClienteTop.ds_cpf.value)) {
			document.frmClienteTop.submit();
		}
		
		if (validaRG(document.frmClienteTop.ds_rg.value)) {
			document.frmClienteTop.submit();
		}	
  }
  else
  {
    alert("Preencha corretamente os Campos Obrigatórios.");
  }
}