﻿/// <reference path="../JQuery/jquery-vsdoc.js" />

var DocumentosEVistos = {};

DocumentosEVistos.Tipos = {};

DocumentosEVistos.Tipos.DocumentosGerais = 1;
DocumentosEVistos.Tipos.VistoDocumentosNecessarios = 2;
DocumentosEVistos.Tipos.VistoFormularios = 3;

DocumentosEVistos.OcultarDocumento = function(boxPadrao) {
    $Get(boxPadrao).className = 'box_padrao box_perguntas_off';
}

DocumentosEVistos.ExibirDocumento = function(boxPadrao) {
    $Get(boxPadrao).className = 'box_padrao box_perguntas_on';
}

DocumentosEVistos.ExibirTodosDocumentos = function() {
    var divs = document.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf('BoxPadraoDocumento') != -1)
        {
            if (divs[i].className == 'box_padrao box_perguntas_on')
                divs[i].className = 'box_padrao box_perguntas_off';
            else
                divs[i].className = 'box_padrao box_perguntas_on';
        }
    }
}

DocumentosEVistos.PrimeiroCarregamento = function() {
    DocumentosEVistos.AjustaExibicao();
    DocumentosEVistos.Busca();
}

DocumentosEVistos.PrimeiroCarregamentoASCX = function()
{
    DocumentosEVistos.AjustaExibicao();
    DocumentosEVistos.BuscaASCX();
}

DocumentosEVistos.TipoAlterado = function() {
    var tipo = DocumentosEVistos.ObtemTipo();
    if (tipo == 0)
    {
        page.radVistosDocumentosNecessarios.checked = true;
        tipo = this.Tipos.VistoDocumentosNecessarios;
    }
    
    DocumentosEVistos.AjustaExibicao();
    DocumentosEVistos.Busca();
}

DocumentosEVistos.TipoAlteradoASCX = function()
{
    var tipo = DocumentosEVistos.ObtemTipo();
    if (tipo == 0)
    {
        page.radVistosDocumentosNecessarios.checked = true;
        tipo = this.Tipos.VistoDocumentosNecessarios;
    }

    DocumentosEVistos.AjustaExibicao();
    DocumentosEVistos.BuscaASCX();
}

DocumentosEVistos.PaisAlterado = function() {
    DocumentosEVistos.Busca();
}

//Oculta ou exibe divs
DocumentosEVistos.AjustaExibicao = function() {
    var tipo = DocumentosEVistos.ObtemTipo();
    page.ulTipoVisto.style.display = ((tipo == this.Tipos.VistoDocumentosNecessarios || tipo == this.Tipos.VistoFormularios) ? '' : 'none');
}

DocumentosEVistos.ObtemTipo = function() {
    if (page.radDocumentosGerais.checked)
        return this.Tipos.DocumentosGerais;
    else if (page.radVistosDocumentosNecessarios.checked)
        return this.Tipos.VistoDocumentosNecessarios;
    else if (page.radVistosFormularios.checked)
        return this.Tipos.VistoFormularios;
    else
        return 0;
}

DocumentosEVistos.Busca = function() {
    page.divResultado.style.display = 'none';
    page.rptDocumentos.DataBind(null);
    
    var tipo = DocumentosEVistos.ObtemTipo();
    var pais = page.dropPais.value;
    
    //Executa a busca
    if (tipo == this.Tipos.DocumentosGerais)
        Ajax.Documento.ListarPorTipo(tipo, DocumentosEVistos.BuscaEnd);
    else
        Ajax.Documento.ListarPorTipoEPais(tipo, pais, DocumentosEVistos.BuscaEnd);
}

DocumentosEVistos.BuscaASCX = function()
{
    page.divResultado.style.display = 'none';
    page.rptDocumentos.DataBind(null);

    var tipo = DocumentosEVistos.ObtemTipo();
    var pais = page.hdnPais.value;

    //Executa a busca
    if (tipo == this.Tipos.DocumentosGerais)
        Ajax.Documento.ListarPorTipo(tipo, DocumentosEVistos.BuscaEnd);
    else
        Ajax.Documento.ListarPorTipoEPais(tipo, pais, DocumentosEVistos.BuscaEnd);
}

DocumentosEVistos.BuscaEnd = function(rt) {
    if (rt.length != 0) {
        page.divResultado.style.display = '';
        page.rptDocumentos.DataBind(rt);
    }
}


/*****************************/
var defaultNomeObj;

function VerificaCidade()
{

jQuery('.jhdnCidade').each
    (
        function(index, value)
        {
            var cidadeId = jQuery(value).val();
            var rptNome = '#rptDocumentos' + cidadeId;
            jQuery(rptNome + " .jlinkExibir").each
            (
                function(index, value)
                {
                    jQuery(this).addClass("jcidadeId_" + cidadeId);
                }
            );
            jQuery(rptNome + " .jlinkOcultar").each
            (
                function(index, value)
                {
                    jQuery(this).addClass("jcidadeId_" + cidadeId);
                }
            );

        }
    );
        
}

DocumentosEVistos.PrimeiroCarregamentoASCX2 = function()
{
    DocumentosEVistos.AjustaExibicao2();
    //DocumentosEVistos.BuscaASCX2();
}

DocumentosEVistos.OcultarDocumento2 = function(boxPadrao, obj)
{
    VerificaCidade();

    var cidadeId = 0;
    var classList = jQuery(obj).attr('class').split(/\s+/);

    $.each(classList, function(index, item)
    {
        if (item.toString().substr(0, 9) == 'jcidadeId')
        {
            cidadeId = item.toString().substr(10, 20);
            //alert(cidadeId);
        }
    });


    //var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
    /*jQuery("#rptDocumentos" + cidadeId + " #" + boxPadrao).removeClass("box_perguntas_on");
    jQuery("#rptDocumentos" + cidadeId + " #" + boxPadrao).addClass("box_perguntas_off");
    */
    jQuery(jQuery("#rptDocumentos" + cidadeId + " .jBoxPadraoDocumento")[boxPadrao]).removeClass("box_perguntas_on");
    jQuery(jQuery("#rptDocumentos" + cidadeId + " .jBoxPadraoDocumento")[boxPadrao]).addClass("box_perguntas_off");
}

DocumentosEVistos.ExibirDocumento2 = function(boxPadrao, obj)
{
    VerificaCidade();
    
    var cidadeId = 0;
    var classList = jQuery(obj).attr('class').split(/\s+/);

    $.each(classList, function(index, item)
    {
        if (item.toString().substr(0, 9) == 'jcidadeId')
        {
            cidadeId = item.toString().substr(10, 20);
            //alert(cidadeId);
        }
    });
    //var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
/*
    jQuery("#rptDocumentos" + cidadeId + " #" + boxPadrao).removeClass("box_padrao box_perguntas_off");
    jQuery("#rptDocumentos" + cidadeId + " #" + boxPadrao).addClass("box_padrao box_perguntas_on");
    */
    jQuery(jQuery("#rptDocumentos" + cidadeId + " .jBoxPadraoDocumento")[boxPadrao]).removeClass("box_perguntas_off");
    jQuery(jQuery("#rptDocumentos" + cidadeId + " .jBoxPadraoDocumento")[boxPadrao]).addClass("box_perguntas_on");
}

DocumentosEVistos.ExibirTodosDocumentos2 = function(idCidade)
{
    var divs = jQuery("#rptDocumentos" + idCidade + " .jBoxPadraoDocumento");

    for (var i = 0; i < divs.length; i++)
    {
        if (divs[i].id.indexOf('BoxPadraoDocumento') != -1)
        {
            if (jQuery(divs[i]).hasClass('box_perguntas_on') == true)
            {
                jQuery(divs[i]).removeClass('box_perguntas_on')
                jQuery(divs[i]).addClass('box_perguntas_off')
            }
            else
            {
                jQuery(divs[i]).removeClass('box_perguntas_off')
                jQuery(divs[i]).addClass('box_perguntas_on')
            }
        }
    }
}

DocumentosEVistos.TipoAlteradoASCX2 = function(objId)
{    
    defaultNomeObj = objId.substr(0, objId.lastIndexOf("_") + 1);

    var tipo = DocumentosEVistos.ObtemTipo2(defaultNomeObj)

    if (tipo == 0)
    {
        jQuery("#" + defaultNomeObj + "radVistosDocumentosNecessarios").attr({ checked: true });
        tipo = this.Tipos.VistoDocumentosNecessarios;
    }

    DocumentosEVistos.AjustaExibicao2(defaultNomeObj);
    DocumentosEVistos.BuscaASCX2(defaultNomeObj);
}

//Oculta ou exibe divs
DocumentosEVistos.AjustaExibicao2 = function(defaultNomeObj)
{
    var tipo = DocumentosEVistos.ObtemTipo2(defaultNomeObj);
    //page.ulTipoVisto.style.display = ((tipo == this.Tipos.VistoDocumentosNecessarios || tipo == this.Tipos.VistoFormularios) ? '' : 'none');
    if (tipo == this.Tipos.VistoDocumentosNecessarios || tipo == this.Tipos.VistoFormularios)
    {
        jQuery("#" + defaultNomeObj + "ulTipoVisto").show();
    }
    else
    {
        jQuery("#" + defaultNomeObj + "ulTipoVisto").hide();
    }
}

DocumentosEVistos.ObtemTipo2 = function(defaultNomeObj)
{
    if (jQuery("#" + defaultNomeObj + "radDocumentosGerais").is(":checked"))
        return this.Tipos.DocumentosGerais;
    else if (jQuery("#" + defaultNomeObj + "radVistosDocumentosNecessarios").is(":checked"))
        return this.Tipos.VistoDocumentosNecessarios;
    else if (jQuery("#" + defaultNomeObj + "radVistosFormularios").is(":checked"))
        return this.Tipos.VistoFormularios;
    else
        return 0;
}

DocumentosEVistos.Busca2 = function(defaultNomeObj)
{
    jQuery("#" + defaultNomeObj + "divResultado").hide();
    page.rptDocumentos.DataBind(null);

    var tipo = DocumentosEVistos.ObtemTipo2(defaultNomeObj);
    var pais = jQuery("#" + defaultNomeObj + "hdnPais").val();
    var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
    
    //Executa a busca
    if (tipo == this.Tipos.DocumentosGerais)
        Ajax.Documento.ListarPorTipo(tipo,
            function(rt)
            {
                if (rt.length != 0)
                {
                    var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
                    jQuery("#" + defaultNomeObj + "divResultado").show();

                    eval("page.rptDocumentos" + cidade + ".DataBind(rt)");
                }
            }
        );
    else
        Ajax.Documento.ListarPorTipoEPais(tipo, pais,
            function(rt)
            {
                if (rt.length != 0)
                {
                    var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
                    jQuery("#" + defaultNomeObj + "divResultado").show();

                    eval("page.rptDocumentos" + cidade + ".DataBind(rt)");
                }
            }
        );
}

DocumentosEVistos.BuscaASCX2 = function(defaultNomeObj)
{
    jQuery("#" + defaultNomeObj + "divResultado").hide();
    var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
        
    eval("page.rptDocumentos" + cidade + ".DataBind(null)");    

    var tipo = DocumentosEVistos.ObtemTipo2(defaultNomeObj);
    var pais = jQuery("#" + defaultNomeObj + "hdnPais").val(); //page.hdnPais.value;

    //Executa a busca
    if (tipo == this.Tipos.DocumentosGerais)
        Ajax.Documento.ListarPorTipo(tipo,
                function(rt)
                {
                    if (rt.length != 0)
                    {
                        var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
                        jQuery("#" + defaultNomeObj + "divResultado").show();

                        eval("page.rptDocumentos" + cidade + ".DataBind(rt)");
                    }
                }        
        );
    else
        Ajax.Documento.ListarPorTipoEPais(tipo, pais,
                function(rt)
                {
                    if (rt.length != 0)
                    {
                        var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
                        jQuery("#" + defaultNomeObj + "divResultado").show();

                        eval("page.rptDocumentos" + cidade + ".DataBind(rt)");
                    }
                }
        );
}

/*DocumentosEVistos.BuscaEnd2 = function(rt)
{
    if (rt.length != 0)
    {
        var cidade = jQuery("#" + defaultNomeObj + "hdnCidade").val();
        jQuery("#" + defaultNomeObj + "divResultado").show();

        eval("page.rptDocumentos" + cidade + ".DataBind(rt)");    
    }
}*/
