//==============================================================================
// Dates.js
//==============================================================================
// This file contains all of the JavaScript date functions, including those to
// display the tcaldate.class Java control as a combo box drop-down.
//
// The code for the following functions is contained herein:
//		DoTextBoxDate(oTextBox)
//		FormDate(sDate)
//		IsDate(iDay, iMonth, iYear)
//		IsNum(sString)
//		ClearLeadChaff(sString, sChaff)
//		AllToChaff(sString, sChaff)
//		MonthToNum(sString)
//		NumToMonth(iNum, sFormat)
//		AbsPos(oObj,sXY)
//		ShowDate(sCalDate)
//		CalPutDate(oApplet,oTextBox)
//		ReplaceString(oldS,newS,fullS){
//==============================================================================



//==============================================================================
// DoTextBoxDate(oTextBox)
//==============================================================================
// This function tests the value of an input text box to make sure that it is a
// proper date and, if so, changes the value of the input text box so that the
// date is displayed in the standard format.  If the input text box value is
// not a recognizable date, it displays a warning message and, if the user
// clicks "OK" maintains the focus on the text box.
//
//  oTextBox - the input text box object on which we want to operate
//==============================================================================
function DoTextBoxDate(oTextBox){
	if (oTextBox.value==""){ return; }
	s = FormDate(oTextBox.value,"yyyy-mm-dd")
	if (s.substr(0,1)=="!"){
		//oTextBox.style.backgroundColor='red';
		if (confirm(s.substr(1)+"\rPlease re-enter...")){
			oTextBox.focus();
			oTextBox.select(); }
	}else{
		//oTextBox.style.backgroundColor='white';
		oTextBox.value = s; }
}


//==============================================================================
// FormDate(sDate)
//==============================================================================
// This function tests the value of string to make sure that it is a proper date
// and either returns that date in a string in the requested format or
// returns a descriptive error message.  To distinguish between the date string
// and an error message string, the error message string has "!" as its first
// character.  The presumed date formats that are handled by this function are:
//     m d y --> (m|mm|mmm|mmmm)( |-|/|,|.)(d|dd)( |-|/|,|.)(|yy|yyy|yyyy)
//     d m y --> (d|dd)( |-|/|,|.)(m|mm|mmm|mmmm)( |-|/|,|.)(|yy|yyy|yyyy)
// In case of ambiguity between the two formats, the former is the default.
//
// sDate - the string to test as a valid date
// sFormat - a template of the desired format using the following flags:
//				d - day with no leading zero
//				dd - day with leading zero
//				m - month as number with no leading zero
//				mm - month as number with leading zero
//				mmm - three letter string for the month
//				mmmm - full name of the month
//				yy - two digit representation of the year with leading zeros
//				yyyy - four digit representation of the year
//==============================================================================
function FormDate(sDate, sFormat) {
	var dYear="", dMonth="", dDay="", sChaff=" -/,.";
	sDate = ClearLeadChaff(sDate,sChaff);
	dMonth = AllToChaff(sDate, sChaff);

	sDate = ClearLeadChaff(sDate.substr(dMonth.length),sChaff);
	dDay = AllToChaff(sDate, sChaff);

	sDate = ClearLeadChaff(sDate.substr(dDay.length),sChaff);
	dYear = AllToChaff(sDate, sChaff);

	if(dMonth.length==4){s=dYear; dYear=dMonth; dMonth=s; s=dDay; dDay=dMonth; dMonth=s;}
	sDate = ClearLeadChaff(sDate.substr(dYear.length),sChaff);

	if (ClearLeadChaff(sDate,sChaff) != ""){ return "!Too many terms."; }
	if (!IsNum(dMonth)){
		if (!IsNum(dDay)){ return "!Invalid day."; }
		dMonth = MonthToNum(dMonth); }
	if (!IsNum(dDay)){
		dDay = MonthToNum(dDay);
		s = dDay;
		dDay = dMonth;
		dMonth = s; }
	if ((dMonth<0) || (dDay<0)){ return "!Invalid month."; }
	if (!IsNum(dYear)){ return "!Invalid year."; }
	if ((dYear.length>4) || (dYear.length<=0)){ return "!Invalid year."; }
	if (dYear.length<3){
		d = new Date();
		//dYear = String(d.getYear() + Number(dYear) - ( (Number(dYear)-50 < 0) ? 0 : 100)) }
		dYear = String(Number(dYear) + ( (Number(dYear)-50 < 0) ? 2000 : 1900)) }
	dDay=String(Number(dDay)); dMonth=String(Number(dMonth)); //get rid of any leading 0s
	if (!IsDate(dDay, dMonth, dYear)){
		s = dDay;
		dDay = dMonth;
		dMonth = s;
		if (!IsDate(dDay, dMonth, dYear)){ return "!Invalid date."; }}

	sFormat = ReplaceString("dd", ((dDay.length < 2) ? "0" : "") + dDay, sFormat);
	sFormat = ReplaceString("d", ((dDay.substr(0,1)=="0") ? dDay.substr(1,2) : dDay), sFormat);
	sFormat = ReplaceString("yyyy", dYear, sFormat);
	sFormat = ReplaceString("yy", dYear.substr(dYear.length-2,dYear.length-1), sFormat);
	if (sFormat.indexOf("mmmm")>=0){ sFormat = ReplaceString("mmmm", NumToMonth(dMonth,"mmmm"), sFormat) }
	else if (sFormat.indexOf("mmm")>=0){ sFormat = ReplaceString("mmm", NumToMonth(dMonth,"mmm"), sFormat); }
	else if (sFormat.indexOf("mm")>=0){ sFormat = ReplaceString("mm", ((dMonth.length < 2) ? "0" : "") + dMonth, sFormat) }
	else if (sFormat.indexOf("m")>=0){ sFormat = ReplaceString("m", (dMonth.substr(0,1)="0") ? dMonth.substr(1,2) : dMonth, sFormat) }
	return sFormat;
}


//==============================================================================
// ReplaceString(oldS,newS,fullS){
//==============================================================================
// This function replaces oldS with newS in the string fullS.
//==============================================================================
function ReplaceString(oldS,newS,fullS){
	for (var i=0; i<fullS.length; i++){
		if (fullS.substring(i,i+oldS.length) == oldS){
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length) }}
	return fullS
}


//==============================================================================
// IsDate(iDay, iMonth, iYear)
//==============================================================================
// This function tests to see if the passed value constitue a valid date.
//==============================================================================
function IsDate(iDay, iMonth, iYear){
	switch (iMonth){
	case "1": case "3": case "5": case "7": case "8": case "10": case "12":
		if (iDay > 31){
			return false; }
		break;
	case "4": case "6": case "9": case "11":
		if (iDay > 30){
			return false; }
		break;
	case "2":
		if (iDay > (((iYear%4==0) && ((!(iYear%100==0)) || (iYear%400==0))) ? 29 : 28 )){
			return false; }
		break;
	default: return false;
	}
	return true;
}


//==============================================================================
// IsNum(sString)
//==============================================================================
// This function tests to see if the passed value is entirely constituted of the
// n umbers 0 to 9
//==============================================================================
function IsNum(sString){
	for (i=0;i<sString.length;i++){
		s = sString.substr(i,1);
		if (s < "0" || s > "9") { return false; }}
	return true;	
}

//==============================================================================
// ClearLeadChaff(sString, sChaff)
//==============================================================================
// This function accepts as input a string (sString) and returns as output the
// same string but without any of the leading characters that might be found in
// the second argument string (sChaff).
//==============================================================================
function ClearLeadChaff(sString, sChaff){
	if (sString.length == 0){
		return ""; }
	if (sChaff.indexOf(sString.substr(0,1))>=0){
		sString = ClearLeadChaff(sString.substr(1),sChaff); }
	return sString;
}


//==============================================================================
// AllToChaff(sString, sChaff)
//==============================================================================
// This function accepts as input a string (sString) and returns as output all 
// of the characters, if any, from the start of that string up to but not
// including any character in the second argument string (sChaff).
//==============================================================================
function AllToChaff(sString, sChaff){
	var sRtn="";
	for (i=0;i<sString.length;i++){
		if (sChaff.indexOf(sString.substr(i,1))>=0){
			break;}
		sRtn+=sString.substr(i,1);}
	return sRtn;
}


//==============================================================================
// MonthToNum(sString)
//==============================================================================
// This function tests to see if the passed string is the name of a month or any
// valid abreviation of the name of a month and returns the numerical equivalent
// of that month.  If the passed string is not a valid month or abreviation, the
// number -1 is returned.
//==============================================================================
function MonthToNum(sString){
	switch (sString.toUpperCase()){
	case "JA": case "JAN": case "JANUARY": return "1"; break;
	case "FE": case "FEB": case "FEBRUARY": return "2"; break;
	case "MR": case "MAR": case "MARCH": return "3"; break;
	case "AP": case "APR": case "APRIL": return "4"; break;
	case "MY": case "MAY": case "MAY": return "5"; break;
	case "JN": case "JUN": case "JUNE": return "6"; break;
	case "JL": case "JUL": case "JULY": return "7"; break;
	case "AU": case "AUG": case "AUGUST": return "8"; break;
	case "SE": case "SEP": case "SEPTEMBER": return "9"; break;
	case "OC": case "OCT": case "OCTOBER": return "10"; break;
	case "NO": case "NOV": case "NOVEMBER": return "11"; break;
	case "DE": case "DEC": case "DECEMBER": return "12"; break; }
	return -1;
}


//==============================================================================
// NumToMonth(iNum, sFormat)
//==============================================================================
// This function returns a string showing the name or an abreviation of the name
// of the month of the passed integer.  If not a valid month number, it returns
// "".  The format of the returning string is indicated in the second parameter
// by sending "mmmm" for the full month name or "mmm" (default) for a three
// letter abreviation.
//==============================================================================
function NumToMonth(iNum, sFormat){
	switch (iNum){
	case "1": case "01": return (sFormat=="mmmm") ? "January" : "Jan"; break;
	case "2": case "02": return (sFormat=="mmmm") ? "February" : "Feb"; break;
	case "3": case "03": return (sFormat=="mmmm") ? "March" : "Mar"; break;
	case "4": case "04": return (sFormat=="mmmm") ? "April" : "Apr"; break;
	case "5": case "05": return (sFormat=="mmmm") ? "May" : "May"; break;
	case "6": case "06": return (sFormat=="mmmm") ? "June" : "Jun"; break;
	case "7": case "07": return (sFormat=="mmmm") ? "July" : "Jul"; break;
	case "8": case "08": return (sFormat=="mmmm") ? "August" : "Aug"; break;
	case "9": case "09": return (sFormat=="mmmm") ? "September" : "Sep"; break;
	case "10": return (sFormat=="mmmm") ? "October" : "Oct"; break;
	case "11": return (sFormat=="mmmm") ? "November" : "Nov"; break;
	case "12": return (sFormat=="mmmm") ? "December" : "Dec"; break;
	}
	return "";
}



////////////////////////////////////////////////////////////////////////////////
//============================================================================//
// The following code is required so that a page can use the tcaldate.class   //
// Java applet control as a combo box drop-down.  The Java applet should be   //
// on the page between a set of "<div></div>" tags.                           //
//                                                                            //
//                                                                            //
IE4 = ( (navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ) )
//if (!IE4){alert("You are not using a compatible browser.  Some of the functionality of this page may not be availalbe.\r\rThis site should be viewed with Internet Explorer version 4.x or later."); }
var oCurBox = 0, oCurDiv=0;
//                                                                            //
//============================================================================//
// PopUp(oCall, oDiv)
//============================================================================//
// This function either displays the oDiv object layer contents at a position
// directly below the oCall object, or hides the layer contents if this
// function has been called for a second time in a row by the same object.  This
// function also keeps track of which object is currently being used in the
// varriable oCurrent so that this information is persistant between function
// calls.
//============================================================================//
function PopUp(oCall, oDiv){
	if (IE4) {
		if (oCall != oCurBox) {
			oDiv.style.pixelLeft = AbsPos(oCall,"x");
			oDiv.style.pixelTop = AbsPos(oCall,"y")+oCall.offsetHeight;
			oDiv.style.visibility = "visible";
			oCurBox = oCall;
			oCurDiv = oDiv; }
		else if(oDiv!=0) {
			oDiv.style.visibility = "hidden";
			oCurBox = 0; } }
}
//                                                                            //
//============================================================================//
// AbsPos(oObj,sXY)
//============================================================================//
// This function returns the absolute postion of an object (sObj) on the
// document in either the horizontal (sXY = "x") or verticle (sXY = "y")
// direction.
//============================================================================//
function AbsPos(oObj,sXY) {
	var iPos = 0;
	while ((oObj.tagName != "BODY") && (oObj.tagName != "DIV")) {
		if (sXY == "x") { iPos += oObj.offsetLeft; }
		else            { iPos += oObj.offsetTop; }
		oObj = oObj.offsetParent; }
	return iPos;
}
//                                                                            //
//============================================================================//
// ShowDate(sCalDate)
//============================================================================//
// This function receives a string as input from the tcaldate.class Java applet
// in the exact form of yyyy/mm/dd and displays this date value in the standard
// form of "yyyy-mm-dd".
//============================================================================//
function ShowDate(sCalDate) {
	if (IE4){
		//sDate = sCalDate.substr(8,2)
		//if (sDate.substr(0,1)=="0"){ sDate=sDate.substr(1,1); }
		//sDate += " " + NumToMonth(sCalDate.substr(5,2),"ddd") + " " + sCalDate.substr(0,4); }
		sDate = sCalDate.substr(0,4) + "-" + sCalDate.substr(5,2) + "-" + sCalDate.substr(8,2)
	}
	oCurBox.value = sDate;
	PopUp(oCurBox,oCurDiv);
}
//                                                                            //
//============================================================================//
// CalPutDate(oApplet,oTextBox)
//============================================================================//
// This function places the date that is currently in the passed text box
// (if it's a valid date) into the Java applet control.
//============================================================================//
function CalPutDate(oApplet,oTextBox){
	s = oTextBox.value
	if (s.substr(1,1)==" "){
		iD=s.substr(0,1);
		iM=MonthToNum(s.substr(2,3));
		iY=s.substr(6,s.length-1); }
	else {
		iD=s.substr(0,2);
		iM=MonthToNum(s.substr(3,3));
		iY=s.substr(7,s.length-1); }
	if ( IsDate(iD,iM,iY) ){ oApplet.putDate(iD,iM,iY) }
}
//                                                                            //
// ... end the tcaldate.class Java applet control code                        //
//============================================================================//
////////////////////////////////////////////////////////////////////////////////


