// JavaScript Document

function setCombo(id, target, fk){
	url = "./contato?id="+id+"&target="+target+"&fk="+fk;
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET", url, true);
	document.getElementById(target).innerHTML = '<br/>Carregando...';
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4){
			document.getElementById(target).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function setComboCadastro(id, target, fk){
	url = "./inscricao?id="+id+"&target="+target+"&fk="+fk;
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET", url, true);
	document.getElementById(target).innerHTML = '<br/>Carregando...';
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4){
			document.getElementById(target).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}



$(document).ready(function() {
	
	unMask();
	setMask();
	
	// Opções de almoço :: retirado (deixei caso seja necessário adicionar novamente).
   	/*var val1 = document.getElementById("opcao1");
	var val2 = document.getElementById("opcao2");
	var val3 = document.getElementById("opcao3");
   	var $nomeTema = "";
   
  	$j("#sessao").change(function(){	
		if ($j(this).attr('value')=='Sim'){
			
			$j("#opcao1").attr("disabled","");	
			
		} else {
			
			val1.selectedIndex = 0;
			document.getElementById("opcao2").length = 0;
			document.getElementById("opcao3").length = 0;
			$j("#opcao1").attr("disabled","disabled");	
			$j("#opcao2").attr("disabled","disabled");	
			$j("#opcao3").attr("disabled","disabled");	
		}
	});   
   	
   	$j("#opcao1").change(function(){	
		$j("#opcao2").attr("disabled","");
		
		//Zera a lista 3
		val2.length = 0;
		val3.length = 0;
		
		val2.options[0] = new Option('Selecione','');
		val3.options[0] = new Option('Selecione','');	
		var c = 1;
		
		//for (i = 1 ;i<=val1.length - 1 ; i++){
		for (i = 1 ;i <= val1.length; i++){
			$nomeTema = 'Tema '+ i;
			
			//Preenche a combo 2
			//if ($j("#opcao1").attr("value") != 'Tema '+ i)  {
			if (($j("#opcao1").attr("value") != 'Tema '+ i) && ($nomeTema != 'Tema 3') ) {
				val2.options[c] = new Option($nomeTema, $nomeTema);
				c++;
			}
			
			//Zera a lista 3
			val3.length = 0;
		}
	});
	
	$j("#opcao2").change(function(){	
		$j("#opcao3").attr("disabled","");
		document.getElementById("opcao3").length = 0;
		document.getElementById("opcao3").options[0] = new Option('Selecione','');	
		var c = 1;
		var val1 = document.getElementById("opcao1"). value;
		var val2 = document.getElementById("opcao2"). value;
		
		//for (i=1;i<=document.getElementById("opcao2").length ;i++){
		for (i=1;i<=document.getElementById("opcao2").length + 1  ;i++){
			$nomeTema = 'Tema '+ i;
			
			//Preenche a lista 3
			//if ( ( val1 != 'Tema '+ i ) && ( val2 != 'Tema '+ i ) ){
			if ( ( val1 != 'Tema '+ i ) && ( val2 != 'Tema '+ i ) && ($nomeTema != 'Tema 3') ){ 
				document.getElementById("opcao3").options[c] = new Option('Tema '+ i, 'Tema '+ i);
				c++;
			}
		}
	});*/
	
	// Abre iframe com PDF
	$("a.fancyFrame").fancybox({
		'frameWidth' : 780, 
		'frameHeight': 505, 
		'overlayShow': true, 
		'hideOnContentClick': false
	}); 
	
	// Lista com tipos de inscrição : habilita e restringe seleção de campos.
	$('label.sessao select#tipo').attr("disabled", true); 
	$('label.tipo input#descricao').attr("disabled", true); 
	
	$("label.sessao select#classificacao").change(function(){	
		if ($(this).val() == 2) { // Não médico
			$('label.sessao select#tipo').removeAttr("disabled");
			$('label.sessao select#tipo').addClass("required");
		} else {
			$('label.sessao select#tipo').attr("disabled", true); 
			$('label.tipo input#descricao').attr("disabled", true);
			$('label.sessao select#tipo').val(""); 
			$('label.tipo input#descricao').val(""); 
			
			// Validação de formulário
			$('label.sessao select#tipo').removeClass("error");
			$('label.sessao select#tipo').removeClass("required");
		}
	});
	
	$("label.sessao select#tipo").change(function(){
		if ($(this).val() == 'Outros') { // Outros
			$('label.tipo input#descricao').removeAttr("disabled");
			// Validação do formulário
			$('label.tipo input#descricao').addClass("required");
		} else {
			$('label.tipo input#descricao').attr("disabled", true);
			$('label.tipo input#descricao').val("");
			
			// Validação de formulário
			$('label.tipo input#descricao').removeClass("required");
			$('label.tipo input#descricao').removeClass("required");
		}
	});
	
	// Clone
	$("input.clone").click(function(){	
		if ($("fieldset.secretariasBox").length < 6){
			var target = $(this).attr("value");
			unMask();
			$("."+target+":first").clone(true).insertAfter("."+target+":last");
			$("."+target+" input.rmclone").removeClass("invisible");
			setMask();
		}
	});
	
	$(".rmclone").click(function(){
		var target = $(this).attr("value");
		var nElements = $("."+target).length;
		if (nElements==2){
			$("."+target+" input.rmclone").addClass("invisible");
		}
		if (nElements>1){						  
			$(this).parent().parent().remove();
		}
	}); 
	
	// Validate forms
	$("#form-agendamento").validate();
	$("#form-cadastro").validate();
	$("#contactus").validate();
});

// Funções responsáveis por atribuir e remover a máscara dos campos.
function setMask(){
	$(".txt-date").mask("99/99/9999");
	$(".txt-phone").mask("(99) 9999-9999");
	$(".txt-cnpj").mask("99.999.999/9999-99");
	$(".txt-cpf").mask("999.999.999-99");
	$(".txt-cep").mask("99999-999");
}

function unMask(){
	$(".txt-date").unmask();	
	$(".txt-phone").unmask();
	$(".txt-cnpj").unmask();
	$(".txt-cpf").unmask();
	$(".txt-cep").unmask();
}