// JavaScript Document
try{
	xmlhttp = new XMLHttpRequest();
}catch(err1){
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(err2){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(err3){
			xmlhttp = false;
			if(err3){
				alert("Desculpe! Mais o seu browser não suporta todas as funcionalidades dessa página.");
			}
		}
	}
}

function runAjax(id, url, complemento, tipo){
	if(id == "conteudoMeio" && complemento == ""){
		return false;
	}
	document.getElementById(id).innerHTML = "<br /><br /><br /><br /><br /><br /><br /><br /><br /><span style='display:block; background-color:red; font-weight:bold; font-size:13px; color:white; padding:10px; width:578px; text-align:center;'>Carregando</span>"
	
	if(complemento == "" || complemento == null){
		xmlhttp.open("GET", url, true);
	}else if(tipo == "formulario"){
		xmlhttp.open("POST", url, true);
	}else{
		xmlhttp.open("GET", url + complemento, true);
	}
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
			retorno = unescape(xmlhttp.responseText.replace(/\+/g," "));
			document.getElementById(id).innerHTML = retorno;
		}
	}
	if(tipo == "formulario"){
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(complemento);
	}else{
		xmlhttp.send(null);
	}
	
	return false;
}

function ajaxCombos(novoCombo, adm, elemento, selecionado, id){
	if(selecionado == ""){
		return false;
	}else{
		var separaAdm = adm.split("|");
		var codAdm = separaAdm[0];
		var codUser = separaAdm[1];
		var codPedido = separaAdm[2];
		if(elemento == "sltTipo"){
			if(document.formProdutos.sltMarca){
				document.getElementById("marca").innerHTML = "";
			}
			if(document.formProdutos.sltModelo){
				document.getElementById("modelo").innerHTML = "";
			}
		}
		xmlhttp.open("GET", "combos.asp?proximo=" + novoCombo + "&adm=" + codAdm + "&codUser=" + codUser + "&codPedido=" + codPedido + "&select=" + selecionado + "&id=" + id + "&codTipo=" + elemento, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				retorno = unescape(xmlhttp.responseText.replace(/\+/g," "));
				document.getElementById(id).innerHTML = retorno;
				if(elemento == 'sltTipo'){
					verificaMarca(codAdm, codUser, codPedido);
				}
			}
		}
		xmlhttp.send(null)
	}
}

function verificaTipo(adm, user, pedido){
	if(document.formProdutos.sltTipo.options.length == 3){
		document.formProdutos.sltTipo.options[2].selected = true;
		ajaxCombos('sltMarca', adm + "|" + user + "|" + pedido, 'sltTipo', document.formProdutos.sltTipo.options[2].value, 'marca');
	}
}

function verificaMarca(adm, user, pedido){
	if(document.formProdutos.sltMarca.options.length == 3){
		document.formProdutos.sltMarca.options[2].selected = true;
		ajaxCombos('sltModelo', adm + "|" + user + "|" + pedido, document.formProdutos.sltTipo.value, document.formProdutos.sltMarca.options[2].value, 'modelo');
	}
}

function submeteForm(){
	document.getElementById("formProdutos").submit();
}

function validaCadastro(actionForm){
	with(document.formCadastro){
		if(txtNome.value == ""){
			alert("Por favor, preencha o campo Nome/Razão Social corretamente.");
			txtNome.focus();
			return false;
		}
		if(txtNasc.value == ""){
			alert("Por favor, preencha o campo Data de Nascimento/Fundação corretamente.");
			txtNasc.focus();
			return false;
		}
		if(rdTipo[0].checked){
			if(sltEstCivil.value == ""){
				alert("Por favor, selecione o Estado Civil.");
				sltEstCivil.focus();
				return false;
			}
		}
		if(rdTipo[0].checked){
			if(sltSexo.value == ""){
				alert("Por favor, selecione o Sexo.");
				sltSexo.focus();
				return false;
			}
		}
		if(txtRG.value == ""){
			alert("Por favor, preencha o campo RG/Insc. Estadual corretamente.");
			txtRG.focus();
			return false;
		}
		if(sltEst == "São Paulo"){
			if(rdTipo(1).checked){
				var insc = txtRG.value;
				insc = insc.replace(".", "");
				insc = insc.replace(",", "");
				insc = insc.replace("-", "");
				insc = insc.replace("/", "");
				var verificador = new String;
				var arValores = [];
				var peso = 1;
				var resultadoFinal = new Number;
				var indCom = new Boolean;
				if(txtRG.value.substr(0, 1) == "P"){
					verificador = insc.substr(1, 8);
					indCom = false;
				}else{
					verificador = insc.substr(0, 8);
					indCom = true;
				}
				for(var i = 0; i < verificador.length; i++){
					arValores[i] = Number(verificador.substr(i, 1)) * peso;
					if(peso == 1){
						peso = 3;
					}else if(peso == 8){
						peso = 10;
					}else{
						peso++;
					}
				}
				if(insc.substr(0, 1) == "P") verificador = Number(insc.substr(verificador.length + 1, 1));
				if(insc.substr(0, 1) != "P") verificador = Number(insc.substr(verificador.length, 1));
				for(var i = 0; i < arValores.length; i++){
					resultadoFinal = resultadoFinal + Number(arValores[i]);
				}
				resultadoFinal = resultadoFinal % 11;
				if(resultadoFinal >= 10) resultadoFinal = 0
				if(resultadoFinal != verificador){
					alert("Você deve digitar uma Inscrição Estadual válida!");
					txtRG.focus();
					return false;
				}else{
					if(indCom){
						verificador = insc.substr(0, insc.length -1);
						peso = 3;
						resultadoFinal = 0;
						for(var i = 0; i < verificador.length; i++){
							arValores[i] = Number(verificador.substr(i, 1)) * peso;
							if(!indCom) peso = 11; indCom = true;
							peso--;
							if(peso == 2) indCom = false;
						}
						for(var i = 0; i < arValores.length; i++) resultadoFinal = resultadoFinal + arValores[i];
						resultadoFinal = resultadoFinal % 11;
						if(resultadoFinal >= 10) resultadoFinal = 0;
						if(resultadoFinal != insc.substr(verificador.length, 1)){
							alert("Você deve digitar uma Incsrição Estadual válida.");
							txtRG.focus();
							return false;
						}
					}
				}
			}
		}
		if(txtCPF.value == ""){
			alert("Por favor, preencha o campo CPF/CNPJ corretamente.");
			txtCPF.focus();
			return false;
		}
		if(rdTipo(0).checked){
			if(txtCPF.value == "00000000000"){
				alert("Você deve digitar um CPF válido");
			}else{
				var cpf = txtCPF.value;
				var nonNumbers = /\D/;
				if(nonNumbers.test(cpf)){
					alert("A verificação de CPF suporta apenas números");
					return false;
				}
				var trecho = new String;
				var verificador = cpf.substr(0, cpf.length - 1);
				var arVerifica = [];
				var peso = 10;
				var resultadoFinal = new Number;
				for(var a = 1; a < 3; a++){
					for(var i = 0; i < verificador.length - 1; i++){
						arVerifica[i] = Number(verificador.substr(i, 1)) * peso;
						peso--;
					}
					for(var i = 0; i < arVerifica.length; i++){
						resultadoFinal = resultadoFinal + Number(arVerifica[i]);
					}
					resultadoFinal = resultadoFinal % 11;
					if(resultadoFinal < 2) resultadoFinal = 0;
					if(resultadoFinal >= 2) resultadoFinal = 11 - resultadoFinal;
					if(Number(verificador.substr(verificador.length - 1, 1)) != resultadoFinal){
						alert("Você deve digitar um CPF válido");
						txtCPF.focus();
						return false;
						break;
					}
					verificador = cpf;
					peso = 11;
					resultadoFinal = 0;
				}
			}
		}else{
			var cnpj = txtCPF.value;
			var nonNumbers = /\D/;
			if (nonNumbers.test(cnpj)){
				alert("A verificação de CNPJ suporta apenas números");
				return false;
			}
			var verificador = cnpj.substr(0, cnpj.length - 2);
			var arVerifica = [];
			var peso = 5;
			var confirmaDados = false;
			var resultadoFinal = new Number;
			for(var o = 1; o < 3; o++){
				for(var i = 0; i < verificador.length; i++){
					arVerifica[i] = Number(verificador.substr(i, 1)) * peso;
					if(confirmaDados){
						peso = 10;
						confirmaDados = false;
					}
					peso--;
					if(peso == 2) confirmaDados = true;
				}
				for(var i = 0; i < arVerifica.length; i++){
					resultadoFinal = resultadoFinal + arVerifica[i];
				}
				resultadoFinal = resultadoFinal % 11;
				if(resultadoFinal < 2) resultadoFinal = 0;
				if(resultadoFinal > 2) resultadoFinal = 11 - resultadoFinal;
				if(String(resultadoFinal) != cnpj.substr(verificador.length, 1)){
					alert("Você deve digitar um CNPJ válido!");
					txtCPF.focus();
					return false;
					break;
				}
				peso = 6;
				verificador = cnpj.substr(0, cnpj.length - 1);
				resultadoFinal = 0;
			}
		}
		if(txtEnd.value == ""){
			alert("Por favor, preencha o campo Endereço para Correspondência corretamente.")
			txtEnd.focus();
			return false;
		}
		if(txtBairro.value == ""){
			alert("Por favor, preencha o campo Bairro corretamente.")
			txtBairro.focus();
			return false;
		}
		if(txtCid.value == ""){
			alert("Por favor, preencha o campo Cidade corretamente.");
			txtCid.focus();
			return false;
		}
		if(sltEst.value == ""){
			alert("Por favor, preencha o campo Estado corretamente.");
			txtEst.focus();
			return false;
		}
		if(txtCEP.value == ""){
			alert("Por favor, preencha o campo CEP corretamente.");
			txtCEP.focus();
			return false;
		}
		if(txtFone.value == ""){
			alert("Por favor, preencha o campo Telefone corretamente.");
			txtFone.focus();
			return false;
		}
		if(txtEmail.value.indexOf("@", 0) == -1 || txtEmail.value.indexOf(".", 0) == -1){
			alert("Por favor, preencha o campo Email corretamente.");
			txtEmail.focus();
			return false;
		}
		if(rdTipo[1].checked){
			if(txtNomeResp.value == ""){
				alert("Por favor, digite o nome do Responsável pela empresa " + txtNome.value + ".");
				txtNomeResp.focus();
				return false;
			}
		}
		if(txtRenda.value == "R$"){
			txtRenda.value = "";
		}
		return true;
	}
}

function validaPessoa(){
	with(document.formCadastro){
		if(rdTipo[1].checked){
			txtNacionalidade.disabled = true;
			txtNacionalidade.value = "";
			sltEstCivil.disabled = true;
			sltEstCivil.options[0].selected = true;
			sltSexo.disabled = true;
			sltSexo.options[0].selected = true;
			txtNomeResp.disabled = false;
			txt2End.disabled = true;
			txt2End.value = "";
			txt2Compl.disabled = true;
			txt2Compl.value = "";
			txt2Bairro.disabled = true;
			txt2Bairro.value = "";
			txt2Cid.disabled = true;
			txt2Cid.value = "";
			slt2Est.disabled = true;
			slt2Est.options[0].selected = true;
			txt2CEP.disabled = true;
			txt2CEP.value = "";
			txt2Fone.disabled = true;
			txt2Fone.value = "";
			txt2Ramal.disabled = true;
			txt2Ramal.value = "";
			txtRG.maxLength = "20";
			txtCPF.maxLength = "14";
			txtRG.onkeydown = "";
		}else if(rdTipo[0].checked){
			txtNacionalidade.disabled = false;
			txtNacionalidade.value = "Brasileiro";
			sltEstCivil.disabled = false;
			sltSexo.disabled = false;
			txtNomeResp.disabled = true;
			txtNomeResp.value = "";
			txt2End.disabled = false;
			txt2Compl.disabled = false;
			txt2Bairro.disabled = false;
			txt2Cid.disabled = false;
			slt2Est.disabled = false;
			txt2CEP.disabled = false;
			txt2Fone.disabled = false;
			txt2Ramal.disabled = false;
			txtRG.maxLength = "9";
			txtCPF.maxLength = "11";
		}
		txtNome.focus();
	}
}

function awaysNum() {
	if (event.keyCode < 45 || event.keyCode > 57 || event.keyCode > 32 && event.keyCode < 48) {
		if (event.keyCode != 13) {
			event.returnValue = false;
		}
	}
}

function validaData(){
	with(document.formCadastro){
		if(txtNasc.value.length == 2){
			txtNasc.value = txtNasc.value + "/"
		}else if(txtNasc.value.length == 5){
			txtNasc.value = txtNasc.value + "/"
		}
	}
}

function validaFone(campo){
	if(campo.value.length == 2){
		campo.value = "(" + campo.value + ") ";
	}else if(campo.value.length == 9){
		campo.value = campo.value + "-";
	}
}

function verificaPessoa(){
	with(document.formCadastro){
		if(rdTipo(0).checked == false && rdTipo(1).checked == false){
			alert("Por favor escolha: Pessoa Física ou Pessoa Jurídica.");
			return false;
		}
	}
}

var retorno = new Boolean();
function validaSeguro(){
	retorno = true;
	with(document.formSeguro){
		if(txt1Ben.value == ""){
			alert("Por favor, preencha o campo 1º Beneficiário corretamente.");
			txt1Ben.focus();
			return false;
		}
	}
	for(var i = 1; i < 7; i++){
		seguroPlus("rd" + i);
		if(retorno == false) return false;
	}
}

function seguroPlus(campo){
	with(document.formSeguro){
		if(eval(campo + "(0)").checked == false && eval(campo + "(1)").checked == false){
			alert("Você deve preencher todas as opções.");
			eval(campo + "(0)").focus();
			retorno = false;
		}
	}
}

function verificaAceito(){
	if(document.formContrato.cbAceito.checked){
		document.formContrato.btAvancar.disabled = false;
	}else{
		document.formContrato.btAvancar.disabled = true;
	}
}

function imprimeContrato(url){
	window.open(url, "contrato", "toolbar=no, location=no, directories=no, status=yes, menubar=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=500, top=20, left=20");
}

function janelaCaract(url){
	window.open(url, "caracteristicas", "toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=500, height=300, top=20, left=20");
}

function abreDuvidas(url){
	window.open(url, "duvidas", "toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=400, height=300, top=20, left=20");
}

function carregaBoleto(){
	document.getElementById("formPag").submit();
}

function formataMoeda(campo,tammax,teclapres){
	var tecla = teclapres.keyCode;
	vr = document.getElementById(campo).value;
	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(tam < tammax && tecla != 8){
		tam = vr.length + 1;
	}
	if(tecla == 8){
		tam = tam - 1;
	}
	if(tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105){
		if(tam <= 2){
			document.formCadastro[campo].value = vr;
		}
		if((tam > 2) && (tam <= 5)){
			document.formCadastro[campo].value = vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam);
		}
		if((tam >= 6) && (tam <= 8)){
			document.formCadastro[campo].value = vr.substr(0, tam - 5) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
		}
		if((tam >= 9) && (tam <= 11)){
			document.formCadastro[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)){
			document.formCadastro[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 <= 17)){
			document.formCadastro[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);
		}
	}else{
		return false;
	}
}

function insereSifrao(elemento){
	if(elemento.value != ""){
		if(elemento.value.substr(0, 2) != "R$"){
			elemento.value = "R$" + elemento.value;
		}
	}
}
