// JavaScript Document


function selecionar_tudo(){ 
   for (i=0;i<document.form_veiculos.elements.length;i++) 
      if(document.form_veiculos.elements[i].type == "checkbox") 
         document.form_veiculos.elements[i].checked=1 

} 
function deselecionar_tudo(){ 
	for (i=0;i<document.form_veiculos.elements.length;i++) 
      if(document.form_veiculos.elements[i].type == "checkbox") 
         document.form_veiculos.elements[i].checked=0 
} 

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ConfirmaExclusao(){
	if(confirm('Tem certeza que deseja realmente excluir esse(s) dado(s)?')) return true; else return false;
}


function FecharJanela(){
	window.close();
}

function FormataData(Campo, teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 5)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
		if (tam >= 5 && tam <=10)
			Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
		}
	}
/////////////////////////////////////////////////////////////////
function FormataHora(Campo, teclapres){
	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(":", "");
	vr = vr.replace(":", "");

	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 5)
			Campo.value = vr.substr(0, 2) + ':' + vr.substr(2, tam);
		if (tam >= 5 && tam < 9)
			Campo.value = vr.substr(0,2) + ':' + vr.substr(2,2) + ':' + vr.substr(4,2);
		}
	}
/////////////////////////////////////////////////////////////////
function FormataCPF(Campo, teclapres){
	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8){
		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
}
/////////////////////////////////////////////////////////////////
function FormataControle(Campo, teclapres){
	var tecla = teclapres.keyCode;


	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	tam = vr.length + 1;

 if (tecla != 9 && tecla != 8)
		if (tam > 7){
			if (tam > 4 && tam < 9)
				Campo.value = vr.substr(0, 4) + '.' + vr.substr(4, tam);
			if (tam >= 9 && tam <13)
				Campo.value = vr.substr(0,4) + '.' + vr.substr(4,4) + '.' + vr.substr(8,tam-8);
			if (tam >= 13 && tam < 17)
				Campo.value = vr.substr(0,4) + '.' + vr.substr(4,4) + '.' + vr.substr(8,4) + '.' + vr.substr(12,tam-12);
			}
		else{
			if (tam > 1 && tam < 5)
				Campo.value = vr.substr(0, 1) + '.' + vr.substr(1, tam);
			if (tam >= 5 && tam < 8)
				Campo.value = vr.substr(0,1) + '.' + vr.substr(1,3) + '.' + vr.substr(4,tam-4);
			}
}
/////////////////////////////////////////////////////////////////
function FormataCNPJ(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;


	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
		}
}
/////////////////////////////////////////////////////////////////
function FormataITR(Campo, teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8){
		if (tam > 1 && tam < 5)
			Campo.value = vr.substr(0, 1) + '.' + vr.substr(1, tam);
		if (tam >= 5 && tam < 8)
			Campo.value = vr.substr(0,1) + '.' + vr.substr(1,3) + '.' + vr.substr(4,tam-4);
		if (tam >= 8)
			Campo.value = vr.substr(0,1) + '.' + vr.substr(1,3) + '.' + vr.substr(4,3) + '-' + vr.substr(7,1);
		}
}
/////////////////////////////////////////////////////////////////
function SaltaCampo(campo,prox,tammax,teclapres){

	var tecla = teclapres.keyCode;
	vr = document.forms[0].elements[campo].value;

	if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108){
	   document.form[campo].value = vr.substr(0, vr.length - 1); }
	else {
		vr = vr.replace("-","");
		vr = vr.replace("/","");
		vr = vr.replace("/","");
		vr = vr.replace(",","");
		vr = vr.replace(".","");
		vr = vr.replace(".","");
		vr = vr.replace(".","");
		vr = vr.replace(".","");
		tam = vr.length;

		if (tecla != 0 && tecla != 9 && tecla != 16 )
			if ( tam == tammax )
				document.forms[0].elements[prox].focus();
		}
}
//////////////////////////////////////////////////////////////////
function CalcularDV(sCampo, iPeso){

	var iTamCampo;
	var iPosicao, iDigito;
	var iSoma1 = 0;
	var iSoma2=0;
	var iDV1, iDV2;

	iTamCampo = sCampo.length;

	for (iPosicao=1; iPosicao<=iTamCampo; iPosicao++){
		iDigito = sCampo.substr(iPosicao-1, 1);
		iSoma1 = parseInt(iSoma1,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao, iPeso)),10);
		iSoma2 = parseInt(iSoma2,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao + 1, iPeso)),10);
		}

	iDV1 = 11 - (iSoma1 % 11);
	if (iDV1 > 9)
		iDV1 = 0;

	iSoma2 = iSoma2 + (iDV1 * 2);
	iDV2 = 11 - (iSoma2 % 11);
	if (iDV2 > 9)
		iDV2 = 0;

	Ret = (parseInt(iDV1 * 10,10) + parseInt(iDV2));

	Ret = "0" + Ret;
	Ret = Ret.substr(Ret.length - 2,Ret.length);

	return(Ret);
}

//////////////////////////////////////////////////////////////////
function Calcular_Peso(iPosicao, iPeso){

	//Pesos
	//CPF 11
	//CNPJ 9
	return (iPosicao % (iPeso - 1)) + 2;
	}

/////////////////////////////////////////////////////////////////
function LimpaCampo(sValor,iBase){
	var tam = sValor.length
	var saida = new String
	for (i=0;i<tam;i++)
		if (!isNaN(parseInt(sValor.substr(i,1),iBase)))
			saida = saida + String(sValor.substr(i,1));
	return (saida);
	}
/////////////////////////////////////////////////////////////////
function TestaForm(theForm,iTipo){
	if (!(TestaNI (theForm.NI,iTipo)))
		return (false);

	if (!(TestaControle (theForm.Controle)))
		return (false);

	var controle = LimpaCampo(theForm.Controle.value,16);
	var tam = controle.length;

	if	(tam == 16){
		if (!(TestaData(theForm.Data)))
			return (false);

		if (!(TestaHora(theForm.Hora)))
			return (false);
		}
	else{
		if (theForm.Data.value != "")	{
			alert('Só preencher data da emissão para certidão emitida pela Internet');
			theForm.Data.value = "";
			theForm.Data.focus();
			return(false);
		}
		if (theForm.Hora.value != "")	{
			alert('Só preencher hora da emissão para certidão emitida pela Internet');
			theForm.Hora.value = "";
			theForm.Hora.focus();
			return(false);
		}
	}
	return (true);
}
/////////////////////////////////////////////////////////////////
function TestaFormItr(theForm,iTipo){
	if (!(TestaNI (theForm.NI,iTipo)))
		return (false);

	if (!(TestaControle (theForm.Controle)))
		return (false);

	return (true);
	}
/////////////////////////////////////////////////////////////////
function TestaNI(cNI,iTipo){
	var NI
	NI = LimpaCampo(cNI.value,10);
	switch (iTipo) {
		case 1:
			if (NI.length != 14){
				alert('O número do CNPJ informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}

			if (NI.substr(12,2) != CalcularDV(NI.substr(0,12), 9)){
				alert('O número do CNPJ informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}
			break;

		case 2:
			if (NI.length != 11){
				alert('O número do CPF informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}

			if (NI.substr(9,2) != CalcularDV(NI.substr(0,9), 11)){
				alert('O número do CPF informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}
			break;

		case 3:
			if (NI.length != 8){
				alert('O número do ITR informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}
			var dv = new String();
			dv = CalcularDV(NI.substr(0,7), 9);
			dv = dv.substr(0,1);
			if (NI.substr(7,1) != dv){
				alert('O número do ITR informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}
			break;

		default:
			return(false);
		}
	return (true);
	}
/////////////////////////////////////////////////////////////////
function TestaControle(cControle){
	var controle;
	controle = LimpaCampo(cControle.value,16);
	var tam = controle.length;
	if	(tam != 7 && tam != 16)	{
		alert('O número da Certidão/Controle informado está incorreto');
		cControle.value = "";
		cControle.focus();
		return(false);
		}
	return(true);
	}
/////////////////////////////////////////////////////////////////
function TestaHora(cHora){
	var hora;
	hora = LimpaCampo(cHora.value,10);
	var tam = hora.length;
	if	(tam != 6){
		alert('A hora está incorreta');
		cHora.value = "";
		cHora.focus();
		return(false);
		}

	var hr = hora.substr(0,2)
	var min = hora.substr (2,2)
	var seg = hora.substr (4,2)
	if ((hr > 23) || (min > 59) || (seg > 59)){
		alert('A hora está incorreta');
		cHora.value = "";
		cHora.focus();
		return(false);
		}
	return(true);
}
/////////////////////////////////////////////////////////////////
function TestaData(cData){
	var data;
	data = LimpaCampo(cData.value,10);
	var tam = data.length;
	if	(tam != 8){
		alert('A data está incorreta');
		cData.value = "";
		cData.focus();
		return(false);
		}
	var dia = data.substr(0,2)
	var mes = data.substr (2,2)
	var ano = data.substr (4,4)
	if (ano < 1994){
		alert('A data está incorreta');
		cData.value = "";
		cData.focus();
		return(false);
		}

	switch (mes){
		case '01':
			if  (dia > 31){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '02':
			if  (dia > 29){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '03':
			if  (dia <= 31){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '04':
			if  (dia <= 30){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '05':
			if  (dia <= 31){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '06':
			if  (dia <= 30) {
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '07':
			if  (dia <= 31){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '08':
			if  (dia <= 31){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '09':
			if  (dia <= 30){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '10':
			if  (dia <= 31){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '11':
			if  (dia <= 30) {
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		case '12':
			if  (dia <= 31){
				alert('A data está incorreta');
				cData.value = "";
				cData.focus();
				return(false);
				}
			break;
		}

	return(true);
	}
///////////////////////////////////////////////////////////////////
function VerAlfaNumerico(pInd){
	var pValor = document.forms[0].elements[pInd].value
	var AuxTam = pValor.length
	for(var j=0;j<AuxTam;j++)
		if ((!IndAlfaNumerico(pValor.charAt(j))) || (pValor.charAt(j) == " ")){
			document.forms[0].elements[pInd].focus();
			document.forms[0].elements[pInd].value = pValor = pValor.substring(0,j)
			}
	}
////////////////////////////////////////////////////////////////////
function IndAlfaNumerico(N){
	for(var i=0;i<10;i++)
	if(N == i)
		return true;
	return false;
	}
////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////
function TESTA(CNUMB,CTYPE)
{
  if(Verify(CNUMB, CTYPE))
  {
    alert(CTYPE+" válido!");
  }
  else
  {
    alert(CTYPE+" inválido!");
  }
  return;
}

//////////////////////////////////////////////////////////////////
function ClearStr(str, char)
{
  while((cx=str.indexOf(char))!=-1)
  {
    str = str.substring(0,cx)+str.substring(cx+1);
  }
  return(str);
}

///////////////////////////////////////////////////////////////
function ParseNumb(c)
{
  c=ClearStr(c,'-');
  c=ClearStr(c,'/');
  c=ClearStr(c,',');
  c=ClearStr(c,'.');
  c=ClearStr(c,'(');
  c=ClearStr(c,')');
  c=ClearStr(c,' ');
  if((parseFloat(c) / c != 1))
  {
    if(parseFloat(c) * c == 0)
    {
      return(c);
    }
    else
    {
      return(0);
    }
  }
  else
  {
    return(c);
  }
}

///////////////////////////////////////////////////////////////////
function Verify(CNUMB,CTYPE)
{
  CNUMB=ParseNumb(CNUMB)
  if(CNUMB == 0)
  {
    return(false);
  }
  else
  {
    g=CNUMB.length-2;
    if(TestDigit(CNUMB,CTYPE,g))
    {
      g=CNUMB.length-1;
      if(TestDigit(CNUMB,CTYPE,g))
      {
        return(true);
      }
      else
      {
        return(false);
      }
    }
    else
    {
      return(false);
    }
  }
}

///////////////////////////////////////////////////////////////////////
function TestDigit(CNUMB,CTYPE,g)
{
  var dig=0;
  var ind=2;
  for(f=g;f>0;f--)
  {
    dig+=parseInt(CNUMB.charAt(f-1))*ind;
    if (CTYPE=='CNPJ')
    { if(ind>8) {ind=2} else {ind++} }
    else
    { ind++ }
  }
  dig%=11;
  if(dig<2)
  {
    dig=0;
  }
  else
  {
    dig=11-dig;
  }
  if(dig!=parseInt(CNUMB.charAt(g)))
  {
    return(false);
  }
  else
  {
    return(true);
  }
}


function formatar(src, mask)
{
  	var i = src.value.length;
  	var saida = mask.substring(0,1);
  	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida)
  	{
        src.value += texto.substring(0,1);
  	}
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function validaCNPJ() {
       CNPJ = document.formulario.cnpj.value;
                 erro = new String;
                 if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! \n\n"; 
                 if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
                 if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
                 }
                 //substituir os caracteres que não são números
               if(document.layers && parseInt(navigator.appVersion) == 4){
                       x = CNPJ.substring(0,2);
                       x += CNPJ. substring (3,6);
                      x += CNPJ. substring (7,10);
                      x += CNPJ. substring (11,15);
                      x += CNPJ. substring (16,18);
                      CNPJ = x; 
               } else {
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace ("-","");
                       CNPJ = CNPJ. replace ("/","");
               }
               var nonNumbers = /\D/;
               if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n"; 
               var a = [];
               var b = new Number;
               var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
               for (i=0; i<12; i++){
                       a[i] = CNPJ.charAt(i);
                       b += a[i] * c[i+1];
}
               if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
               b = 0;
               for (y=0; y<13; y++) {
                       b += (a[y] * c[y]); 
               }
               if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
               if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
                       erro +="Dígito verificador com problema!";
               }
               if (erro.length > 0){
                       alert(erro);
					   document.formulario.cnpj.focus();
                      return false;
               } 
			   //else {
               //     alert("CNPJ valido!");
              // }
               return true;
       }
	   
function validaCPF() {
                 cpf = document.validacao.cpfID.value;
                 erro = new String;
                 if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
                 var nonNumbers = /\D/;
                 if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n"; 
                 if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
                         erro += "Numero de CPF invalido!"
               }
               var a = [];
               var b = new Number;
               var c = 11;
               for (i=0; i<11; i++){
                       a[i] = cpf.charAt(i);
                       if (i < 9) b += (a[i] * --c);
               }
               if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
               b = 0;
               c = 11;
               for (y=0; y<10; y++) b += (a[y] * c--); 
               if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
               if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                       erro +="Digito verificador com problema!";
               }
               if (erro.length > 0){
                       alert(erro);
                       return false;
               }
               return true;
}