﻿function jEnderecos()
{
    //** variaveis privativas 
    var jDS;
    var jDT;
    var tpl;

    //** metodos privativos
    var render = {
        optionSelecione: function()
        {
            tpl.tpl_option.value =  "0";
		    tpl.tpl_option.text = "Selecione";
		    return tpl.tpl_option.getHtml();
        },
		listaEstados: function()
		{
		   	var dt = Enderecos.estados();
		   		
		    var sb = new StringBuilder();
			sb.append(render.optionSelecione());
			for(var i=0;i< dt.value.rows.count(); i++)
			{			
				tpl.tpl_option.value = dt.value.rows[i].CodEstado;
				tpl.tpl_option.text = dt.value.rows[i].Uf;
				sb.append(tpl.tpl_option.getHtml());
			}
			return sb.toString(); 
		}
    };    

    var mountRequest = {		
		struct: function(pCodEndereco, pCodEmpresa)
		{			
			var endereco = new StructType('StructEnderecos');
						
			endereco.codEndereco = (pCodEndereco > 0 ? pCodEndereco : 0);
			
			if (pCodEmpresa == "" && pCodEmpresa == undefined)
			    endereco.codEmpresa = null;
			else
			    endereco.codEmpresa = parseInt(pCodEmpresa);
						
			endereco.cep =          $('txt_CEP').value;
			endereco.logradouro =   $('txt_endereco').value;
			endereco.complemento =  $('txt_complemento').value;
			endereco.bairro =       parseInt($('txt_bairro').value);
			endereco.codEstado =    parseInt($('txt_estado').value);
			endereco.pais =         parseInt($('txt_pais').value);
			endereco.codCidade =    parseInt($('txt_cidade').value);
			endereco.codLocalidade = null;
			return endereco;
		}
	};

    //DOM node ctrlRefs
    var ctrlRefs = {
        divAssociacoes: function(){return $('divAssociacoes');}
    };    
       
    var startEnv = {
        inserir: function(pCodEndereco, pCodEmpresa)
        {
			return Enderecos.inserir(mountRequest.struct(pCodEndereco, pCodEmpresa)); //Adiciona os dados de Endereço no Banco
		},
		listar: function(pCodEndereco, pCodEmpresa)
		{
		    return Enderecos.listar(pCodEndereco, pCodEmpresa);
		},
		listaEstados: function()
		{
            return render.listaEstados();
		}
    };
    
    //** métodos publicos
    this.exec = {
		inserir: function(pCodEndereco, pCodEmpresa)
		{			
		   return startEnv.inserir(pCodEndereco, pCodEmpresa);
		},
		validar: function()
		{
		    if ($('txt_CEP').value.length < 8 && $('txt_endereco').value.length < 5 && $('txt_bairro').value.length < 3 && $('sel_estado').value.length < 2 && $('txt_cidade').value.length < 3 && $('txt_pais').value.length < 3)
		        return true;
		    else
		        return false;
		},
		listar: function(pCodEndereco, pCodEmpresa)
		{
		    return startEnv.listar(pCodEndereco, pCodEmpresa);
		},
		listarEstados: function ()
		{
		   return startEnv.listaEstados();
		}
	};
       
     //construtor
    this.constructor = new function()
    {
		tpl = new Template("Common");
    };
};
