var showingCal = '';
var calDate = new Date();
var calDateSelected = null;
var strMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function showCal (field, mselact) {
	var topSession = getTopSession();
	var showed = showingCal;
	if (showingCal != '') {
		hideCal();
		parent.unKillMSelects();
	}
	if (showed != field) {
		var d = new Date;
		var cDay, cMon, cYear;
		var format = new RegExp(/^\d\d\/\d\d\/\d\d\d\d$/);
		if (format.test(topSession.document.form.all[field].value)) {
			var dateFieldVal = topSession.document.form.all[field].value.split('/');
			cMon = dateFieldVal[0] - 1;
			cDay = dateFieldVal[1];
			cYear = dateFieldVal[2];
			d = new Date(cYear, cMon, cDay);
		} else {
			cDay = d.getDate();
			cMon = d.getMonth();
			cYear = d.getFullYear();
		}
		calDate = d;
		if (calDateSelected == null) {
			calDateSelected = new Date(d);
		}
		var cal = topSession.document.createElement('DIV');
		cal.id = 'calDiv';
		var x = topSession.document.form.all[field].offsetLeft;
		var y = topSession.document.form.all[field].offsetTop;
		y += topSession.document.form.all[field].offsetHeight + 5;
		var p = topSession.document.form.all[field].offsetParent;
		while (p) {
			x += p.offsetLeft;
			y += p.offsetTop;
			p = p.offsetParent;
		}
		with (cal) {
			with (style) {
				left = x + 'px';
				top = y + 'px';
				position = 'absolute';
				zIndex = 10;
			}
			ondeactivate = checkDeactivate;
			className = 'calDiv';
			innerHTML = calendar(d);
		}
		topSession.document.body.insertAdjacentElement('afterBegin', cal);
		showingCal = field;
		if(mselact == true){
			parent.killMSelects();
		}
		topSession.document.all['calDiv'].setActive();
	} else {
		if(mselact == true){
			unKillMSelects();
		}
	}
}

function checkDeactivate() {
	var topSession = getTopSession();
	var ae = topSession.document.activeElement;
	if(ae.firstChild == null){
		hideCal();
	} else if(!topSession.document.all['calDiv'].contains(ae) && ae.firstChild.alt != 'Calendar'){
		hideCal();
	}
}

function hideCal () {
	var topSession = getTopSession();
	if (topSession.document.all['calDiv'] != null) {
		topSession.document.all['calDiv'].outerHTML = '';
		showingCal = '';
		calDateSelected = null;
		unKillMSelects();
	}
}

function clearCal () {
	var topSession = getTopSession();
	topSession.document.all[showingCal].value = '';
	hideCal();
}

function moveCal (dist) {
	var topSession = getTopSession();
	calDate.setDate(1);
	calDate.setMonth(calDate.getMonth() + dist);
	topSession.document.all['calDiv'].innerHTML = calendar(calDate);
}

function calendar (d) {
	var chtml = '<table class="calTable"><tr><td class="calTitle" colspan="7">';
	var yy=d.getFullYear();
	var mm=strMonths[d.getMonth()];
	var s = "";
	var count = 1;
	var fd = (new Date(d.getFullYear(), d.getMonth(), 1)).getDay();
	var ld = 31;
	for (var i=31; i>=28; i--) {
	    if (d.getMonth() == (new Date(d.getFullYear(),d.getMonth(),i)).getMonth()){
	      ld = i;
	      break;
	    }
	}
	
	chtml += '<table style="width: 100%"><tr><th class="calTitle"><a href="#" onclick="parent.moveCal(-12)">&#171;</a></th>';
	chtml += '<th class="calTitle"><a href="#" onclick="parent.moveCal(-1)">&#139;</a></th><th class="calTitle" style="width: 100%">';
	chtml += mm + ' ' + yy + '</th><th class="calTitle"><a href="#" onclick="parent.moveCal(1)">&#155;</a></th>';
	chtml += '<th class="calTitle"><a href="#" onclick="parent.moveCal(12)">&#187;</a></th></tr></table>';
	chtml += '</td></tr>';
	chtml += '<tr class="calHilite"><td>S</td><td>M</td><td>T</td><td>W</td><td>R</td><td>F</td><td>S</td></tr>';
	
	for (var i=0; i<6; i++) {
		chtml += '<tr>';
		for (var j=0; j<7; j++) {
			if (i * 7 + j < fd || count > ld) {
				chtml += '<td>&nbsp;</td>';
			} else {
				chtml += '<td';
				if (count == calDateSelected.getDate() && d.getMonth() == calDateSelected.getMonth() && yy == calDateSelected.getFullYear()) {
					chtml += ' class="calTitle"';
				}
				chtml += '><a href="#" onclick="parent.pickDate(\'';
				chtml += padLeft((d.getMonth() + 1), 2, '0') + '/';
				chtml += padLeft(count, 2, '0') + '/' + yy + '\')">' + count++ + '</a></td>';
			}
		}
		chtml += '</tr>';
	}
	
	chtml += '<tr class="calHilite"><td colspan="7"><a href="#" onclick="parent.hideCal()">[close]</a> ';
	chtml += '<a href="#" onclick="parent.clearCal()">[clear]</a></td></tr>';
	chtml += '</table>';
	return chtml;
}

function pickDate (d) {
	var topSession = getTopSession();
	if(topSession.document.form.all[showingCal].readOnly == false){
		topSession.document.form.all[showingCal].value = d;
		topSession.document.form.all[showingCal].fireEvent('onchange');
	}
	hideCal();
}

function padLeft (str, len, pc) {
	str += '';
	if ((pc == '') || (!(pc.length == 1))) {
		pc = ' ';
	}
	var pl = 0;
	len = parseInt(0 + len, 10);
	if(len <= str.length) {
		return(str);
	}
	pl = len - str.length;
	for (var c = 0; c < pl; c++) {
		str = pc + str;
	}
	return(str);
}

function checkdate(objName) {
	var datefield = objName;
	var err = chkdate(objName);
	if (err > 0) {
		datefield.select();
		if(err == 12){
			alert("The year must be a value between 1752 and 2078. Please try again.");
		} else {
			alert("Invalid date field.  Please try again.");
		}
		datefield.focus();
		return false;
	}
	else {
		return true;
   }
}
function chkdate(objName) {
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear = "";
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return 0;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length < 2 || strDateArray.length > 3) {
				err = 1;
				return err;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				if(strDateArray.length == 3){
					strYear = strDateArray[2];
				}
				else {
					var now = new Date();
					strYear = now.getYear() + '';
				}
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if (strYear.length != 2 && strYear.length != 4){
		err = 11;
		return err;
	}
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
	if(parseInt(strYear) < 1753 || parseInt(strYear) > 2078){
		err = 12;
		return err;
	}

	strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;

	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return err;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return err;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return err;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return err;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return err;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return err;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return err;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return err;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return err;
			}
		}
	}
	datefield.value = padLeft(intMonth,2,'0') + "/" + padLeft(intday,2,'0') + "/" + strYear;
	datefield.fireEvent('onchange');
	return 0;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function doDateCheck(from, to) {
	if (Date.parse(from.value) <= Date.parse(to.value)) {
		alert("The dates are valid.");
	}
	else {
		if (from.value == "" || to.value == "") 
			alert("Both dates must be entered.");
		else 
			alert("To date must occur after the from date.");
	}
}