var day, month, year, code, month2, isie;

function hov_colour(colour)
{
	if (change_col_for == 'bg')
	{
		document.getElementById('event_content').style.background	=	'rgb('+colour+')';
	}
	else
	{
		document.getElementById('event_content').style.color	=	'rgb('+colour+')';	
	}
}
function set_reminder(email, days, event)
{
	req	=	xmlhttp_start();
	req.open('GET', 'set_rem.php?event='+event+'&email='+email+'&days='+days, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (xmlhttp.readyState == 4)
		{
			document.getElementById('rem_form').innerHTML	=	'<hr />Reminder set!';
		}
	}
}
function check_input()
{
	if (document.getElementById('rem_email').value == 'email@domain.com')
	{
		document.getElementById('rem_email').value	=	'';
	}
}
function load_rem_form(event)
{
	req	=	xmlhttp_start();
	
	req.open('GET', 'load_rem.php?event='+event, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (xmlhttp.readyState == 4)
		{
			change_display('cpanel', 'block');
			change_display('rem_form', 'block');
			
			document.getElementById('rem_form').innerHTML	=	req.responseText;
		}
	}
}
function update_event(id, day, month, year)
{
	xmlhttp	=	xmlhttp_start();

	title	=	urlencode(document.getElementById('eevent_title').value);
	type	=	document.getElementById('eevent_type').value;
	content	=	encodeURI(urlencode(document.getElementById('eevent_content').value));
	priv	=	document.getElementById('epriv').value;	
	days	=	document.getElementById('edays').value;
	colour	=	document.getElementById('ecolour').value;

	xmlhttp.open('GET', 'update_event.php?title='+title+'&type='+type+'&content='+content+'&priv='+priv+'&code='+code+'&days='+days+'&colour='+colour+'&id='+id, true);
	xmlhttp.send(null);
		
	xmlhttp.onreadystatechange	=	function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				alert('Event Updated');
				close_cpanel();

				load_calendar(day, month, year);
			}
			else
			{
				alert('There was an error sending the data');
			}
		}
	}
}
function edit_event(id)
{
	change_display('cpanel', 'block');
	change_display('edit_event', 'block');
	
	req	=	xmlhttp_start();
	req.open('GET', 'load_edit_event.php?id='+id, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				document.getElementById('edit_event').innerHTML	=	req.responseText;
			}
		}
	}
}
function load_small_cal_left(month, year, prename, nextname)
{
	req	=	xmlhttp_start();
	req.open('GET', 'load_small_calendar_left.php?smon='+month+'&syear='+year+'&pre='+prename+'&next='+nextname, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				document.getElementById('left_calendar').innerHTML	=	req.responseText;
			}
		}
	}
}
function load_small_cal_right(month, year, prename, nextname)
{
	req	=	xmlhttp_start();
	req.open('GET', 'load_small_calendar_right.php?smon='+month+'&syear='+year+'&pre='+prename+'&next='+nextname, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				document.getElementById('right_calendar').innerHTML	=	req.responseText;
			}
		}
	}
}
function change_display(element, value)
{
	document.getElementById(element).style.display	=	value;
}
function add_colour(colour)
{
	if (change_col_for == 'bg')
	{
		document.getElementById('colour').value			=	colour;
	}
	else
	{
		document.getElementById('fcolour').value			=	colour;	
	}
	change_display('cpick', 'none');
}
function load_calendar(day, month, year, way)
{
	document.getElementById('the_table').style.MozOpacity	=	0.1;
	document.getElementById('right_calendar').innerHTML	=	'Loading Data.....';
	document.getElementById('left_calendar').innerHTML	=	'Loading Data.....';
	change_display('cal_loading_msg', 'block');

	month2		=	month;
	month_names	=	Array();
	month_names[1]	=	'January';
	month_names[2]	=	'February';
	month_names[3]	=	'March';
	month_names[4]	=	'April';
	month_names[5]	=	'May';
	month_names[6]	=	'June';
	month_names[7]	=	'July';
	month_names[8]	=	'August';
	month_names[9]	=	'September';
	month_names[10]	=	'October';
	month_names[11]	=	'November';
	month_names[12]	=	'December';

	url	=	'load_calendar.php?cday='+day+'&cmonth='+month+'&cyear='+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	=	month + 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('current_month').innerHTML	=	month_names[month2]+' '+years;				
				
				load_list(day, month, year);	
				
				/* I think it gets confused if you call these at the same time :( */
				setTimeout('load_small_cal_left('+month+', '+year+', \''+month_names[prev_month]+'\', \''+month_names[next_month]+'\')', 500);
				setTimeout('load_small_cal_right('+month+', '+year+', \''+month_names[prev_month]+'\', \''+month_names[next_month]+'\')', 100);

			}
			else
			{
				alert('There was an error loading the calendar');
			}
		}
	}
	document.getElementById('the_table').style.MozOpacity	=	1;
	change_display('cal_loading_msg', 'none');
}
function update_colour(id, colour)
{
	n_colour	=	colour ? 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.getElementById('ecolour').value			=	colour;
				change_display('change_colour', 'none');
			}
		}
	}
}
function change_colour(id, old_colour)
{
	change_display('cpanel', 'block');
	change_display('change_colour', 'block');
	req	=	xmlhttp_start();
	req.open('GET', 'load_colours.php?id='+id, true);
	req.send(null);
	
	req.onreadystatechange	=	function ()
	{
		if (req.status == 200)
		{
			document.getElementById('change_colour').innerHTML =	req.responseText;
		}
	}
}
function load_list(day, month, year)
{
	document.getElementById('list_table').innerHTML	=	'Loading Data';
	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('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', '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';
	change_display('edit_event', 'none');
	change_display('cpick', 'none');
	change_display('rem_form', '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;
	fcolour	=	document.getElementById('fcolour').value;

	if (title.length < 3)
	{
		alert('Your title must be at least 3 characters long');
		return;
	}
	if (content.length < 3)
	{
		alert('Your content must be at least 3 characters long');
		return;
	}

	xmlhttp.open('GET', 'create_event.php?fc='+fcolour+'&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('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;
	
	if (window.XMLHttpRequest)
	{
		xmlhttp	=	new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		xmlhttp	=	new ActiveXObject('Microsoft.XMLHTTP');
		isie	=	true;
	}
	else
	{
		alert('Cannot Load XMLHTTPRequest Object');
	}
	
	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('There was an error loading the data');
			}
		}
	}
}

/*
	remove_event
	:: This obviously removes an event....
*/
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)
			{
				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('There was an error loading the data');
						}
					}
				}
				alert('Event Deleted');
			}
		}
	}
}

/*
	load_short_events
	::	Loads events titles only, not much use outside this script
*/
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;
			}
		}
	}
}