/*

These are the values you can change to affect the look and feel of the calendar window

	START
*/
var calendarTitle = "specialist insurance quotations";
var fontface = "Arial";
var fontsize = 9;
var bgColour = "#D6D3CE";
var weekendColor = "#DDDDDD";
var calendarColour = "#FFFFFF";
var dateColour = "#000000";
var dateColourHover = "#FF0000";
var dayBGColour = "#848284";
var dayColour = "#FFFFFF";
var dayColourHover = "#FFFFFF";
var calendarHeight = 180;
var calendarWidth = 270;
/*
	END

These are the values you can change to affect the look and feel of the calendar window

*/
function JulianDays(str) 
{
	workDate = new Date(str);
	nday = workDate.getDate();
	nmonth = workDate.getMonth()+1;
	nyear = workDate.getFullYear();

	ggg = 1;
	if(nyear <= 1585) 
	{
		ggg = 0;
	}
	jd = -1 * Math.floor(7 * (Math.floor((nmonth + 9) / 12) + nyear) / 4);
	sss = 1;
	if((nmonth - 9) < 0) 
	{
		sss = -1;
	}
	aaa = Math.abs(nmonth - 9);
	j1 = Math.floor(nyear + sss * Math.floor(aaa / 7));
	j1 = -1 * Math.floor((Math.floor(j1 / 100) + 1) * 3 / 4);
	jd = jd + Math.floor(275 * nmonth / 9) + nday + (ggg * j1);
	jd = jd + 1721027 + 2 * ggg + 367 * nyear - 0.5;

	return Math.floor(jd);
}



function calcAge(dob, dater)
{
	//alert("calcAge(" + dob + ", " + dater + ")");
	givenDate = dob.getDate();
	givenMonth = dob.getMonth();
	givenYear = dob.getFullYear();

	thisDate = dater.getDate();
	thisMonth = dater.getMonth();
	thisYear = dater.getFullYear();

	age = thisYear-givenYear;
	if(givenYear != thisYear)
	{
		if(givenMonth > thisMonth)
		{
			age--;
		}
		else
		if(givenMonth == thisMonth)
		{
			if(givenDate > thisDate)
			{
				age--;
			}
		}
	}

	//alert("givenDate = " + givenDate + "\ngivenMonth = " + givenMonth + "\ngivenYear = " + givenYear + "\n\nthisDate = " + thisDate + "\nthisMonth = " + thisMonth + "\nthisYear = " + thisYear + "\n\nage = " + age);
	return age;
}



var calendarStatus = 0;
var weekend = [0,6];
var gNow = new Date();
var ggWinCal;

Calendar.Months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];

function getMonthNum(monthName)
{
	monthNum = 0;
	for(a = 1; a < 12; a++)
	{
		if(Calendar.Months[a] == monthName)
		{
			monthNum = a;
		}
	}
	return monthNum;
}

// Non-Leap year Month days..
DOMonth = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
// Leap year Month days..
lDOMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];


function Calendar(p_item, p_WinCal, p_month, p_year, s_currentDateString)
{
	if ((p_month == null) && (p_year == null))	return;

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;
	
	if (p_month == null) 
	{
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} 
	else 
	{
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gReturnItem = p_item;

	this.currentDateString = s_currentDateString;
}


Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;
Calendar.print = Calendar_print;


function Calendar_get_month(monthNo) 
{
	return Calendar.Months[monthNo];
}


function getDaysOfMonth(monthNo, p_year) 
{
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) 
	{
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
		{
			return DOMonth[monthNo];
		}
		else
		{
			return lDOMonth[monthNo];
		}
	} 
	else
	{
		return DOMonth[monthNo];
	}
}

function validateLongDate(days, _dd, _mm, _yyyy)
{
	if(days == 1 && _dd.value == "")
	{
		//_dd.focus();
		return;
	}
	if(_mm.value == "")
	{
		//_mm.focus();
		return;
	}
	if(_yyyy.value == "")
	{
		//_yyyy.focus();
		return;
	}
	_numDays = Math.round(_dd.value);
	_maxDays = getDaysOfMonth(_mm.selectedIndex-1, Math.round(_yyyy.options[_yyyy.selectedIndex].value));
	if(_numDays > _maxDays)
	{
		_dd.value = _maxDays;
	}
}

function addYearItem(frm, v, t)
{
	if(t != "")
	{
		frm.length++;
		len = Number(frm.length)-1;
		
		frm[len].text = t;
		frm[len].value = v;

		for(a = len; a > 1; a--)
		{
			if(frm[a].text < frm[a-1].text)
			{	// Swap
				tt = frm[a].text;
				frm[a].text = frm[a-1].text;
				frm[a-1].text = tt;

				tt = frm[a].value;
				frm[a].value = frm[a-1].value;
				frm[a-1].value = tt;
			}
		}
		frm.selectedIndex = -1;
	}
}

function swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2)
{
	//alert("function swapDates(" + _dd1 + ", " + _mm1 + ", " + _yyyy1 + ", " + _h1 + ", " + _m1 + ", " + _s1 + ", " + _dd2 + ", " + _mm2 + ", " + _yyyy2 + ", " + _h2 + ", " + _m2 + ", " + _s2 + ")");
	//alert("The dates have been swapped to keep the earliest one in the first setting.");
	z = _dd1.value; _dd1.value = _dd2.value; _dd2.value = z;
	z = _mm1.selectedIndex; _mm1.selectedIndex = _mm2.selectedIndex; _mm2.selectedIndex = z;
		
	// Setup YYYY1
	y1Val = _yyyy1.value;
	y2Val = _yyyy2.value;
	
	gotcha = 0;
	for(a = 1; a < _yyyy1.length; a++)
	{
		//alert("yyyy1 check " + _yyyy1[a].value);
		if(y2Val == _yyyy1[a].value)
		{
			gotcha = a;
		}
	}
	if(gotcha == 0)
	{
		addYearItem(_yyyy1, y2Val, y2Val);
		//alert("addYearItem(_yyyy1, _yyyy2.value, _yyyy2.value)\naddYearItem(" + _yyyy1 + ", " + y2Val + ", " + y2Val + ")");
	}

	// Setup YYYY2
	gotcha = 0;
	for(a = 1; a < _yyyy2.length; a++)
	{
		//alert("yyyy2 check " + _yyyy2[a].value);
		if(y1Val == _yyyy2[a].value)
		{
			gotcha = a;
		}
	}
	if(gotcha == 0)
	{
		addYearItem(_yyyy2, y1Val, y1Val);
		//alert("addYearItem(_yyyy2, _yyyy1.value, _yyyy1.value)\naddYearItem(" + _yyyy2 + ", " + y1Val + ", " + y1Val + ")");
	}

	_yyyy1.value = y2Val; _yyyy2.value = y1Val;
	z = _h1.value; _h1.value = _h2.value; _h2.value = z;
	z = _m1.value; _m1.value = _m2.value; _m2.value = z;
	z = _s1.value; _s1.value = _s2.value; _s2.value = z;
}

function sortDates(fuller, _dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2, clock)
{
	if(_yyyy1.value < _yyyy2.value)
	{
		// year 1 is less, so date MUST be OK
	}
	else
	if(_yyyy1.value == _yyyy2.value)
	{
		// years are equal, so check month		
		if(_mm1.selectedIndex < _mm2.selectedIndex)
		{
			// month 1 is less, so date MUST be OK
		}
		else
		if(_mm1.selectedIndex == _mm2.selectedIndex)
		{
			// months are equal, so check days
			if(fuller == 1)
			{
				if(_dd1.value < _dd2.value)
				{
					// day 1 is less, so date MUST be OK
				}
				else
				if(_dd1.value == _dd2.value)
				{
					if(clock == 1)
					{
						// days are equal, so check hours
						if(_h1.value < _h2.value)
						{
							// hour 1 is less, so date MUST be OK
						}
						else
						if(_h1.value == _h2.value)
						{
							// hours are equal, so check minutes
							if(_m1.value < _m2.value)
							{
								// minute 1 is less, so date MUST be OK
							}
							else
							if(_m1.value == _m2.value)
							{
								// minutes are equal, so check seconds
								if(_s1.value <= _s2.value)
								{
									// seconds 1 is less or equal, so date MUST be OK
								}
								else
								{
									// seconds 2 is less than seconds 1, swap
									swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
									return true;
								}
							}
							else
							{
								// minute 2 is less than minute 1, swap
								swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
								return true;
							}
						}
						else
						{
							// hour 2 is less than hour 1, swap
							swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
							return true;
						}
					}
					else
					{
						_h1.value = "00";
						_m1.value = "00";
						_s1.value = "00";
						_h2.value = "00";
						_m2.value = "00";
						_s2.value = "00";
						return true;
					}
				}
				else
				{
					// day 2 is less than day 1, swap
					swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
					return true;
				}
			}
			else
			{
				_dd1.value = "01";
				_dd2.value = "01";
				if(clock == 1)
				{
					// days are equal, so check hours
					if(_h1.value < _h2.value)
					{
						// hour 1 is less, so date MUST be OK
					}
					else
					if(_h1.value == _h2.value)
					{
						// hours are equal, so check minutes
						if(_m1.value < _m2.value)
						{
							// minute 1 is less, so date MUST be OK
						}
						else
						if(_m1.value == _m2.value)
						{
							// minutes are equal, so check seconds
							if(_s1.value <= _s2.value)
							{
								// seconds 1 is less or equal, so date MUST be OK
							}
							else
							{
								// seconds 2 is less than seconds 1, swap
								swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
								return true;
							}
						}
						else
						{
							// minute 2 is less than minute 1, swap
							swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
							return true;
						}
					}
					else
					{
						// hour 2 is less than hour 1, swap
						swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
						return true;
					}
				}
				else
				{
					_h1.value = "00";
					_m1.value = "00";
					_s1.value = "00";
					_h2.value = "00";
					_m2.value = "00";
					_s2.value = "00";
					return true;
				}
			}
		}
		else
		{
			// month 2 is less than month 1, swap
			if(fuller == 0)
			{
				_dd1.value = "01";
				_dd2.value = "01";
			}
			if(clock == 0)
			{
				_h1.value = "00";
				_m1.value = "00";
				_s1.value = "00";
				_h2.value = "00";
				_m2.value = "00";
				_s2.value = "00";
			}
			swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
			return true;
		}
	}
	else
	{
		if(fuller == 0)
		{
			_dd1.value = "01";
			_dd2.value = "01";
		}
		if(clock == 0)
		{
			_h1.value = "00";
			_m1.value = "00";
			_s1.value = "00";
			_h2.value = "00";
			_m2.value = "00";
			_s2.value = "00";
		}
		// year 2 is less than year 1, swap
		swapDates(_dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2);
		return true;
	}
	return false;
}

function dateSort(days, d1, d2, item, clock)
{
	eval("_dd1 = document.forms.form."+d1+"['N-"+item+"_dd'];");
	eval("_mm1 = document.forms.form."+d1+"['N-"+item+"_mm'];");
	eval("_yyyy1 = document.forms.form."+d1+"['N-"+item+"_yyyy'];");
	eval("_h1 = document.forms.form."+d1+"['N-"+item+"_h'];");
	eval("_m1 = document.forms.form."+d1+"['N-"+item+"_m'];");
	eval("_s1 = document.forms.form."+d1+"['N-"+item+"_s'];");
	eval("_dd2 = document.forms.form."+d2+"['N-"+item+"_dd'];");
	eval("_mm2 = document.forms.form."+d2+"['N-"+item+"_mm'];");
	eval("_yyyy2 = document.forms.form."+d2+"['N-"+item+"_yyyy'];");
	eval("_h2 = document.forms.form."+d2+"['N-"+item+"_h'];");
	eval("_m2 = document.forms.form."+d2+"['N-"+item+"_m'];");
	eval("_s2 = document.forms.form."+d2+"['N-"+item+"_s'];");
	sortDates(days, _dd1, _mm1, _yyyy1, _h1, _m1, _s1, _dd2, _mm2, _yyyy2, _h2, _m2, _s2, clock);
}

function Calendar_get_daysofmonth(monthNo, p_year) 
{
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) 
	{
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
		{
			return Calendar.DOMonth[monthNo];
		}
		else
		{
			return Calendar.lDOMonth[monthNo];
		}
	} 
	else
	{
		return Calendar.DOMonth[monthNo];
	}
}


function Calendar_calc_month_year(p_Month, p_Year, incr)
{
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) 
	{
		// B A C K W A R D
		if (p_Month == 0) 
		{
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else 
		{
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	return ret_arr;
}


function Calendar_print() 
{
	ggWinCal.print();
}


function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();


Calendar.prototype.getMonthlyCalendarCode = function() 
{
	// Begin Table Drawing code here..
	this.cal_header();
	this.cal_data();
	this.wwrite("</TABLE>");
	return;
}


Calendar.prototype.show = function() 
{
	this.gWinCal.document.open();

	// Setup the page...
	this.wwrite("<HTML>");
	this.wwrite("<HEAD><TITLE>"+calendarTitle+"</TITLE>");
	this.wwrite("</HEAD>");
	this.wwrite("<STYLE>");
	
	this.wwrite("BODY");
	this.wwrite("{");
	this.wwrite("	background-color:"+bgColour+";");
	this.wwrite("}");
	
	this.wwrite("TD");
	this.wwrite("{");
	this.wwrite("	font-family:"+fontface+";");
	this.wwrite("	font-size:"+fontsize+"pt;");
	this.wwrite("	text-align:center;");
	this.wwrite("	height:"+(fontsize*1.5)+"pt;");
	this.wwrite("}");
	
	this.wwrite("SELECT, OPTION");
	this.wwrite("{");
	this.wwrite("	color:"+dateColour+";");
	this.wwrite("	background-color:"+calendarColour+";");
	this.wwrite("	font-family:"+fontface+";");
	this.wwrite("	font-size:"+fontsize+"pt;");
	this.wwrite("	text-align:center;");
	this.wwrite("}");
	
	this.wwrite("TD.days");
	this.wwrite("{");
	this.wwrite("	background-color:"+dayBGColour+";");
	this.wwrite("	color:"+dayColour+";");
	this.wwrite("	font-weight:bold;");
	this.wwrite("}");

	this.wwrite("A.navi:link, A.navi:active, A.navi:visited");
	this.wwrite("{");
	this.wwrite("	color:"+dateColour+";");
	this.wwrite("	text-decoration:none;");
	this.wwrite("}");
	this.wwrite("A.navi:hover");
	this.wwrite("{");
	this.wwrite("	color:"+dateColourHover+";");
	this.wwrite("	text-decoration:none;");
	this.wwrite("	font-weight:bold;");
	this.wwrite("}");
	
	this.wwrite("A.current:link, A.current:active, A.current:visited");
	this.wwrite("{");
	this.wwrite("	color:"+dayColour+";");
	this.wwrite("	text-decoration:none;");
	this.wwrite("}");
	this.wwrite("A.current:hover");
	this.wwrite("{");
	this.wwrite("	color:"+dayColourHover+";");
	this.wwrite("	text-decoration:none;");
	this.wwrite("	font-weight:bold;");
	this.wwrite("}");
	
	this.wwrite("</STYLE>");

	this.wwrite("<BODY TOPMARGIN=0 LEFTMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>");
	this.wwrite("<TABLE align='center' WIDTH='100%' HEIGHT='100%' BORDER=0 CELLSPACING=0 CELLPADDING=0>");
	this.wwrite("<TR valign='center'>");
	this.wwrite("<TD align='center'>");

	// Show navigation buttons
	
	this.wwriteA("<SCR")
	this.wwrite("IPT>");
	
	this.wwrite("function goBaby()");
	this.wwrite("{");
	this.wwriteA("	window.opener.Build(");
	this.wwriteA("'"+this.gReturnItem+"',");
	this.wwriteA("document.forms.form.myMonth.selectedIndex+'',");
	this.wwriteA("'"+this.gYear+"',");
	this.wwriteA("'"+this.currentDateString+"');");
	this.wwrite("}");

	this.wwrite("function goDaddy()");
	this.wwrite("{");
	this.wwriteA("	window.opener.Build(");
	this.wwriteA("'"+this.gReturnItem+"',");
	this.wwriteA("'"+this.gMonth+"',");
	this.wwriteA("document.forms.form.myYear[document.forms.form.myYear.selectedIndex].value,");
	this.wwriteA("'"+this.currentDateString+"');");
	this.wwrite("}");
	
	this.wwriteA("</SCR")
	this.wwrite("IPT>");

	this.wwrite("<TABLE align='center' BORDER=1 BGCOLOR=\""+calendarColour+"\" CELLSPACING=0>");
	this.wwrite("<FORM NAME='form' ACTION='#' METHOD='POST'>");
	this.wwrite("<TR>");
	this.wwrite("	<TD colspan='4' align='right'><SELECT NAME='myMonth' ONCHANGE='javascript:goBaby();' STYLE='width:100%;'>");
	for(myMonth = 0; myMonth < 12; myMonth++)
	{
		this.wwriteA("<OPTION");
		if(myMonth == this.gMonth)
		{
			this.wwriteA(" selected");
		}
		this.wwrite(">  "+returnMonthName(myMonth)+"  </OPTION>");
	}
	this.wwrite("</SELECT></TD>");

	this.wwrite("	<TD colspan='3'><SELECT NAME='myYear' ONCHANGE='javascript:goDaddy();' STYLE='width:100%;'>");
	thisYear = Number(this.gYear);

	for(myYear = (thisYear-100); myYear < (thisYear+10); myYear++)
	{
		this.wwriteA("<OPTION VALUE='"+myYear+"'");
		if(myYear == thisYear)
		{
			this.wwriteA(" selected");
		}
		this.wwrite(">  "+myYear+"  </OPTION>");
	}
	this.wwrite("</SELECT></TD>");
	this.wwrite("</TR>");
	this.wwrite("</FORM>");

	// Get the complete calendar code for the month..
	this.getMonthlyCalendarCode();
	
	this.wwrite("</TD>");
	this.wwrite("</TR>");
	this.wwrite("</TABLE>");
	this.wwrite("</BODY>");
	this.wwrite("</HTML>");
	this.gWinCal.document.close();
}


Calendar.prototype.wwrite = function(wtext) 
{
	this.gWinCal.document.writeln(wtext);
}


Calendar.prototype.wwriteA = function(wtext) 
{
	this.gWinCal.document.write(wtext);
}


Calendar.prototype.cal_header = function() 
{
	this.wwrite("<TR bgcolor='"+dayBGColour+"'>");
	this.wwrite("<TD WIDTH='14%' CLASS='days'>Sun</TD>");
	this.wwrite("<TD WIDTH='14%' CLASS='days'>Mon</TD>");
	this.wwrite("<TD WIDTH='14%' CLASS='days'>Tue</TD>");
	this.wwrite("<TD WIDTH='14%' CLASS='days'>Wed</TD>");
	this.wwrite("<TD WIDTH='14%' CLASS='days'>Thu</TD>");
	this.wwrite("<TD WIDTH='14%' CLASS='days'>Fri</TD>");
	this.wwrite("<TD WIDTH='16%' CLASS='days'>Sat</TD>");
	this.wwrite("</TR>");
	return;
}

Calendar.prototype.cal_data = function() 
{
	var i = 0;
	var j = 0;
	var k = 0;
	var m = 0;
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay = vDate.getDay();
	var vDay = 1;
	var vLastDay = Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay = 0;

	if(this.currentDateString != "")
	{
		this.gotDate = true;
		this.dater = this.currentDateString.split(" ");
		this.dater[0] = Number(this.dater[0]);
		this.dater[1] = returnMonthNumber(this.dater[1])+'';
		this.dater[2] = Number(this.dater[2]);
	}
	else
	{
		this.gotDate = false;
	}

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/

	this.wwrite("<TR>");
	for(i = 0; i < vFirstDay; i++) 
	{
		this.wwriteA("	<TD WIDTH='14%'");
		if(this.write_weekend_string(i))
		{
			this.wwriteA(" BGCOLOR=\""+weekendColor+"\"");
		}
		this.wwrite("> </TD>");
	}
	// Write rest of the 1st week
	for (j = vFirstDay; j < 7; j++) 
	{
		this.format_day(vDay, j);
//		this.format_data(vDay);
		vDay=vDay + 1;
	}
	this.wwrite("</TR>");


	// Write the rest of the weeks
	for (k = 2; k < 7; k++) 
	{
		this.wwrite("<TR>");
		for(j = 0; j < 7; j++) 
		{
			this.format_day(vDay, j);
			vDay = vDay + 1;
			if(vDay > vLastDay) 
			{
				vOnLastDay = 1;
				break;
			}
		}

		if(j == 6)
		{
			this.wwrite("</TR>");
		}
		if (vOnLastDay == 1)
		{
			break;
		}
	}
	
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m = 1; m < (7-j); m++) 
	{
		if (this.gYearly)
		{
			this.wwrite("	<TD WIDTH='14%'");
			if(this.write_weekend_string(j+m))
			{
				this.wwriteA(" BGCOLOR=\""+weekendColor+"\"");
			}
			this.wwrite("> </TD>");
		}
		else
		{
			this.wwrite("	<TD WIDTH='14%'");
			if(this.write_weekend_string(j+m))
			{
				this.wwriteA(" BGCOLOR=\""+weekendColor+"\"");
			}
			this.wwrite("> </TD>");
		}
	}
	
	return;
}



Calendar.prototype.format_day = function(vday, n) 
{
	var currentDay = false;

	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();

	this.wwriteA("<TD WIDTH='14%' align='center'");
	if(vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
	{
		this.wwriteA("BGCOLOR=\""+dayBGColour+"\" COLOR="+calendarColour+"\"");
		currentDay = true;
	}
	else
	if(this.write_weekend_string(n))
	{
		this.wwriteA(" BGCOLOR=\""+weekendColor+"\"");
	}
	else
	{
		this.wwriteA("COLOR="+dayBGColour+"\"");
	}
	this.wwriteA(">");
	formattedDay = this.format_data(vday);
	this.wwriteA("<A HREF=\"\" ONCLICK=\"javascript:self.opener.document.forms."+this.gReturnItem+".value='"+formattedDay+"';window.close();\" ONMOUSEOVER=\"window.status='"+formattedDay+"';return true;\" ONMOUSEOUT=\"window.status='';return true;\" CLASS='");
	if(currentDay)
	{
		this.wwriteA("current");
	}
	else
	{
		this.wwriteA("navi");
	}
	this.wwriteA("'>");
	if(this.gotDate && vday == this.dater[0] && this.gMonth == this.dater[1] && this.gYear == this.dater[2])
	{
		this.wwriteA("<FONT COLOR='"+dateColourHover+"'><B>");
		found = true;
	}
	else
	{
		found = false;
	}
	this.wwriteA(vday);
	if(found)
	{
		this.wwriteA("</B></FONT>");
		found = false;
	}
	this.wwriteA("</A>");
	this.wwriteA("</TD>");
	return;
}



Calendar.prototype.write_weekend_string = function(vday) 
{
	var iii;
	for (iii = 0; iii < weekend.length; iii++) 
	{
		if(vday == weekend[iii])
		{
			return true;
		}
	}
	return false;
}


function returnMonthName(n)
{
	m = "";
	switch (n) 
	{
		case 0 :
			m = "January";
			break;
		case 1 :
			m = "February";
			break;
		case 2 :
			m = "March";
			break;
		case 3 :
			m = "April";
			break;
		case 4 :
			m = "May";
			break;
		case 5 :
			m = "June";
			break;
		case 6 :
			m = "July";
			break;
		case 7 :
			m = "August";
			break;
		case 8 :
			m = "September";
			break;
		case 9 :
			m = "October";
			break;
		case 10 :
			m = "November";
			break;
		case 11 :
			m = "December";
			break;
		default :
			m = "Unknown";
	}
	return m;
}


function returnMonthNumber(n)
{
	m = 0;
	switch (n) 
	{
		case "January" :
			m = 0;
			break;
		case "February" :
			m = 1;
			break;
		case "March" :
			m = 2;
			break;
		case "April" :
			m = 3;
			break;
		case "May" :
			m = 4;
			break;
		case "June" :
			m = 5;
			break;
		case "July" :
			m = 6;
			break;
		case "August" :
			m = 7;
			break;
		case "September" :
			m = 8;
			break;
		case "October" :
			m = 9;
			break;
		case "November" :
			m = 10;
			break;
		case "December" :
			m = 11;
			break;
		default :
			m = 0;
	}
	return m;
}

Calendar.prototype.format_data = function(p_day) 
{
	var vMonth = Number(this.gMonth);
	var vY4 = new String(this.gYear);
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	m = returnMonthName(vMonth);
	return vDD+" "+m+" "+vY4;
}


function Build(p_item, p_month, p_year, s_currentDateString) 
{
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year, s_currentDateString);
	// Choose appropriate show function
	if (gCal.gYearly)	
	{
		gCal.showY();
	}
	else
	{
		gCal.show();
	}
}


function show_calendar() 
{
	p_item = arguments[0];
	p_month = new String(gNow.getMonth());
	p_year = new String(gNow.getFullYear().toString());

	eval("currentDateString = document.forms."+p_item+".value;");

	if(currentDateString != "" && currentDateString != "click icon on right >" && currentDateString != "click icon on left")
	{
		// Get the current value if they have kindly sent it
		dater = currentDateString.split(" ");
		p_month = returnMonthNumber(dater[1])+'';
		p_year = Number(dater[2]);
	}

	vWinCal = window.open("","Calendar","width="+calendarWidth+",height="+calendarHeight+",status="+calendarStatus+",resizable="+calendarStatus+",top=200,left=200");
	vWinCal.opener = self;
	ggWinCal = vWinCal;

	Build(p_item, p_month, p_year, currentDateString);
}


/*
Yearly Calendar Code Starts here
*/
function show_yearly_calendar(p_item, p_year) {
	// Load the defaults..
	if (p_year == null || p_year == "")
	{
		p_year = new String(gNow.getFullYear().toString());
	}

	var vWinCal = window.open("", "Calendar", "scrollbars=yes");
	vWinCal.opener = self;
	ggWinCal = vWinCal;

	Build(p_item, null, p_year);
}

function setDate(toVal, item, yyyy, mm, dd, h, m, s)
{
	eval(toVal + "['N-"+item+"_s'].selectedIndex = " + (s+1) + ";");
	eval(toVal + "['N-"+item+"_m'].selectedIndex = " + (m+1) + ";");
	eval(toVal + "['N-"+item+"_h'].selectedIndex = " + (h+1) + ";");
	eval(toVal + "['N-"+item+"_dd'].selectedIndex = " + (dd+1) + ";");
	eval(toVal + "['N-"+item+"_mm'].selectedIndex = " + (mm+1) + ";");
	
	// Setup new yyyy
	eval("tt = " + toVal + "['N-"+item+"_yyyy'];");
	gotcha = 0;
	for(a = 1; a < tt.length; a++)
	{
		if(yyyy == tt[a].value)
		{
			gotcha = a;
		}
	}
	if(gotcha == 0)
	{
		addYearItem(tt, yyyy, yyyy)
	}
	eval(toVal + "['N-"+item+"_yyyy'].value = " + (yyyy) + ";");	
	return dateCheck(toVal, item);
}

function copyDate(fromVal, toVal)
{
	eval(toVal + "_s.value = " + fromVal + "_s.value;");
	eval(toVal + "_m.value = " + fromVal + "_m.value;");
	eval(toVal + "_h.value = " + fromVal + "_h.value;");
	eval(toVal + "_dd.value = " + fromVal + "_dd.value;");
	eval(toVal + "_mm.value = " + fromVal + "_mm.value;");
	eval(toVal + "_yyyy.value = " + fromVal + "_yyyy.value;");
}
function zeroDate(toVal)
{
	eval(toVal + "_s.selectedIndex = 0;");
	eval(toVal + "_m.selectedIndex = 0;");
	eval(toVal + "_h.selectedIndex = 0;");
	eval(toVal + "_dd.selectedIndex = 0;");
	eval(toVal + "_mm.selectedIndex = 0;");
	eval(toVal + "_yyyy.selectedIndex = 0;");
}

function blankDate(val, item, gDay, gMonth, gYear)
{
	//window.status = val;
	if(gMonth == 0)
	{
		//eval(val+"['N-" + item + "_mm'].focus();");
		return true;
	}
	if(gDay == 0)
	{
		//eval(val+"['N-" + item + "_dd'].focus();");
		return true;
	}
	if(gYear == "")
	{
		//eval(val+"['N-" + item + "_yyyy'].focus();");
		return true;
	}
	return false;
}

function blankTime(val, item, gH, gM, gS)
{
	if(gH == 0)
	{
		//eval(val+"['N-" + item + "_h'].focus();");
		return true;
	}
	if(gM == 0)
	{
		//eval(val+"['N-" + item + "_m'].focus();");
		return true;
	}
	if(gS == "")
	{
		//eval(val+"['N-" + item + "_s'].focus();");
		return true;
	}
	return false;
}

function forcePastDate(val, item, nme)
{
	// ensure the date isn't in the past
	// Order of check = year, month, date
	eval("gMonth = "+val+"['N-" + item + "_mm'].selectedIndex;");
	eval("gDay = "+val+"['N-" + item + "_dd'].selectedIndex;");
	eval("gYear = "+val+"['N-" + item + "_yyyy'].value;");
	if(blankDate(val, item, gDay, gMonth, gYear))
	{
		return false;
	}
	if(gYear > forceYear)
	{
		// Later = Error
		//alert("You cannot post-date the "+nme);
		//eval(val+"['N-" + item + "_yyyy'].focus();");
		return false;
	}
	else
	if(gYear == forceYear)
	{
		// Need to check Month
		gMonth--;
		if(gMonth > forceMonth)
		{
			// Later = Error
			//alert("You cannot post-date the "+nme);
			//eval(val+"['N-" + item + "_mm'].focus();");
			return false;
		}
		else
		if(gMonth == forceMonth)
		{
			// Need to check day
			gDay--;
			if(gDay > forceDay)
			{
				// Later = Error
				//alert("You cannot post-date the "+nme);
				//eval(val+"['N-" + item + "_dd'].focus();");
				return false;
			}
		}
	}
	return true;
}


function forceFutureDate(val, item, nme)
{
	// ensure the date isn't in the past
	// Order of check = year, month, date
	eval("gMonth = "+val+"['N-" + item + "_mm'].selectedIndex;");
	eval("gDay = "+val+"['N-" + item + "_dd'].selectedIndex;");
	eval("gYear = "+val+"['N-" + item + "_yyyy'].value;");
	if(blankDate(val, item, gDay, gMonth, gYear))
	{
		return false;
	}
	if(gYear < forceYear)
	{
		// Early = Error
		//alert("You cannot back-date the "+nme);
		//eval(val+"['N-" + item + "_yyyy'].focus();");
		return false;
	}
	else
	if(gYear == forceYear)
	{
		// Need to check Month
		gMonth--;
		if(gMonth < forceMonth)
		{
			// Early = Error
			//alert("You cannot back-date the " + nme);
			//eval(val+"['N-" + item + "_mm'].focus();");
			return false;
		}
		else
		if(gMonth == forceMonth)
		{
			// Need to check day
			gDay--;
			if(gDay < forceDay)
			{
				// Early = Error
				//alert("You cannot back-date the " + nme);
				//eval(val+"['N-" + item + "_dd'].focus();");
				return false;
			}
		}
	}
	return true;
}


function dateBuilder(val, item)
{
	eval("gMonth = "+val+"['N-" + item + "_mm'].selectedIndex;");
	eval("gDay = "+val+"['N-" + item + "_dd'].selectedIndex;");
	eval("gYear = "+val+"['N-" + item + "_yyyy'].value;");
	if(blankDate(val, item, gDay, gMonth, gYear))
	{
		eval(val+"['N-" + item + ".'].value = '';");
		return false;
	}

	eval("gBuild = "+val+"['N-" + item + "_build'].value;");
	if(gBuild == "0")
	{
		// Standard "DD MonthName YYYY"
		eval(val+"['N-" + item + ".'].value = "+val+"['N-" + item + "_dd'].value + ' ' + "+val+"['N-" + item + "_mm'].options["+gMonth+"].value + ' ' + "+gYear+";");
	}
	else
	{
		// SQL "YYYY-MM-DD"
		eval(val+"['N-" + item + ".'].value = "+gYear+" + '-' + "+val+"['N-" + item + "_mm'].options["+gMonth+"].value + '-' + "+val+"['N-" + item + "_dd'].value;");
	}
	return true;
}

function dateCheck(val, item)
{
	eval(val+"['N-" + item + ".'].value = '';");
	eval("gClock = "+val+"['N-" + item + "_clock'].value;");
	eval("gDays = "+val+"['N-" + item + "_days'].value;");
	if(gClock == "1")
	{
		// Include Time too
		if(!dateBuilder(val, item))return false;
		eval("gH = "+val+"['N-" + item + "_h'].selectedIndex;");
		eval("gM = "+val+"['N-" + item + "_m'].selectedIndex;");
		eval("gS = "+val+"['N-" + item + "_s'].selectedIndex;");
		if(blankTime(val, item, gH, gM, gS))
		{
			return false;
		}
		eval(val+"['N-" + item + ".'].value += ' ' + "+val+"['N-" + item + "_h'].options["+gH+"].value + ':' + "+val+"['N-" + item + "_m'].options["+gM+"].value + ':' + "+val+"['N-" + item + "_s'].options["+gS+"].value;");
	}
	else
	if(gDays == "1")
	{
		// Just Date
		if(!dateBuilder(val, item))
		{
			return false;
		}
	}
	else
	{
		eval("gMonth = "+val+"['N-" + item + "_mm'].selectedIndex;");
		eval("gYear = "+val+"['N-" + item + "_yyyy'].value;");
		if(blankDate(val, item, "01", gMonth, gYear))
		{
			eval(val+"['N-" + item + ".'].value = ''");
			return false;
		}
		eval(val+"['N-" + item + ".'].value = '01 '+"+val+"['N-" + item + "_mm'].options["+gMonth+"].value + ' ' + "+gYear+";");
	}
	//eval("gDate = "+val+"['N-" + item + ".'].value;");
	//alert("dateCheck End, date = " + gDate);
	//return false;
	return true;
	//windowStat(gDate);
}