var myWin;
var selectedObj;
var includeTime;

function OpenCalendar(aEdit)
{
	includeTime = true;
	formName = "forms[0]";
	if(OpenCalendar.arguments.length >= 2)
	{
		includeTime = OpenCalendar.arguments[1];
	}
	if(OpenCalendar.arguments.length >= 3)
	{
		formName = OpenCalendar.arguments[2]; 
	}

	var Obj = eval("document." + formName + "." + aEdit);
	if(Obj.value == "")
	{
		var Now = new Date();
		var LAMPM = " AM";
		var LHour = Now.getHours();

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

		Obj.value = Now.getDate() + "/" + (Now.getMonth() + 1) + "/" + Now.getFullYear();
		if(includeTime)
		{
			Obj.value = Obj.value + " " + LHour + ":" + Now.getMinutes() + ":" + Now.getSeconds() + LAMPM;
		}
	}
	selectedObj = Obj;
	OpenWin('library/Calendar.htm', 260, 240, 0);
}

function AssignDate(aDate)
{
	var dateStr = aDate;

	if(selectedObj != null)
	{
		selectedObj.value = dateStr;
		if(selectedObj.onchange != null)
		{
			selectedObj.onchange();
		}
	}
}

function OpenWin(PageName, NewWidth, NewHeight, Resize)
{
	var aWinLeft = 0;
	var aWinTop = 0;

	var OptionStr = "status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=" + Resize;

	if(document.all)
	{
		aWinLeft = self.screenLeft + ((document.body.clientWidth - NewWidth) / 2);
		aWinTop = window.parent.parent.screenTop;
	}

	if(aWinLeft > 0)
	{
		OptionStr = OptionStr + ",left=" + aWinLeft;
	}

	if(aWinTop > 0)
	{
		OptionStr = OptionStr + ",top=" + aWinTop;
	}

	myCalendar = open(PageName, 'myCalendar', "width=" + NewWidth + ",height=" + NewHeight + "," + OptionStr);
	//myWin.opener = self; cause access denied issue
	myCalendar.focus();
}

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;
}

// 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);
}