//Pop-Up Calendar Code
//mjk 11/05 102456

function callback(y, m, d) {
	document.forms[0].year.value = y;
	document.forms[0].month.value = getMonthName(m);
	document.forms[0].day.value = d;
}

function aftertwodays(y, m, d) {
	return ( (new Date(y, m, d)) - (new Date) > (2 - 1) * 1000 * 86400 - 1000);
}

function past(y, m, d) {
	return( (new Date(y, m, d)) <= (new Date) );
}

function future(y, m, d) {
	return( (new Date(y, m, d+1)) >= (new Date) );
}

function createDateChooser(trigger) {
	var oDC = new DateChooser(trigger, callback, aftertwodays);
	oDC.drawCalendar();
}

//=============================================================

function DateChooser(trigger, callback, filter) {
	this.trigger = trigger;
	this.callback = callback;
	this.filter = filter || function() {return true;};
	this.today = new Date();
	this.month = this.today.getMonth();
	this.year = this.today.getFullYear();
	this.date = this.today.getDate();
	this.calendar = null;
	this.clickFunction = createClickFunction(this, callback);
	this.hoverText = "Click to select";
	this.hoverTextDisabled = "Not available";
}

function createClickFunction(dateChooser, callback) {
	function f() {
		var v = this.firstChild.nodeValue;
		if (v == ">>") {
			dateChooser.drawNextMonth();
			return;
		}
		if (v == "<<") {
			dateChooser.drawPreviousMonth();
			return;
		}
		if ( v.toLowerCase() == "cancel" ) {
			dateChooser.clearCalendar();
			return;
		}
		callback.call(dateChooser, dateChooser.year, dateChooser.month, v);
		dateChooser.clearCalendar();
	}
	return f;
}

function createEscapeClickFunction(dateChooser) {
	function f() {
		dateChooser.clearCalendar();
	}
	return f;
}

DateChooser.prototype.drawCalendar = function() {
	if (document.getElementById) {  // remove old calendar
		var d = document.getElementById("calendar");
		if (d && d.parentNode) {
			d.parentNode.removeChild(d);
		}
	}
	var theDiv = document.createElement("div");
	theDiv.id = "calendar";
	theDiv.innerHTML = this.calendarString();
	this.setClickFunction(theDiv);
	if(this.trigger.offsetParent) {
		theDiv.style.position = "absolute";
		var pos = getElementPosition(this.trigger);
		var topElement = pos.topElement;
		topElement.appendChild(theDiv);
		theDiv.style.zIndex = 1001;
		theDiv.style.left = pos.x;
		theDiv.style.top = pos.y;
		var container = document.getElementById("main") || document.getElementById("datatile");
		var pos = getElementPosition(this.trigger, container);
		if(theDiv.offsetWidth) {
			var overflow = parseInt(pos.x) + theDiv.offsetWidth - container.offsetWidth;
			if (overflow > 0 ) {
				theDiv.style.left = (parseInt(theDiv.style.left) - overflow) + "px";
			}
		}
	} else {
		this.trigger.parentNode.insertBefore(theDiv, this.trigger);
		this.trigger.className += " hidden";
	}
	this.calendar = theDiv;
	var b = document.body;
	if(b.addEventListener) {
		b.removeEventListener('click', removeCalendar, false);
		b.addEventListener('click', removeCalendar, false);
	} else if (b.attachEvent) {
		b.detachEvent('onclick', removeCalendar);
		b.attachEvent('onclick', removeCalendar);
	}
}


DateChooser.prototype.clearCalendar = function() {
	var theDiv = this.calendar;
	theDiv.parentNode.removeChild(theDiv);
	this.trigger.className = this.trigger.className.replace(new RegExp(" hidden"), "");
	// this.wd.onclick = this.wd.saved_onclick;
}

DateChooser.prototype.setClickFunction = function (div) {
	var f = this.clickFunction;
	var y = this.year;
	var m= this.month;
	var list = div.getElementsByTagName("td");
	var t, s;
	for(s in list) {
		t = list[s];
		if(t.className == "null" || t.className == "hdr") { continue; }

		if(!t.firstChild) {continue; }

		if(t.className == 'ctrl') {   // a control
			t.onclick = f;
			t.onmouseover = hi;
			t.onmouseout = lo;
			if (document.all) {
				t.style.cursor = "hand";
			} else {
				t.style.cursor = "pointer";
			}
		} else if(t.className == "n" || t.className == "wkndn" ) {  // disabled
			t.style.cursor = "default";
			t.title = this.hoverTextDisabled;
		} else if(t.className == "today") { // today
			if(this.filter(this.year, this.month, t.firstChild.nodeValue)) {
				t.onclick = f;
				t.onmouseover = hi;
				t.onmouseout = lo;
				if (document.all) {
					t.style.cursor = "hand";
				} else {
					t.style.cursor = "pointer";
				}
				t.title = this.hoverText;
			} else {
				t.title = this.hoverTextDisabled;
				t.style.cursor = "default";
			}
		} else {  // regular day
			t.onclick = f;
			t.onmouseover = hi;
			t.onmouseout = lo;
			if (document.all) {
				t.style.cursor = "hand";
			} else {
				t.style.cursor = "pointer";
			}
			t.title = this.hoverText;
		}
	}
}


DateChooser.prototype.drawNextMonth = function() {
	this.clearCalendar();
	this.month = (this.month + 1) % 12;
	if (this.month == 0) {this.year++;}
	this.drawCalendar();
}

DateChooser.prototype.drawPreviousMonth = function() {
	this.clearCalendar();
	this.month = (this.month - 1);
	if (this.month < 0) { this.month += 12; }
	if (this.month == 11) {this.year--;}
	this.drawCalendar();
}

DateChooser.prototype.calendarString = function() {
	var y = this.year;
	var m = this.month;
	var d = this.date;
	var filt = this.filter;
	var first = new Date(y, m, 1);
	var startDay = first.getDay();
	first = new Date(y, m + 1, 1);
	var last = new Date(first - 40000000);
	var mlen = last.getDate();
	var q = startDay + mlen;
	var showDays = (parseInt(q / 7) + (q % 7 ? 1 : 0)) * 7;
	var n, cl;
	var today = new Date;
	var td = this.tDate || today.getDate();
	var tm = this.tMonth || today.getMonth();
	var ty = this.tYear || today.getFullYear();

	var dayLetters = "SMTWTFS";
	var c = "<table>\n\t<tr>\n";
	c += "\t\t<td class='ctrl' title='Previous month'><<</td>\n\t\t<td class='hdr' colspan='5'>" + getMonthName(m) + " " + y + "</td>\n\t\t<td class='ctrl' title='Next month'>>></td>\n";
	c +="</tr></tr>"
	for(var i = 0; i < 7; i++) {
		c += "\t\t<td class='hdr'>" + dayLetters.substr(i, 1) + "</td>\n";
	}
	c += "\t</tr>\n<tr>\n";
	for(var i = 0; i < showDays; i++) {
		n = i - startDay + 1;
		cl = "d";
		if(n < 1 || n > mlen) {
			n = "&nbsp";
			cl = 'null';
		} else if(i % 7 == 0 || i % 7 == 6) { // weekend
			cl = 'wknd';
		}

		if( !filt(y, m, n) ) { // disabled for selection
			if(cl == 'wknd') {
				cl = 'wkndn';
			} else {
				cl = 'n';
			}
		}

		if (n == td && m == tm && y == ty) { // today
			cl = 'today';
		}
		c += "\t\t<td class='" + cl + "'>" + n + "</td>\n";
		if ( (i + 1) % 7 == 0) {
			c += "\t</tr>\n\t<tr>\n";
		}
	}
	c += "<td class='ctrl' colspan='7' title='Cancel date selection'>Cancel</td></tr></table>\n";

	return(c);
}

function getMonthName(m) {
	var names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	return( names[m] );
}

function hi() {
	this.old = this.className;
	this.className = 'hi';
}

function lo() {
	this.className = this.old;
}



	function TextBoxDataChooser(trigger,datename,filter) {
		var callback = function (y, m, d) {
			var fielddate;

			if (fielddate = document.getElementById(datename)) {
				m = m + 1;
				if (m < 10) m = "0" + m;
				if (d < 10) d = "0" + d;
				fielddate.value = m + '/' + d + '/' + y;
			}
		}
		var oDC = new DateChooser(trigger, callback, eval(filter));
		oDC.drawCalendar();
	}



	function writeCalendar(datename,filter,imageSource){
		if (navigator.userAgent.toLowerCase().indexOf('safari') != -1) { return; }
		if (navigator.userAgent.indexOf('MSIE 5') > 0) { return; }
		if (typeof(document)=='object' &&
		  (datename != null) &&
		  document.getElementById &&
		  typeof(document.getElementById(datename))=='object'){
			document.write(CalendarHTML(datename,filter,imageSource));
		}
	}


	function CalendarHTML(datename,filter,imageSource) {
                        return '<img src=' + imageSource + ' class=\'calendar\' onclick="TextBoxDataChooser(this,\'' + datename + '\',\'' + filter + '\'); return false;" alt=""/>';
	}


	function IsNumericMinMax(objName,minvalue,maxvalue,decimalAllowed)
	{
		var objvalue = objName.value;
		if (objvalue == "") {
			return true;
		}
		if (decimalAllowed==1)
		{
			var NumericExp = /^([-]{0,1})([0-9]{1,})([.]{0,1})([0-9]{0,})$/;
		}
		else
		{
			var NumericExp = /^([-]{0,1})([0-9]{1,})$/;
		}
		var NumericArray = NumericExp.exec(objvalue);
		if (NumericArray == null) {
			alert("Please enter a numeric value");
			objName.select();
			objName.focus();
			return false;
		}

		if (IsNumeric(objvalue,true) == false)
		{
			alert("Please enter a numeric value");
			objName.select();
			objName.focus();
			return false;
		}

		if ((minvalue != "") && (Number(objvalue) < Number(minvalue))) {
			alert("Please enter a value no less than " + minvalue);
			objName.select();
			objName.focus();
			return false;
		}

		if ((maxvalue != "") && (Number(objvalue) > Number(maxvalue))) {
			alert("Please enter a value no more than " + maxvalue);
			objName.select();
			objName.focus();
			return false;
		}
		return true;
	}

function removeCalendar(evt) {
   if (!evt) { var evt = window.evt; }
   var obj = evt.target || evt.srcElement;
   if (obj && !obj.onclick && !IsInCalendar(obj)) {
	   var c = document.getElementById('calendar');
       if(c) { c.parentNode.removeChild(c); }
   }
}

function IsInCalendar(obj) {
	var i;
	for(var x = obj, i = 0; x && (i < 5); x = x.parentNode, i++) {
		if(x.id.toLowerCase() == 'calendar') { return true; }
	}
	return false;
}
