//Constructor
function calendar(id,d,p){
	//alert(readCookie('currMonth'))
	this.id = id;
	this.dateObject = d;
	this.write = writeCalendar;
	this.month = d.getMonth();
	this.length = d.getLength();
	this.date = d.getDate();
	this.day = d.getDay();
	this.year = d.getFullYear();
	this.getFormattedDate = getFormattedDate;
	//get the first day of the month's day
	d.setDate(1);
	this.firstDay = d.getDay();
	//then reset the date object to the correct date
	d.setDate(this.date);
}
var markedDates = new Array()
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

function getFormattedDate(){
	return days[this.day] + ', ' + months[this.month] + ' ' + this.date + ', ' + this.year;
	//return this.month + '/' + this.date + '/' + this.year;
}

function writeCalendar(){
var thisCalPage = new Array()

for(var i=0; i<markedDates.length; i++)
	{
		
	if(markedDates[i].year == this.year & markedDates[i].month == this.month +1)
		{
		thisCalPage[Number(markedDates[i].day)] = markedDates[i]
		}
	}


	var calString = '<div id="calContainer">';
	//write month and year at top of table
	calString += '<table id="cal' + this.id + '"  cellspacing="0" width="300" class="dataSheet" border="0">';
	//write the image – comment out to hide images
	//write the month
	calString += '<tr><th width="100%" class="month" colspan="2"><h2>' + months[this.month] + ', ' + this.year + '</h2><span style=""><img onClick="changeMonth(-1,\'' + this.id + '\')" src="i/menu/jumpBck.gif" style="cursor:hand;"> <img   onClick="changeMonth(+1,\'' + this.id + '\')" src="i/menu/jump.gif" style="cursor:hand;"></span></th></tr>';
	//calString += '<th nowrap></th></tr>'
	//write a row containing days of the week
	//write the body of the calendar
	var i=this.firstDay
	for(j=0;j<this.length;j++){
	if(!(thisCalPage[j+1] == undefined))
	{
	var facStr = new String()
	if(thisCalPage[j+1].catering){facStr +='<img src="i/menu/ico_catering.gif" alt="Catering" /> '}
	if(thisCalPage[j+1].marquee){facStr +='<img src="i/menu/ico_marquee.gif" alt="Marquee/Shelter"/> '}
	if(thisCalPage[j+1].toilets){facStr +='<img src="i/menu/ico_wc.gif" alt="Toilets" /><br />'}else{facStr +='<br />'}
	if(thisCalPage[j+1].desc == "Open" || thisCalPage[j+1].desc == "Registered & Open"){facStr +='<img src="i/menu/ico_open.gif" alt="Registered" /> '}
	if(thisCalPage[j+1].evtTime=="null") {thisCalPage[j+1].evtTime="";}
	
	if(thisCalPage[j+1].desc == "Registered & Birds" || thisCalPage[j+1].desc == "Registered & Birds"){facStr +='<img src="i/menu/ico_registered.gif" alt="Registered" /> '}
	theDesc = '<table cellpadding=0 cellspacing=0 border=0 class="markedDate" width=100%><tr valign=top><td width="70%"><strong>' + thisCalPage[j+1].evt +  '</strong><br /><a class="info" href="courseDetails.asp?location='+ thisCalPage[j+1].location +'&prevUrl=Shoot Calendar">' + thisCalPage[j+1].location + '</a> <br /> ' +thisCalPage[j+1].evtTime+'</td><td width="20%" align=right>' + facStr  + '<tr></table>'
	//old
	//'<br /><input class="microButton" type="button" value="book" onClick="javascript:location.href=\'' + thisCalPage[j+1].bookingStr +'\'"><tr></table>'
	}
	else
	{
	if(String(j+1).length == 1){dayStr = "0" + String(j+1)}else{dayStr = String(j+1)}
	if(String(this.month).length == 1){monthStr = "0" + this.month}else{monthStr = this.month}
	var dateRef = "day=" + dayStr + "&month=" + monthStr + "&year=" + this.year
	//old
	//theDesc = '<input class="microButtonAlt" type="button" value="book" onClick="javascript:location.href=\'booking.asp?' + dateRef + '\'" >'
	
	//dlp
	theDesc =' '
	classStr = ""
	}
	if(i==0 || i==6){theClass="striped"}else{theClass=classStr}
		var displayNum = (j+1);
		 if(displayNum==this.date){
			calString += '<tr><td valign=top id="' + this.id +'selected" class="date" nowrap>' + displayNum + " " +  days[i] + '</td><td>' + theDesc + '</td></tr>';}else{
			//the rest of the numbered cells
			calString += '<tr class="' + theClass + '"><td valign=top id="" nowrap>' + displayNum + " " + days[i] + '</td><td>' + theDesc + '</td></tr>';	}
	i++
	if(i==7){i=0}
	}
	//close the last number row
	//write the nav row
	calString += '</table>';
	calString += '</div>';
	return calString;
}

Date.prototype.getLength = function(){
	//thirty days has September...
	switch(this.getMonth()){
		case 1:
			if((this.getFullYear()%4==0&&this.getFullYear()%100!=0)||this.getFullYear()%400==0)
				return 29; //leap year
			else
				return 28;
		case 3:
			return 30;
		case 5:
			return 30;
		case 8:
			return 30;
		case 10:
			return 30
		default:
			return 31;
	}
}
function changeDate(td,cal){
	//Some JavaScript trickery
	//Change the cal argument to the existing calendar object
	//This is why the first argument in the constructor must match the variable name
	//The cal reference also allows for multiple calendars on a page
	cal = eval(cal);
	document.getElementById(cal.id + "selected").className = "days";
	document.getElementById(cal.id + "selected").id = "";
	td.className = "date";
	td.id = cal.id + "selected";
	//set the calendar object to the new date
	cal.dateObject.setDate(td.firstChild.nodeValue);
	cal = new calendar(cal.id,cal.dateObject);
	//here is where you could react to a date change - I'll just display the formatted date
}

function changeMonth(mo,cal){
	//more trickery!
	cal = eval(cal);
	//The Date object is smart enough to know that it should roll over in December
	//when going forward and in January when going back
	cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
	cal = new calendar(cal.id,cal.dateObject);
	cal.formattedDate = cal.getFormattedDate();
	document.getElementById('calContainer').innerHTML = cal.write();
	createCookie('currMonth',cal.dateObject.getMonth(),0)
	createCookie('currYear',cal.dateObject.getYear(),0)
	//alert(readCookie('currMonth'))
}

function markCalendar(d,desc,type,evtTime){
	
dateStrings = d.split(",")
	for(var i = 0; i<dateStrings.length-1; i++)
	{
	tmp = dateStrings[i].split("_")
	
	date = tmp[0].split("/")
	markedDate = new Object
	markedDate.year = date[2]
	markedDate.month = date[1]
	markedDate.day = date[0]
	markedDate.evt = tmp[1]
	markedDate.desc = tmp[2]
	markedDate.location = tmp[3]
	markedDate.catering = 1 //tmp[4].length%2
	markedDate.marquee = 1 //tmp[5].length%2
	markedDate.toilets = 1 //tmp[6].length%2
	markedDate.evtTime=tmp[7]
	if(markedDate.day.length == 1){dayStr = "0" + markedDate.day}else{dayStr = markedDate.day}
	if(markedDate.month.length == 1){monthStr = "0" + markedDate.month}else{monthStr = markedDate.month}
	markedDate.bookingStr = "booking.asp?day=" + dayStr + "&month=" + monthStr + "&year=" + markedDate.year + "&eventType=Competition" + "&compType=" + markedDate.evt + "&location=" + markedDate.location + "&catering=" + markedDate.catering
	markedDates[markedDates.length] = markedDate
	}
	
}
// cookie cutter

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
