var objPlanos = {};

function inicializarForm(tipo){		
	xajax_obterArrayPlanos(tipo);
	$(function() {
		$('.password').pstrength();
	});
	$("#cidade").tipsy({gravity: $.fn.tipsy.autoWE});
}

function setarArrayPlanos(planos, tipo){		
	objPlanos = planos;	
	if(tipo == "to" || tipo == "E"){
		montarComboPlanos();
	}	
	ajustarFormIncricao($("#plano").val(), $("#produto").val());
	setarValorPlano($('#plano').val());		
}

function montarComboPlanos(){
	var combo = "";
	combo += "<p class='caption'>Plano</p>"
	combo += "<select name='planoInscricao' id='planoInscricao' onchange='selecionarPlano(this.value)'>";
	combo += "<option value=''>Selecione</option>";
	$.each(objPlanos,
		function(nome,dadosPlano){			
			combo += "<option value='"+dadosPlano.nome+"'>"+dadosPlano.descricao+"</option>";
		}
	);
	combo += "</select>";	
	$("#comboPlanos").html(combo);	
}

function selecionarPlano(nomePlano){
	$("#plano").val(nomePlano);
	setarValorPlano(nomePlano);
}

function ajustarFormIncricao(plano, produto){	
	$("#slot_plano").html(objPlanos[plano].descricao);
	if(objPlanos[plano].valorMensal > 0){		
		$("#slot_inf_assinatura").show();
	}else{
		$("#slot_inf_assinatura").hide();
	}
}

function setarValorPlano(nomePlano){	
	if(nomePlano != ""){
		var periodicidade = $("#periodicidade").val();
		$.each(objPlanos,
			function(nome,dadosPlano){
				if(nome == nomePlano){				
					$("#slot_valor").html("R$ "+nroBra(dadosPlano[periodicidade]));
				}			
			}
		);
	}else{
		$("#slot_valor").html("R$ 0,00");
	}	
}

function mostraTermos(){
	$('#termos').show();
}

function formatarCpfCnpj(campo,event){
	if(document.getElementById('tipo').value == 'J'){
		return txtBoxFormat(campo.form, campo.name, '99.999.999/9999-99', event);
	}else if(document.getElementById('tipo').value == 'F'){	
		return txtBoxFormat(campo.form, campo.name, '999.999.999-99', event);
	}
} 

function autoTabCpfCnpj(campo,event){
	if(document.getElementById('tipo').value == 'J'){
		return autoTab(campo, 18, event);
	}else if(document.getElementById('tipo').value == 'F'){
		return autoTab(campo, 14, event);
	}
}

function enviarInscricao(dados){
	displayWait('waitInscricao');	
	xajax_enviarInscricao(dados);
}

function enviarInscricaoTinyMed(dados){
	displayWait('waitInscricao');	
	xajax_enviarInscricaoTinyMed(dados);
}

function enviarInscricaoTinyPro(dados){
	displayWait('waitInscricao');	
	xajax_enviarInscricaoTinyPro(dados);
}

function enviarInscricaoTinyOffice(dados){
	displayWait('waitInscricao');	
	xajax_enviarInscricaoTinyOffice(dados);
}

function verificarDisponibilidadeLogin(login){
	xajax_verificarDisponibilidadeLogin(login);
}

function ativarConta(id){	
	displayWait('waitInscricao');
	xajax_ativarConta(id);
}

function ativarSegmentos(selecao){
	switch (selecao) {
		case "me":
			//setarSegmentosMicroEmpresa();
			$("#slot_segmento").css({display:"none"});
			$("#slot_select_segmento").html("");
			setarLabels("J");
			break;
						
		case "pl":
			setarSegmentosProfissionalLiberal();
			setarLabels("F");
			break;
			
		case "pf":
			setarLabels("F");
			$("#slot_segmento").css({display:"none"});
			$("#slot_select_segmento").html("");
			$("#cnpj").focus();
			break;

		default:
			desabilitarSlotSegmentos();
			break;
	}	
}

function setarLabels(valor) {
	$("#tipo").val(valor);
	if (valor == "F") {
		$("#label_cnpj").html("CPF");
		$("#lbl-nome").html("Nome");
		$("#td_nome_responsavel").hide();
		$("#lbl-crm").html("CRM");
	} else {
		$("#label_cnpj").html("CNPJ");
		$("#lbl-nome").html("Razão Social");
		$("#td_nome_responsavel").show();
		$("#lbl-crm").html("CRM do Responsável");
	}
	$("#slot_cnpj_cpf").css({display:"block"});
}

/*
function setarSegmentosMicroEmpresa(){
	var tpl;
	$("#slot_segmento").css({display:"block"});
	tpl = "<select name='segmento' id='segmento'>";
	tpl += "<option value='com'> Comércio</option>";
	tpl += "<option value='ind'> Indústria</option>";
	tpl += "<option value='ser'> Serviços</option>";
	tpl += "</select>";
	$("#slot_select_segmento").html(tpl);
	$("#segmento").focus();
}
*/

function setarSegmentosProfissionalLiberal(){
	var tpl;
	$("#slot_segmento").css({display:"block"});
	tpl = "<select name='segmento' id='segmento'>";
	tpl += "<option value='adm'> Administrador</option>";
	tpl += "<option value='adv'> Advogado</option>";
	tpl += "<option value='arq'> Arquiteto</option>";
	tpl += "<option value='con'> Contabilista</option>";
	tpl += "<option value='den'> Dentista</option>";
	tpl += "<option value='des'> Designer</option>";	
	tpl += "<option value='eng'> Engenheiro</option>";
	tpl += "<option value='med'> Médico</option>";
	tpl += "<option value='pre'> Prestador de serviços</option>";
	tpl += "<option value='out'> Outro</option>";	
	tpl += "</select>";
	$("#slot_select_segmento").html(tpl);
	$("#segmento").focus();
}

function desabilitarSlotSegmentos(){
	$("#slot_segmento").css({display:"none"});
}

function setIdMunicipio(param){
	$("#idMunicipio").val(param.id);
	$("#uf").val(param.uf);
	$("#cidade").focus();
	$('#cidade').removeClass("ac_error");
	$('#cidade').addClass("tipsyOff");	
	$('#cidade').removeAttr("title");
}

