var day, month, year, code, month2;

function load_calendar(day, month, year, way)
{
	month2		=	month;
	month_names	=	Array();
	month_names[1]	=	'Enero';
	month_names[2]	=	'Febrero';
	month_names[3]	=	'Marzo';
	month_names[4]	=	'Abril';
	month_names[5]	=	'Mayo';
	month_names[6]	=	'Junio';
	month_names[7]	=	'Julio';
	month_names[8]	=	'Agosto';
	month_names[9]	=	'Septiembre';
	month_names[10]	=	'Octubre';
	month_names[11]	=	'Noviembre';
	month_names[12]	=	'Diciembre';

	url	=	'load_calendar.php?day='+day+'&month='+month+'&year='+year;
	
	req	=	xmlhttp_start();
	req.open('GET', url, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				document.getElementById('the_table').innerHTML	=	req.responseText;
				years		=	new String(year);

				next_month	=	month2 + 1;

				if (next_month == 13)
				{
					next_month	=	1;
					month2		=	12;
				}
				next_year	=	new String(month == 12 ? year + 1 : year);
			
				prev_month	=	month2 - 1;
				if (prev_month == 0)
				{
					prev_month	=	12;
					next_month		=	2;
				}
				prev_year	=	new String(month == 1 ? year - 1 : year);

				document.getElementById('prev').innerHTML	=	'<a class="hand" onclick="load_calendar(1, '+prev_month+', '+prev_year+', 1);" href="#">'+month_names[prev_month]+' '+prev_year.substring(2, 4)+'</a>';
				document.getElementById('next').innerHTML	=	'<a class="hand" onclick="load_calendar(1, '+next_month+', '+next_year+', 2);" href="#">'+month_names[next_month]+' '+next_year.substring(2, 4)+'</a>';
				document.getElementById('current_month').innerHTML	=	month_names[month2]+' '+years.substring(2, 4);				
				load_list(day, month, year);
			}
			else
			{
				alert('Hay un error cargando el calendario');
			}
		}
	}
}
function update_colour(id)
{
	n_colour	=	document.getElementById('new_colour').value;

	req	=	xmlhttp_start();
	req.open('GET', 'update_col.php?event='+id+'&colour='+n_colour, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				document.location.reload();
			}
		}
	}
}
function change_colour(id, old_colour)
{
	document.getElementById('cpanel').style.display	=	'block';
	document.getElementById('change_colour').style.display	=	'block';
	document.getElementById('change_colour').innerHTML = '<h3>Cambiar Color</h3>Nuevo Color: #<input type="text" id="new_colour" value="'+old_colour+'" /><br /><input type="submit" onclick="update_colour('+id+');" value="Cambiar" />';
}
function hide_event(element)
{
	document.getElementById(element).style.display	=	'none';
}
function display_event(element)
{
	document.getElementById(element).style.display	=	'block';
}
function load_list(day, month, year)
{
	xmlhttp	=	xmlhttp_start();
	xmlhttp.open('GET', 'load_list.php?day='+day+'&month='+month+'&year='+year, true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange	=	function ()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				document.getElementById('list_table').innerHTML	=	xmlhttp.responseText;
			}
			else
			{
				alert('No se cargo la lista');
			}
		}
	}
}
function toggle_panel()
{
	if (document.getElementById('panel_content').style.display == 'block')
	{
		document.getElementById('panel_content').style.display	=	'none';
		document.getElementById('user_panel').style.MozOpacity	=	0.5;
	}
	else
	{
		document.getElementById('panel_content').style.display	=	'block';
		document.getElementById('user_panel').style.MozOpacity	=	1;
		
		req	=	xmlhttp_start();
		req.open('GET', 'user.php', true);
		req.send(null);
		
		req.onreadystatechange	=	function ()
		{
			if (req.readyState == 4)
			{
				if (req.status == 200)
				{
					document.getElementById('panel_content').innerHTML	=	req.responseText;
				}
				else
				{
					alert('Hay un error cargando el dato');
				}
			}
		}
	}
}
function create_menu(elementId, week, days)
{
	code	=	week;
	for (i = 1; i <= days; i++)
	{
		document.getElementById('s'+i).style.display	=	'none';
	}
	document.getElementById(elementId).style.display	=	'block';
}
function close_menu(element)
{
	document.getElementById(element).style.display	=	'none';
}
function add_event(element, nday, nmonth, nyear, week)
{
	day		=	nday;
	month	=	nmonth;
	month2	=	nmonth;
	year	=	nyear;
	code	=	week;

	document.getElementById('cpanel').style.display		=	'block';
	document.getElementById('add_event').style.display	=	'block';
}
function close_cpanel()
{
	document.getElementById('cpanel').style.display		=	'none';
	document.getElementById('add_event').style.display		=	'none';
	document.getElementById('events').style.display		=	'none';
}
function submit_event()
{
	xmlhttp	=	xmlhttp_start();

	title	=	urlencode(document.getElementById('event_title').value);
	type	=	document.getElementById('event_type').value;
	content	=	encodeURI(urlencode(document.getElementById('event_content').value));
	priv	=	document.getElementById('priv').value;	
	days	=	document.getElementById('days').value;
	colour	=	document.getElementById('colour').value;

	xmlhttp.open('GET', 'create_event.php?title='+title+'&type='+type+'&content='+content+'&day='+day+'&month='+month+'&year='+year+'&priv='+priv+'&code='+code+'&days='+days+'&colour='+colour, true);
	xmlhttp.send(null);
		
	xmlhttp.onreadystatechange	=	function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				alert('Evento Agregado');
				close_cpanel();

				load_calendar(day, month, year);
				load_list(day, month, year);
			}
			else
			{
				alert('Hay un error enviando el dato');
			}
		}
	}
}
function xmlhttp_start()
{
	var xmlhttp;
	/*@cc_on
	
	@if (@_jscript_version >= 5)
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E)
			{
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}
	
	return xmlhttp;
}
function urlencode(str)
{
	return str.replace('&', '%26');
}
function view_events(element, day, month, year)
{
	document.getElementById('the_events').innerHTML	=	'Loading....';
	document.getElementById('cpanel').style.display	=	'block';
	document.getElementById('events').style.display	=	'block';
	xmlhttp	=	xmlhttp_start();
	xmlhttp.open('GET', 'load_events.php?day='+day+'&month='+month+'&year='+year+'&week='+code, true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange	=	function ()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				document.getElementById('the_events').innerHTML	=	xmlhttp.responseText;
			}
			else
			{
				alert('Hay un error cargando el dato');
			}
		}
	}
}
function remove_event(id, day, month, year)
{
	req	=	xmlhttp_start();
	
	req.open('GET', 'remove_event.php?id='+id, true);
	req.send(null);

	req.onreadystatechange = function ()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				load_short_events(day, month, year);
				
				req1	=	xmlhttp_start();
				req1.open('GET', 'load_events.php?day='+day+'&month='+month+'&year='+year+'&week='+code, true);
				req1.send(null);
				
				req1.onreadystatechange	=	function ()
				{
					if (req1.readyState == 4)
					{
						if (req1.status == 200)
						{
							document.getElementById('the_events').innerHTML	=	req1.responseText;
							load_list(day, month, year);
							load_calendar(day, month, year);
						}
						else
						{
							alert('Hay un error cargando el dato');
						}
					}
				}
				
				alert('Evento Borrado');
			}
			else
			{
				alert('Hay un error enviando el dato');
			}
		}
	}
}
function load_short_events(day, month, year)
{
	req	=	xmlhttp_start();
	
	req.open('GET', 'load_short_events.php?day='+day+'&month='+month+'&year='+year, true);
	req.send(null);
	
	req.onreadystatechange	=	function()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				document.getElementById('short_events'+day).innerHTML	=	req.responseText;
			}
			else
			{
				alert('Hay un error cargando el dato');
			}
		}
	}
}