﻿var timerID = 0;
var tStart = null;

function fctTimer()
{
    tStart = new Date();
    document.form1.txtTimer.value = "0";
    timerID  = setTimeout("UpdateTimer()", 1000);
}

function UpdateTimer() 
{
   if(timerID) 
   {
      clearTimeout(timerID);
      clockID  = 0;
   }

   if(!tStart)
      tStart  = new Date();
   
   var tDate = new Date();
   var tDiff = tDate.getTime() - tStart.getTime();

   tDate.setTime(tDiff);

   document.form1.txtTimer.value = tDate.getSeconds();
   timerID = setTimeout("UpdateTimer()", 1000);
   if (document.form1.txtTimer.value == "7")
    {
        window.location.href = 'cadastro.aspx';
    }
}

function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
    window.open(theURL,winName,features);
}
function validaData(obj, args)
{
    if (ValidDate(args.Value) == true)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

function filtraCampo(campo){
	var s = "";
	var cp = "";
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) {  
		if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "."  && vr.substring(i,i + 1) != "," ){
		 	s = s + vr.substring(i,i + 1);}
	}
	campo.value = s;
	return cp = campo.value
}

function formataData(campo){
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

	if ( tam > 2 && tam < 5 )
		campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
	if ( tam >= 5 && tam <= 10 )
		campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
}

function formataCep(campo){
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

	if ( tam > 2 && tam < 5 )

		campo.value = vr.substr( 0, tam - 2  ) + '.' + vr.substr( tam - 2, tam );
	if ( tam >= 5 && tam <= 10 )
		campo.value = vr.substr( 0, 5 ) + '-' + vr.substr( 5, 3 );
}

function formataCPF(campo){
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

//	if ( tam > 2 && tam < 5 )

		campo.value = vr.substr( 0, tam - 2  ) + '.' + vr.substr( tam - 2, tam );
//	if ( tam >= 5 && tam <= 10 )
		campo.value = vr.substr( 0, 3 ) + '.' + vr.substr( 3, 3 ) + '.' + vr.substr( 6, 3 ) + '-' + vr.substr( 9, 3 );
}

function formataValor(campo) {
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

	if ( tam <= 2 ){ 
 		campo.value = vr ; }
 	if ( (tam > 2) && (tam <= 5) ){
 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 6) && (tam <= 8) ){
 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, 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 - 2, 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 - 2, tam ) ; }
 	if ( (tam >= 15) && (tam <= 18) ){
 		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 ) ;}
}

function moveRelogio()
{
    momentoAtual = new Date(); 
    hora = momentoAtual.getHours(); 
    minuto = momentoAtual.getMinutes(); 
    if (minuto < 10){
        minuto = "0" + minuto;
    }

    horaImprimivel = hora + " : " + minuto;
    document.frmCadastro.relogio.value = horaImprimivel;
    setTimeout("moveRelogio()",1000);
}

function OpenPopup(page, width, height)
{
	popup = window.open (page, 'popup', 'width=' + width + ', height=' + height + 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no')
}

function OpenPopupNovo(page, width, height)
{
	popup = window.open (page, 'popup', 'width=' + width + ', height=' + height + 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes')
}


function ValidaEmail(email){
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid

	if (!reg1.test(email) && reg2.test(email)) // if syntax is valid
		return true;
	else
		return false;
}
function ValidaRadioButton(frmCampo){
	botoes = frmCampo.length;
	for (i=0;i<botoes;i++)
	{
		if(frmCampo[i].checked)
			return true;
	}
return false;
}
 
function Trim(str)
{
while (str.charAt(0)  == " ")
str = str.substr(1,str.length -1);

while (str.charAt(str.length-1) == " ")
str = str.substr(0,str.length-1);

return str;
} 

function ValidaDropDown(frmDropDown){
	if (frmDropDown.value == "")
		return false;
	else
		return true;
}
function validacpf(s){ 

if ((s=="00000000000")||(s=="11111111111")||(s=="22222222222")||(s=="33333333333")||(s=="44444444444")||(s=="55555555555")||(s=="6666666666")||(s=="77777777777")||(s=="88888888888")||(s=="9999999999"))
 return false;
var i;
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);   
}
   
if (d1 == 0){   
//alert("CPF Invalido")   
return false; 
	    }  

d1 = 11 - (d1 % 11);
   
if (d1 > 9) d1 = 0; 
  
if (dv.charAt(0) != d1)   
{ 
//alert("CPF Invalido")   
return false; 
} 
  
  
d1 *= 2; 
  
for (i = 0; i < 9; i++)  
{   
d1 += c.charAt(i)*(11-i);   
} 
  
d1 = 11 - (d1 % 11); 
  
if (d1 > 9) d1 = 0; 
  
if (dv.charAt(1) != d1)  
{   
//alert("CPF Invalido")  
return false;   
} 

  
return true; 
  
} 
function ValidDate(objName) {
    var strDate;
    var strDateArray;
    var strDay;
    var strMonth;
    var strYear;
    var booFound = false;
    var intday;
    var intMonth;
    var intYear;
    var datefield = objName;

    //var strSeparatorArray = new Array("-"," ","/",".");
    var strSeparatorArray = new Array("/");

    var intElementNr;
    var strMonthArray = new Array(12);
    strMonthArray[0] = "Jan";
    strMonthArray[1] = "Feb";
    strMonthArray[2] = "Mar";
    strMonthArray[3] = "Apr";
    strMonthArray[4] = "May";
    strMonthArray[5] = "Jun";
    strMonthArray[6] = "Jul";
    strMonthArray[7] = "Aug";
    strMonthArray[8] = "Sep";
    strMonthArray[9] = "Oct";
    strMonthArray[10] = "Nov";
    strMonthArray[11] = "Dec";
    strDate = objName;

    if (strDate.length < 5) {
        if (strDate.length < 1) {
            return true;
        } else {
            return false;
        }
    }

    //Separator - Day - Month - Year
    for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
        if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
            strDateArray = strDate.split(strSeparatorArray[intElementNr]);
            if (strDateArray.length != 3) {
                err = 1;
                return false;
            } else {
                strDay = strDateArray[0];
                strMonth = strDateArray[1];
                strYear = strDateArray[2];
            }
            booFound = true;
        }

    }

    if (!booFound) { return false; }

    if (strYear.length == 2) {
        strYear = '20' + strYear;
    }

    intday = parseInt(strDay, 10);
    if (isNaN(intday)) {
        err = 2;
        return false;
    }

    intMonth = parseInt(strMonth, 10);
    if (isNaN(intMonth)) {
        for (i = 0; i < 12; i++) {
            if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
                intMonth = i + 1;
                strMonth = strMonthArray[i];
                i = 12;
            }
        }
        if (isNaN(intMonth)) {
            err = 3;
            return false;
        }
    }
    intYear = parseInt(strYear, 10);
    if (isNaN(intYear)) {
        err = 4;
        return false;
    }
    if (intMonth > 12 || intMonth < 1) {
        err = 5;
        return false;
    }
    if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
        err = 6;
        return false;
    }
    if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
        err = 7;
        return false;
    }
    if (intMonth == 2) {
        if (intday < 1) {
            err = 8;
            return false;
        }
        if (LeapYear(intYear) == true) {
            if (intday > 29) {
                err = 9;
                return false;
            }
        } else {
            if (intday > 28) {
                err = 10;
                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;
}


/////////////////////////////////////////////////

function valida_cnpj(cnpj)
      { 
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais; 
      digitos_iguais = 1; 
      if (cnpj.length < 14 && cnpj.length < 15) 
            return false; 
      for (i = 0; i < cnpj.length - 1; i++) 
            if (cnpj.charAt(i) != cnpj.charAt(i + 1)) 
                  { 
                  digitos_iguais = 0; 
                  break; 
                  } 
      if (!digitos_iguais) 
            { 
            tamanho = cnpj.length - 2 
            numeros = cnpj.substring(0,tamanho); 
            digitos = cnpj.substring(tamanho); 
            soma = 0; 
            pos = tamanho - 7; 
            for (i = tamanho; i >= 1; i--) 
                  { 
                  soma += numeros.charAt(tamanho - i) * pos--; 
                  if (pos < 2) 
                        pos = 9; 
                  } 
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
            if (resultado != digitos.charAt(0)) 
                  return false; 
            tamanho = tamanho + 1; 
            numeros = cnpj.substring(0,tamanho); 
            soma = 0; 
            pos = tamanho - 7; 
            for (i = tamanho; i >= 1; i--) 
                  { 
                  soma += numeros.charAt(tamanho - i) * pos--; 
                  if (pos < 2) 
                        pos = 9; 
                  } 
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
            if (resultado != digitos.charAt(1)) 
                  return false; 
            return true; 
            } 
      else 
            return false; 
      }  
 
 ////////////////////////////////////////////////////////////////////
 
function preenche(campo){

	var strText = campo.value;
	var tamanho = strText.length;

	campo.value = "";
	for (i=0; i < tamanho; i++){
		if (strText.charAt(i) != "/" && strText.charAt(i) != "-" && strText.charAt(i) != "." && strText.charAt(i) != ",")
			campo.value = campo.value + strText.charAt(i);
	}

	strText = campo.value;
	tamanho = strText.length;

	if (strText != "" ) {

		var intTam = 14 - parseInt(tamanho);

		//preenchimento do campo
		for (i=0; i<intTam; i++){
			strText = "0" + strText;
		}

		//atribuição da variável ao campo
		campo.value = strText;

	}

}

function Trim(str)
{
while (str.charAt(0) == " ")
str = str.substr(1,str.length -1);

while (str.charAt(str.length-1) == " ")
str = str.substr(0,str.length-1);

return str;
} 

function fctVerificaNumero(e)
{
    nav = navigator.appName;
    if(nav == "Microsoft Internet Explorer")
    {
        var varCaractere = String.fromCharCode(event.keyCode)
        if (!fctEhDigitoNumerico(varCaractere))
        {
            event.returnValue = false;
        }
    }
    else if(nav == "Netscape")
    {
        var varCaractere = String.fromCharCode(e.which);
        if (e.keyCode == 0)
        {
            if (!fctEhDigitoNumerico(varCaractere))
            {
                e.preventDefault? e.preventDefault() : e.returnValue = false; 
            }
        }
    }
}

function verificaNumero()
{
	var varCaractere = String.fromCharCode(event.keyCode)
	if (!fctEhDigitoNumerico(varCaractere))
	{
		event.returnValue = false;
	}
}

function fctEhDigitoNumerico(istrDig)
{
	if((istrDig == '0') || (istrDig == '1') || (istrDig == '2') || (istrDig == '3') || (istrDig == '4') || (istrDig =='5') || (istrDig == '6') || (istrDig == '7') || (istrDig == '8') || (istrDig == '9')) 
	{
		return true;
	}
	else
	{
		return false;
	}
}

function fncBuscaCEP()
{
	var cep = document.frmCadastro.txtCEP1.value + document.frmCadastro.txtCEP2.value;
	window.frmCEP.location.reload('BuscaCEP.aspx?cep=' + cep + '&form=frmCadastro');
	document.frmCadastro.txtNumero.focus();
}

function checacpf(obj, args)
{
    if (validacpf(args.Value) == true)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

function checacnpj(obj, args)
{
    if (valida_cnpj(args.Value) == true)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

function openPopUp(url, strTarget, strScrollbars, strToolbar, strLocation, strDirectories, strStatus, strMenubar, strResizable, objWidth, objHeight)
{	
    var popLeft = (screen.width/2) - (objWidth/2);
    var popTop = (screen.height/2) - (objHeight/2);
    window.open(url,strTarget,"scrollbars="+strScrollbars+", toolbar="+strToolbar+", location="+strLocation+", directories="+strDirectories+", status="+strStatus+", menubar="+strMenubar+", resizable="+strResizable+", width="+objWidth+", height="+objHeight+", left="+popLeft+", top="+popTop);
}

