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]	=	'Sepetiembre';
	month_names[10]	=	'Octubre';
	month_names[11]	=	'Noviembre';
	month_names[12]	=	'Deciembre';

	url	= '_sec/calendar/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)
			{
				if (document.getElementById('the_table'))
				{
					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('Error cargando el calendario');
			}
		}
	}
}
function aborra(id) {
req	=	xmlhttp_start();

req.open('GET', '_sec/calendar/remove_event.php?id='+id, true);
req.send(null);

req.onreadystatechange = function ()
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
			{
				document.location.reload();
			}
	}
}
}
function update(id)
{
	n_dura =	document.getElementById('new_dura').value;
	n_tit =	document.getElementById('new_tit').value;
	n_text =	document.getElementById('new_text').value;
	
	req	=	xmlhttp_start();
	req.open('GET', '_sec/calendar/update_col.php?event='+id+'&dura='+n_dura+'&tit='+n_tit+'&text='+n_text, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				document.location.reload();
			}
		}
	}
}

function changeit(old_tit, old_text, id, old_dura)
{
	document.getElementById('cpanel').style.display	=	'block';
	document.getElementById('change_colour').style.display	=	'block';
	document.getElementById('change_colour').innerHTML = '<h3>Modificar Evento</h3>Duracion: <input type="text" id="new_dura" value="'+old_dura+'" /><br />Titulo: <input type="text" id="new_tit" value="'+old_tit+'" /><br />Contenido:<br />&nbsp;&nbsp;&nbsp;<textarea id="new_text" cols="50" rows="10">'+old_text+'</textarea><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button onclick="update('+id+');">Modificar</button> - <button onclick="closeup();">Cerrar</button>';
}
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', '_sec/calendar/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('Could not load list');
			}
		}
	}
}
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', '_sec/calendar/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('There was an error loading the data');
				}
			}
		}
	}
}
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 closeup() {
	document.getElementById('cpanel').style.display	=	'none';
	document.getElementById('change_colour').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;
	tag1 	=	document.getElementById('tag1').value;
	tag2 	=	document.getElementById('tag2').value;
	tag3 	=	document.getElementById('tag3').value;
	

	xmlhttp.open('GET', '_sec/calendar/create_event.php?title='+title+'&type='+type+'&content='+content+'&day='+day+'&month='+month+'&year='+year+'&priv='+priv+'&code='+code+'&days='+days+'&colour='+colour+'&tag1='+tag1+'&tag2='+tag2+'&tag3='+tag3, true);
	xmlhttp.send(null);
		
	xmlhttp.onreadystatechange	=	function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				alert('Event Added');
				close_cpanel();

				load_calendar(day, month, year);
				load_list(day, month, year);
			}
			else
			{
				alert('There was an error sending the data');
			}
		}
	}
}
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', '_sec/calendar/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('There was an error loading the data');
			}
		}
	}
}
function remove_event(id, day, month, year)
{
	req	=	xmlhttp_start();
	
	req.open('GET', '_sec/calendar/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', '_sec/calendar/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('There was an error loading the data');
						}
					}
				}
				
				alert('Event Borrado');
			}
			else
			{
				alert('There was an error sending the data');
			}
		}
	}
}
function load_short_events(day, month, year)
{
	req	=	xmlhttp_start();
	
	req.open('GET', '_sec/calendar/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('Error cargando los datos');
			}
		}
	}
}