//the classes in this file are dependant upon irutil.js
var irDatePicker = new function()
{//irDatePickerImpl handles an array of irDatePickerInstances
	var instances = new Array();
	this.dayNames = Array("Su","M","Tu","W","Th","F","Sa");
	this.monthNames = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
							"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	this.bwdMo = bwdMo;
	this.bwdYr = bwdYr;
	this.close = close;
	this.fwdMo = fwdMo;
	this.fwdYr = fwdYr;
	this.open = open;
	this.pickDate = pickDate;
	this.save = save;
	this.show = show;
	//
	function show(sTextBox,bTime)
	{
		var i = instances.length;
		var span = "irDatePickerSpan" + i;
		document.write("<span id='" + span + "'></span>");
		var dp = new irDatePickerInstance(i,sTextBox,span,bTime);
		instances[i] = dp;
		dp.draw();
	}
	//
	function bwdMo(calIdx)
	{
		instances[calIdx].bwdMo();
	}
	function bwdYr(calIdx)
	{
		instances[calIdx].bwdYr();
	}
	function close(calIdx)
	{
		instances[calIdx].close();
	}
	function fwdMo(calIdx)
	{
		instances[calIdx].fwdMo();
	}
	function fwdYr(calIdx)
	{
		instances[calIdx].fwdYr();
	}
	function open(calIdx)
	{
		instances[calIdx].open();
	}
	function pickDate(calIdx,yr,mo,day)
	{
		instances[calIdx].pickDate(yr,mo,day);
	}
	function save(calIdx)
	{
		instances[calIdx].save();
	}
}
//
function irDatePickerInstance(inIdx,inTxt,inSpan,bTime)
{
	//public methods
	this.bwdMo = bwdMo;
	this.bwdYr = bwdYr;
	this.close = close;
	this.draw = draw;
	this.fwdMo = fwdMo;
	this.fwdYr = fwdYr;
	this.open = open;
	this.pickDate = pickDate;
	this.save = save;
	//private members
	var isOpen = false;
	var myIdx = inIdx;
	var mySpan = inSpan;
	var myTxt = inTxt;
	var selDate = new Date();
	var selHH = selDate.getHours();
	var selMM = selDate.getMinutes();
	var selSS = selDate.getSeconds();
	var incTime = bTime;
	//
	var initStr = get(myTxt).value;
	if (isDate(initStr))
	{
		selDate = new Date(initStr);
		if (incTime)
		{
			var firstColonAt = initStr.indexOf(":");
			if (firstColonAt > 0)
			{//it includes the time, back up to the last preceding space
				for (i=firstColonAt-2;i>-1;i--)
				{
					if (initStr.charAt(i) == " ")
					{
						var t = initStr.substring(i + 1,initStr.length);
						var hms = t.split(":");
						if (hms.length > 0 && ! isNaN(hms[0]))
							selHH = Number(hms[0]);	
						if (hms.length > 1 && ! isNaN(hms[1]))
							selMM = Number(hms[1]);	
						if (hms.length > 2 && ! isNaN(hms[2]))
							selSS = Number(hms[2]);	
						break;
					}
				}
			}
		}
	}
	var showDate = new Date(selDate.getUTCFullYear(),selDate.getMonth(),1);	
	//
	function bwdMo()
	{
		if (showDate.getMonth() == 0)
		{
			showDate = new Date(showDate.getUTCFullYear() - 1,11,1);
		}
		else
		{
			showDate = new Date(showDate.getUTCFullYear(),showDate.getMonth() - 1,1);
		}
		draw();
	}
	function bwdYr()
	{
		showDate = new Date(showDate.getUTCFullYear() - 1,showDate.getMonth(),1);
		draw();
	}
	function close()
	{
		isOpen = false;
		draw();
	}
	function dateString(dt)
	{
		var s = irDatePicker.monthNames[dt.getMonth()] + " " + dt.getDate() 
				+ ", " + dt.getUTCFullYear();
		if (incTime)
		{
			s += " " + lpad(selHH,"0",2) + ":" + lpad(selMM,"0",2) + ":" + lpad(selSS,"0",2);
		}
		return s;
	}
	function draw()
	{
		if (isOpen)		
		{
			drawOpen();
			hide(myTxt);
		}
		else
		{
			drawClosed();
			show(myTxt);
		}
	}
	function drawClosed()
	{
		var s = "<a href=javascript:irDatePicker.open(" + myIdx + ")>"
			+ "<img border=0 src='images/calendar.gif'></a>";	
		get(mySpan).innerHTML = s;
	}
	function drawOpen()
	{
		var s = "<table cellpadding=0 cellspacing=0 style='border:1px solid black;background-color:silver;font-size:8pt;'>";
		for (i=0;i<7;i++)
		{
			s += "<col align=right>";
		}
		s += "<tr>";
		s += "<td colspan=3 align=center style='font-size:8pt;border:1px solid gray;'>";
		s += " <a href=javascript:irDatePicker.bwdMo(" + myIdx + ")><<</a> ";
		s += irDatePicker.monthNames[showDate.getMonth()];
		s += " <a href=javascript:irDatePicker.fwdMo(" + myIdx + ")>>></a>  ";
		s += "</td>";
		s += "<td colspan=3 align=center style='font-size:8pt;border:1px solid gray;'>"
		s += " <a href=javascript:irDatePicker.bwdYr(" + myIdx + ")><<</a> ";
		s += showDate.getUTCFullYear();
		s += " <a href=javascript:irDatePicker.fwdYr(" + myIdx + ")>>></a>  ";
		s += "</td>";
		s += "<td align=right style='border:1px solid gray;'><a href=javascript:irDatePicker.close(" + myIdx + ")>"
		s += "<img border=0 src='images/close.gif' alt='Close without saving'></a></td>";
		s += "</tr>";
		s += "<tr>"
		for (i=0;i<7;i++)
		{
			s += "<td style='font-size:8pt;'>" + irDatePicker.dayNames[i] + "&nbsp;</td>";
		}
		s += "</tr>"
		var box = -1;
		var d = showDate;
		while (d.getMonth() == showDate.getMonth())
		{
			box++;
			if (box == 7)
			{
				s += "</tr><tr>";
				box = 0;
			}	
			if (d.getDate() > 1 || box >= d.getDay())
			{
				var c = "black";
				if (d.getUTCFullYear() == selDate.getUTCFullYear() 
					&& d.getMonth()==selDate.getMonth()
					&& d.getDate()==selDate.getDate())
				{
					c = "red";
				}
				s += "<td style='border:1px solid gray;font-size:8pt;'>&nbsp;"
					+ "<a href=javascript:irDatePicker.pickDate(" + myIdx + "," + d.getUTCFullYear() 
					+ "," + d.getMonth() + "," + d.getDate() + ")"
					+ " style='color:" + c + ";'>" + d.getDate() + "</a>";
				d = new Date(showDate.getUTCFullYear(),showDate.getMonth(),d.getDate() + 1);
			}
			else
			{
				s += "<td>";
			}
			s += "&nbsp;</td>";
		}
		s += "</tr>";
		if (incTime)
		{
			s += "<tr><td colspan=7 align=center>";
			s += "<select id='irDatePicker_" + myIdx + "_hh'>";
			for (i=0;i<24;i++)
			{
				s += "<option value=" + i;
				if (i == selHH)
				{
					s += " selected ";
				}
				s += ">" + lpad(i,"0",2) + "</option>";
			}
			s += "</select>&nbsp;:&nbsp;";
			s += "<select id='irDatePicker_" + myIdx + "_mm'>";
			for (i=0;i<60;i++)
			{
				s += "<option value=" + i;
				if (i == selMM)
				{
					s += " selected ";
				}
				s += ">" + lpad(i,"0",2) + "</option>";
			}
			s += "</select>&nbsp;:&nbsp;";
			s += "<select id='irDatePicker_" + myIdx + "_ss'>";
			for (i=0;i<60;i++)
			{
				s += "<option value=" + i;
				if (i == selSS)
				{
					s += " selected ";
				}
				s += ">" + lpad(i,"0",2) + "</option>";
			}
			s += "</select>";
			s += "<a href=javascript:irDatePicker.pickDate(" + myIdx + "," + selDate.getUTCFullYear() 
					+ "," + selDate.getMonth() + "," + selDate.getDate() + ")>";
			s += "<img border=0 src='images/checkmark.gif' alt='Save and Close'></a>";
			s += "</td><tr>";
		}
		s += "</table>";
		get(mySpan).innerHTML = s;
	}
	function fwdMo()
	{
		if (showDate.getMonth() == 11)
		{
			showDate = new Date(showDate.getUTCFullYear() + 1,0,1);
		}
		else
		{
			showDate = new Date(showDate.getUTCFullYear(),showDate.getMonth() + 1,1);
		}
		draw();
	}
	function fwdYr()
	{
		showDate = new Date(showDate.getUTCFullYear() + 1,showDate.getMonth(),1);
		draw();
	}
	function open()
	{
		isOpen = true;
		draw();
	}
	function pickDate(yr,mo,day)
	{
		selDate = new Date(yr,mo,day,selDate.getHours(),
							selDate.getMinutes(),selDate.getSeconds());
		isOpen = false;
		if (incTime)
		{
			var h = get("irDatePicker_" + myIdx + "_hh").value;
			var m = get("irDatePicker_" + myIdx + "_mm").value;
			var s = get("irDatePicker_" + myIdx + "_ss").value;
			if (! isNaN(h))
				selHH = Number(h);	
			if (! isNaN(m))
				selMM = Number(m);	
			if (! isNaN(s))
				selSS = Number(s);				
		}
		draw();
		get(myTxt).value = dateString(selDate);
	}
	function save()
	{
		isOpen = false;
		pickDate(selDate.getYear(),selDate.getMonth(),selDate.getDay());
		draw();
	}
	//end irDatePickerInstance body
}