var xmlhttp;

function checkSelected()	{
	var option = document.getElementById("lstEventos").value;
	if(option != "")
		document.editarEvento.submit();
	else
		alert("Por favor seleccione un elemento");
}

function stateChanged()	{
	if (xmlhttp.readyState==4)	{
		document.getElementById("calMonth").innerHTML = xmlhttp.responseText;
		myLytebox.updateLyteboxItems();
	}
}

function GetXmlHttpObject()	{
	if (window.XMLHttpRequest)	{
	 	// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
  	}
	if (window.ActiveXObject)	{
  		// code for IE6, IE5
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	return null;
}

function previousMonth()	{	
	curMonth = document.getElementById("txtMes").value;
	curYear = document.getElementById("txtAnio").value;
	
	//	Cleanning the number
	if(curMonth.charAt(0) == '0')
		curMonth = curMonth.substr(1);
	
	prevMonth = parseInt(curMonth) -1;
	prevYear = parseInt(curYear);	
	
	if(prevMonth == 0)	{
		prevMonth = 12;
		prevYear = prevYear -1;
	}

	(document.getElementById("txtMes")).value = prevMonth;
	(document.getElementById("txtAnio")).value = prevYear;
	
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null)	{
  		alert ("Browser does not support HTTP Request");
  		return;
  	}
	
	var url = "getCalendar.php";
	url = url + "?m=" + prevMonth;
	url = url + "&a=" + prevYear;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);

}

function nextMonth()	{
	curMonth = document.getElementById("txtMes").value;
	curYear = document.getElementById("txtAnio").value;
	
	//	Cleanning the number
	if(curMonth.charAt(0) == '0')
		curMonth = curMonth.substr(1);
	
	prevMonth = parseInt(curMonth) +1;
	prevYear = parseInt(curYear);	
	
	if(prevMonth == 13)	{
		prevMonth = 1;
		prevYear = prevYear +1;
	}

	(document.getElementById("txtMes")).value = prevMonth;
	(document.getElementById("txtAnio")).value = prevYear;
	
	
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null)	{
  		alert ("Browser does not support HTTP Request");
  		return;
  	}
	
	var url = "getCalendar.php";
	url = url + "?m=" + prevMonth;
	url = url + "&a=" + prevYear;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function confirmDelete()	{
	var ok = confirm("&#191;Esta seguro que desea borrar el evento&#63;");
	if(ok)	{
		document.eliminarEvento.action = "eliminarProcess.php";
		document.eliminarEvento.submit();
	}		
}

function validate(form)	{

	var cont = true;
   	date = document.getElementById("txtFecha");
	image = document.getElementById("txtImagen");
	title = document.getElementById("txtTitulo");
	place = document.getElementById("txtLugar");
	description = document.getElementById("txtDescripcion");
	   
   	if(!IsDate(date.value)) 	{ 
    	alert('El formato de fecha no es correcto, por favor revise el ejemplo') 
      	date.focus(); 
		cont = false;
   	}
	
	if(title.value == "") 	{ 
    	alert('El titulo del evento esta vacio. Por favor llene el campo') 
      	date.focus(); 
		cont = false;
   	}
	
	if(description.value == "") 	{ 
    	alert('La descripcion del evento esta vacia. Por favor llene el campo') 
      	image.focus(); 
		cont = false;
   	}
	
	if(place.value == "") 	{ 
    	alert('El lugar del evento esta vacia. Por favor llene el campo') 
      	image.focus(); 
		cont = false;
   	}
	
	if(cont)	{
		document.agregarEvento.action = "agregarProcess.php";
		document.agregarEvento.submit();
	}
}

function validateEdit(form)	{

	var cont = true;
   	date = document.getElementById("txtFecha");
	image = document.getElementById("txtImagen");
	title = document.getElementById("txtTitulo");
	place = document.getElementById("txtLugar");
	description = document.getElementById("txtDescripcion");
	   
   	if(!IsDate(date.value)) 	{ 
    	alert('El formato de fecha no es correcto, por favor revise el ejemplo') 
      	date.focus(); 
		cont = false;
   	}
	
	if(title.value == "") 	{ 
    	alert('El titulo del evento esta vacio. Por favor llene el campo') 
      	date.focus(); 
		cont = false;
   	}
	
	if(description.value == "") 	{ 
    	alert('La descripcion del evento esta vacia. Por favor llene el campo') 
      	image.focus(); 
		cont = false;
   	}
	
	if(place.value == "") 	{ 
    	alert('El lugar del evento esta vacia. Por favor llene el campo') 
      	image.focus(); 
		cont = false;
   	}
	
	if(cont)	{
		document.editarEvento.action = "editarProcess.php";
		document.editarEvento.submit();
	}
}


function IsDate(value)	{
	var ret = false;
	
	//	Si la longitud = 10 (DD/MM/AAAA)
	if(value.length == 10)	{
	
		if((value.charAt(4) == "/" && value.charAt(7) == "/") ||
			(value.charAt(4) == "-" && value.charAt(7) == "-"))	{
			
			if( IsNumeric(value.charAt(0)) &&
				IsNumeric(value.charAt(1)) &&
				IsNumeric(value.charAt(2)) && 
				IsNumeric(value.charAt(3)) && 
				IsNumeric(value.charAt(5)) && 
				IsNumeric(value.charAt(6)) &&
				IsNumeric(value.charAt(8)) &&
				IsNumeric(value.charAt(9)) )
					ret = true;
		}
	}

	return ret;
}


function IsNumeric(value)	{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < value.length && IsNumber == true; i++) 	{ 
      Char = value.charAt(i); 
      
	  if (ValidChars.indexOf(Char) == -1) 
         IsNumber = false;
   }

   return IsNumber;   
}
