var xmlHttp;

try
{
	xmlHttp = new XMLHttpRequest();
}
catch (e)
{
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function getSchedule()
{
	xmlHttp.open("GET","get_schedule.php?date="+curDate,true);

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById("schedule").innerHTML = xmlHttp.responseText;
			var arrDate = curDate.split("-");
			var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
			document.title = "Titan TV schedule for " + months[arrDate[1]-1] + " " + arrDate[2] + ", " + arrDate[0];
			document.getElementById("title").innerHTML = document.title;
			document.location.hash = "#"+curDate;
		}
	}

	xmlHttp.send(null);
}

function getDifferentDay(offset)
{
	var arrDate = new Array();
	arrDate = curDate.split("-");
	var objDate = new Date();
	objDate.setYear(arrDate[0]);
	objDate.setMonth(parseInt(arrDate[1]) - 1);
	objDate.setDate(parseInt(arrDate[2]));
	objDate.setDate(objDate.getDate() + offset);
	curDate = objDate.getFullYear() + "-" + (objDate.getMonth() + 1) + "-" + objDate.getDate();

	objDate.setDate(objDate.getDate()-7);
	document.getElementById("subweek").href="#"+dateToString(objDate);

	objDate.setDate(objDate.getDate()+6);
	document.getElementById("subday").href="#"+dateToString(objDate);

	objDate.setDate(objDate.getDate()+2);
	document.getElementById("addday").href="#"+dateToString(objDate);

	objDate.setDate(objDate.getDate()+6);
	document.getElementById("addweek").href="#"+dateToString(objDate);

	getSchedule();
}
function dateToString(objDate)
{
	return objDate.getFullYear() + "-" + (objDate.getMonth() + 1) + "-" + objDate.getDate();
}
function today()
{
	var today = new Date();
	curDate = dateToString(today);
	getDifferentDay(0);
}
function checkHash()
{
	if(location.hash != "")
	{
		var arrHash = location.hash.slice(1).split("-");

		if(!(2007 < arrHash[0] < 9999))
			return;
		if(!(0 < arrHash[1] < 13))
			return;
		if(!(0 < arrHash[2] < 32))
			return;

		curDate = location.hash.slice(1);

		getDifferentDay(0);
	}
}

