//GENERAL CALENDAR FUNCTIONS
function isLeapYear(year) {
	if ( (year % 4) == 0) { // It is exactly divisible by 4
		if ( (year % 100) == 0) { // It is exactly divisible by 100
			var result = ((year % 400) == 0); //Is it also exactly divisible by 400?
		} else {
			var result = 1;
		}
	} else { // It is not exactly divisible by 4 - It is not a leap year
		var result = 0;
	}

	return result;
}

function getNumDaysInMonth(month, year) {
	var numDaysArr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var numDays = numDaysArr[month - 1];
	if (month == 2 && isLeapYear(year)) numDays = 29;
	return numDays;
}

function intToMonth(intVal, mode) { // mode = [null/long | short], 1 = January
	intVal = (1 * intVal) - 1;

	var monthArr = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var monthShortArr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

	if (mode == "short") {
		return monthShortArr[intVal];
	} else {
		return monthArr[intVal];
	}
}

function monthToInt(monthString, mode) { // mode = [null | padZero]
	var monthInt;

	switch (monthString) {
		case "Jan":
		case "January":
				monthInt = 1;
				break;
		case "Feb":
		case "February":
				monthInt = 2;
				break;
		case "Mar":
		case "March":
				monthInt = 3;
				break;
		case "Apr":
		case "April":
				monthInt = 4;
				break;
		case "May":
				monthInt = 5;
				break;
		case "Jun":
		case "June":
				monthInt = 6;
				break;
		case "Jul":
		case "July":
				monthInt = 7;
				break;
		case "Aug":
		case "August":
				monthInt = 8;
				break;
		case "Sep":
		case "September":
				monthInt = 9;
				break;
		case "Oct":
		case "October":
				monthInt = 10;
				break;
		case "Nov":
		case "November":
				monthInt = 11;
				break;
		case "Dec":
		case "December":
				monthInt = 12;
				break;
	}

	//if (mode == "padZero" && monthInt < 10) monthInt = "0"+monthInt;
	if (mode == "padZero") monthInt = calPad(monthInt);

	return monthInt;
}

function isHoliday(month, day, dow) { // dow = 0-6 = Sun-Sat
	var holidayMatch = false;
	
	var date = day+"-"+dow;

	switch (month.toString()) {
		case "1": //Jan
				var holidayArr = new Array("01-1", "01-2", "01-3", "01-4", "01-5", "02-1", "15-1", "16-1", "17-1", "18-1", "19-1", "20-1", "21-1"); //New Year's, Closest weekday to Jan 1 - MLK Day, Third Monday
				break;
		case "2": //Feb
				var holidayArr = new Array("16-1", "17-1", "18-1", "19-1", "20-1", "21-1", "22-1"); //MLK Day, 3rd Monday
				break;
		case "5": //May
				var holidayArr = new Array("24-1", "25-1", "26-1", "27-1", "28-1", "29-1", "30-1"); //Memorial Day, Last Monday
				break;
		case "7": //Jul
				var holidayArr = new Array("04-1", "04-2", "04-3", "04-4", "04-5", "03-5", "05-1"); //Independence Day, Closest weekday to Jul 4
				break;
		case "9": //Sep
				var holidayArr = new Array("01-1", "02-1", "03-1", "04-1", "05-1", "06-1", "07-1"); //Labor Day, First Monday
				break;
		case "11": //Nov
				var holidayArr = new Array("11-1", "11-2", "11-3", "11-4", "11-5", "10-5", "12-1", "22-4", "23-4", "24-4", "25-4", "26-4", "27-4", "28-4", "23-5", "24-5", "25-5", "26-5", "27-5", "28-5", "29-5"); //Veterans's Day, Closest weekday to Nov 11 - Thanksgiving, Fourth Thursday and Friday
				break;
		case "12": //Dec
				var holidayArr = new Array("25-1", "25-2", "25-3", "25-4", "25-5", "24-5", "26-1", "31-5"); //Christmas, Closest weekday to Dec 25 - New Year's, Closest weekday to Jan 1
				break;
	}
	
	if (holidayArr) {
		for (var i = 0; i < holidayArr.length; i++) {
			if (holidayArr[i] == date) {
				holidayMatch = true;
				break;
			}
		}
	}

	return holidayMatch;
}

function dateStringToDateCode(dateString) {
	// "10 Jan 2006" -> "20060110"
	var datestring_arr = dateString.split(" ", 3);
	return datestring_arr[2]+""+monthToInt(datestring_arr[1], "padZero")+""+pad(datestring_arr[0], "0", 2);
}

function dateCodeToDateString(dateCode) {
	// "20060110" -> "10 Jan 2006"
	dateCode = dateCode.toString();
	return noZeroPad(dateCode.substr(6, 2))+" "+intToMonth(dateCode.substr(4, 2), "short")+" "+dateCode.substr(0, 4);
}

function numDaysBetweenDates(date1, date2) {
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(difference_ms / ONE_DAY) + 1;
}

function dateStringToDateObj(date_string) {
	var datestring_arr = date_string.split(" ", 3);
	var date_obj = new Date(parseInt(datestring_arr[2]), (monthToInt(datestring_arr[1], "padZero") - 1), pad(datestring_arr[0], "0", 2), 0, 0, 0, 0);
	
	return date_obj;
}



//SELECTION CALENDAR OPEN FUNCTIONS
var selectedDate = null;

function openSelectionCalendar(dateField) { //show current year, gray out days before today, "< 2008 >" for year selection
	var winDim = getWindowDimensions();
	var dateString = dateStringToDateCode(document.getElementById(dateField).value)
	var year = dateString.substr(0, 4);
	daySelectionCalendar(year, dateField);
	displayModalBackground();
	displaySelectionCalendar(Math.floor((winDim.width - CAL_OVERLAY_WIDTH) / 2), 150);
}

function openRangeSelectionCalendar(startDateObj, endDateObj, mode) { //mode = select-start/select-end
	var winDim = getWindowDimensions();
	rangeSelectionCalendar(SEL_START_MONTH, SEL_START_YEAR, startDateObj, endDateObj, mode);
	displayModalBackground();
	displaySelectionCalendar(Math.floor((winDim.width - CAL_OVERLAY_WIDTH) / 2), 135);
}




//DIV ELEMENTS
document.write("<div id='modalBackground' onClick='hideSelectionCalendar();'></div>");
document.write("<div id='selectionCal' style='z-index: 101; position: absolute; visibility: hidden; top: 0px; left: 0px;'></div>");




//SELECTION CALENDAR MAIN FUNCTIONS
var calSelectionTarget;
var CAL_OVERLAY_WIDTH = 443;

function displayModalBackground() {
	var winDim = getWindowDimensions();
	document.getElementById("modalBackground").style.height = winDim.pageHeight + "px";
	document.getElementById("modalBackground").style.visibility = "visible";
}

function hideModalBackground() {
	document.getElementById("modalBackground").style.visibility = "hidden";
}

function displaySelectionCalendar(xPos, yPos) {
	var selectionCalDiv = document.getElementById("selectionCal");
	selectionCalDiv.style.left = xPos + "px";
	selectionCalDiv.style.top = yPos + "px";
	selectionCalDiv.style.visibility = "visible";
}

function clearSelectionCalDiv() {
	document.getElementById("selectionCal").innerHTML = "";
}

function hideSelectionCalendar() { //show current year, gray out days before today, "< 2008 >" for year selection
	//document.getElementById("selectionCal").style.display = "none";
	hideModalBackground();
	document.getElementById("selectionCal").style.visibility = "hidden";
	clearSelectionCalDiv();
}

//***** BEGIN SINGLE DAY SELECTION FUNCTIONS *****\\
function selectDay(dayCode, oldDayCode) {
	document.getElementById("img"+dayCode).style.backgroundColor = "#00FFAA";
	if (document.getElementById("img"+oldDayCode)) document.getElementById("img"+oldDayCode).style.background = "none";
	calSelectionTarget.value = dateCodeToDateString(dayCode);
	calSelectionTarget = null;
	setTimeout("hideSelectionCalendar()", 500);
}

function daySelectionMonth(month, year, selectedDay, minDate, maxDate) {
	html = "";


	html += "<div class='monthLabel'>"+intToMonth(month, "long")+"</div> \n";

	html += "<div class='dayLabels'>";
		html += "<img src='images/calendar_elements/day_heading_Su.gif' width=15 height=13 border=0>";
		html += "<img src='images/calendar_elements/day_heading_M.gif' width=15 height=13 border=0>";
		html += "<img src='images/calendar_elements/day_heading_T.gif' width=15 height=13 border=0>";
		html += "<img src='images/calendar_elements/day_heading_W.gif' width=15 height=13 border=0>";
		html += "<img src='images/calendar_elements/day_heading_Th.gif' width=15 height=13 border=0>";
		html += "<img src='images/calendar_elements/day_heading_F.gif' width=15 height=13 border=0>";
		html += "<img src='images/calendar_elements/day_heading_Sa.gif' width=15 height=13 border=0>";
	html += "</div> \n";
	
	var dateObj = new Date(year, (month - 1), 1, 0, 0, 0, 0);
	var dow = dateObj.getDay();
	var numDays = getNumDaysInMonth(month, year);
	var counter = 0;
	var day = 1;

	html += "<div class='weeks'>\n";
	
		for (var i = 0; i < 6; i++) {
			html += "<div class='weekHolder'> \n";
			for (var j = 0; j < 7; j++) {
				if (counter < dow || counter >= (dow + numDays)) {
					html += "<img src='images/calendar_elements/d-blank.gif' width=15 height=13 border=0>";
				} else {
					var dayCode = calPad(day);
					var thisDate = year + "" + calPad(month) + "" + dayCode;
					var imgBGColor = (thisDate == selectedDay)? "background-color: #00FFAA;": "";
					if (thisDate >= minDate && thisDate <= maxDate) {
						var imgMode = "norm"; //(thisDate == currentDay)? "active": "norm";
						var aPre = "<a href='javascript:;' onClick='selectDay("+thisDate+", "+selectedDay+");'>";
						var aPost = "</a>";
					} else {
						var imgMode = "disabled";
						var aPre = "";
						var aPost = "";
					}
					html += aPre + "<img id='img"+thisDate+"' style='"+imgBGColor+"' src='images/calendar_elements/d"+dayCode+"-"+imgMode+".gif' width=15 height=13 border=0>" + aPost;
					day++;
				}
				counter++;
			}
			html += "</div> \n";
		}
	
	html += "</div> \n";

	return html;
}

function daySelectionCalendar(year, dateField) {
	html = "";

	var numCols = 4;
	var numRows = 3;
	
	if (dateField != null) calSelectionTarget = document.getElementById(dateField);
	selectedDay = dateStringToDateCode(calSelectionTarget.value);
	var selectedYear = selectedDay.substr(0, 4);
	
	var currentDay = CURR_YEAR + "" + calPad(CURR_MONTH) + "" + calPad(CURR_DAY); //CURR_YEAR, CURR_MONTH, CURR_DAY constants must be provided in another script, such as a "parameters.php" script
	
	var month = 1; //January
	
	var minYear = SEL_START_YEAR;
	var maxYear = SEL_END_YEAR;
	
	var minDate = SEL_START_YEAR + "" + calPad(SEL_START_MONTH) + "" + calPad(SEL_START_DAY);
	var maxDate = SEL_END_YEAR + "" + calPad(SEL_END_MONTH) + "" + calPad(SEL_END_DAY);

	html += "<table class='selectionCalendar'> \n";
		html += "<tr> \n";
			html += "<td class='title' colspan="+numCols+">";

				html += "<table class='layoutAssistant' style='width: 100%;'> \n";
					html += "<tr> \n";
						html += "<td class='default' style='width: 17px;'><img class='buttonRect' src='images/spacer.gif' width=17 height=17 border=0></td>";
						html += "<td class='default''>&nbsp;</td> \n";
						if (year > minYear) {
							html += "<td class='default' style='width: 17px;'><div class='bitmapButton'><a class='leftArrow' href='javascript: daySelectionCalendar("+((1 * year) - 1)+", null);'><img class='buttonRect' src='images/spacer.gif' width=17 height=17 border=0></a></div></td>";
						} else {
							html += "<td class='default' style='width: 17px;'><img class='buttonRect' src='images/spacer.gif' width=17 height=17 border=0></td>";
						}
						html += "<td class='default' style='text-align: center; vertical-align: middle; width: 1%; padding: 0px 6px;'><p class='year'>" + year + "</p></td> \n";
						if (year < maxYear) {
							html += "<td class='default' style='width: 17px;'><div class='bitmapButton'><a class='rightArrow' href='javascript: daySelectionCalendar("+((1 * year) + 1)+", null);'><img class='buttonRect' src='images/spacer.gif' width=17 height=17 border=0></a></div></td>";
						} else {
							html += "<td class='default' style='width: 17px;'><img class='buttonRect' src='images/spacer.gif' width=17 height=17 border=0></td>";
						}
						html += "<td class='default''>&nbsp;</td> \n";
						html += "<td class='default' style='width: 17px;'><div class='bitmapButton'><a class='Delete' href='javascript: hideSelectionCalendar();'><img class='buttonRect' src='images/spacer.gif' width=17 height=17 border=0></a></div></td>";
					html += "</tr> \n";
				html += "</table> \n";

			html += "</td> \n";
		html += "</tr> \n";

		for (var i = 0; i < numRows; i++) {
			html += "<tr> \n";
			for (var j = 0; j < numCols; j++) {
				html += "<td class='monthHolder'> \n";
					html += daySelectionMonth(month, year, selectedDay, minDate, maxDate);
				html += "</td> \n";
				month++;
			}
			html += "</tr> \n";
		}

	html += "</table> \n";

	document.getElementById("selectionCal").innerHTML = html;
}
//***** END SINGLE DAY SELECTION FUNCTIONS *****\\
