var op = "";
function getCallBackDadoEscolar(retorno){
	if (retorno['erro'] != "" && retorno['erro'] != null){
		alert(retorno['erro']);
	}

	if (retorno['erro'] == null || retorno['tipo'] == 'turno'){
	
		// Adiciona o curso na tabela da página.
		var tabela = document.getElementById("dadosEscolares");

		var ind = (indiceAlteracao != -1) ? indiceAlteracao : tabela.rows.length;

		var curso = document.getElementById("cdCurso");
		var cursoArray = curso.options[curso.selectedIndex].value.split(";");
		var cdCurso = cursoArray[0]; 
			
		if (indiceAlteracao == -1) {
			tabela.insertRow(ind);
			tabela.rows[ind].insertCell(0);
			tabela.rows[ind].insertCell(1);
			tabela.rows[ind].insertCell(2);
			tabela.rows[ind].insertCell(3);
			tabela.rows[ind].insertCell(4);
			tabela.rows[ind].insertCell(5);
		} else {
			indiceAlteracao = -1;
		}
		tabela.rows[ind].cells[0].align = "center";
		tabela.rows[ind].cells[0].innerHTML = document.getElementById("cdEscola").options[document.getElementById("cdEscola").selectedIndex].text;
		tabela.rows[ind].cells[1].align = "center";
		tabela.rows[ind].cells[1].innerHTML = document.getElementById("cdCurso").options[document.getElementById("cdCurso").selectedIndex].text;
		tabela.rows[ind].cells[2].align = "center";

		if (document.getElementById("nrMatricula").value == ""){
			tabela.rows[ind].cells[2].innerHTML = "&nbsp;";
		} else {
			tabela.rows[ind].cells[2].innerHTML = document.getElementById("nrMatricula").value;
		}
		tabela.rows[ind].cells[3].align = "center";
		tabela.rows[ind].cells[3].innerHTML = $('nrAnosem').value;
		var dsTurno = "";
		if (retorno['turno'].length > 0){
			for (var i = 0; i < retorno['turno'].length; i++){
				switch (retorno['turno'][i]) {
					case '1' : dsTurno = "MANHA " + dsTurno;
					         break;
					case '2' : dsTurno = "TARDE " + dsTurno;
					         break;
					case '3' : dsTurno = "NOITE " + dsTurno;
					         break;
					case '4' : dsTurno = "MISTO " + dsTurno;
					         break;
				}
			}
		} else {
			dsTurno = "-";
		}
		tabela.rows[ind].cells[4].align = "center";
		tabela.rows[ind].cells[4].innerHTML = dsTurno;
		tabela.rows[ind].cells[5].align = "center";
		tabela.rows[ind].cells[5].innerHTML = "<a href='javascript:popupAnular("+cdCurso+", " + ind +")'>Anular</a><br>" +
											  "<a href='javascript:selecionaDadoEscolar("+cdCurso+", "+ind+")'>Atualizar</a>";
		var situacao = retorno["situacao"];
		if (document.getElementById("vagas") && (situacao == 1 || situacao == 2 || situacao == 3 ||	situacao == 8)){
			document.getElementById("vagas").style.display = '';
		}
		clearFieldsDadosEscolares();
		
	} else {
		clearFieldsDadosEscolares();
	}
}

function clearFieldsDadosEscolares(){
		var turnos = document.getElementsByName("cdQualificador");
		for (var i = 0; i < turnos.length; i++){
			turnos[i].disabled = false;
			turnos[i].checked  = false;
		}
		
		document.getElementById("tpNivel").value       = "";
		document.getElementById("tpNivel").disabled = false;
		
		document.getElementById("nrMatricula").value   = "";
		document.getElementById("nrAnosem").value	   = "";

		document.getElementById("cdEscola").value = "";
		document.getElementById("cdEscola").disabled = false;
		document.getElementById("cdEscola").options.length = 1;

		document.getElementById("cdCurso").value = "";
		document.getElementById("cdCurso").onchange();
		document.getElementById("cdCurso").disabled = false;
		document.getElementById("cdCurso").options.length = 1;
		
		document.getElementById("btnAdicionar").style.display = '';
		document.getElementById("btnAlterar").style.display = 'none';
		document.getElementById("btnCancelar").style.display = 'none';
}

function removeDadoEscolar(cdCurso, ind, acao){
	if (acao == 'anular'){
		var cdMotivo = document.getElementById("cdMotivoAnulado").value;
		if (cdMotivo == ''){
			alert('Por favor, informe o motivo da anulação do curso.');
			document.getElementById("cdMotivoAnulado").focus();
		}else{
			function anulaCursoRow(){
     			document.getElementById("dadosEscolares").rows[ind].cells[4].innerHTML = "-";
				document.getElementById("dadosEscolares").rows[ind].cells[5].innerHTML = 
					"<a href='javascript:retomaCurso("+cdCurso+", "+ind+")'>Retomar</a>";
				hideElementById('containerAnulacao');
				document.getElementById("cdMotivoAnulado").value = '';
			}
			dadoEscolarManager.removeDadoEscolar(cdMotivo, cdCurso, acao, anulaCursoRow);
		}
	} else if (acao == 'remover'){
		function removeCursoRow(){
			document.getElementById("dadosEscolares").deleteRow(ind);
		}
		dadoEscolarManager.removeDadoEscolar(null, cdCurso, acao, removeCursoRow);
	}
}

function selecionaDadoEscolar(cdCurso, ind){
	indiceAlteracao = ind;
	dadoEscolarManager.selecionaDadoEscolar(cdCurso, getCallbackSelecionaDadoEscolar);
}

function getCallbackSelecionaDadoEscolar(retorno){
	if (retorno != undefined && retorno != null){
		
		clearFieldsDadosEscolares();
		
		document.getElementById("tpNivel").value = retorno["tpNivel"];
		document.getElementById("tpNivel").disabled = true;
		
		document.getElementById("nrMatricula").value = retorno["nrMatricula"];

		document.getElementById("cdEscola").options.length = 1;
		document.getElementById("cdEscola").options[1] = 
			new Option(retorno['nmEscola'],	retorno['cdEscola']);		
		document.getElementById("cdEscola").selectedIndex = 1;
		document.getElementById("cdEscola").disabled = true;

		document.getElementById("cdCurso").options.length = 1;		
		document.getElementById("cdCurso").options[1] =	new Option(
				retorno['nmCurso'], 
				retorno['cdCurso'] + ";" + 
				retorno['nrDuracao'] + ";" +
				retorno['tpRegime'] + ";" + 
				retorno['tpNivel']
		);		
		document.getElementById("cdCurso").selectedIndex = 1;
		document.getElementById("cdCurso").disabled = true;
		
		var tipoRegime = '';
		var duracao = '';
		if (retorno['tpRegime'] == 'A'){
			tipoRegime = " Anos";
			duracao = "Ano";			
		} else {
			tipoRegime = " Semestres";
			duracao = "Semestre";			
		}
		
		if (retorno['tpNivel'] == 'M' && retorno['tpRegime'] == 'S') {
			document.getElementById("nrAnosem").value = retorno['nrSemestre'];
		} else {
			document.getElementById("nrAnosem").value = retorno['nrAnosem'];
		}
		document.getElementById("semestre").innerText = retorno['nrDuracao'] + tipoRegime;
		document.getElementById("dsDuracao").innerText = duracao;
						
		for (var i = 0; i < retorno['turnos'].length; i++){
			document.getElementsByName("cdQualificador")[retorno['turnos'][i]-1].checked = true;
			switch (retorno['turnos'][i]) {
				case 1 : setManha();
				         break;
				case 2 : setTarde();
				         break;
				case 3 : setNoite();
				         break;			
			}
		}
		
		document.getElementById("btnAdicionar").style.display = 'none';
		document.getElementById("btnAlterar").style.display = '';
		document.getElementById("btnCancelar").style.display = '';
		document.location.href = '#final';
	}
}

function retomaCurso(cdCurso, ind) {
	function retomaCursoRow(retorno){
		if (retorno['erro'] == null) {
			document.getElementById("dadosEscolares").rows[ind].cells[5].innerHTML = 
				"<a href='javascript:popupAnular("+cdCurso+", "+ind+")'>Anular</a><br>" + 
				"<a href='javascript:selecionaDadoEscolar("+cdCurso+", "+ind+")'>Atualizar</a>";
			
			if (document.getElementById("vagas") && retorno['liberaVagas']){
				document.getElementById("vagas").style.display = '';
			}
			alert("Seu cadastro foi atualizado com sucesso, para validá-lo é necessário comparecer pessoalmente ao CIEE mais próximo, munido do comprovante de matrícula atualizado, além de seus documentos (RG e CPF).");
			selecionaDadoEscolar(cdCurso, ind);
		} else {
			alert(retorno['erro']);
		}
	}
	dadoEscolarManager.retomaCurso(cdCurso, retomaCursoRow);
} 

function findTpNivel(){
	var curso = document.getElementById("cdCurso").value;
	dadoEscolarManager.buscaTpNivel(curso, cursoTpNivel);	
}

function cursoTpNivel(lista){
	if (lista[1] == 'A' || lista[0] == 'S'){
		document.getElementById("nrAnosem").disabled = false;
		document.getElementById("nrSemestre").disabled = true;
		document.getElementById("nrSemestre").value = '';
	}else {
		document.getElementById("nrAnosem").disabled = true;
		document.getElementById("nrAnosem").value = '';
		document.getElementById("nrSemestre").disabled = false;
	}
}

function addOptionToSelect(select, text, value) {
	var optionElm = document.createElement('option');
	var optionText = document.createTextNode(text);
	optionElm.value = value;
	
	return select.appendChild(optionElm.appendChild(optionText).parentNode);		
}

function getCallBackFindEscolasPorNivel(retorno){

	var cdCurso = document.getElementById("cdCurso");
	cdCurso.options.length = 0;
	addOptionToSelect(cdCurso, 'Selecione...', '');
	
	var cdEscola = document.getElementById("cdEscola");
	cdEscola.options.length = 0;
	addOptionToSelect(cdEscola, 'Selecione...', '');
	
	for (var i = 0; i < retorno.length; i++){
		addOptionToSelect(cdEscola, retorno[i]['nmEscola'], retorno[i]['cdEscola']);
	}
	
	hideWait();
}

function getCallBackFindCursosPorEscola(retorno){
	
	var cdCurso = document.getElementById("cdCurso");
	cdCurso.options.length = 0;
	addOptionToSelect(cdCurso, 'Selecione...', '');
	
	for (var i = 0; i < retorno.length; i++){
		addOptionToSelect(cdCurso, 
				retorno[i]['nmCurso'], 
				retorno[i]['cdCurso']   + ";" +
				retorno[i]['nrDuracao'] + ";" +
				retorno[i]['tpRegime']  + ";" +
				retorno[i]['tpNivel']);
	}
	
	hideWait();
}
