//Specify affected tags. Add or remove from list:
var tgs = new Array( 'div','td','tr','p' );

//Specify spectrum of different font sizes:
var szs = new Array( '11','12','14','16','18' );
var startSz = 1;

function mudaTamanho( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	if ( sz < 0 ) sz = 0;
	if ( sz > 4 ) sz = 4;
	startSz = sz;
		
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	cEl.style.fontSize = szs[ sz ];

	for ( i = 0 ; i < tgs.length ; i++ ) {
		cTags = cEl.getElementsByTagName( tgs[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}


function valida_dados() {
    var s;
    var cpf_cob = document.getElementById('cpf_b1').value + document.getElementById('cpf_b2').value + document.getElementById('cpf_b3').value + document.getElementById('cpf_b4').value;
        
    if (cpf_cob.length == 0) {
       alert("O CPF é um campo obrigatório !");
       document.getElementById('cpf_b1').focus();
       return false;
    }

    s = limpa_string(cpf_cob);
    if (s.length == 11) {
       if (valida_CPF(cpf_cob) == false ) {
          alert("O CPF não é válido !");
          document.getElementById('cpf_b1').focus();
          return false;
       }  
       }else{
          alert("O CPF não é válido !");
          document.getElementById('cpf_b1').focus();
          return false;
        }

    if (document.cadastro.juridica[1].checked == true) {

    var cnpj_cob = document.getElementById('cnpj_b1').value + document.getElementById('cnpj_b2').value + document.getElementById('cnpj_b3').value + document.getElementById('cnpj_b4').value + document.getElementById('cnpj_b5').value;

    if (cnpj_cob.length == 0) {
       alert("O CNPJ é um campo obrigatório !");
       document.getElementById('cnpj_b1').focus();
       return false;
       }
     
    s = limpa_string(cnpj_cob);
    if (s.length == 14) {
       if (valida_CNPJ(cnpj_cob) == false ) {
          alert("O CNPJ não é válido !");
          document.getElementById('cnpj_b1').focus();
          return false;
          }
       }else{
          alert("O CNPJ não é válido !");
          document.getElementById('cnpj_b1').focus();
          return false;
       }
    }

    }

function valida_CNPJ(s)
 {
	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)
	{
		return false;
	}
	d1 *= 2;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
        return true;
    }

function valida_CPF(s)
{
	var i;
	s = limpa_string(s);
	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) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)
	{
		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)
	{
		return false;
	}
        return true;
   }

function limpa_string(S){
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
	for (var i=0; i<S.length; i++){
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0){temp=temp+digito}
	}
	return temp
}

function ZeraPJuridica()
      {
      document.getElementById('fantasia').value="";
      document.getElementById('razaosocial').value="";
      document.getElementById('cnpj_b1').value="";
      document.getElementById('cnpj_b2').value="";
      document.getElementById('cnpj_b3').value="";
      document.getElementById('cnpj_b4').value="";
      document.getElementById('cnpj_b5').value="";
      document.getElementById('inscricaoestadual').value="";
      }

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode; 
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
        if(arr[index] == ele)
            found = true;
        else
            index++;
        return found;
    }

function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
return true;
}

function enviar() {
       var resposta;

       resposta = valida_cep();
       if (resposta == true)
           document.carrinho.submit ();
}
function valida_cep() {

     Form = document.carrinho;
     if (Form.cep1.value.length == 0) {
	alert("Por favor digite um CEP !");
        Form.cep1.focus();
        return false;
     }
     if (Form.cep2.value.length == 0) {
	alert("Por favor digite um CEP !");
        Form.cep2.focus();
        return false;
     }
     s = limpa_string(Form.cep1.value);
     if (s.length != 5) {
	alert("O CEP deve ter caracteres numericos !");
        Form.cep1.focus();
        return false;
     }
     s = limpa_string(Form.cep2.value);
     if (s.length != 3) {
	alert("O CEP deve ter caracteres numericos !");
        Form.cep2.focus();
        return false;
     }
     return true;
}

function limpa_string(S){
// Deixa so' os digitos no numero
var Digitos = "0123456789";
var temp = "";
var digito = "";
    for (var i=0; i<S.length; i++){
      digito = S.charAt(i);
      if (Digitos.indexOf(digito)>=0){temp=temp+digito}
    }
    return temp
}

 function semtab() { checatab=false; } 
 function comtab() { checatab=true; } 

 checatab=true;
 function mostra_cep() {

   Form = document.carrinho;
   if ( (Form.cep1.value.length == 5) && (checatab) ) { 
          Form.cep2.focus();
          checatab=false;
   } 
 } 

function RetornoAjax( id, url, container)
{
var xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)
{
alert ("Este browser não suporta HTTP Request")
return
}

var url=url;
url=url+id;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById(container).innerHTML=xmlHttp.responseText
}
}
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function GetXmlHttpObject()
{
var objXMLHttp=null

if (window.XMLHttpRequest)
objXMLHttp=new XMLHttpRequest()
else if (window.ActiveXObject)
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")

return objXMLHttp
}

function check1(){
	var strS;
	var temp;

	temp=document.cadveiculo.id_montadora;
	if(temp.options[temp.selectedIndex].value == ""){
		strS = "Por favor selecione uma marca e em seguida o modelo do veículo."
		alert(strS);
		return false;
	}

	temp=document.cadveiculo.id_carro;
	if(temp.options[temp.selectedIndex].value == ""){
		strS = "Por favor selecione o modelo do veículo."
		alert(strS);
		return false;
	}
	document.cadveiculo.nome_montadora.value = document.cadveiculo.id_montadora.options[document.cadveiculo.id_montadora.selectedIndex].text;
	document.cadveiculo.nome_carro.value = document.cadveiculo.id_carro.options[document.cadveiculo.id_carro.selectedIndex].text;
      if (document.cadveiculo.versao.value == "")
         { alert ("Informe a versão do veículo!"); return false; }
      if (document.cadveiculo.placa.value == "")
         { alert ("Informe a placa do veículo!"); return false; }
      if (document.cadveiculo.cor.value == "")
         { alert ("Informe a cor do veículo!"); return false; }
      if (document.cadveiculo.ano.value == "")
         { alert ("Informe o ano de fabricação do veículo!"); return false; }
      if (document.cadveiculo.modelo.value == "")
         { alert ("Informe o ano modelo do veículo!"); return false; }
      if (document.cadveiculo.ano.value > document.cadveiculo.modelo.value)  
         { document.cadveiculo.ano_temp.value = document.cadveiculo.ano.value
           document.cadveiculo.ano.value = document.cadveiculo.modelo.value
           document.cadveiculo.modelo.value = document.cadveiculo.ano_temp.value }
      if (document.cadveiculo.combustivel.value == "")
         { alert ("Informe o combustível do veículo!"); return false; }
      if (document.cadveiculo.valor.value == "")
         { alert ("Informe o valor do veículo, ou informe 'Consulte'!"); return false; }
      if (document.cadveiculo.observacoes.value == "")
         { alert ("Preencha alguma informação útil do veículo no campo observações!"); return false; }
      document.getElementById('campos').style.display='none';
      document.getElementById('aguarde').style.display='';
      document.cadveiculo.submit();
	return true;
}