function pickDate(ctrlid, month, year, showtime)
{
	var datepicker = document.getElementById('datepicker');
	var ctrl = document.getElementById(ctrlid);
	
	datepicker.style.left = findLeftOfControl(ctrl) + "px";
	datepicker.style.top = (findTopOfControl(ctrl) + ctrl.offsetHeight) + "px";
	
	if(datepicker.style.visibility=="visible" && month==-1 && year==-1)
	{
		datepicker.style.visibility = "hidden";
		return;
	}
	datepicker.style.visibility = "visible";
	
	var today = new Date();
	if(year==-1) year = today.getFullYear();
	if(month==-1) month = today.getMonth();
	
	var prevmonth = month - 1;
	var prevyear = year;
	if(prevmonth==-1)
	{
		prevmonth = 11;
		prevyear--;
	}
	var nextmonth = month + 1;
	var nextyear = year;
	if(nextmonth>11)
	{
		nextmonth = 0;
		nextyear++;
	}
	
	var date = new Date(year, month, 1);
	
	var startIdx = date.getDay();
	var daysInMonth = getDaysInMonth(month, year);
	
	var days = Array("S", "M", "T", "W", "T", "F", "S");
	var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	
	var html = "<table width=\"100%\">";
	html += "<tr class=\"header\">";
	html += "<td><a href=\"javascript:pickDate('" + ctrlid + "', " + prevmonth + ", " + prevyear + ");\">&lt;&lt;</a></td><td colspan=\"5\">" + months[month] + " " + year + "</td><td><a href=\"javascript:pickDate('" + ctrlid + "', " + nextmonth + ", " + nextyear + ");\">&gt;&gt;</a></td>";
	html += "</tr>";
	html += "<tr class=\"header\">";
	for(var i = 0; i < 7; i++)
	{
		html += "<td>" + days[i] + "</td>";
	}
	
	html += "<tr>";
	
	for(var i = 0; i < startIdx; i++)
	{
		html += "<td>&nbsp;</td>";
	}
	
	for(var i = 1; i <= daysInMonth; i++)
	{
		if(startIdx==7)
		{
			startIdx = 0;
			html += "</tr><tr>";
		}
		
		html += "<td><a href=\"javascript:setDate('" + ctrlid + "', " + i + ", " + month + ", " + year + ", " + showtime + ");\">" + i + "</a></td>";
		
		startIdx++;
	}
	html += "</tr>";
	
	if(showtime)
	{
		html += "<tr>";
		html += "<td colspan=\"7\">";
		html += "<select id=\"datepicker_hour\"><option value=\"1\">1</option></select>:<select id=\"datepicker_minute\"><option value=\"0\">00</option></select> <select id=\"datepicker_ampm\"><option value=\"am\">am</option></select>";
		html += "</td>";	
		html += "</tr>";
	}
	
	html += "</table>";
	
	datepicker.innerHTML = html;
}

function getDaysInMonth(month, year)
{
	return 32 - new Date(year, month, 32).getDate();
}

function setDate(ctrlid, day, month, year, showtime)
{
	if(day<10)
		day = "0" + day;
	if(month+1<10)
		month = "0" + (month+1);
	else
		month++;
		
	if(showtime)
	{
		var hour = document.getElementById('datepicker_hour').value;
		var minute = document.getElementById('datepicker_minute').value;
		var ampm = document.getElementById('datepicker_ampm').value;
		if(ampm=="pm")
			hour = hour + 12;
		if(ampm=="am" && hour==24)
			hour = 0;
		if(ampm=="pm" && hour==24)
			hour = 12;
		
		if(hour<10)
			hour = "0" + hour;
		if(minute<10)
			minute = "0" + minute;
	}
		
	document.getElementById(ctrlid).value = day + "/" + month + "/" + year;
	if(showtime) document.getElementById(ctrlid).value += " " + hour + ":" + minute + ":00";
	hideDatePicker();
}

function hideDatePicker()
{
	document.getElementById('datepicker').style.visibility = "hidden";
}

function findTopOfControl(control)
{
	//returns the top of the control in pixels
	//
	//control:	the control to get the top of
	
	var boxTop = 0;
	
	if(control.offsetParent)
	{
		do
		{
			boxTop += control.offsetTop;
		} while(control = control.offsetParent);
	}
	
	return boxTop;
}

function findLeftOfControl(control)
{
	//returns the left of the control, in pixels
	//
	//control:	the control to get the left of
	
	var boxLeft = 0;
	
	if(control.offsetParent)
	{
		do
		{
			boxLeft += control.offsetLeft;
		} while(control = control.offsetParent);
	}

	return boxLeft;
}
