////////////////////////////////////////////////////////////////////////////////
///// Jonatas Riciano Silveira	
///// Data: 21/07/08
///// Descriçao: Funçoes js para os Representantes
////////////////////////////////////////////////////////////////////////////////



function representante()
{
    if ($("#frmRepresentante").size() > 0)
    {
	    // Validacao			
	    $("#frmRepresentante").validate(
	    {
		    submitHandler: function()
		    {
			    carregaRepresentantes($("#strEstado").val(),$("#strPais").val());
		    },
		    errorPlacement: function(error, element) 
		    {
			    error.appendTo( $("#erro" + element.attr("id")) );
		    },			
		    errorClass: "erro",
		    errorElement: "span",
		    rules:
		    {
			    strPais:
			    {
				    required: true
			    }
		    },
		    messages:
		    {
			    strPais:
			    {
				    required: 'Selecione seu Pais'
			    }
		    }
	    });		

	    if ($("#frmRepresentante").length > 0)
	    {			
		    var strPais = $("#strPais").val();
    		
		    if (strPais == "Brasil")
		    {
			    $("#btnBusca").hide();
			    $("#divEstado").show();
		    }
		    else
		    {
			    $("#divEstado").hide();
			    $("#btnBusca").show();
		    }
    		
		    $("#strPais").change(function()
		    {
			    var strPais = $("#strPais").val();

			    if (strPais != "Brasil")
			    {
				    $("#divEstado").hide();
				    $("#btnBusca").show();
			    }
			    else 
			    {
				    $("#btnBusca").hide();
				    $("#divEstado").show();
			    }
		    });		
	    }
    	
    	
	    // Validacao			
	    $("#frmRepresentanteGeral").validate(
	    {
		    errorPlacement: function(error, element) 
		    {
			    error.appendTo( $("#erro" + element.attr("id")) );
		    },			
		    errorClass: "erro",
		    errorElement: "span",
		    rules:
		    {
			    strPais:
			    {
				    required: true
			    }
		    },
		    messages:
		    {
			    strPais:
			    {
				    required: 'Selecione seu Pais'
			    }
		    }
	    });		

	    if ($("#frmRepresentanteGeral").length > 0)
	    {			
		    var strPais = $("#strPais").val();
    		
		    if (strPais == "Brasil")
		    {
			    $("#btnBusca").hide();
			    $("#divEstado").show();
		    }
		    else
		    {
			    $("#divEstado").hide();
			    $("#btnBusca").show();
		    }
    		
		    $("#strPais").change(function()
		    {
			    var strPais = $("#strPais").val();

			    if (strPais != "Brasil")
			    {
				    $("#divEstado").hide();
				    $("#btnBusca").show();
			    }
			    else 
			    {
				    $("#btnBusca").hide();
				    $("#divEstado").show();
			    }
		    });		
	    }
    }
	
}

function carregaRepresentantes(strEstado,strPais)
{
	jQuery.ajax({
		type: "POST",
		url: "RepresentanteResultado.asp",
		data: "strPais="+escape(strPais)+"&strEstado="+escape(strEstado),
		contentType: "application/x-www-form-urlencoded; charset=iso-8859-2;",
		complete: function(res,status)
		{
			var retorno = res.responseText;
			//alert(res.responseText);
			if(retorno.replace("+"," "))
			{
				try
				{
					
					$("#divResultadoRepresentantes").html(unescape(res.responseText).replace(/[+]/g,"  "));
				}
				catch(e)
				{
						
				}
			}
			if (strPais == "Brasil")
			{
				$("#btnBusca").hide();
				$("#divEstado").show();
				if($("#strEstado option[value=\""+strEstado+"\"]").val() == strEstado)
				{
					$("#strEstado option[value=\""+strEstado+"\"]").attr("selected","selected");
				}
				else
				{
					$("#strEstado").append("<option selected=\"selected\" value=\""+strEstado+"\">"+strEstado+"</option>");
				}
			}
			else
			{
				$("#divEstado").hide();
				$("#btnBusca").show();
			}
			
			if($("#strPais option[value=\""+strPais+"\"]").val() == strPais)
			{
				$("#strPais option[value=\""+strPais+"\"]").attr("selected","selected");
			}
			else
			{
				$("#strPais").append("<option selected=\"selected\" value=\""+strPais+"\">"+strPais+"</option>");
			}
		}
	});
}
