$(document).ready(function() {    
    //$("input#login").val('Login (CPF)');
    //$("input#login").mask("");
    $("input#cpf").mask("999.999.999-99");
    
    $('#ajax-cep-x, #ajax-cep-0, #ajax-cep-1, #ajax-cep-2').bind('click', function()
	{
		var elementId = $(this).attr("id").toString();
		var id = "-"+elementId.substr(elementId.length-1,1);
		var holderEnd = $("#holder-endereco"+id);
		var holderCep = $("#holder-cep"+id);
		var cep = $("#holder-cep"+id).find("#buscaCep").val();
		
		if(cep == "")
		{
			alert("Preencha o CEP.");
			return false;
		}
		else
		{
			jQuery.ajax
			(
				{
					url:    '_services/AJAX-buscacep.php',
					cache:  false,
					type:   'POST',
					data:	"cep="+cep,
					dataType: 'json',
					error: function()
					{
						alert('Ocorreu um erro.');
					},
					success:
					function(res)
					{
						if(res.uf == "" || res.logradouro == "" || res == "nada")
						{
							alert("Seu CEP não foi identificado pelo sistema, entre em contato com a Central de Relacionamento para atualizar seu endereço através do telefone "+$("#telIncorporadora").val());
						}
						else
						{
							holderEnd.show();
							holderCep.hide();
							holderEnd.find('#sCEP').val(cep);
							holderEnd.find('#sRua').val(res.tipo_logradouro+' '+res.logradouro);
							holderEnd.find('#sBairro').val(res.bairro);
							holderEnd.find('#sCidade').val(res.cidade);
							holderEnd.find('#sEstado').val(res.uf);
							
							holderEnd.find('#tipoLogradouro option').each(function(){
								if($(this).html() == res.tipo_logradouro.toUpperCase()){
									$(this).attr("selected", "selected");
								}
							});
						}
					}
				}
			)    
		}
        
    });
});

function openNovocliente() {
	tb_show(false,"cadastro.php?TB_iframe=true&amp;width=520&amp;height=570&amp;modal=false&amp;margem=especial",null);
}

function openEsquecisenha() {
	tb_show(false,"senha.php?TB_iframe=true&amp;width=520&amp;height=570&amp;modal=false&amp;margem=especial",null);
}

function telefoneClientePortal() {
	tb_show(false,"telefones.php?TB_iframe=true&amp;width=520&amp;height=570&amp;modal=false&amp;margem=especial",null);
}

function lightBox(){
	$('#bground').show() ;
}

function popup(url, param){
	if (param != undefined)
		window.open(url, "popup", param);
	else
		window.open(url, "popup", "width=793,height=552");
}

function popup_ba(url){
	window.open(url, "popup", "width=793,height=552");
}

function openChat(urlChat){
	pageTracker._trackPageview("portalcliente/chat");
	popup(urlChat,'width=580,height=580');
}
