var editing = -1, idEditing;
var askListaEspera = false;
var askAcudiente = false;
var askColegio = false;


function fnCargada(){
	fr.lstServices.selectedIndex = 0;

	if(fr.logged.value == "N" && location.href.indexOf("ccid=") >= 0)alert("Ya no esta conectado con Campus en Línea, ingrese nuevamente");
}


function cleanTexts(){
	var descSrv = new Array();

	for(i=0; i<17; i++)descSrv[i] = "";

	setFields(descSrv);
	mostrarRequisitos("");
	fr.instrucciones.value = ""
}


function cleanBlocks(){
	//colegio
	fr.colegioTxt.value = fr.colegio.value = ""
	fr.escolaridad.selectedIndex = 0

	//acudiente
	fr.tmpTipoId3ro.selectedIndex = 0
	fr.tmpDocumento3ro.value = fr.tmpNombre3ro.value = fr.tmpApellidos3ro.value = ""
}


function showService(){
	var iv, ix = fr.lstServices.selectedIndex;
	var tmpConse;

	askListaEspera = askAcudiente = askColegio = false;

	fr.bWait.disabled = true;
	limpiarTarifasGrupos();
	iv = fr.lstServices.options[ix].value;

	cleanTexts();
	cleanBlocks();

	tblCole.style.display = "none";
	tblAcu.style.display = "none";

	if(iv == 'F' || iv == '%')return;

	var descSrv = iv.split("<c>");
	fr.consecutivo.value = descSrv[16];
	descSrv[16] = fr.lstServices.options[ix].text;

	if(descSrv[18] == "S")askListaEspera = true;

	setFields(descSrv);
}


function setFields(vect){
	var tmpCS, tmpCI = new Array();

	if(vect[6] != ""){
		tmpCI = fr.cursosInsc.value.split("<r>");
		tmpCS = vect[4]+"<c>"+vect[6]+"<c>"+vect[8]+"<c>"+vect[0]+"<c>"+vect[2]+"<c>"+vect[9]+"<c>"+vect[11];

		for(var i=0; i<tmpCI.length; i++){
			if(tmpCS == tmpCI[i]){
				alert("Usted ya se encuentra inscrito en el curso seleccionado");
				return;
			}
		}
	}

	fr.facultad.value = vect[0];
	fr.dependencia.value = vect[2];
	fr.forma_extension.value = vect[4];
	fr.tipo_servicio.value = vect[6];
	fr.servicio.value = vect[8];
	fr.ccosto.value = vect[9];
	fr.fechaInicio.value = vect[11];

	fr.txtFacultad.value = vect[1];
	fr.txtDependencia.value = vect[3];
	fr.txtForma_extension.value = vect[5];
	fr.txtTipo_servicio.value = vect[7];
	fr.txtCcosto.value = vect[10];
	fr.txtServicio.value = vect[16];

	if(vect[12].length > 0 && vect[12] != vect[11])vect[11] += " al " + vect[12]

	escribirTexto("tmpFacultad", vect[1])
	escribirTexto("tmpDependencia", vect[3])
	escribirTexto("tmpFechaInicio", vect[11])
	escribirTexto("tmpResponsableText", vect[13])
	escribirTexto("tmpTelefonoText", vect[14])
	escribirTexto("tmpEmailText", vect[15])
 
	if(vect[0] != "")loadLovs();
}


function loadLovs(){
	var ix, ixV = '', ixT = '', name = '', url =  '', txtName;

	showMessage("msg", 200);

	url = "lovTarifasGrupos.php?fac="+fr.facultad.value;
	url+= "&dep="+fr.dependencia.value;
	url+= "&fex="+fr.forma_extension.value;
	url+= "&tse="+fr.tipo_servicio.value;
	url+= "&ser="+fr.servicio.value;
	url+= "&cco="+fr.ccosto.value;
	url+= "&fec="+fr.fechaInicio.value;
	url+= "&idservicio="+fr.consecutivo.value;

	loader(url);
}


function limpiarTarifasGrupos(){ fr.lstTarifas.length = fr.lstGrupos.length = 0 }


function mostrarTarifasGrupos(tarifas, grupos){
	var tmp1, tmp2, cnt, txt;

	if(tarifas == "")alert("El servicio seleccionado no tiene tarifas");
	else	{
		tmp1 = tarifas.split("<r>");
		cnt = tmp1.length;
		fr.lstTarifas.length = cnt;
		
		for(i=0; i<cnt; i++){
			tmp2 = tmp1[i].split("<c>");
			txt = tmp2[1]+" [$"+tmp2[2]+"]";
			fr.lstTarifas.options[i] = new Option(txt, tmp2[0]);
		}
	}

	if(grupos == ""){
		if(askListaEspera){
			alert("El servicio seleccionado no tiene grupos, puede anotarse en lista de espera");
			fr.bWait.disabled = false;
		}
		else alert("El servicio seleccionado no tiene cupos disponibles");
	} else {
		tmp1 = grupos.split("<r>");
		cnt = tmp1.length;
		fr.lstGrupos.length = cnt;

		for(i=0; i<cnt; i++){
			tmp2 = tmp1[i].split("<c>");
			txt = tmp2[0]+". Horario ["+tmp2[3]+"] Aula ["+tmp2[2]+"] Cupo ["+tmp2[4]+"]";
			fr.lstGrupos.options[i] = new Option(txt, tmp2[0]);
		}
	}

	document.getElementById("msg").style.visibility = "hidden";
	if(tarifas != "" && grupos != ""){ 
		fr.bSave.disabled = false 
	}
}


function validateInscriptions(){
	if(validateSelect(fr.tmpTipoId, "Tipo documento") < 0)return true;
	if(validateText(fr.tmpDocumento, "Documento", 5, true) == false)return true;
	if(validateText(fr.tmpNombre, "Nombres", 2, false) == false)return true;
	if(validateText(fr.tmpApellidos, "Apellidos", 2, false) == false)return true;
	if(validateText(fr.tmpTelefono, "Teléfono", 7, false) == false)return true;
	if(validateSelect(fr.genero, "Género") < 0)return true;
	if(fr.tmpCelular.value != "")if(validateText(fr.tmpCelular, "Celular", 10, true) == false)return true;
	if(fr.tmpEmail.value != "")if(validateEmail(fr.tmpEmail) == false)return true;

	return false;
}


function validateAcudiente(){
	if(validateSelect(fr.tmpTipoId3ro, "Tipo documento del responsable") < 0)return true;
	if(validateText(fr.tmpDocumento3ro, "Documento del responsable", 5, true) == false)return true;
	if(validateText(fr.tmpNombre3ro, "Nombre del responsable", 10, false) == false)return true;

	return false;
}


function validateColegio(){
	if(validateText(fr.colegio, "Colegio", 5, false) == false)return true;
	if(validateSelect(fr.escolaridad, "Nivel de escolaridad") < 0)return true;

	return false;
}


function mostrarRequisitos(reqs){ if(reqs != "")fr.requisitos.value = " - "+reqs.replace(/<r>/g, "\n - ") }


function mostrarInstrucciones(inst){ if(inst != "")fr.instrucciones.value = inst.replace(/<r>/g, "\n") }


function mostrarInfoAdicional(info){
	if(info.indexOf("INFO_COLEGIO") >= 0){
		tblCole.style.display = ""
		alert("No olvide ingresar la información del colegio")
		askColegio = true
	}
	if(info.indexOf("INFO_ACUDIENTE") >= 0){
		tblAcu.style.display = ""
		alert("No olvide ingresar la información del responsable o acudiente")
		askAcudiente = true
	}
}


function activarInfoPers(){
	fr.tmpTipoId.disabled = false;
	fr.tmpDocumento.readOnly = fr.tmpNombre.readOnly = false; 
	fr.tmpApellidos.readOnly = fr.lstTarifas.disabled = false;
	fr.lstGrupos.disabled = fr.lstRh.disabled = false; 

	if(fr.logged.value == "S"){
		fr.lstTarifas.disabled = fr.lstGrupos.disabled = fr.tmpTipoId.disabled = true;
		fr.tmpDocumento.readOnly = true;
	}
}


function calcelInscription(){
	var tmp, ln, ix, i, tmp2;

	tmp = fr.txtInscritos.value.split("<r>");
	ln = tmp.length;

	//borramos los inscritos ingresados
	for(i=0; i<ln; i++)tmp2 = tmp[i].split("<c>");
 
	fr.reset();
	fr.lstServices.selectedIndex = fr.lstGrupos.length = fr.lstTarifas.length = 0;
	fr.lstServices.disabled = false;
	fr.bSave.disabled = true;
	cleanTexts();
}


function saveInscription(){
	var ix, tmp = new Array(13);

	if(fr.logged.value == "S" && fr.nivel.value == "N"){
		if(fr.txtServicio.value.toLowerCase().indexOf("examen competencia lectora") >= 0){
			alert("Usted no puede inscribirse a esta actividad\n ya que no cumple con el requisito de nivel académico exigido");
			return;
		}
	}

	if(validateText(fr.facultad, "servicio", 1, false) == false)return;
	if(validateInscriptions())return; 

	//acudiente
	if(askAcudiente)if(validateAcudiente())return; 
	if(askColegio)if(validateColegio())return; 

	ix = fr.tmpTipoId.selectedIndex;
	tmp[0] = fr.tmpTipoId.options[ix].value;
	tmp[1] = fr.tmpDocumento.value;
	tmp[2] = fr.tmpNombre.value;

	ix = fr.lstTarifas.selectedIndex;
	tmp[3] = fr.lstTarifas.options[ix].value;

	ix = fr.lstGrupos.selectedIndex;
	tmp[4] = fr.lstGrupos.options[ix].value;

	ix = fr.lstRh.selectedIndex;
	if(ix > 0)tmp[5] = fr.lstRh.options[ix].value;
	else tmp[5] = "";

	ix = fr.lstEps.selectedIndex;
	if(ix > 0)tmp[6] = fr.lstEps.options[ix].text;
	else tmp[6] = "";
	if(tmp[6] == "%")tmp[6] = "";

	tmp[7] = fr.tmpTelefono.value;
	tmp[8] = fr.tmpCelular.value;
	tmp[9] = fr.tmpEmail.value;

	ix = fr.tmpTipoId3ro.selectedIndex;
	tmp[10] = fr.tmpTipoId3ro.options[ix].value;
	tmp[11] = fr.tmpDocumento3ro.value;
	tmp[12] = fr.tmpNombre3ro.value;

	fr.txtInscritos.value = tmp.join("<c>");

	consultarOtrosCursosInsc();
}


function saveListaEspera(){
	if(validateInscriptions())return;

	showMessage("msg", 200);

	url = "saveListaEspera.php?serv="+fr.consecutivo.value;
	url+= "&ced="+fr.tmpDocumento.value;
	url+= "&nom="+fr.tmpNombre.value+" "+fr.tmpApellidos.value;
	url+= "&tele="+fr.tmpTelefono.value;
	url+= "&email="+fr.tmpEmail.value;
	url+= "&celu="+fr.tmpCelular.value;

	var ix = fr.tmpTipoId.selectedIndex;
	url+= "&tipoId="+fr.tmpTipoId.options[ix].value;
	
	loader(url);
}


function rdoSaveListaEspera(rdo){
	var msg1 = "";
	document.getElementById("msg").style.visibility = "hidden";

	if(rdo == "1")msg = "Ha sido inscrito en la lista de espera para esta activadad o servicio";
	else msg = "No pudo ser inscrito en la lista de espera";

	alert(msg);
}


function consultarOtrosCursosInsc(){
	var ix, ixV = '', ixT = '', name = '', url =  '', txtName;

	showMessage("msg", 200);

	url = "otrosCursosInsc.php?fac="+fr.facultad.value;
	url+= "&dep="+fr.dependencia.value;
	url+= "&ced="+fr.tmpDocumento.value;

	loader(url);
}


function rdoConsultaOtrasInsc(rdo){
	var msg1 = "";
	document.getElementById("msg").style.visibility = "hidden";

	if(rdo != ""){
		var tmp1 = rdo.split("!"), tmp2;

		for(var i=0; i<tmp1.length-1; i++){
			tmp2 = tmp1[i].split("|");
			msg1 += "\n - "+tmp2[0]+", desde "+tmp2[1]+" hasta "+tmp2[2];
		}

		msg1 = "La persona con el documento "+fr.tmpDocumento.value+" está inscrito para los siguientes servicios y/o actividades:\n"+msg1+"\n\n";
		alert(msg1);
	}

	validarTarifa();
}


function validarTarifa(){
	var tmp = fr.txtInscritos.value.split("<c>");
	var tmpId = tmp[1];
	
	if(askAcudiente)tmpId = fr.tmpDocumento3ro.value; 

	url = "validarTarifa.php?serv="+fr.consecutivo.value;
	url+= "&tar="+tmp[3];
	url+= "&ced="+tmpId;

	loader(url);
}


function rdoValidarTarifa(txt){
	var tmp = txt.split("|");

	if(tmp[0] == "S"){
		if(confirm("Confirma que desea realizar la inscripción?")){
			fr.lstTarifas.disabled = fr.lstGrupos.disabled = fr.tmpTipoId.disabled = false;
			fr.submit();
		}
	}
	else alert(tmp[1]);
}


function buscarColegio(){
	openTB("http://www.udea.edu.co/php_listados/listar.php?tipo=c&exec=parent.window.showColegio", 230, 380)
}


function showColegio(rdo){
	var tmp1 = rdo.split("<r>")

	for(var i=0; i<tmp1.length; i++){
		var tmp2 = tmp1[i].split("<c>")

		if(tmp2[0] == "cole"){
			fr.colegioTxt.value = tmp2[2]
			fr.colegio.value = tmp2[1]
		}

	}
	closeTB()
}

activarInfoPers();
