var xmlhttp=null;
var prevMonth = 0;
var prevYear = 0;
var nextMonth = 0;
var nextYear = 0;
var nextDay = 0;
var prevDay = 0;
var currentYear = 0;
var currentMonth = 0;

function firstPopulate()
{
	if (xmlhttp.readyState==4)
	{
		if (xmlhttp.status==200)
    		{
			populate_calendar( currentYear, currentMonth );
		}
		else
		{
			alert("Problem retrieving XML data");
		}
	}
}

function loadComicXML( year, month, webpage )
{
	currentYear = year;
	currentMonth = month-1;
	if (window.XMLHttpRequest)
	{// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject)
		{// code for IE5 and IE6
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=firstPopulate;
		xmlhttp.open("GET",webpage,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

weekdays = new Array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" );
months = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
rowcol = new Array( "a", "b", "c", "d", "e", "f", "g" );

function numExpander( numExpanderX )
{
	if( numExpanderX > 9 )
		return "" + numExpanderX;
	return "0" + numExpanderX;
}

function getComicData( comicDataNode, comicDataName )
{
	return comicDataNode.getElementsByTagName( comicDataName )[0].childNodes[0].nodeValue;

}

function getDateText( theDate )
{
	return "" + theDate.getFullYear() + numExpander(theDate.getMonth()+1) + numExpander( theDate.getDate() );
}

function populate_calendar( year, month )
{
	inMonth = false;
	afterMonth = false;
	lastRow = false;

	row = 0;
	col = 0;

	currentYear = year;
	currentMonth = month;
	thisComicSubDate = "" + currentYear + numExpander( currentMonth+1 );

	currentDay = new Date();
	currentDay.setFullYear( year, month, 1 );

	calMonth = document.getElementById( "calmonth" );
	clearChildNodes( calMonth );
	calMonth.appendChild( document.createTextNode( months[currentDay.getMonth()] + " " + currentDay.getFullYear() ) );

	currentDay.setDate( currentDay.getDate() - 1 );
	currentDay.setDate( currentDay.getDate() - currentDay.getDay() );
	//alert( "" + weekdays[currentDay.getDay()] + " " + months[currentDay.getMonth()] + " " + currentDay.getDate() + ", " + currentDay.getFullYear() );

	currentDayText = getDateText( currentDay );

	xmlComics = xmlhttp.responseXML.documentElement.getElementsByTagName("comic");
	var currentComicNum = 0;
	for( currentComicNum = 0 ; currentComicNum < xmlComics.length ; currentComicNum++ )
		if( currentDayText <= getComicData( xmlComics[currentComicNum], "pushdate" ) )
			break;

	prevMonth = currentDay.getMonth();
	prevYear = currentDay.getFullYear();
	calPrev = document.getElementById( "calprev" );
	clearChildNodes( calPrev );
	firstComicSubDate = "" + getComicData( xmlComics[0], "pushdate" ).substr( 0, 6 );
	if( !(firstComicSubDate == thisComicSubDate) )
	{
		anode = document.createElement( "a" );
		anode.href = "#";
		anode.onclick = function(){ return populate_calendar( prevYear, prevMonth ); };
		anode.appendChild( document.createTextNode( "<" ) );
		anode.className = "calprevnext";
		calPrev.appendChild( anode );
	}

	while( !afterMonth || currentDay.getDay() )
	{
		if( currentDay.getDate() == 1 )
		{
			if( !inMonth )
				inMonth = true;
			else
				afterMonth = true;
		}
		if( currentComicNum < xmlComics.length )
		{
			if( getDateText( currentDay ) == getComicData( xmlComics[currentComicNum], "pushdate" ) )
			{
				setDay( currentDay.getDate(), row, col, inMonth, afterMonth, lastRow, getComicData( xmlComics[currentComicNum], "id" ) );
				for( ++currentComicNum ; currentComicNum < xmlComics.length ; currentComicNum++ )
				{
					if( getDateText( currentDay ) < getComicData( xmlComics[currentComicNum], "pushdate" ) )
						break;
				}
			}
			else
				setDay( currentDay.getDate(), row, col, inMonth, afterMonth, lastRow, "0" );
		}
		else
			setDay( currentDay.getDate(), row, col, inMonth, afterMonth, lastRow, "0" );
		currentDay.setDate( currentDay.getDate() + 1 );
		if( col == 6 )
		{
			row++;
			col = 0;
			testDate = new Date(currentDay);
			testDate.setDate( currentDay.getDate() + 7 );
			if( inMonth && currentDay.getMonth() != testDate.getMonth() )
				lastRow = true;
		}
		else
			col++;
	}


	nextMonth = currentDay.getMonth();
	nextYear = currentDay.getFullYear();
	calNext = document.getElementById( "calnext" );
	clearChildNodes( calNext );
	lastComicSubDate = "" + getComicData( xmlComics[xmlComics.length - 1], "pushdate" ).substr( 0, 6 );
	if( !(lastComicSubDate == thisComicSubDate) )
	{
		anode = document.createElement( "a" );
		anode.href = "#";
		anode.onclick = function(){ return populate_calendar( nextYear, nextMonth ); };
		anode.appendChild( document.createTextNode( ">" ) );
		anode.className = "calprevnext";
		calNext.appendChild( anode );
	}

	calLastRow = document.getElementById( "callastrow" );

	if (navigator.appVersion.indexOf("MSIE") != -1)
	{
		if( row == 6 )
			calLastRow.style.display = "inline";
		else
			calLastRow.style.display = "none";
	}
	else
	{
		if( row == 6 )
			calLastRow.style.visibility = "visible";
		else
			calLastRow.style.visibility = "collapse";
	}

	document.getElementById( "calendar" ).style.visibility = "visible";

	return false;
}

function clearChildNodes( theElement )
{
	while( theElement.hasChildNodes() )
	{
		theElement.removeChild( theElement.childNodes[0] );
	}
}

function setDay( calDay, row, col, inMonth, afterMonth, lastRow, comicId )
{
	var baseClass = ""
	var extraClass = ""

	if( lastRow )
		baseClass = "cal-day-bottom";
	else
		baseClass = "cal-day";

	if( comicId != "0" )
	{
		if( !inMonth || afterMonth )
			extraClass = " cal-lun-day";
		else
			extraClass = " cal-lcn-day";
	}
	else
		if( !inMonth || afterMonth )
			extraClass = " cal-uun-day"

	currentDayRowCol = "calday"+rowcol[row]+rowcol[col];
	currentDayCell = document.getElementById( currentDayRowCol );

	clearChildNodes( currentDayCell );

	currentDayCell.className = baseClass + extraClass;
	if( comicId != "0" )
	{
		currentDayCell.appendChild( document.createElement( "a" ) );
		currentDayCell = currentDayCell.childNodes[0];
		currentDayCell.href = "index.php?id=" + comicId;
		currentDayCell.className = "calday";
	}
	currentDayCell.appendChild( document.createTextNode( "" + calDay ) );
}
