var month_string = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
var day_string = new Array("S", "M", "T", "W", "T", "F", "S");

sqCalendarDisplay.prototype.constructor = new sqCalendarDisplay();
sqCalendarDisplay.prototype.sqSetScrollLimits = sqCalendarDisplay_sqSetScrollLimits;
sqCalendarDisplay.prototype.sqSetSelectableLimits = sqCalendarDisplay_sqSetSelectableLimits;
sqCalendarDisplay.prototype.sqUpdate = sqCalendarDisplay_sqUpdate;
sqCalendarDisplay.prototype.sqDraw = sqCalendarDisplay_sqDraw;
sqCalendarDisplay.prototype.sqScroll = sqCalendarDisplay_sqScroll;
sqCalendarDisplay.prototype.sqScrollTo = sqCalendarDisplay_sqScrollTo;
sqCalendarDisplay.prototype.Click = sqCalendarDisplay_Click;
sqCalendarDisplay.prototype.sqSetSelected = sqCalendarDisplay_sqSetSelected;
sqCalendarDisplay.prototype.sqClearSelected = sqCalendarDisplay_sqClearSelected;
sqCalendarDisplay.prototype.sqCreate = sqCalendarDisplay_sqCreate;

sqCalendarDisplay.prototype.sqID_to_Date = sqCalendarDisplay_sqID_to_Date;
sqCalendarDisplay.prototype.sqDate_to_ID = sqCalendarDisplay_sqDate_to_ID;
sqCalendarDisplay.prototype.sqIsDateExpired = sqCalendarDisplay_sqIsDateExpired;
sqCalendarDisplay.prototype.sqIsDateSelectable = sqCalendarDisplay_sqIsDateSelectable;

sqCalendarDisplay.prototype.sqDrawDays = sqCalendarDisplay_sqDrawDays;
sqCalendarDisplay.prototype.sqDrawSelectableDays = sqCalendarDisplay_sqDrawSelectableDays;
sqCalendarDisplay.prototype.sqGetHumanDateString = sqCalendarDisplay_sqGetHumanDateString;
sqCalendarDisplay.prototype.sqGetSQLDateString = sqCalendarDisplay_sqGetSQLDateString;

function sqCalendarDisplay(_day, _month, _year, _divname)
{
	this.visible = false;
	this._date = new SB_Date();
	this._date.fromDMY(_day, _month, _year);
	this.divname = _divname;

	this.first_of_month = -1;
	this.days_in_month = -1;
	this.selectedDay = -1;
	this.selectedMonth = -1;
	this.selectedYear = -1;

	this.LowLimit = undefined;
	this.HiLimit = undefined;

	var get_today = new Date();

	this.HilightBookingStart = false;

	this.booked_colour = 'red';
	this.not_booked_colour = 'white';
	this.expired_days_colour = '#0000b0';
	this.expired_bg_colour = '#a0a0a0';
	this.current_bg_colour = '#cccccc';
	this.current_days_colour = 'black';
	this.selected_colour = '#ff0000';

	this.sqselectable = false;
/*
	this.lowSelectableLimit = new SB_Date();
	this.hiSelectableLimit = new SB_Date();
	this.lowSelectableLimit.fromDMY(10,2,2010);
	this.hiSelectableLimit.fromDMY(15,2,2010);
	this.lowSelectableDays = this.lowSelectableLimit.to_int();
	this.hiSelectableDays = this.hiSelectableLimit.to_int();
	*/
}


function sqCalendarDisplay_sqCreate()
{
	if (gbi(this.divname+"_wrapper") == undefined)
		return;
	var wid = 30;
	var height = 10;
	var mon = '0_';
	var newTable = document.createElement("table");
	var newBod = document.createElement("tbody");
	var newTR = document.createElement("tr");
/* minus button*/
	var newTD = document.createElement("td");
	var newText = document.createTextNode('');
	newTD.setAttribute("id", "BUTTON_CALENDAR_MINUS_" + this.divname);
	setClassAttribute(newTD, "calendar_minus_button");
	newTD.appendChild(newText);
	newTR.appendChild(newTD);

/*selected month display*/
	var newTD = document.createElement("td");
	var newText = document.createTextNode('Jan');
	newTD.setAttribute("id", this.divname + "month_title");
	setClassAttribute(newTD, "month_title");
	newTD.appendChild(newText);
	newTR.appendChild(newTD);
/*selected year display*/
	var newTD = document.createElement("td");
	var newText = document.createTextNode('2007');
	newTD.setAttribute("id", this.divname + "year_title");
	setClassAttribute(newTD, "year_title");
	newTD.appendChild(newText);
	newTR.appendChild(newTD);

/* plus button*/
	var newTD = document.createElement("td");
	var newText = document.createTextNode('');
	newTD.setAttribute("id", "BUTTON_CALENDAR_PLUS_" + this.divname);
	setClassAttribute(newTD, "calendar_plus_button");
	newTD.appendChild(newText);
	newTR.appendChild(newTD);


	newTR.appendChild(newTD);
	newBod.appendChild(newTR);

	newTable.appendChild(newBod);
	document.getElementById(this.divname+"_wrapper").appendChild(newTable);
	
	newTable = document.createElement("table");
	newBod = document.createElement("tbody");

	newTR = document.createElement("tr");
	for (i=0; i < 7; i++)
	{
		newTD = document.createElement("td");
		newText = document.createTextNode("n");
		newTD.setAttribute("id", this.divname + "title" + i);
		setClassAttribute(newTD, "day_of_week");
		newTD.appendChild(newText);
		newTR.appendChild(newTD);
	}
	newBod.appendChild(newTR);

	var count = 0;
	for (i=0; i<6; i++)
	{
		newTR = document.createElement("tr");
		for (d=0; d < 7; d++)
		{
			newTD = document.createElement("td");
			newText = document.createTextNode('');
			newTD.setAttribute("id", "CALENDAR_" + this.divname + "." + Number((i*7)+d) );
			setClassAttribute(newTD, "calendar_day");
			newTD.appendChild(newText);
			newTR.appendChild(newTD);
		}
		newBod.appendChild(newTR);
	}
	newTable.appendChild(newBod);
	document.getElementById(this.divname+"_wrapper").appendChild(newTable);
}


/*how far you can scroll in each direction*/
function sqCalendarDisplay_sqSetScrollLimits(low_month, low_year, hi_month, hi_year)
{
	this.LowLimit = new SB_Date();
	this.LowLimit.fromDMY(1, low_month, low_year);
	this.HiLimit = new SB_Date();
	this.HiLomit.fromDMY(1, hi_month, hi_year);
}

/*first and last day which respond to a click*/
function sqCalendarDisplay_sqSetSelectableLimits(low_day, low_month, low_year, hi_day, hi_month, hi_year)
{
	this.lowSelectableLimit = new SB_Date();
	this.hiSelectableLimit = new SB_Date();
	this.lowSelectableLimit.fromDMY(low_day, low_month, low_year);
	this.hiSelectableLimit.fromDMY(hi_day, hi_month, hi_year);
	this.lowSelectableDays = this.lowSelectableLimit.to_int();
	this.hiSelectableDays = this.hiSelectableLimit.to_int();
	this.sqScrollTo(low_month, low_year);
	this.sqDrawSelectableDays();
}


function sqCalendarDisplay_sqUpdate()
{
}

function sqCalendarDisplay_sqDrawSelectableDays()
{
	var i;

	/*alpha display at top - always s m t w   etc*/
	for (i=0; i < 7; i++)
	{
		day_str = this.divname + "title"+i;
		gbi(day_str).firstChild.nodeValue = day_string[i];
	}

	for (i=0; i < 42; i++)
	{
		day_str = "CALENDAR_" + this.divname + "." + i;
		var selected = false;



		if ( ( (i+1)-this.first_of_month == this.selectedDay) && this._date.month == this.selectedMonth && this._date.year == this.selectedYear)
			selected = true;
		var d = this.sqID_to_Date(i);
		var bgc = this.not_booked_colour;
		if (d > 0 )
		{
			if (this.sqIsDateSelectable(d, this._date.month, this._date.year) == false)
			{
				document.getElementById(day_str).style.color = this.expired_days_colour; 				
				document.getElementById(day_str).style.backgroundColor = this.expired_bg_colour; 	
				bgc = this.expired_bg_colour;	
				document.getElementById(day_str).style.cursor = "auto";
			}
			else
			{
				document.getElementById(day_str).style.backgroundColor = this.current_bg_colour; 	
				document.getElementById(day_str).style.color = this.current_days_colour;
				document.getElementById(day_str).style.cursor = "pointer";
			}
			if (selected)
				document.getElementById(day_str).style.color = this.selected_colour;

			document.getElementById(day_str).firstChild.nodeValue = d;
			document.getElementById(day_str).style.visibility = "visible";
		}
		else
		{
			document.getElementById(day_str).firstChild.nodeValue = '';	
			document.getElementById(day_str).style.visibility = 'hidden';	
		}	
	}
}



function sqCalendarDisplay_sqDrawDays()
{
	var i;

	/*alpha display at top - always s m t w   etc*/
	for (i=0; i < 7; i++)
	{
		day_str = this.divname + "title"+i;
		gbi(day_str).firstChild.nodeValue = day_string[i];
	}

	for (i=0; i < 42; i++)
	{
		day_str = "CALENDAR_" + this.divname + "." + i;
		var selected = false;



		if ( ( (i+1)-this.first_of_month == this.selectedDay) && this._date.month == this.selectedMonth && this._date.year == this.selectedYear)
			selected = true;
		var d = this.sqID_to_Date(i);
		var bgc = this.not_booked_colour;
		if (d > 0 )
		{
			if (this.sqIsDateExpired(d))
			{
				document.getElementById(day_str).style.color = this.expired_days_colour; 				
				document.getElementById(day_str).style.backgroundColor = this.expired_bg_colour; 	
				bgc = this.expired_bg_colour;	
			}
			else
			{
				document.getElementById(day_str).style.backgroundColor = this.current_bg_colour; 	
				document.getElementById(day_str).style.color = this.current_days_colour;
			}
			if (selected)
				document.getElementById(day_str).style.color = this.selected_colour;

			document.getElementById(day_str).firstChild.nodeValue = d;
			document.getElementById(day_str).style.visibility = "visible";
		}
		else
		{
			document.getElementById(day_str).firstChild.nodeValue = '';	
			document.getElementById(day_str).style.visibility = 'hidden';	
		}	
	}
}


function sqCalendarDisplay_sqDraw()
{
	if (gbi(this.divname + 'year_title') == undefined)
		return;
	this.first_of_month = first_of_month(this._date.month, this._date.year);

	this.days_in_month = days_per_month(this._date.month, this._date.year);
	var day_str;

	gbi(this.divname + 'year_title').firstChild.nodeValue = this._date.year;

	gbi(this.divname + 'month_title').firstChild.nodeValue = month_string[this._date.month-1];

//for horseshow, we will use sqDrawSelectableDays
//	this.sqDrawDays();
	this.sqDrawSelectableDays();
}

function sqCalendarDisplay_sqID_to_Date(div_id)
{
	if ((div_id>= this.first_of_month)&&( div_id < this.days_in_month + this.first_of_month))
		return div_id-this.first_of_month+1;
	else
		return -1;
}

function sqCalendarDisplay_sqDate_to_ID(dat)
{
	return dat + this.first_of_month -1;
}

/*based on this_day - which is set from server*/
function sqCalendarDisplay_sqIsDateExpired(dat)
{
	var daygetter = new SB_Date();
	daygetter.fromDMY(dat, this._date.month, this._date.year);
	var this_day = new SB_Date();
	this_day.fromSQL(server_today_str);
	return (this_day.to_int() > daygetter.to_int());
}

function sqCalendarDisplay_sqIsDateSelectable(d,m,y)
{
/*
	var daygetter = new SB_Date();
	daygetter.fromDMY(dat, this._date.month, this._date.year);
	var this_day = new SB_Date();
	this_day.fromSQL(server_today_str);
	return (this_day.to_int() > daygetter.to_int());
*/

	compare = new SB_Date();
	compare.fromDMY(d,m,y);
	compare_days = compare.to_int();
	if (compare_days < this.lowSelectableDays)
		return false;
	if (compare_days > this.hiSelectableDays)
		return false;
	return true;
}



function sqCalendarDisplay_sqScroll(direction)
{
	month = this._date.month;
	year = this._date.year;
	canScroll = false;		
	if (direction == 1)
	{
		if (this.HiLimit == undefined)
			canScroll = true;
		else
		{
			if (year < this.HiLimit.year )
				canScroll = true;
			else if (year == this.HiLimit.year && month < this.HiLimit.month )
				canScroll = true;
		}
		if (!canScroll)
			return;

		if (month <= 11)
			month++;
		else
		{
			month = 1;
			year++;
		}

	}
	else
	{
		if (this.LowLimit == undefined && year > 2000) /*cos the code won't work before year 2000*/
			canScroll = true;
		else
		{
			if (year > this.LowLimit.year )
				canScroll = true;
			else if (year == this.LowLimit.year && month > this.LowLimit.month )
				canScroll = true;
		}
		if (!canScroll)
			return;
		if (this.LowLimit != undefined)
		{
			if (year == this.LowLimit.year )
			{
				if (month > this.LowLimit.month)
					month--;
			}
			else
			{
				if (month > 1 )
					month--;
				else
				{
					month = 12;
					year--;
				}
			}
		}
		else
		{
			if (month > 1 )
				month--;
			else
			{
				month = 12;
				year--;
			}
		}
	}
	this._date.month = month;
	this._date.year = year;
	this.sqDraw();
}

function sqCalendarDisplay_sqScrollTo(month, year)
{
	this._date.month = month;
	this._date.year = year;
	this.sqDraw();
}


function sqCalendarDisplay_Click(_day, _allow_expired, start_end)
{
	if ((_day < this.first_of_month)||(_day >= this.days_in_month+this.first_of_month))
		return false;
	if (this.sqIsDateSelectable(parseInt(_day, 10)+1, this._date.month, this._date.year) == false)
		return false;
	this.selectedYear = this._date.year;
	this.selectedMonth = this._date.month;
	this.selectedDay = this.sqID_to_Date(_day);
	this.sqDraw();
	this.visible = false;
	return true;
}

function sqCalendarDisplay_sqSetSelected(_day, _month, _year)
{
	this.selectedYear = _year;
	this.selectedMonth = _month;
	this.selectedDay = _day;
	this.sqDraw();
}

function sqCalendarDisplay_sqClearSelected()
{
	this.selectedYear = -1;
	this.selectedMonth = -1;
	this.selectedDay = -1;
	this.sqDraw();
}


function sqCalendarDisplay_sqGetHumanDateString()
{
	return(this.selectedDay + "/" +  this.selectedMonth  + "/" +  this.selectedYear);
}

function sqCalendarDisplay_sqGetSQLDateString()
{
	var d = new String(this.selectedDay);
	var m = new String(this.selectedMonth);
	var y = new String(this.selectedYear);
	if (d.length < 2)
		d = "0" + d;
	if (m.length < 2)
		m = "0" + m;
	return(y + "-" + m + "-" + d );
}

function sqCalendarDisplay_sqSetTodayIfNotSet(d,m,y)
{
	var d = this.selectedDay;
	var m = this.selectedMonth;
	if (d.length < 2)
		d = "0" + d;
	if (m.length < 2)
		m = "0" + m;
	return(this.selectedYear + "-" + m + "-" + d );
}


	


function start_day_plus()
{
	start_calendar.sqScroll(1);
}

function start_day_minus()
{
	start_calendar.sqScroll(-1);
}


function end_day_minus()
{
	end_calendar.sqScroll(-1);
}


function end_day_plus()
{
	end_calendar.sqScroll(+1);
}


function first_of_month(month, year)
{
/*1st jan 2000 is saturday*/
	var anchor = new SB_Date();
	anchor.fromDMY(1, 1, 2000);
	var test = new SB_Date();
	test.fromDMY(1, month, year);
	var diff = 	test.to_int() - anchor.to_int();
	/*add 6 (offset from sat 1.1.2000) & modulo 7*/
	return ((diff +6)%7);
}


function days_per_month(month, year)
{
	/*30 days september, april june nov = 4 6 9 11*/
	if (month == 4 || month == 6 || month == 9 || month == 11)
		return 30;
	else if (month == 2) /*feb*/
	{
		if ((year % 4) == 0 )
			return 29;
		else
			return 28;
	}
	return 31;
}

function increment_date(_date)
{
	var day = _date.day;
	var month = _date.month;
	var year = _date.year;

	day++;
	if (day > days_per_month(month, year))
	{
		day = 1; 
		month++;
		if (month > 12)
		{
			month = 1;
			year++;
		}
	}
	var _ret = new SB_Date();
	_ret.fromDMY(day, month, year);
	return _ret;
}

function decrement_date(_date)
{
	var day = _date.day;
	var month = _date.month;
	var year = _date.year;


	if (day > 1 )
	{
		day--;
	}
	else
	{
		if (month > 1 )		
		{
			month--;
			day = days_per_month(month, year);
		}
		else
		{
			year--;
			month = 12; 
			day = 31; /*31 days cos its december*/
		}
	}

	var _ret = new SB_Date();
	_ret.fromDMY(day, month, year);
	return _ret;
}


