/*---------------------------------------------------------------------
	JavaScript DOM Date Picker javascript widget

	REQUIREMENTS:
		Works in conjunction with /assets/css/datepicker.css
	
	USAGE:
		Make sure function InitDatePickerControls() is called in the window.onload event (found in /assets/js/init.js).
		For any input type="text" element for a date, be sure to give it a class="DatePicker".
-----------------------------------------------------------------------*/

aMonthsLong = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
aMonthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];

aDaysLong = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
aDaysAbbr = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
aDaysShort = ["S", "M", "T", "W", "T", "F", "S"];

aDays = aDaysShort;
aMonths = aMonthsLong;
var dateFormat = "mm/dd/yyyy"
var NumDays = aDays.length;	

var sTempInputVal;
var sTempInputValSelected;

var currentDate = new Date();
var currentDay  = currentDate.getDate();
var currentMonth = currentDate.getMonth();
var currentYear = currentDate.getYear();

function InitDatePickerControls() {
	if (!document.getElementById) return;
	
	var aInput = document.getElementsByTagName('input');
	for (var i = 0; i < aInput.length; i++) {
		x = i;
		if (aInput[i].className == 'DatePicker') {
			aInput[i].onfocus = function() {
				sTempInputVal =  this.getAttribute('value');
				sTempInputVal =  this.value;
				if (sTempInputVal != "") {
					prevDate = new Date(sTempInputVal);
					DatePickerMonth = prevDate.getMonth();
					DatePickerYear = prevDate.getYear();
				} else {
					DatePickerMonth = currentMonth;
					DatePickerYear = getNiceYear(currentYear);
				}
				NewDatePicker(this.getAttribute('id'), DatePickerMonth, DatePickerYear);
			}
			aInput[i].onblur = function() {
				//setTimeout(this.blur(), 0);
				//RevertDatePicker(this.getAttribute('id'), sTempInputVal);
				//setTimeout("RevertDatePicker(this.getAttribute('id'), sTempInputVal)", 0);
			}
		}
	}
}


function NewDatePicker(callerID, currentMonth, currentYear) {
	if (document.getElementById('DatePickerBlock')) {
		var node = document.getElementById('DatePickerBlock');
		node.parentNode.removeChild(node);
	}
	ShowDatePicker(callerID, currentMonth, currentYear);
}


function RevertDatePicker(callerID, passbackVal) {
	//setTimeout('void', 100);
	//alert("revert " + callerID + " back to " + passbackVal);
	var DatePickerBlockNode = document.getElementById('DatePickerBlock');
	DatePickerBlockNode.parentNode.removeChild(DatePickerBlockNode);
	var callerIDnode = document.getElementById(callerID);
	callerIDnode.setAttribute('className', 'DatePicker');
	callerIDnode.setAttribute('class', 'DatePicker');
	callerIDnode.value = passbackVal;
	//	Set all "SelectedLabel" Label classes to "UnSelectedLabel"
	var aLabels = document.getElementsByTagName('label');
	for (var i = 0; i < aLabels.length; i++) {
		x = i;
		aLabels[i].setAttribute('className', 'UnSelectedLabel');
		aLabels[i].setAttribute('class', 'UnSelectedLabel');
	}
}

function SelectDate(callerID, passbackVal) { 
	//alert("SelectDate: replace " + callerID + " with " + passbackVal);
	var DatePickerBlockNode = document.getElementById('DatePickerBlock');
	DatePickerBlockNode.parentNode.removeChild(DatePickerBlockNode);
	
	var callerIDnode = document.getElementById(callerID);
	callerIDnode.value = passbackVal;
	callerIDnode.setAttribute('className', 'DatePicker');
	callerIDnode.setAttribute('class', 'DatePicker');
		
	//	Set all "SelectedLabel" Label classes to "UnSelectedLabel"
	var aLabels = document.getElementsByTagName('label');
	for (var i = 0; i < aLabels.length; i++) {
		x = i;
		aLabels[i].setAttribute('className', 'UnSelectedLabel');
		aLabels[i].setAttribute('class', 'UnSelectedLabel');
	}
	//	Set all "SelectedDatePicker" Input classes to "DatePicker"
	var aInput = document.getElementsByTagName('input');
	for (var i = 0; i < aLabels.length; i++) {
		x = i;
		if ( (aInput[i].getAttribute('className') == 'SelectedDatePicker') || (aInput[i].getAttribute('class') == 'SelectedDatePicker')) {
			aInput[i].setAttribute('className', 'DatePicker');
			aInput[i].setAttribute('class', 'DatePicker');
		}
	}
}


function ShowDatePicker(callerID, month, year) {
	month = parseInt(month);
	year = parseInt(year);

	//	Set label of selected input to "SelectedLabel"
	var aLabels = document.getElementsByTagName('label');
	for (var i = 0; i < aLabels.length; i++) {
		x = i;
		if (aLabels[i].getAttribute('for') == callerID) {
			aLabels[i].setAttribute('className', 'SelectedLabel');
			aLabels[i].setAttribute('class', 'SelectedLabel');
		}
	}
	//	Set selected input to "SelectedDatePicker", backup value and set to ""
	var callerIDnode = document.getElementById(callerID);
	
	callerIDnode.setAttribute('className', 'SelectedDatePicker');
	callerIDnode.setAttribute('class', 'SelectedDatePicker');
	sTempInputValSelected = callerIDnode.value;
	callerIDnode.value = "";

	var myBody=document.getElementsByTagName("body").item(0);
	var myCalendarControl = document.createElement("div");
	myCalendarControl.setAttribute('id', 'DatePickerBlock');

	MoveToByNode(myCalendarControl, GetAbsXPosByNode(callerIDnode), GetAbsYPosByNode(callerIDnode) + GetHeightByNode(callerIDnode));
	
	myCalendarTable = document.createElement("table");
	myCalendarTableHead = document.createElement("thead");
	myCalendarTableBody = document.createElement("tbody");
	myCalendarTableFoot = document.createElement("tfoot");
	myCalendarTable.appendChild(myCalendarTableHead);
	myCalendarTable.appendChild(myCalendarTableBody);
	myCalendarTable.appendChild(myCalendarTableFoot);

	myCalendarTableRow = document.createElement("tr");
		myCalendarTableHeader = document.createElement("th");
		myCalendarTableDateLink = document.createElement("a");
		prevmonth = month - 1;
		prevyear = year;
		if (prevmonth < 0) {
			prevmonth = 11;
			prevyear = prevyear - 1;
		}
		myCalendarTableDateLink.setAttribute("href","javascript:NewDatePicker('" + callerID + "','" + prevmonth + "','" + prevyear + "')");
		myCalendarTableDataText = document.createTextNode("<<");
		myCalendarTableDateLink.appendChild(myCalendarTableDataText);
		myCalendarTableHeader.appendChild(myCalendarTableDateLink);
		myCalendarTableRow.appendChild(myCalendarTableHeader);

		myCalendarTableHeader = document.createElement("th");
		myCalendarTableHeader.style.textAlign = "center";
		myCalendarTableHeader.style.fontSize = "120%";
		
		myCalendarTableHeader.setAttribute("colSpan","5");
		myCalendarTableDataText = document.createTextNode(getNiceMonthName(month) + " " + getNiceYear(year));
		myCalendarTableHeader.appendChild(myCalendarTableDataText);
		myCalendarTableRow.appendChild(myCalendarTableHeader);

		myCalendarTableHeader = document.createElement("th");
		myCalendarTableDateLink = document.createElement("a");
		
		nextmonth = month + 1;
		nextyear = year;
		if (nextmonth > 11) {
			nextmonth = 0;
			nextyear = nextyear + 1;
		}
		myCalendarTableDateLink.setAttribute("href","javascript:NewDatePicker('" + callerID + "','" + nextmonth + "','" + nextyear + "')");
		myCalendarTableDataText = document.createTextNode(">>");
		myCalendarTableDateLink.appendChild(myCalendarTableDataText);
		myCalendarTableHeader.appendChild(myCalendarTableDateLink);
		myCalendarTableRow.appendChild(myCalendarTableHeader);
	myCalendarTableHead.appendChild(myCalendarTableRow);

	myCalendarTableRow = document.createElement("tr");
		for (var i = 0; i < NumDays; i++) {
			myCalendarTableHeader = document.createElement("th");
			myCalendarTableHeader.appendChild(document.createTextNode(aDays[i]));
			myCalendarTableRow.appendChild(myCalendarTableHeader);	
		}
	myCalendarTableHead.appendChild(myCalendarTableRow);

	var rowNum = 0;
	var buildDay = 0;
	lastWeekInMonth = false;
	showDate = false;
	while (lastWeekInMonth == false) {
		myCalendarTableRow = document.createElement("tr");
		for(i = 0; i < NumDays; i++) {
			if ( (rowNum == 0) && (i == getStartDayOfMonth(month, year)) ) {
				buildDay = 1;
				showDate = true;
			}
			if ( buildDay > getDaysInMonth(month, year) ) {
				showDate = false;
			}
			myCalendarTableData = document.createElement("td");
			if (showDate == false) {
				myCalendarTableDataText = document.createTextNode(" ");
				myCalendarTableData.appendChild(myCalendarTableDataText);
			} else {
				var month2Digit = getNiceMonthNum(month)
				var day2Digit = buildDay
				switch (dateFormat) {
					case "mm/dd/yyyy":
						if (month2Digit < 10) {
							month2Digit = "0" + month2Digit
						}
						if (day2Digit < 10) {
							day2Digit = "0" + day2Digit
						}
						sTempSelected = month2Digit + "/" + day2Digit + "/" + getNiceYear(year);
						break;
					case "dd/mm/yyyy":
						sTempSelected = day2Digit + "/" + month2Digit + "/" + getNiceYear(year);
						break;
					default:
						break;
				}
				myCalendarTableDateLink = document.createElement("a");
				myCalendarTableDateLink.setAttribute("href","javascript:SelectDate('"+callerID+"','"+sTempSelected+"')");
				myCalendarTableDataText = document.createTextNode(buildDay);
				myCalendarTableDateLink.appendChild(myCalendarTableDataText);
				myCalendarTableData.appendChild(myCalendarTableDateLink);
				buildDay = buildDay + 1;
			}
			myCalendarTableRow.appendChild(myCalendarTableData);
		}
		myCalendarTableBody.appendChild(myCalendarTableRow);
		rowNum = rowNum + 1;
		if (rowNum == 6) {
			lastWeekInMonth = true;
		}
	}

	//		table footer with cancel button
	myCalendarTableRow = document.createElement("tr");
	myCalendarTableData = document.createElement("td");
	myCalendarTableData.setAttribute("colSpan","7");
	myCalendarTableDateLink = document.createElement("a");
	myCalendarTableDateLink.setAttribute("href","javascript:RevertDatePicker('"+callerID+"','"+sTempInputValSelected+"')");
	myCalendarTableDataText = document.createTextNode("cancel");
	myCalendarTableDateLink.appendChild(myCalendarTableDataText);
	myCalendarTableData.appendChild(myCalendarTableDateLink);
	myCalendarTableRow.appendChild(myCalendarTableData);
	myCalendarTableFoot.appendChild(myCalendarTableRow);

	myCalendarTable.appendChild(myCalendarTableBody);
	myCalendarControl.appendChild(myCalendarTable);
	myCalendarTable.setAttribute("cellPadding","3");
	myCalendarTable.setAttribute("cellSpacing","0");
	sTempSelected = "";

	myCalendarCancel = document.createElement("a");
	myCalendarCancel.setAttribute("href","javascript:CloseDatePicker('" + callerID + "','" + sTempInputValSelected + "')");
	myCalendarCancelText = document.createTextNode("cancel");
	myCalendarCancel.appendChild(myCalendarCancelText);
	myBody.appendChild(myCalendarControl);
}


function getStartDayOfMonth(month, year) {
	//	function to return day of week for first day of month. (0="Sunday, 1="Monday", etc...)
	month = parseInt(month);
	year = parseInt(year);
	var getStartDayOfMonthDate = new Date();
	getStartDayOfMonthDate.setMonth(month);
	getStartDayOfMonthDate.setFullYear(getNiceYear(year));
	getStartDayOfMonthDate.setDate(1);
	return getStartDayOfMonthDate.getDay();
}
function getDaysInMonth(month, year) {
	var checkLeapYearDate;
	month = parseInt(month);
	year = parseInt(year);
	var daysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	//	Check to see if it is a leap year:
	if (month == 1) {
		checkLeapYearDate = new Date();
		checkLeapYearDate.setMonth(1);
		checkLeapYearDate.setYear(year);
		checkLeapYearDate.setDate(29);
		if (checkLeapYearDate.getMonth() == 2) {
			daysInMonth[1] = 28;
		}
	}
	return daysInMonth[month];
}
function getNiceMonthName(month) {
	return aMonths[month];
}
function getNiceMonthNum(month) {
	return month + 1;
}
function getNiceYear(year) {
	return (year < 1000) ? year + 1900 : year;
}
function getNiceDay(day) {
	return day+1;
}


/*---------------
	Add to window.onload event
---------------*/
addEvent(window, 'load', InitDatePickerControls, false);

