// JavaScript Document
var ventana;

function muestraFecha(returnField)
{
	 w = window.open ("popups/month.php?f="+returnField+"&actual="+document.getElementById(returnField).value, "Fechas","width=300,height=330,top=150,left=200"); 
	 w.window.resizeTo(300,330);
	 w.focus();
}

function selectedData()
{
	fecha = document.getElementById('popup_date').value;
	hora = document.getElementById('popup_hour').value;
	minutos = document.getElementById('popup_min').value;
	return fecha+" "+hora+":"+minutos+":00";
}

function selectedDay(y, m, d)
{
	document.getElementById("popup_date").value=y+"-"+m+"-"+d;
}

function validaHora(field)
{
	value=field.value;
	if(isNaN(value) || value<0 || value>23)
	{
		field.value=12;
		alert("La hora tiene que ser un numero entre 0 y 23");
		field.focus();
	}
}

function validaMinutos(field)
{
	value=field.value;
	if(isNaN(value) || value<0 || value>59)
	{
		field.value="00";
		alert("Los minutos tienen que ser un numero entre 0 y 59");
		field.focus();
	}
}

function enviaFormulario(form_id, validation)
{
	continuar = true;
	if(arguments.length==2)
	{
		continuar = eval(validation);
	}
	if(continuar)
	{
		document.getElementById(form_id).submit();
	}
}


function reseteaFormulario(form_id)
{
	document.getElementById(form_id).reset();
}

function validaUsuario()
{
	if(document.getElementById('usuario').value.length<2 || document.getElementById('nombre').value.length<2)
	{
		alert('Por favor rellene al menos el nombre del alumno y el usuario que utilizara');
		return false;
	}
	else
	{
		return true;
	}
	return false;
}

function salir()
{
	if(confirm('Recuerde salir siempre de la aplicacion con el boton para evitar problemas de seguridad.\nAsegurese de haber guardado todos los datos primero\n\nEsta seguro de que desea salir ahora?'))
	{
		window.location='index.php?s=default&a=logout';
	}
}

//////////////		AJAX-BASICO		/////////////////////////

var xmlHttpReq;

function doRequestObject() 
{
	var http_request = false;
	// Mozilla, Safari,...
	if (window.XMLHttpRequest)
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
		}
	}
	// IE
	else if (window.ActiveXObject)
	{
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (!http_request)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	return http_request;
}

// Prepara un dato para enviar al servidor
function prepareAjaxVariableToSend(id)
{
    return id+'='+escape(document.getElementById(id).value);
}

function prepareAjaxVariableToSendFromPopup(id)
{
    return id+'='+escape(window.opener.document.getElementById(id).value);
}

// Prepara un dato para enviar al servidor
function prepareAjaxCheckBoxToSend(id)
{
    return id+'='+escape(document.getElementById(id).checked);
}

// Prepara un dato para enviar al servidor (value = key)
function prepareAjaxSelectToSend(id)
{
    return id+'='+escape(document.getElementById(id).options[document.getElementById(id).selectedIndex].value);
}

// Prepara un dato para enviar al servidor (texto)
function prepareAjaxSelectTextToSend(id)
{
    return id+'='+escape(document.getElementById(id).options[document.getElementById(id).selectedIndex].text);
}

function getMessage(xmldoc)
{
	var code = -1;
	var message = "Error general";
	try
	{
		code = xmldoc.getElementsByTagName('code').item(0).childNodes.item(0).data;
		message = xmldoc.getElementsByTagName('message').item(0).firstChild.data;
	}
	catch(e)
	{
	}
	return new Array(code, message);
}

function doStandardAjax(strURL, datos)
{	
	//Preparamos el objeto de ajax para enviar datos
    xmlHttpReq = doRequestObject();
	//Preparamos el tipo de envio
    xmlHttpReq.open('POST', strURL, true);
	//Establecemos cabezeras
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//Se ejecutara la funcion de standardRefresh();
	xmlHttpReq.onreadystatechange = standardRefresh;
	//Ejecutamos el envio de datos con los datos almacenados en la variable datos
    xmlHttpReq.send(datos);
}


function standardRefresh()
{
	if (xmlHttpReq.readyState == 4)
	{
		if (xmlHttpReq.status == 200)
		{
			var xmldoc = xmlHttpReq.responseXML;
			//document.write(xmlHttpReq.responseText);
			var messageArray = getMessage(xmldoc);
			var code = messageArray[0];
			var message = messageArray[1];
			if(code<0)
			{
				alert(message);
			}
			//location.reload(true);
		}
	}
}

////////////////////// FIN DE AJAX-BASICO /////////////////////////////

function eligeAlumno(seminarioId)
{
	 w = window.open ("popups/alumnos.php?seminarioId="+seminarioId, "Alumnos","width=450,height=420,top=150,left=200"); 
	 w.window.resizeTo(450,420);
	 w.focus();
}

function limitText(string, limit, classBody, stringHeader, classHeader)
{
	switch(arguments.length)
	{
		case 1:
		{
			limit=20;
		}
		case 2:
		{
			classBody="dvbdy1";
		}
		case 3:
		{
			stringHeader="";
		}
		case 4:
		{	
			classHeader="dvhdr1";
			break;
		}
		default:
		{
			limit=20;
			classBody="dvbdy1";
			stringHeader="";
			classHeader="dvhdr1";
			break;
		}
	}
	
	if(string.length<=limit)
	{
		return string;
	}
	else
	{
		return boxOver(string.substr(0, limit-3)+"...", string, classBody, stringHeader, classHeader);
	}
}

function boxOver(string, stringBody, classBody, stringHeader, classHeader)
{
	
	switch(arguments.length)
	{
		case 1:
		{
			classBody="dvbdy1";
		}
		case 2:
		{
			stringHeader="";
		}
		case 3:
		{	
			classHeader="dvhdr1";
			break;
		}
		default:
		{
			classBody="dvbdy1";
			stringHeader="";
			classHeader="dvhdr1";
			break;
		}
	}
	
	return "<span title='cssbody=["+classBody+"] cssheader=["+classHeader+"] header=["+stringHeader+"] body=["+stringBody+"]'>"+string+"</span>";
}

function agregarAlumnoATabla(seminarioId, alumnoId, nombre, email)
{
		var tbl = window.opener.document.getElementById('tabla_alumnos');
		var lastRow = tbl.rows.length;  
		var iteration = lastRow+1; // if there's no header row in the table, then iteration = lastRow + 1
		var row = tbl.insertRow(lastRow);
		
		//Id de la fila
		row.id =  "dinamico"+alumnoId;
		
		//Creacion de celdas
		var first_cell = row.insertCell(0);
		var second_cell = row.insertCell(1);
		var third_cell = row.insertCell(2);
		var fourth_cell = row.insertCell(3);
		var fifth_cell = row.insertCell(4);
		
		first_cell.innerHTML=limitText(nombre, 25, 'cuerpoBoxOver');
		second_cell.innerHTML="&nbsp;";
		third_cell.innerHTML=limitText(email, 23, 'cuerpoBoxOver');
		fourth_cell.innerHTML="&nbsp;";
		fifth_cell.innerHTML="<a href='javascript:doBajaSeminario(\""+seminarioId+"\", \""+alumnoId+"\", \"dinamico"+alumnoId+"\")'><img src=\"img/btn_desmatricular.gif\" alt=\"Desmatricula al alumno del seminario\" title=\"Desmatricula al alumno del seminario\" width=\"69\" height=\"24\" border=\"0\" /></a>";

		first_cell.vAlign ='middle';
		second_cell.vAlign ='middle';
		third_cell.vAlign ='middle';
		fourth_cell.vAlign ='middle';
		fifth_cell.vAlign ='middle';
		
		first_cell.align ='left';
		second_cell.align ='left';
		third_cell.align ='left';
		fourth_cell.align ='left';
		fifth_cell.align ='left';
		
		window.opener.document.getElementById('alumnos').value=parseInt(window.opener.document.getElementById('alumnos').value)+1;
		if(window.opener.document.getElementById("tr-1")!=null)
		{
			window.opener.document.getElementById('tabla_alumnos').deleteRow(window.opener.document.getElementById("tr-1").rowIndex);
		}
}

	function confirma(mensaje, enlace)
	{
		if(confirm(mensaje))
		{
			window.location=enlace;
		}
	}
	
	
	function change_day_actividades(year, month, day)
	{
		window.location = "index.php?s=default&a=actividades&actual="+year+"-"+month+"-"+day;
	}
	
	function change_day(year, month, day)
	{
		QueryString.set('actual', year+"-"+month+"-"+day);
		QueryString.go();
	}