var Mascara = {
	moedaVal: 0,
	validar: function(){
		$("#cpf").mask("999.999.999-99");
		$("#rg").mask("99.999.999-*");
		$("#cnpj").mask("99.999.999/9999-99");
		$("#cep").mask("99999-999");
		$("#data").mask("99/99/9999");
		$("#ult_emprego_data").mask("99/9999");
		$("#telefone, #celular, #fax").mask("(999) 9999-9999");
		$("#salario_pret, #ult_emprego_sal").keyup(Mascara.moeda);
	},
	
	moeda: function(){
		val = this.value.replace("R$", "");
		val = val.replace(" ", "");
		val = val.replace(",00", "");
		val = val.replace(",", "");
		val = val.replace(".", "");
		//alert(val);
		val = "R$ " + val + ",00";
		
		Mascara.moedaVal = val;
		this.value = Mascara.moedaVal;
	}
};

var PFPJ = {
	init: function(){
		$(".main_form .pf input").attr("disabled", "disabled");
		$(".main_form .pj").hide();
		$(".main_form #pj, .main_form #pf").click(PFPJ.mudar);
	},
	
	mudar: function(){
		if(this.value == "Pessoa Física"){
			$(".main_form .pf").show();
			$(".main_form .pf input").attr("disabled", "");
			$(".main_form .pj").hide();
		}
		else {
			$(".main_form .pj").show();
			$(".main_form .pj input").attr("disabled", "");
			$(".main_form .pf").hide();
		}
	}
};

var CEPAjax = {
	
	init : function(){
		$("#cep").blur(CEPAjax.getAddress);
	},
	
	getAddress : function(){
		if(this.value.length == 9){
			$("#cep").attr("style", "background: url('img/ld.gif') right bottom no-repeat;");
			$.ajax({
				type: "get",
				url: "http://ricardoriogo.com.br/get.php?cep=" + this.value,
				cache: false,
				dataType: "script",
				success: function(){
					$("#cep").removeAttr("style");
					$("#tp_log").attr("value", resultadoCEP["tipo_logradouro"]);
					$("#log").attr("value", resultadoCEP["logradouro"]);
					$("#bairro").attr("value", resultadoCEP["bairro"]);
					$("#estado option").removeAttr("selected");
					$("#estado option[value='" + resultadoCEP["uf"] + "']").attr("selected", "selected");
					$("#cidade").attr("value", resultadoCEP["cidade"]);
					$("#num").focus();
				}
			});
		}
	}
};

var OutroBox = {
	init: function(){
		OutroBox.checar();
		$("#internet, #e-mail, #revista, #amigo, #outro").click(OutroBox.checar);
	},
	
	checar: function(){
		if($("input[name='onde']:checked").attr("value") == "Outro"){
			$("#outro2").attr("disabled", "");
		}
		else {
			$("#outro2").attr("disabled", "disabled");
		}
	}
};
/*
var OutroBox = {
	init: function(){
		OutroBox.checar();
		$("#internet, #e-mail, #revista, #amigo, #outro").click(OutroBox.checar);
	},
	
	checar: function(){
		if($("#outroinput:checked").attr("value") == "Outro"){
			$("#outro2box").show();
		}
		else {
			$("#outro2box").hide();
		}
	}
};
*/

var cityState = {
	fistTime : true,
	
	init: function(){
		$("#estado").change(cityState.carregarCidades);//cityState.carregarCidades
	},
	
	carregarCidades: function(){
		if(cityState.fistTime){
			$("#cidade").attr("id", "cidadeOld").attr("name", "cidadeOld");
			$("#cidadeOld").hide();
			$("#cidadeExtra").attr("id", "cidade").attr("name", "cidade");
			$("#cidade").show();
			cityState.fistTime = false;
		}
		$("#cidade").html("<option selected='selected'> Carregando ...<option>");
		$.ajax({
			type: "get",
			url: "get_city.php?uf=" + this.value,
			cache: false,
			dataType: "html",
			success: function(html){
				$("#cidade").html(html);
			}
		});
		
	}
};

$(function(){
	Mascara.validar();
	PFPJ.init();
	CEPAjax.init();
	OutroBox.init();
	cityState.init();
});
