// JavaScript Document

/*********************************************************************/
/*************************** NEW FUNCTIONS ***************************/
/*********************************************************************/

function validateFields()
{
	var allOK=1;
	var KOFields="";

	var regexp1 = /^[69][0-9]{2}\s?[0-9]{6}$/;
	var regexp2 = /^9[0-9]\s?[0-9]{3}\s?[0-9]{4}$/;
	var regexp3 = /^[bB](11|7|9)\-[0-9]{3,4}(\.[0-9])?$/;
	var regexp4 = /^(io|iO|Io|IO)\-[0-9]{3}$/;
	var regexp5 = /^[kK]\-[0-9]{4}$/;
	var regexp6 = /^[gG](5|6)\-[0-9]{3}$/;
	var regexp7 = /^[a-zA-ZáàéèíìóòúùñïüÁÀÉÈÍÌÓÒÚÙÑÏÜ]+(\s[a-zA-ZáàéèíìóòúùñïüÁÀÉÈÍÌÓÒÚÙÑÏÜ]+)?$/;
	var regexp8= /^[a-zA-Z0-9]+((_|\.|\-)[a-zA-Z0-9]+)?@[a-zA-Z0-9]+((_|\.|\-)[a-zA-Z0-9]+)?\.[a-zA-Z]{2,4}$/;

	if(!document.forms[0].form101.value.match(regexp7))
	{
		KOFields+="NOMBRE\n\r";
		allOK=0;
	}
	if(!document.forms[0].form102.value.match(regexp7))
	{
		KOFields+="APELLIDO1\n\r";
		allOK=0;
	}

	if(!document.forms[0].form103.value.match(regexp7))
	{
		KOFields+="APELLIDO2\n\r";
		allOK=0;
	}

	if(!document.forms[0].form108.value.match(regexp1) && !document.forms[0].form108.value.match(regexp2))	
	{
		KOFields+="TELEFONO\n\r";
		allOK=0;
	}

	if(!document.forms[0].form107.value.match(regexp3) && !document.forms[0].form107.value.match(regexp4) && !document.forms[0].form107.value.match(regexp5) && !document.forms[0].form107.value.match(regexp6))
	{
		KOFields+="DESPACHO\n\r";
		allOK=0;
	}

	if(!document.forms[0].form109.value.match(regexp8))
	{
		KOFields+="CORREO\n\r";
		allOK=0;
	}

	if(allOK==0) alert("Los campos\n\r"+KOFields+"son incorrectos");
	return allOK;
}

function checkTimetable()
{
	var allok=1;

	for(var i=0;i<6;i++)
	{
		if(document.getElementById("form2"+i+"0").checked==1)
		{
			timeIN=makeHHMM(document.getElementById("form2"+i+"2").selectedIndex,document.getElementById("form2"+i+"3").selectedIndex);
			timeOUT=makeHHMM(document.getElementById("form2"+i+"4").selectedIndex,document.getElementById("form2"+i+"5").selectedIndex);
			if(timeIN>=timeOUT)
				allok=allok*0;
		}
	}	
	return allok;
}

function checkPeriod()
{
	dateIN=makePeriod(document.getElementById("form302").options[document.getElementById("form302").selectedIndex].value,
				document.getElementById("form303").options[document.getElementById("form303").selectedIndex].value,
				document.getElementById("form304").options[document.getElementById("form304").selectedIndex].value);
	dateOUT=makePeriod(document.getElementById("form305").options[document.getElementById("form305").selectedIndex].value,
				document.getElementById("form306").options[document.getElementById("form306").selectedIndex].value,
				document.getElementById("form307").options[document.getElementById("form307").selectedIndex].value);
	if(dateIN>=dateOUT && document.getElementById("form104").selectedIndex==1) return 0;
	return 1;
}

function makeHHMM(hh,mm)
{
	return (hh*100 + mm)
}

function makePeriod(dd,mm,yy)
{
	return (yy*10000 + mm*100 + dd);
}

function submitForm()
{
	if(!validateFields()) exit(1);
//	if(!checkTimetable())
//		alert("Algunas horas de consulta / concertadas son incorrectas.");
//	else if(!checkPeriod())
//		alert("La fecha de inicio de la beca ha de ser menor que la de fin");
	else
	{
		alert("Introducir datos...");
		document.forms[0].submit();
	}
}

function submitModForm(data)
{
	document.form1.form101.value==data[1] ? alert("MODIFIED") : alert("NOT MODIFIED") ;	
}

function cleanForm()
{
	var inputs=document.getElementsByTagName("input");
	for(i=0;i<inputs.length;i++)
	{ 
		if(inputs[i].type=="checkbox") inputs[i].checked=0;
		else if(inputs[i].type=="text") inputs[i].value="";
	}

	var selOpts=document.getElementsByTagName("select");
	for(var i=0;i<selOpts.length;i++)
	{	
		selOpts[i].selectedIndex=0;
		if((selOpts[i].name.substr(4,1)-'0')==2)
			enableTimetable(selOpts[i]);
	}
	document.getElementById('divB').style.visibility="hidden";
	document.getElementById('divP').style.visibility="hidden";
}

function initValues(type,data)
{
	if(type==0) cleanForm();
	else 
	{
		fillFormValues(data);
	}
}


function fillFormValues(data)
{
	document.form1.form101.value=data[1];
	document.form1.form102.value=data[2];
	document.form1.form103.value=data[3];

	data[4]<2000?putIndex("form104",2):putIndex("form104",data[4]);
	detectUser(document.form1.form104);

	putIndex("form105",data[5]);
	putIndex("form106",data[6]);
	document.form1.form107.value=data[7];
	document.form1.form108.value=data[8];
	document.form1.form109.value=data[9];
	document.form1.form110.value=data[11];
	putIndex("form111",data[12]);
	if(document.getElementById('divB').style.visibility=="visible")
	{
		putIndex("form301",data[14]);
		decodeDate("form304","form303","form302",data[15]);
		decodeDate("form307","form306","form305",data[16]);
		putIndex("form308",data[17]);
	}
	else(document.getElementById('divP').style.visibility=="visible")
	{
		putIndex("form401",data[14]);
	}
}

function decodeDate(yy,mm,dd,dateDB)
{
	putIndex(yy,dateDB.substring(0,4));
	dateDB.substring(5,6) ? putIndex(mm,dateDB.substring(6,7)) : putIndex(mm,dateDB.substring(5,7));
	putIndex(dd,dateDB.substring(8,10));
}

function enableTimetable(selector)
{
	var head = selector.name.substr(0,6);

	if(selector.checked==1)
		for(var i=1;i<6;i++)
			document.getElementById(head+i).disabled=false;
	else
		for(var i=1;i<6;i++)
			document.getElementById(head+i).disabled=true;
}

function detectUser(element)
{
	switch(element.options[element.selectedIndex].value)
	{
		case "2000":		
			document.getElementById('divB').style.visibility="visible";
			document.getElementById('divP').style.visibility="hidden";
			break;
		case "2":
			document.getElementById('divB').style.visibility="hidden";
			document.getElementById('divP').style.visibility="visible";
			break;
		default:
			document.getElementById('divB').style.visibility="hidden";
			document.getElementById('divP').style.visibility="hidden"
			break;
	}
}

function putIndex(element,selection)
{
	//alert(selection);
	for(var i=0;i<document.getElementById(element).options.length;i++)
	{
		if(document.getElementById(element).options[i].value==selection) 
		{
			document.getElementById(element).options[i].selected=true;
			break;
		}
	}	
}

/*********************************************************************/
/*************************** OLD FUNCTIONS ***************************/
/*********************************************************************/


function showDate(idioma)
{
	var data = new Date();
	var mes_actual = new Array(" de Gener"," de Febrer"," de Mar&#231;"," d'Abril"," de Maig"," de Juny"," de Juliol"," d'Agost",
			" de Setembre"," d'Octubre"," de Novembre"," de Desembre"," de Enero"," de Febrero"," de Marzo"," de Abril",
			" de Mayo"," de Junio"," de Julio"," de Agosto"," de Septiembre"," de Octubre"," de Noviembre"," de Diciembre");
	var dia = data.getDate();
	var mes = data.getMonth();
	var any = data.getFullYear();
	var data_actual;

	if(idioma==0) data_actual=dia+mes_actual[mes]+" "+any;
	if(idioma==1) data_actual=dia+mes_actual[mes+12]+" "+any;
	document.write(data_actual);
}

function fullDate(toGet)
{
	var data = new Date();
	var toReturn = data.getFullYear()+"-"+data.getMonth()+"-"+data.getDate()+" ";
	(toGet==0) ? toReturn+=data.getHours()+":"+ data.getMinutes()+":"+data.getSeconds() : toReturn=data.getHours()+":"+ data.getMinutes()+":"+data.getSeconds();
	return toReturn;
}

function selectInitPage()
{	
	var page;

	page=document.location.href.substring(document.location.href.indexOf("#")+1,document.location.href.length);
	if(page=="escrituracreativa") 
		newWindow("./cursos/"+page+".htm");
	else if(page=="lenguaextranjera") 
		newWindow("./cursos/"+page+".htm");

}	

function initMSIEMenuFunctions()
{
	if(document.all&&document.getElementById)
	{
		var nodeLI=document.getElementsByTagName("LI");
		for(var i=0; i<nodeLI.length; i++)
		{
			nodeLI[i].onmouseover=function()
			{ 
				this.className+=" over";
			}
			nodeLI[i].onmouseout=function()
			{ 
				this.className=this.className.replace(" over","");
			}
		}
	}
}

function showMenu()
{
	document.write("<ul class='level1'>");
	document.write("<li><a href='./index.html' style='width:133px;'>Inicio</a></li>");
	document.write("<li><a href='#'>Departamento</a>");
	document.write("<ul class='level2'>");
	document.write("<li><a href='./secretaria.php' style='width:133px;'>Secretar&#237;a</a></li>");
	document.write("<li><a href='./organitzacio.php' style='width:133px;'>Organizaci&#243;n</a></li>");
	document.write("<li><a href='./professorat.php' style='width:133px;'>Profesorado</a></li>");
	document.write("<li><a href='./becaris.php' style='width:133px;'>Becarios</a></li>");
	document.write("<li><a href='javascript:newWindow(\"http://sia.uab.es/NASApp/cde/control/iniciop?entradaPublica=true\");' style='width:133px;'>Buscar</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href='#'>Docencia</a>");
	document.write("<ul class='level2'>");
	document.write("<li><a href='./dfacult.php' style='width:133px;'>Unidades</a></li>");
	document.write("<li><a href='#'>Titulaciones</a>");
	document.write("<ul class='level3'>");
	document.write("<li><a href='./dtitulp.php' style='width:133px;'>Propias</a></li>");
	document.write("<li><a href='./dtitule.php' style='width:133px;'>Externas</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href='#'>Postgrado</a>");
	document.write("<ul class='level3'>");
	document.write("<li><a href='./postgrautc.php' style='width:153px;'>Tercer Ciclo</a></li>");
	document.write("<li><a href='./postgraufc.php' style='width:153px;'>Formaci&#243;n Continuada</a></li>");
	document.write("<li><a href='./postgraumo.php' style='width:153px;'>M&#225;steres Oficiales</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href='#'>Estudiantes</a>");
	document.write("<ul class='level2'>");
	document.write("<li><a href='#'>Erasmus/S&#243;crates</a>");
	document.write("<ul class='level3'>");
	document.write("<li><a href='./intercanviau.php' style='width:153px;'>Estudiantes UAB</a></li>");
	document.write("<li><a href='./intercanviae.php' style='width:153px;'>Estudiantes Extranjeros</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href='#' style='width:133px;'>Ayudas y Becas</a>");
	document.write("<ul class='level3'>");
	document.write("<li><a href='./beques.php' style='width:153px;'>Informaci&#243;n general</a></li>");
	document.write("<li><a href='./novetatsBeques.php' style='width:153px;'>Novedades</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href=\"http://treball-campus.uab.es/\" onClick=\"window.open(this.href, this.target, ''); return false;\" style='width:133px;'>Bolsa de Trabajo</a></li>");

	
	document.write("<li><a href='./curriculum.php' onClick=\"window.open(this.href, this.target, 'width=440px,height=300px'); return false;\" style='width:133px;'>Curr&#237;culum</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href=''>Investigaci&#243;n</a>");
	document.write("<ul class='level2'>");
	document.write("<li><a href='#' style='width:133px;'>L&#237;neas de investigaci&#243;n</a>");
	document.write("<ul class='level3'>");
	document.write("<li><a href='./alenesp.php' style='width:183px;'>Lengua Espa&#241;ola</a></li>");
	document.write("<li><a href='./alitesp.php' style='width:183px;'>Literatura Espa&#241;ola</a></li>");
	document.write("<li><a href='./ateolit.php' style='width:183px;'>Teor&#237;a de la Literatura y Literatura Comparada</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href='./recercapn.php' style='width:133px;'>Plan Nacional I+D+I</a></li>");
	document.write("<li><a href='./recercage.php' style='width:133px;'>Grupos Estables I+D</a></li>");
	document.write("</ul>");
	document.write("</li>");
	document.write("<li><a href='./nodisponiblee.php' style='width:133px;'>Enlaces</a></li>");
	document.write("</ul>");
}

function newWindow(direction)
{
	if(navigator.appName=="Netscape")
		window.open(direction,"Link","width=800,height=600,scrollbars=yes,resizable=no");
	if(navigator.appName=="Microsoft Internet Explorer")
		window.open(direction,"Link","width=800,height=600,scrollbars=yes,resizable=no");
}

function newWindowForm(direction)
{
	if(navigator.appName=="Netscape")
		window.open(direction,"Form","width=480,height=260,location=NO,menubar=NO,status=NO,titlebar=NO,toolbar=NO,scrollbars=NO,resizable=NO,");
	if(navigator.appName=="Microsoft Internet Explorer")
		window.open(direction,"Form","width=480,height=280,location=NO,menubar=NO,status=NO,titlebar=NO,toolbar=NO,scrollbars=NO,resizable=NO,");

}

function printDoc(pageTitle, idBox)
{
	alert("Trying to print");
	pageTitle=pageTitle+" - Departamento de Filolog&#237;a Espa&#241ola UAB";
	var toPrint="<html><head><title>"+pageTitle+"</title><link rel='stylesheet' type='text/css' href='./stylesN.css'>";
	toPrint+="<!--[if IE]><link rel='stylesheet' type='text/css' href='./stylesIE.css'><![endif]--></head><body style='background-color:#FFFFFF'>";
	toPrint+="<p align='center' class='EstiloTitle1'>"+pageTitle+"</p><p>&nbsp;</p>"+document.getElementById(idBox).innerHTML+"</body></html>";

	var printWin = window.open("","printDoc");

	printWin.document.open();
	printWin.document.write(toPrint);
	printWin.document.close();
	printWin.print();
}

function lastModifiedF()
{
	var vector=document.lastModified.split(' ');
	var date=vector[0].split('/');
	vector[0]=date[1]+"/"+date[0]+"/"+date[2];

	return vector[0]+' '+vector[1];
}


function mySubmit(thisForm)
{

	alert(fullDate());
	return;

	tinyMCE.triggerSave(true,true);
	thisForm.submit();

	return;

	var arg1=document.getElementById('cabecera').value;
	tinyMCE.triggerSave(true,true);
        var arg2=tinyMCE.get('cuerpo').getContent();
        var arg3=document.getElementById('imagen').value;
        
	var args="action=1&arg1="+encodeURI(arg1)+"&arg2="+encodeURI(arg2)+"&arg3="+encodeURI(arg3);

	alert(args);

	XMLHttp.open("POST","./gestNews.php",true);
	XMLHttp.onreadystatechange=sendOnReady;
	XMLHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XMLHttp.setRequestHeader('Content-length',args.length);
	XMLHttp.setRequestHeader('Connection', "close");
	XMLHttp.send(args);
	return;
}


function sendOnReady()
{
	var optSel="";
	if(XMLHttp.readyState==4)
	{
		if( XMLHttp.status==200)
		{
			var responseLength=XMLHttp.responseXML.documentElement.getElementsByTagName('resultados').length;
			for(var i=0;i<responseLength; i++)
				optSel+= XMLHttp.responseXML.documentElement.getElementsByTagName('resultados')[i].childNodes[0].nodeValue;
			if(optSel=="")
			{
				alert("CACAFUTI");
			}
			else alert(optSel);
		}
	}
}



function newAjaxElement(){
	var XMLHttp=false;
     
     	try{ XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");} catch(e){
         	try{ XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");} catch(E){};
        }
	if(!XMLHttp && typeof XMLHttpRequest!='undefined'){
		XMLHttp = new XMLHttpRequest();
	}
	return XMLHttp;
} 

