var xmlhttp=false;
var divoption = " ";
var caminho = " "; // Utilizado em GRUPO E SUBGRUPO - PRODUTOCARRO_ADMIN
var divoption2 = " ";
var caminho2 = " "; // Utilizado em GRUPO E SUBGRUPO - PRODUTOCARRO_ADMIN
var divoption3 = " ";
var caminho3 = " "; // Utilizado em GRUPO E SUBGRUPO - PRODUTOCARRO_ADMIN
var divoption4 = " ";
var caminho4 = " "; // Utilizado em GRUPO E SUBGRUPO - PRODUTOCARRO_ADMIN

function iniciaAjax(){
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
}

function existe_EMAIL(l_value)
{
	iniciaAjax();
	document.getElementById("existe_email").innerHTML = "Verificando email... ";
	
	xmlhttp.open("GET", "retornacpfcnpj.php?tipo=email&email=" + l_value);
	
	xmlhttp.onreadystatechange=processaEMAIL;

	xmlhttp.send(null);
	
	return true;
}
function processaEMAIL()
{
	if (xmlhttp.readyState==4) {
		document.getElementById("existe_email").innerHTML = xmlhttp.responseText;
	}
}

function retorna_confirmados(campo){
	iniciaAjax();
	document.getElementById("encontro"+campo).innerHTML = "Pesquisando...";

	xmlhttp.open("GET", "retornaencontro.php?cod_encontro="+campo);
	xmlhttp.onreadystatechange=processa;
	xmlhttp.send(null);
	
	function processa()
	{
		if (xmlhttp.readyState==4) {
			document.getElementById("encontro"+campo).innerHTML = xmlhttp.responseText;
		}
	}

}

function retorna_nome_produto(campo){
	iniciaAjax();
	document.getElementById("den_produto").value = "Pesquisando...";

	xmlhttp.open("GET", "retornadenominacoes.php?tipo=produto&num_produto="+campo);
	xmlhttp.onreadystatechange=processa;
	xmlhttp.send(null);
	
	function processa()
	{
		if (xmlhttp.readyState==4) {
			document.getElementById("den_produto").value = xmlhttp.responseText;
		}
	}

}

function retorna_nome_produto_e_preco(l_num_produto){
	iniciaAjax();
	document.getElementById("den_produto").value = "Pesquisando...";
	document.getElementById("preco2").innerHTML = "Pesquisando...";
	
	var l_option_cod_fornecedor = document.forms[0].cod_fornecedor;
	var l_cod_fornecedor = l_option_cod_fornecedor.options[l_option_cod_fornecedor.selectedIndex].value;
	xmlhttp.open("GET", "retornadenominacoes.php?tipo=produtopreco&num_produto="+l_num_produto+"&cod_fornecedor="+l_cod_fornecedor);
	xmlhttp.onreadystatechange=processa;
	xmlhttp.send(null);
	
	function processa()
	{
		if (xmlhttp.readyState==4) {
			document.getElementById("preco2").innerHTML 	= "R$ " + xmlhttp.responseText;
			document.getElementById("preco").value			= xmlhttp.responseText;
			retorna_nome_produto(l_num_produto);
		}
	}
	

	/*
	function processa2() {
		xmlhttp.open("GET", "retornadenominacoes.php?tipo=produtopreco&num_produto="+l_num_produto+"&cod_fornecedor="+l_cod_fornecedor);
		xmlhttp.onreadystatechange=processa3;
		xmlhttp.send(null);
		
		function processa3()
		{
			if (xmlhttp.readyState==4) {
				document.getElementById("den_produto").value = xmlhttp.responseText;
				processa2();
			}
		}
		
	}
	*/

}

function showCidade(l_funcao, l_default) {
	iniciaAjax();
	document.getElementById("cidade").innerHTML = "Aguarde... ";
	
	if(l_funcao == "" || l_funcao == " "){
		xmlhttp.open("GET", "retornaoptionmodelo.php?tipo=" + l_default);
	} else {
		xmlhttp.open("GET", "retornaoptionmodelo.php"+ l_funcao);
	}
	
	xmlhttp.onreadystatechange=processa;
	xmlhttp.send(null);
	
	function processa()
	{
		if (xmlhttp.readyState==4) {
			document.getElementById("cidade").innerHTML = xmlhttp.responseText;
		}
	}
}

function showProduto(linke){
	iniciaAjax();
	
	xmlhttp.open("GET", "aguarde.htm");
	xmlhttp.onreadystatechange=processaAguarde;
	xmlhttp.send(null);
	
	function processaAguarde()
	{
		if (xmlhttp.readyState==4) {
			document.getElementById("produto").innerHTML = xmlhttp.responseText;
			processa_prod();
		}
	}
	
	function processa_prod(){
		
		xmlhttp.open("GET", linke);
		xmlhttp.onreadystatechange=processa;
		xmlhttp.send(null);
		
		function processa()
		{
			if (xmlhttp.readyState==4) {
				document.getElementById("produto").innerHTML = xmlhttp.responseText;
			}
		}
	}
	
}

function showProdutoAdminIndex(linke){
	iniciaAjax();
	
	xmlhttp.open("GET", "aguarde.htm");
	xmlhttp.onreadystatechange=processaAguarde;
	xmlhttp.send(null);
	
	function processaAguarde()
	{
		if (xmlhttp.readyState==4) {
			document.getElementById("retorno_produto_admin").innerHTML = xmlhttp.responseText;
			processa_prod();
		}
	}
	
	function processa_prod(){
		
		xmlhttp.open("GET", linke);
		xmlhttp.onreadystatechange=processa;
		xmlhttp.send(null);
		
		function processa()
		{
			if (xmlhttp.readyState==4) {
				document.getElementById("retorno_produto_admin").innerHTML = xmlhttp.responseText;
			}
		}
	}
	
}

function showOption(l_funcao, l_divoption, l_value, l_default){
	//alert(l_funcao+' '+l_divoption+' '+l_value+' '+l_default);

	iniciaAjax();
	document.getElementById(l_divoption).innerHTML = "Aguarde... ";
	
	if(l_value == "" || l_value == " "){
		xmlhttp.open("GET", "retornaoptionmodelo.php?tipo=" + l_default);
	} else {
		xmlhttp.open("GET", "retornaoptionmodelo.php"+ l_funcao);
	}
	
	divoption = l_divoption;

	if (divoption=="option_chassi" || divoption=="option_veiculo"){
		xmlhttp.onreadystatechange=processaOptionModeloVarios;
	} else {
		xmlhttp.onreadystatechange=processaOptionModelo;
	}

	xmlhttp.send(null);
}

function processaOptionModelo()
{
	if (xmlhttp.readyState==4) {
		document.getElementById(divoption).innerHTML = xmlhttp.responseText;
	}
}

//############## CADASTRO VEICULO
function showOptionVarios(l_funcao, l_divoption, l_value, l_default)
{

	iniciaAjax();
	document.getElementById(l_divoption).innerHTML = "Aguarde... ";
	

	if(l_value == ""){
		xmlhttp.open("GET", "retornaoptionmodelo.php?tipo=" + l_default);
	} else {
		xmlhttp.open("GET", "retornaoptionmodelo.php"+ l_funcao);
	}
	
	divoption = l_divoption;

	xmlhttp.onreadystatechange=processaOptionModeloVarios;

	xmlhttp.send(null);
}

function processaOptionModeloVarios()
{
	if (xmlhttp.readyState==4) {
		document.getElementById(divoption).innerHTML = xmlhttp.responseText;
		if(divoption=="option_chassi"){
				showOptionVarios('','option_veiculo','','defaultveiculo');
		} else {
			if(divoption=="option_veiculo"){
				showOptionVarios('','optionmodelo','','defaultmarca');
			}
		}
	}
}

//############## CADASTRO PRODUTO X VEICULO
function showOptionVariosProduto(l_funcao, l_divoption, l_value, l_default)
{

	iniciaAjax();
	document.getElementById(l_divoption).innerHTML = "Aguarde... ";
	

	if(l_value == ""){
		xmlhttp.open("GET", "retornaoptionmodelo.php?tipo=" + l_default);
	} else {
		xmlhttp.open("GET", "retornaoptionmodelo.php"+ l_funcao);
	}
	
	divoption = l_divoption;

	xmlhttp.onreadystatechange=processaOptionModeloVariosProduto;

	xmlhttp.send(null);
}

function processaOptionModeloVariosProduto()
{
	if (xmlhttp.readyState==4) {
		document.getElementById(divoption).innerHTML = xmlhttp.responseText;
		if(divoption=="option_chassi"){
				showOptionVariosProduto('','option_veiculo','','defaultveiculo');
		} else {
			if(divoption=="option_veiculo"){
				showOptionVariosProduto('','optionmodelo','','defaultmarca');
			} else {
				if(divoption=="optionmodelo"){
					showOptionVariosProduto('','subgrupooption','','defaultsubgrupo');
				} else {
					if(divoption=="subgrupooption"){
						showOptionVariosProduto('','produto','','defaultproduto');
					}
				}
			}
		}
	}
}

function validacpf(Objcpf){ 
//	return true;
  
    var cpf = Objcpf.value;
    exp = /\.|\-/g
    cpf = cpf.toString().replace( exp, "" ); 
    var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
    var soma1=0, soma2=0;
    var vlr =11;
    
    for(i=0;i<9;i++){
        soma1+=eval(cpf.charAt(i)*(vlr-1));
        soma2+=eval(cpf.charAt(i)*vlr);
        vlr--;
    }    
    soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
    soma2=(((soma2+(2*soma1))*10)%11);
    
    var digitoGerado=(soma1*10)+soma2;
    if(digitoGerado!=digitoDigitado){
        alert('CPF Invalido!'); 
		return false;	
	}
	alert(digitoGerado);
	alert(digitoDigitado);
	return true;
  
} 

//############ VERIFICA CPF

function existe_cpf(l_value)
{
	iniciaAjax();

	document.getElementById("existecpf").innerHTML = "Verificando CPF... ";
	
	xmlhttp.open("GET", "retornacpfcnpj.php?tipo=CPF&valor=" + l_value);
	
	xmlhttp.onreadystatechange=processaCPF;

	xmlhttp.send(null);
	
	if (erro==true){
		/*
		if(validacpf(l_value)==true){
			return true;
		} else {
			return false;
		}
		*/
		return true;
	} else {
		return false;	
	}
}

function processaCPF()
{
	erro = true;
	if (xmlhttp.readyState==4) {
		document.getElementById("existecpf").innerHTML = xmlhttp.responseText;
		existeCPF = strripos(xmlhttp.responseText,"CPF já utilizado");
		if (existeCPF > 0 ){
			erro = true;
		} else {
			erro = false;	
		}
	}
}
//############ VERIFICA CNPJ

function existe_cnpj(l_value)
{
	iniciaAjax();

	document.getElementById("existecnpj").innerHTML = "Verificando CNPJ... ";
	
	xmlhttp.open("GET", "retornacpfcnpj.php?tipo=CNPJ&valor=" + l_value);
	
	xmlhttp.onreadystatechange=processaCNPJ;

	xmlhttp.send(null);
	
	if (erro==true){
		return true;
	} else {
		return false;	
	}
}
function processaCNPJ()
{
	erro = true;
	if (xmlhttp.readyState==4) {
		document.getElementById("existecnpj").innerHTML = xmlhttp.responseText;
		existeCNPJ = strripos(xmlhttp.responseText,"CNPJ já utilizado");
		if (existeCNPJ > 0 ){
			erro = true;
		} else {
			erro = false;	
		}
	}
}

function strripos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // *     example 1: strripos('Kevin van Zonneveld', 'E');
    // *     returns 1: 16
 
    var i = (haystack+'').toLowerCase().lastIndexOf( (needle+'').toLowerCase(), offset ); // returns -1
    return i >= 0 ? i : false;
}

//################ GRUPO E SUBGRUPO - PRODUTOCARRO_ADMIN
function showOptionGrupoSubgrupo(l_funcao, l_divoption, l_value, l_default, l_value2, l_default2, l_value3, l_default3,l_value4, l_default4)
{
	//l_funcao -> caminho para buscar o subgrupo
	//l_divoption -> div subgrupo
	
	//l_value -> caminho para buscar o produto
	//l_default -> div produto
	
	//l_value2 -> caminho para buscar o chassis
	//l_default2 -> div chassis
	
	//l_value3 -> caminho para buscar o veiculo
	//l_default3 -> div veiculo
	
	//l_value4 -> caminho para buscar o modelo
	//l_default4 -> div modelo

	iniciaAjax();
	document.getElementById(l_divoption).innerHTML = "Aguarde... ";
	

	if(l_value == ""){
		xmlhttp.open("GET", "retornaoptionmodelo.php?tipo=" + l_default);
	} else {
		xmlhttp.open("GET", "retornaoptionmodelo.php"+ l_funcao);
	}
	
	divoption = l_divoption;
	caminho   = l_value;
	
	divoption2 = l_default2;
	caminho2   = l_value2;
	
	divoption3 = l_default3;
	caminho3   = l_value3;
	
	divoption4 = l_default4;
	caminho4   = l_value4;
	
	xmlhttp.onreadystatechange=processaOptionGrupoModeloVarios;

	xmlhttp.send(null);
}

function limpa_option(l_divoption){
	opcao  ='<select name="generico" class="input" id="generico">';
	opcao += "<option value=\" \">Selecione o item acima...</option>";
	opcao += "</select>";
	
	document.getElementById(l_divoption).innerHTML = opcao;
}

function processaOptionGrupoModeloVarios()
{
	if (xmlhttp.readyState==4) {
		document.getElementById(divoption).innerHTML = xmlhttp.responseText;
		if(divoption=="subgrupooption"){
			if(caminho3!=""){
				showOptionGrupoSubgrupo(caminho,'produto','#',' ', caminho2, divoption2, caminho3, divoption3, caminho4, divoption4);
			}
			limpa_option("produto");
		} else {
			if(divoption=="produto"){
				if(caminho3!=""){
					showOptionGrupoSubgrupo(caminho2,divoption2,'#',' ',caminho2, divoption2, caminho3, divoption3, caminho4, divoption4);
				}
			} else {
				if(divoption=="option_chassi"){
					showOptionGrupoSubgrupo(caminho3,divoption3,'#',' ',caminho2, divoption2, caminho3, divoption3, caminho4, divoption4);
				} else {
					if(divoption=="option_veiculo"){
						showOptionGrupoSubgrupo(caminho4,divoption4,'#',' ',caminho2, divoption2, caminho3, divoption3, caminho4, divoption4);
					} 
				}	
			}
		}
		
	}
}

// GRUPO E SUBGRUPO - PRODUTOCARRO_ADMIN - 2º Parte



