/* mostrar lista Error Sign */
function addErrorSign(elemIdArray,formId){
	
	var $msgContainer;
	if ( !formId ) $msgContainer = $j('fieldset:first')
	else $msgContainer = $j('#'+formId);
	
	if ( $j('fieldset #errorsign_fields').length == 0 ){
		$msgContainer.prepend("<p class='errorsign_main' id='errorsign_fields'>Por favor, corrija os erros abaixo:</p>");
	
		$j(elemIdArray).each(function(i){
			$j('#'+elemIdArray[i]).after("<span class='errorsign'>!</span>");
		});
		
		if ( $j('form').hasClass("formcartoesbradesco") ) {
			if ( $j('form fieldset').hasClass("step1-3") ) {
				validadeStep3Nome();
				validadeStep2DataNasc();
				validadeStep2Sexo();
				validadeStep2EstadoCivil();
				validateStepNomeConjuge();
				validateStepEscolaridade();
				validateStepNacionalidade();
				validateStepNomeMae();
				validateStepNumeroDependente();
				validateStepOcupacao();
				validateStepEmpresa();
				validateStepPrefissao();
				validateStepSalario();
				validateStepRenda();
				validateStepOrigemOutrasRendas();
				validateStepTipoAtividade();
				validateStepTempoTrabalho();
				validateStepBanco();
				validateStepAgencia();
				validateStepTempoAposentado();
			} else if ( $j('form fieldset').hasClass("step2-3") ) {
				validadeStep3Telefone();
				validateStepTelefone2();
				validadeStep3Email();
				validadeStep2CPF();
				validadeStep2CEP();
				validateStepTipoResidencia();
				validateStepCEPCorrespondencia();
			} else if ( $j('form fieldset').hasClass("step3-3") ) {
				validateStepNomeCartao();
				validateStepVencimento();
				if ( $j('#numeroAdicionais #sel_numeroAdicionais option:eq(1)').attr('selected') ) {
					validationStepsBradescoCartaoAdicional1();
				} else if ( $j('#numeroAdicionais #sel_numeroAdicionais option:eq(2)').attr('selected') ) {
					validationStepsBradescoCartaoAdicional1();
					validationStepsBradescoCartaoAdicional2();
				}
				validadeStep3Captcha();
			}
		} else {
			if ( $j('form fieldset').hasClass("step1-3") ) {
				try {
					var switchCatID = $j('#switchCatID').text();
					switch (parseInt(switchCatID)) {
						case 9:
							validationStep1SeguroSaudeFamiliar();
							break;
						case 10:
							validationStep1SeguroSaudeIndividual();
							break;
						case 11:
							validationStep1SeguroSaudeEmpresarial();
							break;
						default:
							break;
					}
				} catch (e) {
					// TODO: handle exception
				}
			} else if ( $j('form fieldset').hasClass("step2-3") ) {
				if ( $j('form fieldset').hasClass("emp") ) {
					validadeStep2PossuiCNPJ();
					validadeStep3CNPJ();
					validadeStep3NomeEmpresa();
					validadeStep2Porte();
					validadeStep2CEP();
				} else {
					validadeStep2DataNasc();
					validadeStep2Sexo();
					validadeStep2CPF();
					validadeStep2RendaMensal();
					validadeStep2EstadoCivil();
					validadeStep2CEP();
				}
			} else if ( $j('form fieldset').hasClass("step3-3") ) {
				validadeStep3Nome();
				validadeStep3Email();
				validadeStep3Telefone();
				validadeStep3Captcha();
			}
		}
	}
	goToAncoraError();
}
 
/*
 * Direciona a tela de comentários para os erros de formulário
 */
function goToAncoraError(){
	if ( $j("#frm_comment_news #errorsign_fields").hasClass("errorsign_main") ) {
		$j("html, body").animate({
			scrollTop: $j("#form_comment").offset().top
		},500);
	}
}

/* 
 * Validação de errors da categoria Catões Bradesco - Cartôes Adicionais 
 */
function validationStepsBradescoCartaoAdicional1(){
	validateStepTextField("nomeAcicional_1", "text_nomeAcicional_1");
	validadeStepDataNascAdicional(1);
	validateStepCPFAdicional("cpfAdicional_1", "cpfAdicional_1");
	validateStepParentescoAdicional("parentescoAdicional_1", "sel_parentescoAdicional_1");
}
function validationStepsBradescoCartaoAdicional2(){
	validateStepTextField("nomeAcicional_2", "text_nomeAcicional_2");
	validadeStepDataNascAdicional(2);
	validateStepCPFAdicional("cpfAdicional_2", "cpfAdicional_2");
	validateStepParentescoAdicional("parentescoAdicional_2", "sel_parentescoAdicional_2");
}

/* 
 * Validação de errors da categoria Seguro Saúde Individual 
 */
function validationStep1SeguroSaudeIndividual(){
	validadeStep1Radio("possuiDoenca", "radio_possuiDoenca");
	if ( ($j('#possuiDoenca #possui_doenca').is(":checked")) )
		validadeStep1TextBox("qualDoenca", "qual_doenca");
	validadeStep1Radio("possuiPlano", "radio_possuiPlano");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("quantoTempo", "tempo");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("listSeguradora", "seguradora");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("valorAtual", "valor_atual");
}

/* 
 * Validação de errors da categoria Seguro Saúde Empresarial 
 */
function validationStep1SeguroSaudeEmpresarial(){
	validadeStep1Radio("possuiPlano", "radio_possuiPlano");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("quantoTempo", "tempo");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("listSeguradora", "seguradora");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("valorAtual", "valor_atual");
}

/* 
 * Validação de errors da categoria Seguro Saúde Familiar 
 */
function validationStep1SeguroSaudeFamiliar(){
	var liIDSel = $j('.sexo').parent().parent().attr("id");
	var selectId = $j('.sexo').attr("id");
	validadeStep1Select(liIDSel, selectId);
	
	var liIDDataNas = $j('select.idade').parent().parent().attr("id");
	var selectIdDataNas = $j('select.idade').attr("id");
	var selectIdSplited = selectIdDataNas.split("-")[1];
	validadeStep1DataNascSegSauFamiliar(liIDDataNas, selectIdSplited);
	
	validadeStep1Radio("possuiDoenca", "radio_possuiDoenca");
	if ( ($j('#possuiDoenca #possui_doenca').is(":checked")) )
		validadeStep1TextBox("qualDoenca", "qual_doenca");
	validadeStep1Radio("possuiPlano", "radio_possuiPlano");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("quantoTempo", "tempo");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("listSeguradora", "seguradora");
	if ( ($j('#possuiPlano #oferece').is(":checked")) )
		validadeStep1Select("valorAtual", "valor_atual");
}

/* 
 * Mostrar Error Sign no termo de uso 
 */
function addTermsErrorSign(){
	if($j('fieldset #errorsign_terms').length==0){
		$j('fieldset').prepend("<p class='errorsign_main' id='errorsign_terms'>Para prosseguir, é necessário aceitar os Termos de Uso e Política de Privacidade abaixo:</p>");
		$j('#cb_disclaimer').after("<span class='errorsign'>!</span>");
	}
}

/* 
 * Mostrar Error Sign no Captcha 
 */
function addCaptchaErrorSign(){
	if ( $j('fieldset #errorsign_fields').length == 0 ){
		$j('fieldset').prepend("<p class='errorsign_main' id='errorsign_fields'>Para prosseguir, é necessário digitar corretamente o código da imagem abaixo:</p>");
		$j('#seq').after("<span class='errorsign'>!</span>");
	}
}

$j(document).ready(function(){

	//Adicionar novo campo (ex.: e-mail , telefone)
	$j('.add_one').click(function(){
		
		$j(this).toggleClass('add').toggleClass('remove');
		
		var thisInputWrapper = $j(this).parent().find('.input_wrapper:last');
		if ($j(thisInputWrapper).hasClass('hidden')){
			$j(thisInputWrapper).find('input, select').removeAttr('disabled')
		} else {
			$j(thisInputWrapper).find('input, select').attr('disabled', 'disabled')
		}
		$j(thisInputWrapper).toggleClass('hidden');
		
	});
	
	$j('#models h3').click(function(){
		openModel(this);
	});
	
	/*
	var MeuBlog = '[{"nome":"Igor Escobar","ano":"2008","assunto":"Tecnologia"},{"nome":"Regis Escobarsss","ano":"2008","assunto":"Tecnologia"}]';
	var obj = $j.parseJSON(MeuBlog);
	alert( "Nome: " + obj[1].nome );*/
	
	
	// Possui CNPJ
	if( $j("#possuicnpj_sim").is(":checked") ){
		$j(".cnpj").show();
	} else {
		$j(".cnpj").hide();
	}
	$j(".possuiplano_radio").live("click",function(){
		
		if( $j("#possuicnpj_sim").is(":checked") ){
			$j(".cnpj").show();
		} else {
			$j(".cnpj").hide();
		}
		
	});

	loadStatusStep();
});

/*
 * Seta os estados dos Steps
 * 
 * param numStep
 * return
 */
function setStep(numStep) {
	$j("#step"+numStep).addClass("active");
	for (var i = 1; i < 6; i++) {
		if(numStep != i){
			$j("#step"+i).removeClass("finished");
			$j("#step"+i).removeClass("active");
			if(numStep > i)
				$j("#step"+i).addClass("finished");
		}
	}
}

/*
 * Verifica qual step foi carregado
 * 
 * param
 * return
 */
function loadStatusStep() {
	try {
		var numStep = parseInt($j("#list_steps").attr("class").split("step")[1]);
		switch (numStep) {
		case 1:
			setStep(1);
			break;
		case 2:
			setStep(2);
			break;
		case 3:
			setStep(3);
			break;
		case 4:
			setStep(4);
			break;
		case 5:
			setStep(5);
			break;
		case 6:
			setStep(6);
			break;
	
		default:
			setStep(1);
			break;
		}
	} catch (e) {
		e.message;
	}
}

/* Abre os modelos */
function openModel(el){
	$j(el).toggleClass('opened').next('div').toggle();
}

/* Mecanismo dos steps1 das Categs */
var lib = {	
		toggleElem: function(arr) {
	for(i=0; i<arr.length; i++) {
		var fname = "[name=" + arr[i] + "]";
		var lname = "[for^=" + arr[i] + "]";
		var field = $j(fname);
		if($j(field).is(':visible')) {
			$j(field).attr("disabled", true).parents('li').hide();
			$j(lname).hide();

		} else {
			$j(field).attr("disabled", false).parents('li').show();
			$j(lname).show();
		}
	}
	return false;
},
hideElem: function(arr) {
	for(i=0; i<arr.length; i++) {
		var fname = "[name=" + arr[i] + "]";
		//var lname = "[for^=" + arr[i] + "]";
		var field = $j(fname);
		$j(field).attr("disabled", true).parents('li').hide();
		//$j(lname).hide();
	}
	return false;
},
showElem: function(arr) {
	for(i=0; i<arr.length; i++) {
		var fname = "[name=" + arr[i] + "]";
		//var lname = "[for^=" + arr[i] + "]";
		var field = $j(fname);
		$j(field).attr("disabled", false).parents('li').show();
		//$j(lname).show();
	}
	return false;
}
};

var actions = {
initDetalhesVeiculo: function() {
	$j('[name=kit_gas]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('valor_kitgas'));
		else 					lib.hideElem(new Array('valor_kitgas'));
	});
	$j('[name=possui_antifurto]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('qual_anti_furto'));
		else 					lib.hideElem(new Array('qual_anti_furto'));
	});
},
initInformacoesPessoais: function() {
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('profissao'));
		else 					lib.hideElem(new Array('profissao'));
	});

	function ativProfissional(){
		$j('li.atividade').hide().find(':input').attr('disabled', 'disabled');
		var selVal = $j('#sel_atividade option:selected').val();
		$j('#atividade'+selVal).show().find(':input').removeAttr('disabled');
	}
	ativProfissional();
	$j('#sel_atividade').change(function(){ativProfissional();});
},
initUtilizacaoVeiculo: function() {
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('profissao'));
		else { lib.hideElem(new Array('profissao'));
			
		}
	});
	$j('[name=estuda]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('uso_local_estudo'));
		else 					lib.hideElem(new Array('uso_local_estudo'));
	});
	$j('[name=residencia]').change(function(){
		if($j(this).val()=="4") $j("#residenciaOutros").show();
		else 					$j("#residenciaOutros").hide();
	});
},
initInformacoesCobertura: function() {
	$j('[name=objetivo]').click(function(){
		if($j(this).val()=="1") {
			lib.showElem(new Array('seguradora'));
			lib.showElem(new Array('classe_bonus'));
		} else {
			lib.hideElem(new Array('seguradora'));
			lib.hideElem(new Array('classe_bonus'));
		}
	});
},
initInformacoesFinanciamentoVeiculoUsado: function() {

	$j('[name=estadoCivil]').click(function(){
		if($j(this).val()=="2") lib.showElem(new Array('conjuge'));
		else 					lib.hideElem(new Array('conjuge'));
	});
},
initInformacoesFinanciamentoVeiculoNovo: function() {

	$j('[name=estadoCivil]').click(function(){
		if($j(this).val()=="2") lib.showElem(new Array('conjuge'));
		else 					lib.hideElem(new Array('conjuge'));
	});
},
initInformacoesCreditoPessoal: function() {

	$j('[name=estadoCivil]').click(function(){
		if($j(this).val()=="2") lib.showElem(new Array('conjuge'));
		else 					lib.hideElem(new Array('conjuge'));
	});
},
initInformacoesPlano: function() {

	$j('[name=possuiDoenca]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('doenca'));
		else 					lib.hideElem(new Array('doenca'));
	});
	$j('[name=possuiplano]').click(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('tempoPlano'));
			lib.showElem(new Array('operadoraPlano'));
			lib.showElem(new Array('valorPlano'));
		} else {
			lib.hideElem(new Array('tempoPlano'));
			lib.hideElem(new Array('operadoraPlano'));
			lib.hideElem(new Array('valorPlano'));
		}
	});
	$j('[name=fumante]').click(function(){
		if($j(this).val()=="0") lib.showElem(new Array('ex_fumante'));
		else 					lib.hideElem(new Array('ex_fumante'));
	});
},
initRefinanVeiculo: function() {
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('profissao'));
		else 					lib.hideElem(new Array('profissao'));
	});
},

initConsorcioAutomotivo: function() {
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('profissao'));
		else 					lib.hideElem(new Array('profissao'));
	});
},
initSeguroVidaIndividual: function() {		
	$j('[name=fumante]').click(function(){
		if($j(this).val()=="false") lib.showElem(new Array('exfumante'));
		else 					lib.hideElem(new Array('exfumante'));
	});
	$j('[name=outros]').click(function(){
		if($j(this).val()=="outros" && $j(this).is(':checked')) lib.showElem(new Array('cobertura_outros'));
		else lib.hideElem(new Array('cobertura_outros'));
	});
	$j('[name=possui_seguro]').click(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('tempoSeguro'));
			lib.showElem(new Array('seguradora'));
			lib.showElem(new Array('valorSeguro'));
		} else {
			lib.hideElem(new Array('tempoSeguro'));
			lib.hideElem(new Array('seguradora'));
			lib.hideElem(new Array('valorSeguro'));
		}
	});
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('profissao'));
		else 					lib.hideElem(new Array('profissao'));
	});
	$j('[name=contribuicao]').click(function(){
		if($j(this).val()=="1") {
			lib.showElem(new Array('mensal'));
			lib.hideElem(new Array('unica'));
		}
		else {
			lib.showElem(new Array('unica'));
			lib.hideElem(new Array('mensal'));
		}
	});

},

initSeguroVidaEmpresarial: function() {
	$j('[name=cobertura]').click(function(){
		if($j(this).val()=="5" && $j(this).is(':checked')) lib.showElem(new Array('cobertura_outros'));
		else lib.hideElem(new Array('cobertura_outros'));
	});
	$j('[name=possui_plano]').click(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('quanto_tempo'));
			lib.showElem(new Array('seguradora'));
			lib.showElem(new Array('valor_atual'));
		} else {
			lib.hideElem(new Array('quanto_tempo'));
			lib.hideElem(new Array('seguradora'));
			lib.hideElem(new Array('valor_atual'));
		}
	});
	$j('[name=outros]').click(function(){
		if($j(this).val()=="outros" && $j(this).is(':checked')) lib.showElem(new Array('cobertura_outros'));
		else lib.hideElem(new Array('cobertura_outros'));
	});
	$j('[name=contribuicao]').click(function(){
		if($j(this).val()=="1") {
			lib.showElem(new Array('mensal'));
			lib.hideElem(new Array('unica'));
		}
		else {
			lib.showElem(new Array('unica'));
			lib.hideElem(new Array('mensal'));
		}
	});
},	
initSeguroVidaIndividual: function() {		
	$j('[name=fumante]').click(function(){
		if($j(this).val()=="false") lib.showElem(new Array('exfumante'));
		else 					lib.hideElem(new Array('exfumante'));
	});
	$j('[name=outros]').click(function(){
		if($j(this).val()=="outros" && $j(this).is(':checked')) lib.showElem(new Array('cobertura_outros'));
		else lib.hideElem(new Array('cobertura_outros'));
	});
	$j('[name=possui_seguro]').click(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('tempoSeguro'));
			lib.showElem(new Array('seguradora'));
			lib.showElem(new Array('valorSeguro'));
		} else {
			lib.hideElem(new Array('tempoSeguro'));
			lib.hideElem(new Array('seguradora'));
			lib.hideElem(new Array('valorSeguro'));
		}
	});
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('profissao'));
		else 					lib.hideElem(new Array('profissao'));
	});
	$j('[name=contribuicao]').click(function(){
		if($j(this).val()=="1") {
			lib.showElem(new Array('mensal'));
			lib.hideElem(new Array('unica'));
		}
		else {
			lib.showElem(new Array('unica'));
			lib.hideElem(new Array('mensal'));
		}
	});
	
},
initSeguroVidaMulher: function() {		
	$j('[name=fumante]').click(function(){
		if($j(this).val()=="false") lib.showElem(new Array('exfumante'));
		else 					lib.hideElem(new Array('exfumante'));
	});
	$j('[name=outros]').click(function(){
		if($j(this).val()=="outros" && $j(this).is(':checked')) lib.showElem(new Array('cobertura_outros'));
		else lib.hideElem(new Array('cobertura_outros'));
	});
	$j('[name=possui_seguro]').click(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('tempoSeguro'));
			lib.showElem(new Array('seguradora'));
			lib.showElem(new Array('valorSeguro'));
		} else {
			lib.hideElem(new Array('tempoSeguro'));
			lib.hideElem(new Array('seguradora'));
			lib.hideElem(new Array('valorSeguro'));
		}
	});
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('profissao'));
		else 					lib.hideElem(new Array('profissao'));
	});
	$j('[name=contribuicao]').click(function(){
		if($j(this).val()=="1") {
			lib.showElem(new Array('mensal'));
			lib.hideElem(new Array('unica'));
		}
		else {
			lib.showElem(new Array('unica'));
			lib.hideElem(new Array('mensal'));
		}
	});
	
},
initSeguroNautico: function() {			
	$j('[name=possui_seguro]').click(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('quanto_tempo'));
			lib.showElem(new Array('seguradora'));
		} else {
			lib.hideElem(new Array('quanto_tempo'));
			lib.hideElem(new Array('seguradora'));
		}
	});		
},
initConsorcioImobiliario: function() {			
	$j('[name=trabalha]').click(function(){
		if($j(this).val()=="sim") {
			lib.showElem(new Array('profissao'));
		} else {
			lib.hideElem(new Array('profissao'));
		}
	});
},
initCreditoPessoal: function() {			
	$j('[name=possuiConta]').change(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('possuiCheque'));
		} else {
			lib.hideElem(new Array('possuiCheque'));
		}
	});	
	$j('[name=ocupacaoPrincipal]').change(function(){
		if($j(this).val()=="outros") $j("#liOutros").show();//lib.showElem(new Array('liOutros'));
		else 					$j("#liOutros").hide();//lib.hideElem(new Array('liOutros'));
	});
},
initEmprestimoConsignado: function() {			
	$j('[name=possuiEmprestimo]').change(function(){
		if($j(this).val()=="true") {
			lib.showElem(new Array('valorEmprestimoAproximado'));
		} else {
			lib.hideElem(new Array('valorEmprestimoAproximado'));
		}
	});	
},
initSeguroResidencial: function() {
	$j('[name=possuiBanco]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('qualBanco'));
		else 					lib.hideElem(new Array('qualBanco'));
	});
	$j('[name=possuiCartao]').click(function(){
		if($j(this).val()=="true") lib.showElem(new Array('qualCartao'));
		else 					lib.hideElem(new Array('qualCartao'));
	});
}

};

