// Class Calendar
var CalendarInstanceCount = 0;
var CalendarInstance = new Array();

function Calendar()
{
	CalendarInstanceCount++;
	this.CalendarID = CalendarInstanceCount;
	CalendarInstance[this.CalendarID] = this;

	this.Input;
	this.DateAndTime = true;

	this.YearControl;
	this.MonthControl;
	this.HourControl;
	this.MinuteControl;
	this.TimeTypeControl;

	var Now = new Date();
	this.CurrentDate = Now.getDate();
	this.CurrentMonth = Now.getMonth();
	this.CurrentYear = Now.getFullYear();

	this.TodayStr = this.CurrentDate + "/" + (this.CurrentMonth + 1) + "/" + this.CurrentYear;

	this.DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
}

Calendar.prototype.Initialise = function()
{
		var src = "<div id='Calendar" + this.CalendarID + "' class='DialogCalendar' style='display: none'>";
		src += "<div style='float: right'><img src='images/icon_delete.png' id='CalendarCloseBtn" + this.CalendarID + "' width='32' height='32'/></div>";
		src += "<div class='DialogPadding'>";
		src += "<div class='DialogTitle'>";
		src += "<div id='CalendarCaption" + this.CalendarID + "'>Calendar</div>";
		src += "</div>";
		src += "<div class='Padding'>";
		src += "<form name='CalendarForm" + this.CalendarID + "'>";
		src += "<div>";
		src += "<div style='float: right'>";
		src += "<input type='button' class='CalendarBtn' value=' < ' name='CalendarDownBtn" + this.CalendarID + "' onclick='CalendarInstance[" + this.CalendarID + "].SubYear()' />";
		src += "<input type='text' class='CalendarYearEdit' name='CalendarYearEdit" + this.CalendarID + "' size='4' maxlength='4' value='2001' readonly />";
		src += "<input type='button' class='CalendarBtn' value=' > ' name='CalendarUpBtn" + this.CalendarID + "' onclick='CalendarInstance[" + this.CalendarID + "].AddYear()' />";
		src += "</div>";
		src += "<select name='CalendarMonthCmb" + this.CalendarID + "' class='CalendarMonthCmb' onchange='CalendarInstance[" + this.CalendarID + "].MakeCalendar()'>";
		src += "<option value=0>January";
		src += "<option value=1>February";
		src += "<option value=2>March";
		src += "<option value=3>April";
		src += "<option value=4>May";
		src += "<option value=5>June";
		src += "<option value=6>July";
		src += "<option value=7>August";
		src += "<option value=8>September";
		src += "<option value=9>October";
		src += "<option value=10>November";
		src += "<option value=11>December";
		src += "</select>";
		src += "<div style='clear: both'></div>";
		src += "</div>";
		src += "<div align='center' class='Padding'>";
		
		if (this.DateAndTime)
		{
			src += "Time ";
			src += "<input type='text' class='CalendarTime' name='CalendarHourEdit" + this.CalendarID + "' size='3' maxlength='2'> : <input type='text' class='CalendarTime' name='CalendarMinuteEdit" + this.CalendarID + "' size='3' maxlength='2'>";
			src += "&nbsp;<select class='CalendarTime' name='CalendarTimeType" + this.CalendarID + "'>";
			src += "<option value='AM'>AM";
			src += "<option value='PM'>PM";
			src += "</select>";
		}

		src += "&nbsp;</div>";
		src += "</form>";
		src += "<div class='DialogCalendarContent' id='CalendarContent" + this.CalendarID + "'>";
		src += "</div>";
		src += "</div>";
		src += "</div>";
		src += "</div>";

		document.write(src);

		this.YearControl = eval("document.CalendarForm" + this.CalendarID + ".CalendarYearEdit" + this.CalendarID);
		this.MonthControl = eval("document.CalendarForm" + this.CalendarID + ".CalendarMonthCmb" + this.CalendarID);

		this.HourControl = eval("document.CalendarForm" + this.CalendarID + ".CalendarHourEdit" + this.CalendarID);
		this.MinuteControl = eval("document.CalendarForm" + this.CalendarID + ".CalendarMinuteEdit" + this.CalendarID);
		this.TimeTypeControl = eval("document.CalendarForm" + this.CalendarID + ".CalendarTimeType" + this.CalendarID);

		// Event
		var closeBtn = document.getElementById("CalendarCloseBtn" + this.CalendarID);
		var calendarPanel = "Calendar" + this.CalendarID;
		closeBtn.addEventListener("click", function() {
			HideBack();
			SetDisplay(calendarPanel, false);
		}, false);
}

Calendar.prototype.Show  = function(aCaption, aInput)
{
	WriteText("CalendarCaption" + this.CalendarID, aCaption);
	this.Input = aInput;

	ShowBack();
	
	SetDisplay("Calendar" + this.CalendarID, true);

	if(this.Input.value == "")
	{
		var Now = new Date();
		var LAMPM = " AM";
		var LHour = Now.getHours();

		if(LHour >= 12)
		{
			LAMPM = " PM";
			LHour = LHour - 12;
		}

		this.Input.value = Now.getDate() + "/" + (Now.getMonth() + 1) + "/" + Now.getFullYear();
		if (this.DateAndTime)
		{
			this.Input.value += " " + LHour + ":" + Now.getMinutes() + ":" + Now.getSeconds() + LAMPM;
		}
	}

	var TimeStr = "";
	var DateStr = this.Input.value;

	var DateTimeStr = DateStr.split(" ");
	var Temp = DateTimeStr[0].split("/");
	var AMPM = "AM";
	var TimeOnly = "";
	if(DateTimeStr.length > 1)
	{
		TimeStr = DateTimeStr[1];
		TimeOnly = DateTimeStr[1];
	}
	if(DateTimeStr.length > 2)
	{
		TimeStr = TimeStr+ " " + DateTimeStr[2];
		AMPM = DateTimeStr[2];
	}

	this.YearControl.value = Temp[2];
	this.MonthControl.options[Temp[1] - 1].selected = true;

	if (this.DateAndTime)
	{
		this.HourControl.value = 0;
		this.MinuteControl.value = 0;
		var TimePart = TimeOnly.split(":");
		if(TimePart.length > 0 && TimePart[0] != "")
		{
			this.HourControl.value = TimePart[0];
		}
		if(TimePart.length > 1 && TimePart[1] != "")
		{
			this.MinuteControl.value = TimePart[1];
		}
		SetValue(this.TimeTypeControl, AMPM);
	}

	this.MakeCalendar();
}

Calendar.prototype.Close = function()
{
	HideBack();
	SetDisplay("Calendar" + this.CalendarID, false);
}

Calendar.prototype.MakeCalendar = function()
{
	// Get Month and Year
	var Index = GetIndex(this.MonthControl);
	var iMonth = parseInt(this.MonthControl.options[Index].value);
	var iYear = parseInt(this.YearControl.value); 

	// find number of days in a leap year for february
	if(iMonth == 1)
	{
		if( ((iYear % 400) == 0) || ( ((iYear % 4) == 0) && ((iYear % 100) != 0) ) )
		{
			this.DaysInMonth[1] = 29;
		}
		else
		{
			this.DaysInMonth[1] = 28;
		}
	}

	var DateObj = new Date(iYear, iMonth, 1);
	var FirstDay = DateObj.getDay() + 7;
	var DayCounter = 1;
	var Counter = 1;

	var CalPage = "<table cellpadding='1' cellspacing='0' border='0' bgcolor='#000000' align='center'><tr><td>";
	CalPage += "<table cellpadding='4' cellspacing='0' border='0' bgcolor='#ffffff'>";
	CalPage += "<tr align='center'>";
	CalPage += "<td width='35'><b class='CalendarTitle'>Mon</b></td>";
	CalPage += "<td width='35'><b class='CalendarTitle'>Tue</b></td>";
	CalPage += "<td width='35'><b class='CalendarTitle'>Wed</b></td>";
	CalPage += "<td width='35'><b class='CalendarTitle'>Thu</b></td>";
	CalPage += "<td width='35'><b class='CalendarTitle'>Fri</b></td>";
	CalPage += "<td width='35'><b class='CalendarTitle'>Sat</b></td>";
	CalPage += "<td width='35'><b class='CalendarTitle'>Sun</b></td>";
	CalPage += "</tr>";
	CalPage += "<tr><td colspan='7' height='2'><hr class='CalendarLine'></td></tr>";

	for(var Row = 1; Row <= 7; Row++)
	{
		CalPage += "<tr align='center'>";
		for(var Col = 1; Col <= 7; Col++)
		{
			// Check for start of first date
			if( (Counter >= FirstDay) && (DayCounter <= this.DaysInMonth[iMonth]) )
			{
				// Show current date
				if( (DayCounter == this.CurrentDate) && (iMonth == this.CurrentMonth) && (iYear == this.CurrentYear) )
				{
					CalPage += "<td><a href='javascript:CalendarInstance[" + this.CalendarID + "].SelectedDate(" + DayCounter + ")' class='CalendarSelLink'>" + DayCounter + "</a></td>";
				}
				else
				{
					CalPage += "<td><a href='javascript:CalendarInstance[" + this.CalendarID + "].SelectedDate(" + DayCounter + ")' class='CalendarLink'>" + DayCounter + "</a></td>";
				}
				DayCounter++;
			}
			else
			{
				CalPage += "<td>&nbsp;</td>";
			}
			Counter++;
		}
		CalPage += "</tr>";
	}

	CalPage += "<tr>";
	CalPage += "<td colspan='4'><a href='javascript:CalendarInstance[" + this.CalendarID + "].SetToday()' class='CalendarLink'><font color='red'>Today:&nbsp;</font>" + this.TodayStr + "</a></td>";
	CalPage += "<td colspan='3' align='right'><a href='javascript:CalendarInstance[" + this.CalendarID + "].ClearDate()' class='CalendarLink'>Clear</a>&nbsp;</td>";
	CalPage += "</tr>";
	CalPage += "</table></td></tr></table>";

	WriteText("CalendarContent" + this.CalendarID, CalPage);
}

Calendar.prototype.AddYear = function ()
{
	var Num = parseInt(this.YearControl.value); 
	this.YearControl.value = Num + 1;
	this.MakeCalendar();
}

Calendar.prototype.SubYear = function ()
{
	var Num = parseInt(this.YearControl.value); 
	this.YearControl.value = Num - 1;
	this.MakeCalendar();
}

Calendar.prototype.SetToday = function()
{
	this.YearControl.value = this.CurrentYear;
	this.MonthControl.options[this.CurrentMonth].selected = true;
	this.MakeCalendar();
}

Calendar.prototype.ClearDate = function()
{
	this.Input.value = "";
	this.Close();

	if(this.Input.onchange != null)
	{
		this.Input.onchange();
	}
}

Calendar.prototype.SelectedDate = function(day)
{
	var Index = GetIndex(this.MonthControl);
	var iMonth = parseInt(this.MonthControl.options[Index].value);
	var iYear = parseInt(this.YearControl.value); 

	var DateStr = day + "/" + (iMonth + 1) + "/" + iYear;
	if (this.DateAndTime)
	{
		DateStr += " " + this.HourControl.value;
		DateStr += ":" + this.MinuteControl.value + ":00";
		DateStr += " " + GetValue(this.TimeTypeControl);
	}

	this.Input.value = DateStr;
	this.Close();

	if(this.Input.onchange != null)
	{
		this.Input.onchange();
	}
}

// Calendar Common Function
function ConvertDateTime(aDate)
{
	var DD = aDate.getDate();
	var DMM = aDate.getMonth() + 1; 	
	var HH = aDate.getHours();
	var MM = aDate.getMinutes();
	var SS = aDate.getSeconds();
	var AMPM = "";
	
	var TimeStr = "";

	if(DD < 10)
	{
		TimeStr += "0" + DD;
	}
	else
	{
		TimeStr += DD;
	}

	TimeStr += "/";

	if(DMM < 10)
	{
		TimeStr += "0" + DMM;
	}
	else
	{
		TimeStr += DMM;
	}

	TimeStr += "/" + aDate.getFullYear() + " ";
	
	if(HH >= 12)
	{
		if(HH > 12)
		{
			HH = HH  - 12;
		}
		AMPM = "PM";
	}
	else
	{
		if(HH == 0)
		{
			HH = 12;
		}
		AMPM = "AM";
	}

	TimeStr += HH + ":";

	if(MM < 10)
	{
		TimeStr += "0" + MM;
	}
	else
	{
		TimeStr += MM;
	}
	
	TimeStr += ":";

	if(SS < 10)
	{
		TimeStr += "0" + SS;
	}
	else
	{
		TimeStr += SS;
	}

	TimeStr += " " + AMPM;

	return TimeStr;
}

function ConvertDateOnly(aDate)
{
	var DD = aDate.getDate();
	var DMM = aDate.getMonth() + 1; 	
	var HH = aDate.getHours();
	
	var TimeStr = "";

	if(DD < 10)
	{
		TimeStr += "0" + DD;
	}
	else
	{
		TimeStr += DD;
	}

	TimeStr += "/";

	if(DMM < 10)
	{
		TimeStr += "0" + DMM;
	}
	else
	{
		TimeStr += DMM;
	}

	TimeStr += "/" + aDate.getFullYear();

	return TimeStr;
}

function ConvertTimeOnly(aDate)
{
	var HH = aDate.getHours();
	var MM = aDate.getMinutes();
	var AMPM = "";
	
	var TimeStr = "";

	if(HH >= 12)
	{
		if(HH > 12)
		{
			HH = HH  - 12;
		}
		AMPM = "PM";
	}
	else
	{
		if(HH == 0)
		{
			HH = 12;
		}
		AMPM = "AM";
	}

	TimeStr += HH + ":";

	if(MM < 10)
	{
		TimeStr += "0" + MM;
	}
	else
	{
		TimeStr += MM;
	}

	TimeStr += " " + AMPM;

	return TimeStr;
}

function ConvertTimeCompact(aDate)
{
	var HH = aDate.getHours();
	var MM = aDate.getMinutes();

	var TimeStr = "";

	if(HH >= 12)
	{
		if(HH > 12)
		{
			HH = HH  - 12;
		}
	}
	else
	{
		if(HH == 0)
		{
			HH = 12;
		}
	}

	TimeStr += HH + ":";

	if(MM < 10)
	{
		TimeStr += "0" + MM;
	}
	else
	{
		TimeStr += MM;
	}

	return TimeStr;
}

function ConvertShortTimeOnly(aDate)
{
	var HH = aDate.getHours();
	var MM = aDate.getMinutes();
	var AMPM = "";
	
	var TimeStr = "";

	if(HH >= 12)
	{
		if(HH > 12)
		{
			HH = HH  - 12;
		}
		AMPM = "PM";
	}
	else
	{
		if(HH == 0)
		{
			HH = 12;
		}
		AMPM = "AM";
	}

	TimeStr += HH;

	TimeStr += " " + AMPM;

	return TimeStr;
}

function ConvertShortHourOnly(aDate)
{
	var HH = aDate.getHours();
	var MM = aDate.getMinutes();
	
	var TimeStr = "";

	if(HH >= 12)
	{
		if(HH > 12)
		{
			HH = HH  - 12;
		}
	}
	else
	{
		if(HH == 0)
		{
			HH = 12;
		}
	}

	TimeStr += HH;

	return TimeStr;
}

// dd/mm/yyyy hh:mm:ss am/pm
function StrToDate(dateStr)
{
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

	var dateTimeArray = dateStr.split(" ");
	var dateArray = dateTimeArray[0].split("/");

	var dateFormat = dateArray[0] + " " + m_names[dateArray[1] - 1] + " " + dateArray[2];
	if(dateTimeArray.length > 1)
	{
		dateFormat += " " + dateTimeArray[1];
	}

	if(dateTimeArray.length > 2)
	{
		dateFormat += " " + dateTimeArray[2];
	}

	var date = new Date(dateFormat);

	date.setDate(dateArray[0]);
	date.setMonth(dateArray[1] - 1);
	date.setYear(dateArray[2]);

	return date;
}

function StrTimeToDate(timeStr)
{
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

	var date = new Date();

	var dateStr = date.getDate() + " " + m_names[date.getMonth()] + " " + date.getFullYear() + " " + timeStr;
	
	return new Date(dateStr);
}

function AddDaysToDate(theDate, days) {
	return new Date(theDate.getTime() + days * 24 * 60 * 60 * 1000);
}

function SQLDateToDate(sqlDate) {
	var dateArray = sqlDate.split("-");
	return dateArray[2] + "/" + dateArray[1] + "/" + dateArray[0];
}

function DateToSQLDate(dateStr) {
	if(dateStr != "") {
		var dateArray = dateStr.split("/");
		return dateArray[2] + "-" + dateArray[1] + "-" + dateArray[0];
	}

	return "";
}