//  Date/Time Library
// (c) 2008 MRI
//
// Provides date and calendar functions.
//
// Public Interface:

//      GetDateString(Date)         Returns a string formatted month/day/4-digit year, e.g. 01/01/2008
//      GetTimrString(Date)         Returns a string formatted hour:minute a|p, e.g. 12:00a

//      isDate(Date String)         Returns true if the string is a date
//      getDateKey(Date)            Returns an ordinal date string: year/month/day
//      getDateTimeKey(Date)        Returns an ordinal time string: getDateKey + Hour + Minute + Second
//      getShortMonthName(Date)     Returns the short month of some date: Jan, Feb, Mar...
//      getMonthName(Date)          Returns the month of some date: January, February, March...
//      getShortWeekdayName(Date)   Returns the short weekday name of some date: Mon, Tue, Wed...
//      getWeekdayName(Date)        Returns the weekday name of some date: Monday, Tuesday, Wednesday...
//      getAddDay(Date, Index)      Returns a date modified by the specified number of days
//      getAddMonth(Date, Index)    Returns a date modified by the specified number of months     

//      FindCalendarDate(Date, List)
//          Returns the first occurrence of the specified date in an array of dates,
//          returning -1 if the date is not found.

//      Pop-Up Calendar Routines

//      onCalendarClose             Default calendar close function
//      GetCalendarCell(dDate,aList,aIgnoreList,sCellClass,sSelectedClass,sIgnoreClass,iSingle,bFuture) {
//      GetCalendarMonth(dtStr,sDateControl,sIgnoreControl,sTableClass,sDayHeaderClass,sCellClass,sSelectedClass,sEmptyClass,sIgnoreClass,iSingle,bFuture) {

var sBaseCalendarURL = "/";

function GetDateString(dDate) {
	if (dDate != "") {
		return ZeroStr(dDate.getMonth()+1,2) + "/" + ZeroStr(dDate.getDate(),2) + "/" + dDate.getFullYear();
	}
}

function GetTimeString(dDate) {
	if (dDate != "") {
		return(ZeroStr(siif((dDate.getHours() < 13),dDate.getHours(),dDate.getHours()-12),2)+":"+
			ZeroStr(dDate.getMinutes(),2) + siif((dDate.getHours() < 12),"a","p"));
	}
}

function GetDateTimeKey(dDate) {
    if (dDate != "") {
        return dDate.getFullYear() +
                ZeroStr(dDate.getMonth() + 1, 2) +
                ZeroStr(dDate.getDate(), 2) +
                ZeroStr(dDate.getHours(), 2) +
                ZeroStr(dDate.getMinutes(), 2) +
                ZeroStr(dDate.getSeconds(), 2) +
                ZeroStr(dDate.getMilliseconds(), 2);
    }
}

var pOnCalendarClose = function() {
	oDiv.style.visibility = 'hidden';
}

function onCalendarClose() {
	pOnCalendarClose();
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
//  if (isNaN(dtStr)) dtStr = '';
	if (dtStr == '') return true;

	var daysInMonth = new Array();

	daysInMonth[1] = 31;
	daysInMonth[2] = 28;
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;
		
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr = strYear

	if (parseInt(strYear) % 4 == 0) daysInMonth[2] = 29;
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	var month=parseInt(strMonth)
	var day=parseInt(strDay)
	var year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
//		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false
	}
return true
}

function getShortMonthName(dDate) {
	var iMonth = dDate.getMonth();
	var months = new Array(12);
   	months[0]  = "Jan";
   	months[1]  = "Feb";
   	months[2]  = "Mar";
   	months[3]  = "Apr";
   	months[4]  = "May";
   	months[5]  = "Jun";
   	months[6]  = "Jul";
   	months[7]  = "Aug";
   	months[8]  = "Sep";
   	months[9]  = "Oct";
   	months[10] = "Nov";
   	months[11] = "Dec";
	return months[iMonth]
}

function getMonthName(dDate) {
	var iMonth = dDate.getMonth();
	var months = new Array(12);
   	months[0]  = "January";
   	months[1]  = "February";
   	months[2]  = "March";
   	months[3]  = "April";
   	months[4]  = "May";
   	months[5]  = "June";
   	months[6]  = "July";
   	months[7]  = "August";
   	months[8]  = "September";
   	months[9]  = "October";
   	months[10] = "November";
   	months[11] = "December";
	return months[iMonth]
}

function getShortWeekdayName(dDate) {
	var iDay = dDate.getDay();
	var days = new Array(7);
   	days[0]  = "Sun";
   	days[1]  = "Mon";
   	days[2]  = "Tue";
   	days[3]  = "Wed";
   	days[4]  = "Thu";
   	days[5]  = "Fri";
   	days[6]  = "Sat";
	return days[iDay]
}

function getWeekdayName(dDate) {
	var iDay = dDate.getDay();
	var days = new Array(7);
   	days[0]  = "Sunday";
   	days[1]  = "Monday";
   	days[2]  = "Tuesday";
   	days[3]  = "Wednesday";
   	days[4]  = "Thursday";
   	days[5]  = "Friday";
   	days[6]  = "Saturday";
	return days[iDay]
}

function getAddDay(dDate,iIndex) {
	return new Date(dDate.valueOf() + (iIndex*24*3600*1000));
}

function getAddMonth(dDate,iIndex) {
	var iMonth = dDate.getMonth() + iIndex;
	var iYear  = dDate.getFullYear();
	var iDay   = dDate.getDate();
	
	if (iMonth < 0) {
		iMonth = 11;
		iYear  = iYear - 1;
	} else if (iMonth > 11) {
		iMonth = 0;
		iYear  = iYear + 1;
	}

	if ((iMonth == 1) && (iDay > 27)) {
		iDay = 27;
	} else if ((iDay == 31) && (iMonth != 2)) {
		if ((iMonth == 3) || (iMonth == 5) || (iMonth == 8) || (iMonth == 10)) {
			iDay = 30;
		}
	}

//	alert((iMonth+1) + "/" + iDay + "/" + iYear);
	var dReturn = new Date((iMonth+1) + "/" + iDay + "/" + iYear);
	return dReturn;
}

function getDateKey(dDate) {
	var iMonth = dDate.getMonth() + 1;
	var iYear  = dDate.getFullYear();
	var iDay   = dDate.getDate();
	return(ZeroStr(iYear,4) + ZeroStr(iMonth,2) + ZeroStr(iDay,2));
}

function getDateTimeKey(dDate) {
	var iHour  = dDate.getHours();
	var iMin   = dDate.getMinutes();
	var iSec   = dDate.getSeconds();
	return(getDateKey(dDate) + ZeroStr(iHour,2) + ZeroStr(iMin,2) + ZeroStr(iSec,2));
}

function FindCalendarDate(sDate,aList) {
	var i;
	var dCheck;
	var iReturn = -1;
	var dDate   = new Date(sDate);
	
	for (i=0;i < aList.length;i++) {
		dCheck = new Date(aList[i]);
		
		if (GetDateString(dDate) == GetDateString(dCheck)) {
			iReturn = i;
			break;
		} 	
	}
	return iReturn	
}

function GetCalendarCell(dDate,aList,aIgnoreList,sCellClass,sSelectedClass,sIgnoreClass,iSingle,bFuture) {
	var s;
	var sClass;
	var sParam = '"' + GetDateString(dDate) + '"';
	var dNow   = new Date();
	var sPointer = '"pointer"';
	
	if ((bFuture) && (dDate < dNow)) {
		sClass = sIgnoreClass;
	} else if (FindCalendarDate(GetDateString(dDate),aIgnoreList) == -1) {
		if (FindCalendarDate(GetDateString(dDate),aList) > -1) {
			sClass = sSelectedClass;
		} else {
			sClass = sCellClass;
		}
	} else {
		sClass = sIgnoreClass;
	}

	s = "<td class='" + sClass + "' align='right' onclick='onCalendarCellClick(" + 
	    sParam + "," + iSingle + "," + siif(bFuture,"true","false") + ");' " +
	    "onmouseover='this.style.cursor=" + sPointer + "'>" +
		siif((sClass == sIgnoreClass), "", "") +
		"<span class='" + sClass + "' style='text-decoration: none'>" + dDate.getDate() + "</span>" +
		siif((sClass == sIgnoreClass),"","") + "</td>" + CRLF;
		
	return s;
}

function GetCalendarMonth(dtStr,sDateControl,sIgnoreControl,sTableClass,sDayHeaderClass,sCellClass,sSelectedClass,sEmptyClass,sIgnoreClass,iSingle,bFuture) {
	var s;
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var iMonth = parseInt(strMonth);
	var bDone=false;
	var iRows=1;
	var iDay=1;
	var dDate=new Date(dtStr);
	var dToday=new Date();
	var iMonth=dDate.getMonth()+1;
	var dFirst=new Date(iMonth+'/01/'+dDate.getFullYear());
	var dCurrent = new Date(GetDateString(dFirst));
	var dNext;
	var sNext;
	var sPointer      = '"pointer"';
	var dPreviousDate = getAddMonth(dDate,-1);
	var dNextDate	  = getAddMonth(dDate,1);
	var sPreviousDate = '"' + GetDateString(dPreviousDate) + '"';
	var sNextDate     = '"' + GetDateString(dNextDate) + '"';
	var sToday        = '"' + GetDateString(dToday) + '"';
	var sDates        = document.getElementById(sDateControl).value;
	var aDates        = DecodeDelimittedList(sDates,",");
	var sIgnore       = document.getElementById(sIgnoreControl).value;
	var aIgnore       = DecodeDelimittedList(sIgnore,",");
	
	s = "<table class='" + sTableClass + "' style='z-index:99'>" + CRLF +
	    "<tr><td colspan=7 class='" + sSelectedClass + "' align='center'>" + CRLF +
	    "<table border=0 cellpadding=0 cellspacing=0 width='100%'><tr>" + CRLF +
		"<td align='center' class='" + sSelectedClass + "'>" + getShortMonthName(dDate) + " " + dDate.getFullYear() + "</td>" + CRLF +
		"<td width='18' align='center' onmouseover='this.style.cursor=" + sPointer + "' onclick='onCalendarClick(" + sPreviousDate + ",-1," + iSingle + "," + siif(bFuture, "true", "false") + ");'><img src='" + sBaseCalendarURL + "images/calprevious.gif' title='Previous Month' alt='Previous Month' height=16 width=16 border=0></td>" + CRLF +
		"<td width='18' align='center' onmouseover='this.style.cursor=" + sPointer + "' onclick='onCalendarClick(" + sNextDate + ",1," + iSingle + "," + siif(bFuture, "true", "false") + ");'><img src='" + sBaseCalendarURL + "images/calnext.gif' title='Next Month' alt='Next Month' height=16 width=16 border=0></td>" + CRLF +
		"<td width='18' align='center' onmouseover='this.style.cursor=" + sPointer + "' onclick='onCalendarClick(" + sToday + ",1," + iSingle + "," + siif(bFuture, "true", "false") + ");'><img src='" + sBaseCalendarURL + "images/calnow.gif' title='Current Month' alt='Current Month' height=16 width=16 border=0></td>" + CRLF +
		"<td width='18' align='center' onmouseover='this.style.cursor=" + sPointer + "' onclick='onCalendarClose();'><img src='" + sBaseCalendarURL + "images/calclose.gif' title='Close' alt='Close' height=16 width=16 border=0></td>" + CRLF;
	
	s = s + "</tr></table>" + CRLF + "</td></tr>" + CRLF;
		
    s = s + "<tr>" + CRLF +
			"<td class='" + sDayHeaderClass + "' align='center'>S</td>" + CRLF +
			"<td class='" + sDayHeaderClass + "' align='center'>M</td>" + CRLF +
			"<td class='" + sDayHeaderClass + "' align='center'>T</td>" + CRLF +
			"<td class='" + sDayHeaderClass + "' align='center'>W</td>" + CRLF +
			"<td class='" + sDayHeaderClass + "' align='center'>R</td>" + CRLF +
			"<td class='" + sDayHeaderClass + "' align='center'>F</td>" + CRLF +
			"<td class='" + sDayHeaderClass + "' align='center'>S</td>" + CRLF +
			"</tr>" + CRLF;

	while (!bDone) {
		s = s + "<tr>" + CRLF;
				
		if (iRows == 1) {
			if (dFirst.getDay() > 0) {
				for (i=0;i<dFirst.getDay();i++) {
					s = s + "<td class='" + sEmptyClass + "'></td>" + CRLF;
				}	
			}
				
			for (i=0;i<6-dFirst.getDay()+1;i++) {
				s    = s + GetCalendarCell(dCurrent,aDates,aIgnore,sCellClass,sSelectedClass,sIgnoreClass,iSingle,bFuture);
				iDay = iDay + 1;
				dCurrent.setDate(iDay);
			}
		} else {
			for (i=0;i<=6;i++) {
				sNext = (dDate.getMonth()+1) + "/" + (iDay+1) + "/" + dDate.getFullYear();
				dNext = new Date(sNext);
				
				if (isDate(sNext)) {
					s = s + GetCalendarCell(dCurrent,aDates,aIgnore,sCellClass,sSelectedClass,sIgnoreClass,iSingle,bFuture);
					
				} else {
				    sNext = (dDate.getMonth()+1) + "/" + iDay + "/" + dDate.getFullYear();
					dNext =	new Date(sNext);
						
					if (isDate(sNext)) {
						s    = s + GetCalendarCell(dCurrent,aDates,aIgnore,sCellClass,sSelectedClass,sIgnoreClass,iSingle,bFuture);
					} else {
						s     = s + "<td class='" + sEmptyClass + "'></td>" + CRLF;
						bDone = true;
					}
				}
				
				iDay = iDay + 1;
				dCurrent.setDate(iDay);
			}
		}
						
		s     = s + "</tr>" + CRLF;	
		iRows = iRows + 1;	
		
		if ((!bDone) && (iRows == 6)) {
		    bDone = (iDay > 31);
		}
		
//		if ((!bDone) && (iMonth == 2) && (iRows > 4)) {
//		    bDone = (iDay > 28);
//		}
	}
	
	s = s + '</table>';
	return s;
}




