// JavaScript Document

/* USED IN:
	-CETO AWARD NOMINATION FORMS
	-ARCHIVE/WAREHOUSE APP
*/

// JavaScript Date Picker Version 0.2.1
// This Date Picker witten by Paul Gallo on Jun 12 2006
// Tested on IE5.0, IE5.5, IE6, FF1.5
// To check if a entered date is valid for form verification, use isInputValid(inputobject);
// Results will be result[0] = valid (true/false) result[1] = month, result[2] = day, result[3] = year

// to use include calendar02.js and calendar.css in this directory (or your own CSS), give the input field that you would like to
// associate with the calendar a class of "calendarinput", and it should work!

var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var shortMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("S", "M", "T", "W", "T", "F", "S");
var disabledDays = new Array(0,0,0,0,0,0,0);
var disableDaysBeforeToday = false;
var calendars = new Array();
var selectedDay, selectedMonth, selectedYear;
var disabledArray = new Array();
d = new Date();
var defaultShownMonth = d.getMonth()+1;
var defaultShownYear = d.getUTCFullYear();

// createDisabled('06/21/2006');
// createDisabled('6/22/2006');

//addLoadEvent(setLoad);

Event.observe(window, 'load', function() {
	setLoad();
});

function setLoad() {
	
	document.onclick = bodyClick;	
	if (document.captureEvents) document.captureEvents(Event.CLICK);
	
	inputs = document.getElementsByTagName("input");
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].className == "calendarinput") {
			tempdiv = document.createElement("div");
			document.body.appendChild(tempdiv);
			randomName= "cal" + Math.round(Math.random() * 10000);
			calendars[inputs[i].id] = randomName;
			tempdiv.setAttribute("class", "calendarobj");
			tempdiv.setAttribute("id", randomName);
			tempdiv.className = "calendarobj";
			tempdiv.id = randomName;
			inputs[i].onclick = function() { openCalDefault(this, randomName); }
			inputs[i].onkeyup = function() { openCalDefault(this, randomName); }
		}
	}
}

function createDisabled() {
	if (arguments.length < 1) { return false; }
	datevar = arguments[0];
	if (arguments.length < 1) {
		classvar = arguments[1];
	} else {
		classvar = "disabled"; 
	}
	var re = /^(\d{1,2})[\/-](\d{1,2})[\/-](\d{4})$/;
	valid = re.test(datevar);
	tempmonth = removeLeadingZeros(RegExp.$1);
	tempday = removeLeadingZeros(RegExp.$2);
	tempyear = removeLeadingZeros(RegExp.$3);
	
	if (!disabledArray[tempyear]) {
		disabledArray[tempyear] = new Array();
	}
	if(!disabledArray[tempyear][tempmonth]) {
		disabledArray[tempyear][tempmonth] = new Array(); 
	}
	disabledArray[tempyear][tempmonth][tempday] = classvar;
}

function removeLeadingZeros(a) {
	while (a.charAt(0)== "0") {
		a = a.substring(1,a.length);
	} return a;
}

function checkDisabled(checkYear, checkMonth, checkDay) {
	checkYear = removeLeadingZeros(checkYear.toString());
	checkMonth = removeLeadingZeros(checkMonth.toString());
	checkDay = removeLeadingZeros(checkDay.toString());

	if (disabledArray[checkYear]) {
		if (disabledArray[checkYear][checkMonth]) {
			if (disabledArray[checkYear][checkMonth][checkDay]) {
				return disabledArray[checkYear][checkMonth][checkDay];
			} else { return false; }
		} else { return false; }
	} else { return false; }
}

function checkDisabledWeekday(checkYear, checkMonth, checkDay) {
	tempdate = new Date(checkYear, checkMonth-1, checkDay, 23, 59, 59, 999)
	dotw = tempdate.getDay();
	if (disabledDays[dotw] == 1) {
		return true;
	}
	
	if (disableDaysBeforeToday) {
		if (tempdate < new Date()) {
			return true;
		}
	}
	
	return false;
}

function drawcal(calname, inputname, shownMonth, shownYear, valid) {
	closeAllCalendars();
	
	if (shownMonth < 1) { shownMonth = 12; shownYear--; }
	if (shownMonth > 12) { shownMonth = 1; shownYear++; }
	
	var d = new Date(shownYear,shownMonth-1,1);
	var numdays = getDays(shownMonth-1, shownYear);
	var startday = d.getDay();
	var inputbox = document.getElementById(inputname);
	var cal = document.getElementById(calname);
		
	for (i=0; i<cal.childNodes.length; i++){ 
		cal.removeChild(cal.firstChild);
	}
	
	caltable = document.createElement("table");
	caltable.setAttribute("id", "caltable");
	caltable.id = "caltable";
	cal.appendChild(caltable);
	//----------
	calthead = document.createElement("thead");
	caltable.appendChild(calthead);
	// keep Spacing and Padding uppercase, this fixes an IE bug.
	caltable.setAttribute('cellSpacing','0');
	caltable.setAttribute('cellPadding','0');
	
	if (!valid) {
		temptr = document.createElement("tr");
		calthead.appendChild(temptr);
		tempth = document.createElement("th");
		tempth.setAttribute('class','invalid');
		tempth.className = "invalid";
		// keep colSpan with uppercase 'S', this fixes an IE bug.
		tempth.setAttribute('colSpan','7');
		temptr.appendChild(tempth);
		temptext = document.createTextNode("Select a valid date");
		tempth.appendChild(temptext);
	}

	temptr = document.createElement("tr");
	calthead.appendChild(temptr);
	
	tempth = document.createElement("th");
	tempth.inputname = inputname;
	tempth.className = "arrow";
	tempth.onclick = function() {currentinputvalid = isInputValid(document.getElementById(inputname)); drawcal(calname, inputname, shownMonth-1, shownYear, currentinputvalid[0]); return false; }
	temptr.appendChild(tempth);
	tempa = document.createElement("a");
	tempa.setAttribute('href', '#');
	tempa.setAttribute('tabIndex', '10000');
	tempa.inputname = inputname;
	tempa.onclick = function() {currentinputvalid = isInputValid(document.getElementById(inputname)); drawcal(calname, inputname, shownMonth-1, shownYear, currentinputvalid[0]); return false; }
	tempth.appendChild(tempa);
	temptext = document.createTextNode("<");
	tempa.appendChild(temptext);
	
	tempth = document.createElement("th");
	// keep colSpan with uppercase 'S', this fixes an IE bug.
	tempth.setAttribute('colSpan','5');
	tempth.setAttribute('class','caltitle');
	tempth.className = 'caltitle';
	temptr.appendChild(tempth);
	
	tempinput = document.createElement('select');
	tempinput.id = "selectMonth";
	tempinput.name = "selectMonth";
	for (i=1; i<13; i++) {
		tempoption = document.createElement('option');
		tempoption.value = i;
		if (shownMonth == i) { tempoption.setAttribute('selected','selected'); }
		temptext = document.createTextNode(shortMonths[i-1]);
		tempoption.appendChild(temptext);
		tempinput.onchange = function() { shownMonth = this.value; currentinputvalid = isInputValid(document.getElementById(inputname)); drawcal(calname, inputname, parseInt(shownMonth), shownYear, currentinputvalid[0]); return false; }
		tempinput.appendChild(tempoption);
	}
	tempth.appendChild(tempinput);
	
	tempinput = document.createElement('select');
	tempinput.id = "selectYear";
	tempinput.name = "selectYear";
	for (i=1980; i<2011; i++) {
		tempoption = document.createElement('option');
		tempoption.value = i;
		if (shownYear == i) { tempoption.setAttribute('selected','selected'); }
		temptext = document.createTextNode(i);
		tempoption.appendChild(temptext);
		tempinput.onchange = function() { shownYear = this.value; currentinputvalid = isInputValid(document.getElementById(inputname)); drawcal(calname, inputname, parseInt(shownMonth), shownYear, currentinputvalid[0]); return false; }
		tempinput.appendChild(tempoption);
	}
	tempth.appendChild(tempinput);
	
	//temptext = document.createTextNode(months[shownMonth-1] + " " + shownYear);
	//tempth.appendChild(temptext);
	
	temptr2 = document.createElement("tr");
	calthead.appendChild(temptr2);
	for (i=0; i<7; i++) {
		tempth = document.createElement("th");
		temptr2.appendChild(tempth);
		temptext = document.createTextNode(days[i]);
		tempth.appendChild(temptext);
		tempth.setAttribute('class','dayname');
		tempth.className = "dayname";
	}
	
	tempth = document.createElement("th");
	tempth.inputname = inputname;
	tempth.className = "arrow";
	tempth.onclick = function() { currentinputvalid = isInputValid(document.getElementById(inputname)); drawcal(calname, inputname, parseInt(shownMonth)+1, shownYear,  currentinputvalid[0]); return false;}
	temptr.appendChild(tempth);
	tempa = document.createElement("a");
	tempa.setAttribute('href', '#');
	tempa.setAttribute('tabIndex', '10000');
	tempa.inputname = inputname;
	tempa.onclick = function() { currentinputvalid = isInputValid(document.getElementById(inputname)); drawcal(calname, inputname, parseInt(shownMonth)+1, shownYear, currentinputvalid[0]); return false; }
	temptext = document.createTextNode(">");
	tempth.appendChild(tempa);
	tempa.appendChild(temptext);
	//----------
	
	caltbody = document.createElement("tbody");
	caltable.appendChild(caltbody);
	
	curday = 1;
	
	for (i=0; i<6; i++) {
		if (curday <= numdays) {
			temptr = document.createElement("tr");
			caltbody.appendChild(temptr);
			for (j=0; j<7; j++) {
				temptd = document.createElement("td");
				temptr.appendChild(temptd);
				if (curday <= numdays) {
					if (i==0 && startday > j) {
						temptext = document.createTextNode("");
						temptd.setAttribute('class','blank');
						temptd.className = "blank";
					} else {
						if (!disabledDays[j] == 1 && !(disableDaysBeforeToday && new Date(shownYear, shownMonth-1, curday, 23, 59, 59, 999) < new Date())) {
					
							temptd.thisday = curday;
							temptext = document.createTextNode(curday);
							
							if (selectedYear == shownYear && selectedMonth == shownMonth && selectedDay == curday) {
								temptd.setAttribute("class", "selected");
								temptd.className = "selected";
							}
							
							disabled = checkDisabled(shownYear, shownMonth, curday);
							if (disabled) {
								temptd.setAttribute("class", disabled);
								temptd.className = disabled;
							} else {
								temptd.onclick = function() { clearSelected(calname); selectDate(this, this.thisday, shownMonth, shownYear, inputname);setTimeout("closeCalendar('"+calname+"')", 150); }
							}
							
						} else {
							temptd.thisday = curday;
							temptext = document.createTextNode(curday);
							temptd.setAttribute("class", "disabled");
							temptd.className = "disabled";
						}
						
						curday++;
					}
				} else {
					temptext = document.createTextNode("");
					temptd.setAttribute('class','blank');
					temptd.className = "blank";
				}
					temptd.appendChild(temptext);
			}
		}
	}
	
	// Thanks Patrick for the help with positioning the calendar correctly!
	myparent = inputbox;
	temptop = 0; templeft = 0;
	while( myparent != null ) {
		
		if (myparent.currentStyle) { parentLeftBorder = parseInt(myparent.currentStyle.marginLeft.substr(0,myparent.currentStyle.marginLeft.length-2)); }		
		if (!isDefined("parentLeftBorder") || isNaN(parentLeftBorder)) { parentLeftBorder = 0; }
		
		//alert(myparent.tagName + "," + parentLeftBorder);
		
		temptop += myparent.offsetTop;
		templeft += myparent.offsetLeft;
		//if (myparent.tagName != "BODY") {
		//	templeft -= parentLeftBorder;	
		//}
		myparent = myparent.offsetParent;
	}
	
	cal.style.left = (templeft + 1) + "px";
	cal.style.top = (temptop + inputbox.clientHeight + 2) + "px";
	cal.style.display = "block";
	
}

function selectDate(a,d,m,y, inputname) {
	a.setAttribute('class','selected');
	a.className = 'selected';
	selectedDay = d;
	selectedYear = y;
	selectedMonth = m;
	document.getElementById(inputname).value = m+"/"+d+"/"+y;
}

function clearSelected(calname) {
	var cal = document.getElementById(calname);
	alltds = cal.getElementsByTagName("td");
	for (i=0; i<alltds.length; i++) {
		if (alltds[i].getAttribute("class") == "selected") {	
			alltds[i].setAttribute("class", "");
			alltds[i].className = "";
		}
	}
}

function getDays(month, year) {
	// Test for leap year when February is selected.
	if (1 == month) {
		return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
	} else {
		return daysInMonth[month];
	}
}

function closeAllCalendars() {
	divs = document.getElementsByTagName("div");
	for (i=0; i<divs.length; i++) {
		if (divs[i].className == "calendarobj") {
			closeCalendar(divs[i].id)
		}
	}
}

function closeCalendar(calname) {
	var cal = document.getElementById(calname);
	cal.style.display = "none";
}

function openCalDefault(thisinput, calname) {
	valid = isInputValid(thisinput);
	
	if (valid[0]) {
		selectedMonth = valid[1];
		selectedDay = valid[2];
		selectedYear = valid[3];
		shownMonth = selectedMonth;
		shownYear = selectedYear;
	} else {
		selectedMonth = null;
		selectedDay = null;
		selectedYear = null;
		shownMonth = null;
		shownYear = null;
	}
	if (selectedDay && !checkDisabled(selectedYear, selectedMonth, selectedDay) && !checkDisabledWeekday(selectedYear, selectedMonth, selectedDay)) {
		drawcal(calname, thisinput.id, selectedMonth, selectedYear, true);
	} else {
		drawcal(calname, thisinput.id, defaultShownMonth, defaultShownYear, false);
	}
}

function isInputValid(thisinput) {

	var re = /^(\d{1,2})[\/-](\d{1,2})[\/-](\d{4})$/;
	valid = new Array();
	valstatus = re.test(thisinput.value) && isValidDate( RegExp.$3, RegExp.$1, RegExp.$2 );
	if (valid) {
		valid[1] = RegExp.$1;
		valid[2] = RegExp.$2;
		valid[3] = RegExp.$3;
	} else {
		valid[1] = null;
		valid[2]= null;
		valid[3] = null;
	}
	if (valstatus && !checkDisabled(valid[3], valid[1], valid[2]) && !checkDisabledWeekday(valid[3], valid[1], valid[2])) {
		valid[0] = true;
	} else {
		valid[0] = false;
	}
	return valid;
}



/*
// This is only an example of how to check for a valid date

function checkForm(formobj) {
	if(!isInputValid(formobj["selectedDate"])[0]) {
		alert("Not a valid date.");
		return false;
	}
}
*/
