jQuery(document).ready(function(){
    function desaCampos(){
        /*funcao que desabilita a navegabilidade dos selects*/
        jQuery("#estadoID").attr("disabled", true);
        if(jQuery(document).find("#regiaoID").length>0)jQuery("#regiaoID").attr("disabled", true);
        jQuery("#cidadeID").attr("disabled", true);};

    function habiCampos(){
        /*funcao que habilita a navegabilidade dos selects*/
        jQuery("#estadoID").attr("disabled", false);
        if(jQuery(document).find("#regiaoID").length>0)jQuery("#regiaoID").attr("disabled", false);
        jQuery("#cidadeID").attr("disabled", false);};

    function cargReg(uf){
        /*funcao que carrega as regioes no select da Regiao*/
        regiaoDef=''
        jQuery.vErrorClear("#lblregiaoID")
        if(jQuery("#regiaoAtl"))regiaoDef=jQuery("#regiaoAtl").val();
        desaCampos();
        
        jQuery("#regiaoID").html('<option value="">Carregando...</option>');
        jQuery.getJSON("../control/ajax/cargRegiao.pt",{estado: uf,regiao:regiaoDef}, function(regs){
            jQuery("#regiaoID").html(regs);});
    };

    function cargCidade(uf,regiao){
        /*funcao que carrega as cidade no select da Cidade*/
        cidadeDef=""
        jQuery.vErrorClear("#lblcidadeID")
        if (jQuery("#cidadeAtl"))cidadeDef=jQuery("#cidadeAtl").val()

        jQuery("#cidadeID").html('<option value="">Carregando...</option>');
        jQuery.getJSON("../control/ajax/cargCidades.pt",{estado: uf,regiao:regiao,cidade:cidadeDef,tipo:1}, function(cids){
             jQuery("#cidadeID").html(cids);habiCampos();})
    };

    /*ativa as frases nos selects assim que o document é lido*/
    if(jQuery(document).find("#regiaoID").length>0)jQuery("#regiaoID").html('<option value="">--- Selecione Estado ---</option>');
    jQuery("#cidadeID").html('<option value="">--- Selecione Estado ---</option>');

    if(jQuery("#estadoID").val()!=''){
        /*sem tem estado mostra a regiaoAlt e a cidadeAtl no selects assim que é dado o load no documento*/
        desaCampos();
        if(jQuery(document).find("#regiaoID").length>0){
            cargReg(jQuery("#estadoID").val());
            cargCidade(jQuery("#estadoID").val(),jQuery("#regiaoAtl").val(),1);
        }else{cargCidade(jQuery("#estadoID").val(),"",1);}
    }

    jQuery("#estadoID").change(function(){
        desaCampos();
        /*quando o estado é alterado recarrega as regioes e cidades do estado selecionado*/
        regiaoAt = jQuery(document).find("#regiaoID").length /*verifica se tem regiaoID na tela*/
        if(jQuery(this).val()!=''){
            if(regiaoAt>0)cargReg(jQuery(this).val()); /*carrega as regioes do estado selecionado*/
            cargCidade(jQuery(this).val(),'',1);/*carrega as cidades do estado selecionado*/
        }else{
            if(regiaoAt>0)jQuery("#regiaoID").html('<option value="">--- Selecione Estado ---</option>');
            jQuery("#cidadeID").html('<option value="">--- Selecione Estado ---</option>');
        }
    });

    jQuery("#regiaoID").change(function(){
        /*carrega as cidades da regiao selecionada*/
        desaCampos();
        cargCidade(jQuery("#estadoID").val(),jQuery(this).val(),1);
    });


    jQuery("#cidadeID").change(function(){
        /*quando a cidade é alterada é verificado se a regiao foi selecionada, caso no foi selecionado o processo seleciona a regiao da cidade*/
        regiaoAt = jQuery(document).find("#regiaoID").length /*verifica se tem regiaoID na tela*/
        if(regiaoAt>0){
            /*habilita a regiao da cidade selecionada*/
            if(jQuery("#regiaoID").val()==''){
                jQuery.getJSON("../control/ajax/cargCidades.pt",{estado: jQuery("#estadoID").val(),regiao:'',cidade:jQuery("#cidadeID").val(),tipo:2}, function(regs){
                    jQuery("#regiaoID option[value='"+regs+"']").attr("selected", "selected");});};
        };
    });
});

        