/*----------------------------------------------------------------------------
Formatação para qualquer mascara
-----------------------------------------------------------------------------*/
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 formatarEmail(src){
	var str = new String(src.value);        
	var acentos = new String('àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ');        
	var SemAcento = new String('aaeouaoaeioucuAAEOUAOAEIOUCU');        
	var c = new String();        
	var i = new Number();        
	var x = new Number();        
	var res = '';                
	for (i = 0; i<str.length; i++) {                
		c = str.substring(i,i+1);                
		for (x=0; x< acentos.length; x++) {                        
			if (acentos.substring(x,x+1) == c) { c = SemAcento.substring(x,x+1); }                                                        
		}                
		res += c;        
	}        
	src.value = res;
	src.value = src.value.toLowerCase();

}



function formatarNumero(src, mask){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	
	
	if (document.all)
	var tecla = event.keyCode;
	else if(document.layers)
	var tecla = num.which;
	if (tecla > 47 && tecla < 58) {
		if (texto.substring(0,1) != saida) {
		src.value += texto.substring(0,1);
		}
	return true;
	} else	{
	if (tecla != 8)
		event.keyCode = 0;
	else
		
		return true;
	}
	
}


function formatarNumeroEsp(src,mask){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	
	if (document.all)
	var tecla = event.keyCode;
	else if(document.layers)
	var tecla = num.which;
	if (tecla > 47 && tecla < 58) {
		if (texto.substring(0,1) != saida) {
		src.value += texto.substring(0,1);
		}
	return true;
	} else	{
	if (tecla != 8)
		event.keyCode = 0;
	else
		
		return true;
	}
	
}



function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MudaMenu(opt) {
	if (opt == '') {
		document.getElementById('venda').src = '../_img/bto_venda_off.jpg';
		document.getElementById('compre').src = '../_img/bto_compre_off.jpg';
		document.getElementById('garan').src = '../_img/bto_garan_off.jpg';
		document.getElementById('motor').src = '../_img/bto_motor_off.jpg';
		document.getElementById('encon').src = '../_img/bto_encon_off.jpg';
		document.getElementById('links').src = '../_img/bto_links_off.jpg';
		document.getElementById('falec').src = '../_img/bto_falec_off.jpg';
		document.getElementById('dossie').src = '../_img/bto_dossie_off.jpg';
		document.getElementById('antif').src = '../_img/bto_antif_off.jpg';
		document.getElementById('comprasegura').src = '../_img/bto_comprasegura_off.jpg';
	} else {
		document.getElementById(opt).src = '../_img/bto_' + opt + '_on.jpg';
	}
}

function MudaContrato() {
	
	if (document.getElementById('aceitarCadastro').checked == true && document.getElementById('aceitarContrato').checked == true) {
		document.getElementById('btoCadastrar').disabled=false;
	} else {
		document.getElementById('btoCadastrar').disabled=true;
	}
	
	//if (document.getElementById('btoCadastrar').disabled == true) {
	//	document.getElementById('btoCadastrar').disabled=false;
	//} else {
	//	document.getElementById('btoCadastrar').disabled=true;
	//}
}

function MudaContratoAnuncio() {
	if (document.getElementById('btoCadastrar').disabled == true) {
		document.getElementById('btoCadastrar').disabled=false;
	} else {
		document.getElementById('btoCadastrar').disabled=true;
	}
}


function validaCadastroAnuncioValor(){
d = document.frmAlterar;

	if (d.marca.value == ""){
	alert("Selecione a MARCA!");
	d.marca.focus();
	return false;
	}
	
	if (d.modelo.value == ""){
	alert("Selecione o MODELO!");
	d.modelo.focus();
	return false;
	}
	
	if (d.portas.value == ""){
	alert("Selecione a quantidade de PORTAS!");
	d.portas.focus();
	return false;
	}
	
	if (d.tipocambio.value == ""){
	alert("Selecione o TIPO DE CÂMBIO!");
	d.tipocambio.focus();
	return false;
	}

	if (d.placafinal.value == ""){
	alert("Selecione o FINAL DA PLACA!");
	d.placafinal.focus();
	return false;
	}
		
	if (d.valor.value.length < 7){
	alert("Valor deve ser maior que 999,99!");
	d.valor.focus();
	return false;
	}
	

	if (d.valor.value == ""){
	alert("O campo VALOR deve ser preenchido!");
	d.valor.focus();
	return false;
	}
}


function validaExcluiAnuncio(){
d = document.frmExcluir;

	//alert(d.valorinicial.value);
	//alert(d.valorvenda.value);
	
	//if (d.valorvenda.value == ""){
	//alert("O campo VALOR DE VENDA deve ser preenchido!");
	//d.valorvenda.focus();
	//return false;
	//}
	
	
	if (d.remove[1].checked == false) {
	
	if (d.valorvenda.value.length < 7){
	alert("Valor deve ser maior que 999,99!");
	d.valorvenda.focus();
	return false;
	}
	
	if (d.valorvenda.value == ""){
	alert("O campo VALOR deve ser preenchido!");
	d.valorvenda.focus();
	return false;
	}
	
	}

}

function validaInserirAnuncio(){
d = document.frmAnuncio;

if (d.iduserinsert.value == "0") {
	if (d.placa.value == ""){
	alert("O campo PLACA deve ser preenchido!");
	d.placa.focus();
	return false;
	}
	
	if (d.laudo.value == ""){
	alert("O campo LAUDO deve ser preenchido!");
	d.laudo.focus();
	return false;
	}
}

if (d.iduserinsert.value == "1") {
	if (d.placa.value == ""){
	alert("O campo PLACA deve ser preenchido!");
	d.placa.focus();
	return false;
	}
}

if (d.iduserinsert.value == "2") {
	if (d.placa.value == ""){
	alert("O campo VOUCHER deve ser preenchido!");
	d.placa.focus();
	return false;
	}
}

if (d.iduserinsert.value == "3") {
	if (d.placa.value == ""){
	alert("O campo VOUCHER deve ser preenchido!");
	d.placa.focus();
	return false;
	}
}




}





function validaForm(){
d = document.frmCadastro;
	
	if (d.doc.value == "0") {
		if (d.U_DOC_CPF.value == ""){
			 alert("O campo CPF deve ser preenchido!");
			 d.U_DOC_CPF.focus();
			 return false;
		}
	} else {
		if (d.U_DOC_CNPJ.value == ""){
			 alert("O campo CNPJ deve ser preenchido!");
			 d.U_DOC_CNPJ.focus();
			 return false;
		}
	}

	if (d.doc.value == "1") {
		if (d.tipostatus.value == ""){
				 alert("Escolha o TIPO!");
				 d.tipostatus.focus();
				 return false;
		}
	}

	if (d.doc.value == "1") {
		if (d.empresa.value == ""){
				 alert("O campo EMPRESA deve ser preenchido!");
				 d.empresa.focus();
				 return false;
		}
	}
	
	if (d.doc.value == "1") {
		if (d.contato.value == ""){
				 alert("O campo CONTATO deve ser preenchido!");
				 d.contato.focus();
				 return false;
		}
	}


	if (d.doc.value == "0") {
		if (d.firstName.value == ""){
				 alert("O campo NOME deve ser preenchido!");
				 d.firstName.focus();
				 return false;
		}
	}
	
	if (d.doc.value == "0") {
		if (d.lastName.value == ""){
				 alert("O campo SOBRENOME deve ser preenchido!");
				 d.lastName.focus();
				 return false;
		}
	}
	
	if (d.Email.value == ""){
			 alert("O campo EMAIL deve ser preenchido corretamente!");
			 d.Email.focus();
			 return false;
	}
	
	var entrada = d.Email
	var arroba = entrada.value.indexOf('@')
	var ponto = entrada.value.substring(arroba + 1, entrada.length)
	for (var i = 0; i < ponto.length; i++) {
	if (ponto.charAt(i) == ".") {
		break
	}
	}
	if (entrada.value.length <= 0) {
		alert("O campo EMAIL deve ser preenchido corretamente!");
		d.Email.focus();
		return false;
	}
	if (arroba < 1) {
		alert("O campo EMAIL deve ser preenchido corretamente!");
		d.Email.focus();
		return false;
	}
	if (ponto.charAt(i) != ".") {
		alert("O campo EMAIL deve ser preenchido corretamente!");
		d.Email.focus();
		return false;
	}

    
	if (d.Email.value != d.Emailsec.value) {
		alert("Os campos de E-mail não conferem!");
		d.Emailsec.focus();
		return false;
	}

	//if (d.Emailsec.value == ""){
//			 alert("O campo EMAIL ALTERNATIVO deve ser preenchido!");
//			 d.Emailsec.focus();
//			 return false;
//	}
		
	if (!d.sexo[0].checked && !d.sexo[1].checked) {
		   alert("Escolha o SEXO!")
		   return false;
	}
	
	if (d.U_ADDRESS.value == ""){
			 alert("O campo ENDEREÇO deve ser preenchido!");
			 d.U_ADDRESS.focus();
			 return false;
	}
	
	if (d.numero.value == ""){
			 alert("O campo NUMERO deve ser preenchido!");
			 d.numero.focus();
			 return false;
	}
	
		
	if (d.U_ZIP.value == ""){
			 alert("O campo CEP deve ser preenchido!");
			 d.U_ZIP.focus();
			 return false;
	}
	
	if (d.U_ZIP.value != "") {
		
		var cep = d.U_ZIP.value;
		
		if(cep.length != 8 || cep == "00000000" || cep == "11111111" ||
		cep == "22222222" || cep == "33333333" || cep == "44444444" ||
		cep == "55555555" || cep == "66666666" || cep == "77777777" ||
		cep == "88888888" || cep == "99999999"){
		window.alert("CEP inválido. Tente novamente.");
		return false;
		}
		
		if (cep.length != 8) 
		{
		window.alert("CEP inválido. Tente novamente.");
		return false;
		}

		
	}
	
	
	if (d.U_BAIRRO.value == ""){
			 alert("O campo BAIRRO deve ser preenchido!");
			 d.U_BAIRRO.focus();
			 return false;
	}
	
	if (d.U_CITY.value == ""){
			 alert("O campo CIDADE deve ser preenchido!");
			 d.U_CITY.focus();
			 return false;
	}
	
	var sOpcao = d.U_STATE.options[d.U_STATE.selectedIndex].value;
	if (d.U_STATE.value == ""){
			 alert("O campo ESTADO deve ser preenchido!");
			 d.U_STATE.focus();
			 return false;
	}
	
	if (d.U_PHONE.value == ""){
			 alert("O campo TELEFONE deve ser preenchido!");
			 d.U_PHONE.focus();
			 return false;
	}
	
	
	
	//if (d.U_PHONE.value != "") {
	//	
	//	var fone = d.U_PHONE.value;
	//	var filtro = /^\d{2}-\d{4}.\d{4}$/i;
	//	if(!filtro.test(fone)){
	//	window.alert("Telefone inválido. Tente novamente.");
	//	return false;
	//	}
		
	//	if(fone == "00-0000.0000" || fone == "11-1111.1111" ||
	//	fone == "22-2222.2222" || fone == "33-3333.3333" || fone == "44-4444.4444" ||
	//	fone == "55-5555.5555" || fone == "66-6666.6666" || fone == "77-7777.7777" ||
	//	fone == "88-8888.8888" || fone == "99-9999.9999"){
	//	window.alert("Telefone inválido. Tente novamente.");
	//	return false;
	//	}

	//}
	
	
	
	
	//var entrada = d.U_PHONE
//	var arroba = entrada.value.indexOf('-')
//	var ponto = entrada.value.substring(arroba + 1, entrada.length)
//	for (var i = 0; i < ponto.length; i++) {
//	if (ponto.charAt(i) == ".") {
//		break
//	}
//	}
//	if (entrada.value.length <= 0) {
//		alert("O campo TELEFONE deve ser preenchido!");
//		d.U_PHONE.focus();
//		return false;
//	}
//	if (arroba < 2) {
//		alert("O campo TELEFONE deve ser preenchido!");
//		d.U_PHONE.focus();
//		return false;
//	}
//	if (ponto.charAt(i) != ".") {
//		alert("O campo TELEFONE deve ser preenchido!");
//		d.U_PHONE.focus();
//		return false;
//	}
	
//	if (d.U_FAX.value == ""){
//			 alert("O campo FAX deve ser preenchido!");
//			 d.U_FAX.focus();
//			 return false;
//	}
	
	if (!d.garantido[0].checked && !d.garantido[1].checked) {
	   alert("Diga se possui cadastro como CLIENTE GARANTIDO!")
	   return false;
	}

 	 
	 return true;
}

function Enum(num){
	if (document.all)
		var tecla = event.keyCode;
	else if(document.layers)
		var tecla = num.which;
		if (tecla > 47 && tecla < 58)
			return true;
		else
			{
				if (tecla != 8)
					event.keyCode = 0;
				else
					return true;
			}
}




function validaFormInicio(){
d = document.frmCadastro;

	
	if (!d.doc[0].checked && !d.doc[1].checked) {
		   alert("Escolha o DOCUMENTO!")
		   return false;
	}
	
	if (d.doc[0].checked == true) {
		if (d.U_DOC_CPF.value == ""){
			 alert("O campo CPF deve ser preenchido!");
			 d.U_DOC_CPF.focus();
			 return false;
		}
	} else {
		if (d.U_DOC_CNPJ.value == ""){
			 alert("O campo CNPJ deve ser preenchido!");
			 d.U_DOC_CNPJ.focus();
			 return false;
		}
	}
	
	
	if (d.doc[0].checked == true) {
		if (d.U_DOC_CPF.value != ""){
			
			
			var cpf = d.U_DOC_CPF.value;
			
			if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
			cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
			cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
			cpf == "88888888888" || cpf == "99999999999"){
			window.alert("CPF inválido. Tente novamente.");
			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))){
			window.alert("CPF inválido. Tente novamente.");
			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))){
			window.alert("CPF inválido. Tente novamente.");
			return false;
			}
			return true;


			
			
			
			
		}
	} else {
		if (d.U_DOC_CNPJ.value != ""){
			
			var cnpj = d.U_DOC_CNPJ.value;
			var c = d.U_DOC_CNPJ;
			var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj = c.value.replace(/\D+/g, '');
     digitos_iguais = 1;
    if (cnpj.length != 14) 
            {
                 alert('CNPJ inválido. Tente novamente.');
                 c.focus();
                 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))
{
                 alert('CNPJ inválido. Tente novamente.');
                 c.focus();
                 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)){
                 alert('CNPJ inválido. Tente novamente.');
                 c.focus();
                 return false;
                 } else {
     // alert('CNPJ  OK !');
            return true;
            }
           } else {
           alert('CNPJ inválido. Tente novamente.');
           c.focus();
           return false;
           }

			



		}
	}
	
	
	
}

function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
}




function validaContato(){
d = document.frmContato;
	
	if (d.nome.value == ""){
	alert("O campo NOME deve ser preenchido!");
	d.nome.focus();
	return false;
	}
	if (d.email.value == ""){
	alert("O campo E-MAIL deve ser preenchido!");
	d.email.focus();
	return false;
	}
	
	var entrada = d.email;
	var arroba = entrada.value.indexOf('@');
	var ponto = entrada.value.substring(arroba + 1, entrada.length);
	for (var i = 0; i < ponto.length; i++) {
	if (ponto.charAt(i) == ".") {
		break;
	}
	}
	if (entrada.value.length <= 0) {
		alert("O campo EMAIL deve ser preenchido corretamente!");
		d.email.focus();
		return false;
	}
	if (arroba < 1) {
		alert("O campo EMAIL deve ser preenchido corretamente!");
		d.email.focus();
		return false;
	}
	if (ponto.charAt(i) != ".") {
		alert("O campo EMAIL deve ser preenchido corretamente!");
		d.email.focus();
		return false;
	}
	
	
	if (d.fone.value == ""){
	alert("O campo TELEFONE deve ser preenchido!");
	d.fone.focus();
	return false;
	}
	
	if (d.fone.value != "") {
		
		var fone = d.fone.value;
		var filtro = /^\d{2}-\d{4}.\d{4}$/i;
		if(!filtro.test(fone)){
		window.alert("O campo TELEFONE deve ser preenchido corretamente!");
		return false;
		}

	}
	
	if(fone.length != 12 || fone == "00-0000.0000" || fone == "11-1111.1111" ||
		fone == "22-2222.2222" || fone == "33-3333.3333" || fone == "44-4444.4444" ||
		fone == "55-5555.5555" || fone == "66-6666.6666" || fone == "77-7777.7777" ||
		fone == "88-8888.8888" || fone == "99-9999.9999"){
		window.alert("O campo TELEFONE deve ser preenchido corretamente!");
		return false;
	}
	
	if (d.strmsg.value == ""){
	alert("O campo MENSAGEM deve ser preenchido!");
	d.strmsg.focus();
	return false;
	}
	
}



function validaBusca() {
d = document.frmBusca;

	if (d.anode.value > d.anoate.value){
			 alert("O campo Ano Até deve ser igual ou maior que o campo Ano De!");
			 d.anoate.focus();
			 return false;
	}

return true;

}



//-----------------------------------------------------
//Funcao: MascaraMoeda
//Sinopse: Mascara de preenchimento de moeda
//Parametro:
//   objTextBox : Objeto (TextBox)
//   SeparadorMilesimo : Caracter separador de milésimos
//   SeparadorDecimal : Caracter separador de decimais
//   e : Evento
//Retorno: Booleano
//Autor: Gabriel Fróes - www.codigofonte.com.br
//-----------------------------------------------------
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;
}
