$(document).ready(function(){
	$("#tr_bolsa").hide();
	$("#secao").change(function(){
	if($(this).val()=="72525")
	$("#tr_bolsa").fadeIn();
	else
	$("#tr_bolsa").hide();
	});
	
	//inicializa variavel de mensagem
	var msg = "";

	$("#buscaInst").hover(function(){
		$(this).css("text-decoration","underline")
		$(this).css("color","blue");
	},function(){
		$(this).css("background","#FFFFFF")
		$(this).css("color","");
	});
	
	/*
	 * mensagens durante 'ajax'
	 */
	 
	//esconde mensagem quanto termina
	$("#loadingPai").ajaxStop(function(){
		$(this).hide();
	});
	//mostra erro
	$("#divMsg").ajaxError(function(request, settings){
		$("#divMsgPai").show();
		$("#divMsg").empty();
	  	$(this).append("Error requesting page " + settings.url + "");
	});
	//mensagem durante a busca
	$("#loadingPai").ajaxStart(function(){
		$(this).show();
		$("#loading").empty();
		$("#loading").append(msg+"<br />&nbsp;<br /><img src='./imagens/load.gif' border='0'>");
	});
	/*
	 * Fim
	 */
	
	//quando muda estado, busca cidades
	$("#uf").change(function(){
		msg = "Aguarde, buscando cidades ";
		$.post("ajax/ajaxBuscaCidades.php", {uf: $(this).val()}, function(result) {
			$("#cidade").html(result);
		});
	});
	//quando muda estado, busca cidades
	$("#uf2").change(function(){
		msg = "Aguarde, buscando cidades ";
		$.post("ajax/ajaxBuscaCidades.php", {uf: $(this).val()}, function(result) {
			$("#cidade2").html(result);
		});
	});
	//quando muda área, busca cursos
	$("#cursoArea").change(function(){

		msg = "Aguarde, buscando cursos ";
		$.post("ajax/ajaxBuscaCursos.php", {cursoArea: $(this).val()}, function(result) {
			$("#cursoCodigo").html(result);
		});
	});
	
	//busca instituições de ensino
	$("#buscaInst").click(function(){
		$("#ies").show();
		if(!$("#local_brasil").attr("checked") && !$("#local_exterior").attr("checked")){
			$("#local_brasil").attr("checked","true"); 
			$("#codIES").show();
		} else if($("#local_brasil").attr("checked")){
			$("#codIES").show();		
		} else if($("#local_exterior").attr("checked")){
			$("#codIESExterior").show();
		}
	});

	$("#fecharIes, #codIES, #codIESExterior").click(function(){
		escolhaIES();
	});
	$("#local_brasil").click(function(){
		$("#codIESExterior").hide();
		$("#codIES").show();
	});
	$("#local_exterior").click(function(){
		$("#codIES").hide();
		$("#codIESExterior").show();
	});

	$("#fecDivErro").click(function(){
		$("#divErro").hide();
	});

	var esconde = 1;
	if($("#local_brasil").attr("checked") && $("#codIES").val() == 0){
		esconde = 0;
	} else if($("#local_exterior").attr("checked") && $("#codIESExterior").val() == 0){
		esconde = 0;	
	}
	if(esconde == 1){
		$("#trOutraIes").hide();
		escolhaIES();
	}

});

function escolhaIES(){
	$("#ies").hide();
	/*
	 * verifica qual a faculdade selecionada
	 * se selecionou outra ies, mostra campo para digitacao
	 */
	var valorIes = 0;
	var tipo = "brasil";
	if($("#local_brasil").attr("checked")){
		valorIes = $("#codIES").val();
		tipo = "brasil";
		
	} else if($("#local_exterior").attr("checked")){
		valorIes = $("#codIESExterior").val();
		tipo = "exterior";
	}

	if(valorIes > 0){
		$.post("ajax/ajaxBuscaIes.php", {tipo: tipo, iesCodigo: valorIes}, function(result) {
			$("#noneIes").html(result + " <br />");
		});
	}
	if(valorIes == 0 && valorIes != ""){
		$("#trOutraIes").show();
		$("#noneIes").empty();
	} else if(valorIes == ""){
		$("#noneIes").empty();
	}
	else $("#trOutraIes").hide();
}
