/* ==============================
    SISTEMA DE CONTROLE DE VEÍCULOS
    Arquivo de Rotinas JavaScript
	Criado em Setembro/2003
   ==============================
*/


/* VALIDAÇÃO SIMPLES */

function valSimples() { 
  var n=document.forms[0];
  for ( var i=0;i<n.length;i++ ) {
    if (n.elements(i).value == "") {
  	  window.alert("Preencha o campo "+n.elements(i).name);
      n.elements(i).focus();
      return (false)
    }
  }
  return (true)
}


/* VERIFICAÇÃO DE CNPJ */
function isNUMB(c) {
  if((cx=c.indexOf(","))!=-1) c = c.substring(0,cx)+"."+c.substring(cx+1);
  if((parseFloat(c) / c != 1)) {
    if(parseFloat(c) * c == 0) {
      return(1);
    } else {
      return(0);
    }
  } else {
    return(1);
  }
}

function LIMP(c) {
  while((cx=c.indexOf("-"))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
  while((cx=c.indexOf("/"))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
  while((cx=c.indexOf(","))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
  while((cx=c.indexOf("."))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
  while((cx=c.indexOf("("))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
  while((cx=c.indexOf(")"))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
  while((cx=c.indexOf(" "))!=-1) c = c.substring(0,cx)+c.substring(cx+1);
  return(c);
}

function checaCNPJ(CNPJ) {
  CNPJ = LIMP(CNPJ);
  if(isNUMB(CNPJ) != 1) {
    return(0);
  } else {
    if(CNPJ == 0) {
      return(0);
    } else {
      g=CNPJ.length-2;
      if(RealTestaCNPJ(CNPJ,g) == 1) {
        g=CNPJ.length-1;
        if(RealTestaCNPJ(CNPJ,g) == 1) {
          return(1);
        } else {
          return(0);
        }
      } else {
        return(0);
      }
    }
  }
}

function RealTestaCNPJ(CNPJ,g) {
  var VerCNPJ=0;
  var ind=2;
  var tam;
  for(f=g;f>0;f--) {
    VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
    if(ind>8) {
      ind=2;
    } else {
      ind++; 
    }
  }
  VerCNPJ%=11;
  if(VerCNPJ==0 || VerCNPJ==1) {
    VerCNPJ=0;
  } else {
    VerCNPJ=11-VerCNPJ;
  }
  if(VerCNPJ!=parseInt(CNPJ.charAt(g))) {
    return(0);
  } else {
    return(1);
  }
}

/* VERIFICAÇÃO DE CPF */
function checaCPF (CPF_MASCARADO) {  
  var CPF = CPF_MASCARADO.substring(0,3) + CPF_MASCARADO.substring(4,7) + CPF_MASCARADO.substring(8,11) + CPF_MASCARADO.substring(12,14);
  if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" || CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||	CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||	CPF == "88888888888" || CPF == "99999999999") return false;
  soma = 0;
  for (i=0; i < 9; i ++) soma += parseInt(CPF.charAt(i)) * (10 - i);
  resto = 11 - (soma % 11);
  if (resto == 10 || resto == 11) resto = 0;
  if (resto != parseInt(CPF.charAt(9))) return false;
  soma = 0;
  for (i = 0; i < 10; i ++) soma += parseInt(CPF.charAt(i)) * (11 - i);
  resto = 11 - (soma % 11);
  if (resto == 10 || resto == 11) resto = 0;
  if (resto != parseInt(CPF.charAt(10))) return false;
  return true;
 }

/* MASCARAS */
function Mascara (formato, keypress, objeto) { 
  campo = eval (objeto);
  // CEP = 18030-000
  if (formato == 'CEP') {
    separador = '-';
    conjunto1 = 5;
    if (campo.value.length == conjunto1) campo.value = campo.value + separador;
  }
  // DATA = 12/12/2000
  if (formato == 'DATA') {
    separador = '/';
    conjunto1 = 2;
    conjunto2 = 5;
    if (campo.value.length == conjunto1) campo.value = campo.value + separador;
    if (campo.value.length == conjunto2) campo.value = campo.value + separador;
  }
  // TELEFONE = 9999-9999
  if (formato == 'TELEFONE') {
    separador = '-';
    conjunto1 = 4;
    if (campo.value.length == conjunto1) campo.value = campo.value + separador;
  }
  //RG = 11.333.444-0
  if (formato == 'RG') {
    separador1 = '.';
    separador2 = '-';
    conjunto1 = 2;
    conjunto2 = 6;
    conjunto3 = 10;  
    if (campo.value.length == conjunto1) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto2) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto3) campo.value = campo.value + separador2;
  }
  //CPF = 111.333.444-00
  if (formato == 'CPF') {
    separador1 = '.';
    separador2 = '-';
    conjunto1 = 3;
    conjunto2 = 7;
    conjunto3 = 11;
	if (campo.value.length == conjunto1) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto2) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto3) campo.value = campo.value + separador2;
  }
    //INSCRIÇÃO ESTADUAL = 111.333.444.000
  if (formato == 'INSC') {
    separador1 = '.';
    conjunto1 = 3;
    conjunto2 = 7;
    conjunto3 = 11;
	if (campo.value.length == conjunto1) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto2) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto3) campo.value = campo.value + separador1;
  }
  //CNPJ = 00.000.000/0001-00
  if (formato == 'CNPJ') {
    separador1 = '.';
    separador2 = '/';
    separador3 = '-';
    conjunto1 = 2;
    conjunto2 = 6;
    conjunto3 = 10;
	conjunto4 = 15;
    if (campo.value.length == conjunto1) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto2) campo.value = campo.value + separador1;
    if (campo.value.length == conjunto3) campo.value = campo.value + separador2;
    if (campo.value.length == conjunto4) campo.value = campo.value + separador3;
  }
}

function MascaraDupla(keypress, objeto) {
  campo = eval (objeto);
  if (campo.value.length > 14) {
    if (campo.value.substr(3,1) == '.') campo.value = campo.value.substr(0,2) + '.' +campo.value.substr(2,1) + campo.value.substr(4,2) + '.' + campo.value.substr(6,1) + campo.value.substr(8,2) + '/' + campo.value.substr(10,1) + campo.value.substr(12,3);
    Mascara('CNPJ', keypress, objeto);
  } else {
    Mascara('CPF', keypress, objeto);
  }  
}

 function MascaraDuplaRG(keypress, objeto) { 
  campo = eval (objeto); 
  if (campo.value.length > 12) {  
    if (campo.value.substr(2,1) == '.') campo.value = campo.value.substr(0,2) + campo.value.substr(3,1) + '.' + campo.value.substr(4,2) + campo.value.substr(7,1) + '.' + campo.value.substr(8,2) + campo.value.substr(11,1) + '.' + campo.value.substr(12,1); 
    Mascara('INSC', keypress, objeto); 
  } else { 
    Mascara('RG', keypress, objeto); 
  }   
}

/* VALIDAÇÃO CADASTRO CLIENTES */
function valCliente() { 
  var n=document.forms[0];
  for ( var i=0;i<n.length;i++) {
    if (n.elements(i).value == "") {
	  if (n.elements(i).name == "RG" || n.elements(i).name == "Celular" || n.elements(i).name == "Fax" || n.elements(i).name == "Email") {
	    /* Faz nada */
	  } else {
        window.alert("Preencha o Campo "+n.elements(i).name);
	    n.elements(i).focus();
		return (false);
	  }
	}
	if (n.elements(i).name == "CEP" && n.elements(i).value.length < 9) {
	  window.alert("Preencha corretamente o "+n.elements(i).name+"\n ex.: 18123-321");
	  n.elements(i).focus();
	  return (false);
	}
  	if (n.elements(i).name == "DOC") {
	  if (n.elements(i).value.length < 15) { 
	    if (checaCPF(n.elements(i).value)) {
	      /* Faz nada */
	    } else { 
	      window.alert ('Preencha corretamente o Campo CPF');
	      n.elements(i).focus();
		  return (false);
	    }
	  } else  {
	    if (checaCNPJ(n.elements(i).value)) {
		  /* Faz nada */
		} else { 
		  window.alert ('Preencha corretamente o Campo CNPJ');
		  n.elements(i).focus();
		  return (false);
		}
	  }
	}
  }
  return (true);
}



 /* VALIDAÇÃO CADASTRO VENDEDORES */

function valVendedor() { 
  var n=document.forms[0];
	for ( var i=0;i<n.length;i++) {
      if (n.elements(i).value == "") {
		  if (n.elements(i).name == "RG" || n.elements(i).name == "Endereco" || n.elements(i).name == "Bairro" || n.elements(i).name == "CEP" || n.elements(i).name == "Celular" || n.elements(i).name == "Fax" || n.elements(i).name == "Email") { }
		  else {
			  window.alert("Preencha o Campo "+n.elements(i).name);
			  n.elements(i).focus();
			  return (false)
		  }
	  }

	}
  return (true)
}


// ===============
// Verificar Placa

function VerificaPlaca(Placa) {
	var rangeinvalido = /[^0-9a-zA-Z]/
	var rangenumeros = /[0-9]/
	var rangeletras = /[a-zA-Z]/
	pos=Placa.value.length
	i=0
	while (i<pos) {
		Placa.value = Placa.value.substring(0,i) + Placa.value.substring(i,i+1).replace(rangeinvalido,"") + Placa.value.substring(i+1,pos)
		i++;
	}
	mascaraplaca=Placa.value
	i=0
	while (i<3) {
		mascaraplaca=mascaraplaca.substring(0,i)+mascaraplaca.substring(i,i+1).replace(rangeletras,"X")+mascaraplaca.substring(i+1,7)
		i++
	}
	i=3
	while (i<7) {
		mascaraplaca=mascaraplaca.substring(0,i)+mascaraplaca.substring(i,i+1).replace(rangenumeros,"9")+mascaraplaca.substring(i+1,7)
		i++
	}
	if (! (mascaraplaca=="XXX9999")){
		return false;
	}
}

function ValidaPlaca(Placa) {
	if (VerificaPlaca(Placa) == false) {
	alert("Informe a Placa corretamente. \nEx.:AAA9999");
	return (false)	}
}


// ================
// Cadastro Veiculo

function validarVeiculo() { 
  var n=document.forms[0];
	for ( var i=0;i<n.length;i++) {
      if (n.elements(i).value == "") {
		  if (n.elements(i).name == "Versao" || n.elements(i).name == "OutrosDados" || n.elements(i).name == "Portas" || n.elements(i).name == "CORRETOR" || n.elements(i).name == "TELCORRETOR") { }
		  else {
			  window.alert("Preencha o campo "+n.elements(i).name);
			  n.elements(i).focus();
			  return (false)
		  }
	  }
	}

	if (isNaN(n.Preco.value)) {
		window.alert("Preencha o campo Preco \nsomente com números, \nsem pontos ou vírgulas");
		n.Preco.focus();
		return (false)
	}
  return (true)
}



// ======================
// Função de Lista Carros

function setDynaList(arrDL){

 var oList1 = document.forms[arrDL[2]].elements[arrDL[1]];
 var oList2 = document.forms[arrDL[2]].elements[arrDL[3]];
 var arrList = arrDL[5];
 
 clearDynaList(oList2);
 
 if (oList1.selectedIndex == -1){
  oList1.selectedIndex = 0;
 }

 populateDynaList(oList2, oList1[oList1.selectedIndex].value, arrList);
 return true;
}
 
function clearDynaList(oList){

 for (var i = oList.options.length; i >= 0; i--){
  oList.options[i] = null;
 }
 
 oList.selectedIndex = -1;
}

 
function populateDynaList(oList, nIndex, aArray){
   oList.options[oList.options.length] = new Option("", "");
   for (var i = 0; i < aArray.length; i= i + 3){
	if (aArray[i] == nIndex){
     oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i + 2]);
    }
   }
   if (oList.options.length == 0){
   oList.options[oList.options.length] = new Option("** Modelo **",0);
  }
   oList.selectedIndex = 0;
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function Envia(arrDL){

 var selList1 = document.form1.selList1.value;
 var selList2 = document.form2.selList2.value;
}
