
/*
	Calendar Control:
-used for date entries into text fields in forms on web pages
-use the following example to use the calendar control:
	-Copy the following files into the "CalendarControlJS1.2" directory
		(put it in the root directory of the server)
		-calendarBottom.asp
		-calendarTop.asp
		-include_calendar.js

	-Include the JavaScript file:
		<SCRIPT language="JavaScript1.1" src="include_ClientSide/CalendarControl1.2/include_calendar.js"></SCRIPT>

	-Make sure you actually name the form on the web page:
		<FORM name="frmTestCal" method="post" action="ThisPage.asp">

	-For the text area, be sure to give it a name:
		<INPUT type="text" name="txtDate1_dat" size="20">

	-For the command button/image, set the onClick event as follows:
	 (the name and value of the button makes no difference)
		<INPUT type="button" name="cmdCal1" value="\/"
		 onClick="openCalendarWindow(window.document.frmTestCal.txtDate1_dat,txtDate1_dat.value,'AmdY','<%=Application("WebVirtualRoot")%>include_ClientSide/CalendarControlJS1.2/');">
	 or
		<IMG src="<%=Application("WebVirtualRoot")%>include_ClientSide/CalendarControlJS1.2/images/icon_calendar.gif" alt="Calendar Control" vspace=0 hspace=0 border=0
		 onClick="openCalendarWindow(window.document.frmTestCal.txtDate1_dat,txtDate1_dat.value,'AmdY','<%=Application("WebVirtualRoot")%>include_ClientSide/CalendarControlJS1.2/');">

-the Calendar Control takes in 4 parameters:
	1. target text field - where the output of the control should go to
	2. initial value - starting date the calendar should default to
		(if none is given or the one read is invalid, the current month and year is displayed)
	3. format options - a string of characters describing what format the output should be in:
		(a)merican format	--> mm/dd/yy
		(A)merican format	--> Month dd, yyyy
		(b)ritish format	--> dd/mm/yy
		(B)ritish format	--> dd Month, yyyy
		(s)ortable format	--> yy/mm/dd
		(S)ortable format	--> yyyy, Month dd

		(d)ay of week		--> prepends 'Mon.' to beginning of the output string
		(D)ay of week		--> prepends 'Monday' to beginning of the output string

		(y)ear				--> forces 2 digit year
		(Y)ear				--> forces 4 digit year

		(m)onth				--> forces month name to be displayed as Jan.

		(F)					--> fixed width dates (months and days have 2 digits, years have 4)

		-standard format (if no options are given) is "a"
		-if multiple formats are used, the characters towards the end of this list will be the ones used.
			(ie. options = "aABsdD" basically results in having options = "sD")
	4. calendar path - the path from the root of the web to the location of the Calendar Control files
*/


// --- global variables (begin) ---

var targetField;
var newwin = new Object();

var arrMonthLong, arrMonthShort, arrDayNameLong, arrDayNameShort;
arrMonthLong = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
arrMonthShort = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
arrDayNameLong  = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
arrDayNameShort = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

var strCalendarControlPath;

var dateToday, hdnMonth, hdnDate, hdnYear, hdnFormat;
var args;
args = getArgs();

// default values
	hdnFormat = 0;
	dateToday = new Date();
	hdnMonth = dateToday.getMonth();
	hdnDay = dateToday.getDate();
	hdnYear = dateToday.getYear() + 1900;

// input values
	if (args.hdnMonth)  hdnMonth  = parseInt(args.hdnMonth);
	if (args.hdnDay)    hdnDay    = parseInt(args.hdnDay);
	if (args.hdnYear)   hdnYear   = parseInt(args.hdnYear);
	if (args.hdnFormat) hdnFormat = args.hdnFormat;

// y2k modification
	if ((hdnYear >= 0) && (hdnYear < 50))  hdnYear = hdnYear+2000;
	if ((hdnYear > 49) && (hdnYear < 200)) hdnYear = hdnYear+1900;

// --- global variables (end) ---



function getArgs() {
//  gets any arguments passed into the querystring
	var args = new Object();

	var strQuery = location.search.substring(1);		// get query string
	var arrArgPairs = strQuery.split("&")				// break at commas
	for (i = 0; i < arrArgPairs.length; i++) {
		var pos = arrArgPairs[i].indexOf('=');			// look for "name=value"
		if (pos == -1) continue;						// if not found, skip
		var argName = arrArgPairs[i].substring(0,pos);	// extract the name
		var value = arrArgPairs[i].substring(pos+1);	// extract the value
		args[argName] = unescape(value);				// store as a property
	}
	
	return args;
}

function itChanged() {
//  is triggered whenever the values of either of the selection boxes (month or year) are changed
	hdnMonth = frmCalendar.selMonth.value;
	hdnYear = frmCalendar.selYear.value;
	buildCalendar(hdnMonth+"/1/"+hdnYear,hdnFormat);
}

function buildCalendar(thisDate,hdnFormat) {
//  (essentially) reloads the bottom frame (calendar) to the new month and year
	var arrDate = thisDate.split("/");
	//strCalendarControlPath = window.parent.opener.strCalendarControlPath; // unnecessary, but still may be useful at a later point
	top.frames['fraCalBottom'].document.location.href = "calendarBottom.asp?hdnMonth=" + arrDate[0] + "&hdnDay=" + arrDate[1] + "&hdnYear=" + arrDate[2] + "&hdnFormat='" + hdnFormat + "'";
}

function updateField(value) {
// updates the target field and closes the calendar window
	targetField.value = value;
	targetField.focus();
	newwin.close();
}

function verifyDate(thisDate) {
// returns true if the date is valid (mm/dd/yy)
	var arrDate = thisDate.split("/");

	if ((isNaN(arrDate[0])) || (arrDate[0] < 1) || (arrDate[0] > 12)) return false;
	if ((isNaN(arrDate[1])) || (arrDate[1] < 1) || (arrDate[1] > 31)) return false;
	if ((isNaN(arrDate[2])) || (arrDate[2] < 0)) return false;
	return true;
}

function openCalendarWindow(targetLocation,hdnDate,hdnFormat,rstrCalendarPath) {
// sets the calendar control's path for use in the rest of the functions
	strCalendarControlPath = rstrCalendarPath;
// opens the calendar window and creates the frameset, calling up top and bottom frames
	newwin = window.open(strCalendarControlPath+"calendarTop.asp","CalendarWindow","height=220,width=200,status=no,toolbar=no,menubar=no,location=no,top=100,left=200",true);
// calendarTop.asp is used because it is essentially a blank screen and quick to reload (it's written over right away anyways!)

	var hdnMonth, hdnDay, hdnYear;
	targetField = targetLocation;

	if (verifyDate(hdnDate)) {
		var arrDate = hdnDate.split("/");
		hdnMonth = arrDate[0] - 1;
		hdnDay = arrDate[1];
		hdnYear = arrDate[2];
	} else {
		var today = new Date();
		hdnMonth = today.getMonth();
		hdnDay = today.getDate();
		//hdnYear = today.getYear() + 1900;
		hdnYear = today.getYear();
		if (hdnYear < 1000) hdnYear += 1900;
	}

	// if hdnFormat was never declared in the calling function, set it to '0' (not 'undefined')
	var new_undefined_variable;
	if (hdnFormat == new_undefined_variable) { hdnFormat = "0"; }

	newwin.document.writeln();
	newwin.document.writeln();
	newwin.document.writeln();
	newwin.document.writeln("<HTML><HEAD><TITLE>Calendar Control &nbsp; &nbsp; &nbsp; </TITLE></HEAD><FRAMESET rows='28,*' frameborder=0 framespacing=0>");
	newwin.document.writeln("<FRAME src=\""+strCalendarControlPath+"calendarTop.asp?hdnMonth=",hdnMonth,"&hdnDay=",hdnDay,"&hdnYear=",hdnYear,"&hdnFormat='",hdnFormat,"'\" scrolling=no frameborder=0 marginheight=0 marginwidth=0 name='fraCalTop'>");
	newwin.document.writeln("<FRAME src=\""+strCalendarControlPath+"calendarBottom.asp?hdnMonth=",hdnMonth,"&hdnDay=",hdnDay,"&hdnYear=",hdnYear,"&hdnFormat='",hdnFormat,"'\" scrolling=no frameborder=0 marginheight=0 marginwidth=0 name='fraCalBottom'>");
	newwin.document.writeln("</FRAMESET></HTML>");
	newwin.document.close();
}

function goNxtMth() {
	if (document.frmCalendar.selMonth.selectedIndex == document.frmCalendar.selMonth.length-1) {
		if (document.frmCalendar.selYear.selectedIndex == document.frmCalendar.selYear.length-1) {
			//alert('You cannot go beyond this date.');
			alert('If you wish to go beyond this date, please close this window, type in the approximate date desired and then click the calendar control again.');
		} else {
			document.frmCalendar.selMonth.selectedIndex = 0;
			document.frmCalendar.selYear.selectedIndex++;
		}
	} else {
		document.frmCalendar.selMonth.selectedIndex++;
	}
	itChanged();
}

function goPrvMth() {
	if (document.frmCalendar.selMonth.selectedIndex == 0) {
		if (document.frmCalendar.selYear.selectedIndex == 0) {
			//alert('You cannot go beyond this date.');
			alert('If you wish to go beyond this date, please close this window, type in the approximate date desired and then click the calendar control again.');
		} else {
			document.frmCalendar.selMonth.selectedIndex = document.frmCalendar.selMonth.length-1;
			document.frmCalendar.selYear.selectedIndex--;
		}
	} else {
		document.frmCalendar.selMonth.selectedIndex--;
	}
	itChanged();
}

function doCalendarTop() {
	document.write('<INPUT type="button" value=" &lt; " style="font-size: 8pt;" onClick="goPrvMth();">');
	document.write('&nbsp;');

// Month selection box
	document.write("<SELECT name='selMonth' onChange='itChanged();'>");
	for (i = 0; i < 12; i++) {
		document.write("<OPTION value=",i);
		if (i == hdnMonth) {
			document.write(" SELECTED");
		}
		document.write(">",arrMonthLong[i],"</OPTION>");
	}
	document.write("</SELECT>");

// Year selection box
	document.write("<SELECT name='selYear' onChange='itChanged();'>");
	for (i = hdnYear - 4; i < hdnYear + 5; i++) {
		document.write("<OPTION value=",i);
		if (i == hdnYear) {
			document.write(" SELECTED");
		}
		document.write(">",i,"</OPTION>");
	}
	document.write("</SELECT>");

	document.write('&nbsp;');
	document.write('<INPUT type="button" value=" &gt; " style="font-size: 8pt;" onClick="goNxtMth();">');
}

function doCalendarBottom() {
	var dateMonth1, dateMonth1Next, intDayCounter, strOutputData, intDaysThisMonth;
	var pos, strOutYear, strOutMonth, strOutDate, strOutForm;
	var dateToday, intTodayDay, intTodayMonth, intTodayYear;

	dateMonth1 = new Date(hdnYear, hdnMonth, 1);			// the first day of the requested month
	dateMonth1Next = new Date(hdnYear, hdnMonth, 1);		// the first day of the requested month
	dateMonth1Next.setMonth(dateMonth1Next.getMonth()+1);	// the first day of the next month after the requested month
	dateMonth1Next.setDate(dateMonth1Next.getDate()-1);		// the last day of the requested month

	document.write("<TR>");
	intDayCounter = 0;										// counts which day of the week it is currently on

	while (intDayCounter < dateMonth1.getDay()) {			// filler cells before the 1st of the month
		top.frames['fraCalBottom'].document.write("<TD class=dayEmpty></TD>");
		intDayCounter++;
	}

	intDaysThisMonth = dateMonth1Next.getDate();
	dateToday = new Date();
	intTodayDate = dateToday.getDate();
	intTodayMonth = dateToday.getMonth();
	intTodayYear = dateToday.getYear();

	for (i = 1; i <= intDaysThisMonth; i++) {
		document.write("<TD class=day");
		if ((i == intTodayDate) && (dateMonth1.getMonth() == intTodayMonth) && (dateMonth1.getYear() == intTodayYear)) {
			document.write("Today");
		} else {
			document.write("Month");
		}
		document.write(" align='center' bgcolor='yellow'><A href=\"calendarBottom.asp?hdnMonth=",dateMonth1.getMonth(),"&hdnDay=",i,"&hdnYear=",dateMonth1.getYear(),"&hdnFormat='",hdnFormat,"'\" onClick=\"self.parent.opener.updateField('");

		strOutputData = "";

	// which format to output the data
		strOutYear = hdnYear;
		strOutMonth = hdnMonth;
		strOutDate = i;
		strOutForm = "a";

		if (hdnFormat == "undefined") { hdnFormat = ""; }

		pos = hdnFormat.indexOf('d');
		if (pos != -1) { strOutputData = arrDayNameShort[intDayCounter] + ". "; }
		pos = hdnFormat.indexOf('D');
		if (pos != -1) { strOutputData = arrDayNameLong[intDayCounter] + ", "; }

		pos = hdnFormat.indexOf('Y');
		if (pos != -1) {
			if ((strOutYear >= 0) && (strOutYear < 50))  strOutYear = strOutYear+2000;
			if ((strOutYear > 49) && (strOutYear < 200)) strOutYear = strOutYear+1900;
		}
		pos = hdnFormat.indexOf('y');
		if (pos != -1) {
			strOutYear = strOutYear.toString();
			strOutYear = strOutYear.substring(strOutYear.length-2,strOutYear.length);
		}

		pos = hdnFormat.indexOf('a');
		if (pos != -1) { strOutForm = "a"; }
		pos = hdnFormat.indexOf('A');
		if (pos != -1) { strOutForm = "A"; }
		pos = hdnFormat.indexOf('b');
		if (pos != -1) { strOutForm = "b"; }
		pos = hdnFormat.indexOf('B');
		if (pos != -1) { strOutForm = "B"; }
		pos = hdnFormat.indexOf('s');
		if (pos != -1) { strOutForm = "s"; }
		pos = hdnFormat.indexOf('S');
		if (pos != -1) { strOutForm = "S"; }
		pos = hdnFormat.indexOf('F');
		if ((pos != -1) && ((strOutForm == "a") || (strOutForm == "b") || (strOutForm == "s"))) { strOutForm += "Fixed"; }

		switch(strOutForm) {
			case 'a':
				strOutputData += (strOutMonth+1) + "/";
				strOutputData += strOutDate + "/";
				strOutputData += strOutYear;
				break;
			case 'aFixed':
				if (strOutMonth < 9)
					{ strOutputData += "0" + (strOutMonth+1) + "/"; }
				else
					{ strOutputData +=       (strOutMonth+1) + "/"; }
				if (strOutDate < 10)
					{ strOutputData += "0" +  strOutDate + "/"; }
				else
					{ strOutputData +=        strOutDate + "/"; }
				if (strOutYear < 100)
					{ strOutputData += "00" + strOutYear; }
				else
					{ strOutputData +=        strOutYear; }
				break;
			case 'b':
				strOutputData += strOutDate + "/";
				strOutputData += (strOutMonth+1) + "/";
				strOutputData += strOutYear;
				break;
			case 'bFixed':
				if (strOutDate < 10)
					{ strOutputData += "0" +  strOutDate + "/"; }
				else
					{ strOutputData +=        strOutDate + "/"; }
				if (strOutMonth < 9)
					{ strOutputData += "0" + (strOutMonth+1) + "/"; }
				else
					{ strOutputData +=       (strOutMonth+1) + "/"; }
				if (strOutYear < 100)
					{ strOutputData += "00" + strOutYear; }
				else
					{ strOutputData +=        strOutYear; }
				break;
			case 's':
				strOutputData += strOutYear + "/";
				strOutputData += (strOutMonth+1) + "/";
				strOutputData += strOutDate;
				break;
			case 'sFixed':
				if (strOutYear < 100)
					{ strOutputData += "00" + strOutYear + "/"; }
				else
					{ strOutputData +=        strOutYear + "/"; }
				if (strOutMonth < 9)
					{ strOutputData += "0" + (strOutMonth+1) + "/"; }
				else
					{ strOutputData +=       (strOutMonth+1) + "/"; }
				if (strOutDate < 10)
					{ strOutputData += "0" +  strOutDate; }
				else
					{ strOutputData +=        strOutDate; }
				break;
			case 'A':
				strOutMonth = arrMonthLong[hdnMonth];
				pos = hdnFormat.indexOf('m');
				if (pos != -1) { strOutMonth = arrMonthShort[hdnMonth] + "."; }
				strOutputData += strOutMonth + " ";
				strOutputData += strOutDate + ", ";
				strOutputData += strOutYear;
				break;
			case 'B':
				strOutMonth = arrMonthLong[hdnMonth];
				pos = hdnFormat.indexOf('m');
				if (pos != -1) { strOutMonth = arrMonthShort[hdnMonth] + "."; }
				strOutputData += strOutDate + " ";
				strOutputData += strOutMonth + ", ";
				strOutputData += strOutYear;
				break;
			case 'S':
				strOutMonth = arrMonthLong[hdnMonth];
				pos = hdnFormat.indexOf('m');
				if (pos != -1) { strOutMonth = arrMonthShort[hdnMonth] + "."; }
				strOutputData += strOutYear + ",";
				strOutputData += strOutMonth + " ";
				strOutputData += strOutDate;
				break;
		}

		document.write(strOutputData);
		document.write("')\"><FONT face=arial color=#000000>",i,"</FONT></A></TD>");				// close the day's cell
		intDayCounter++;									// push the counter to the next day of the week
		if (intDayCounter == 7) {							// end of the week
			document.write("</TR><TR>");					// start a new week
			intDayCounter = 0;								// reset the counter back to Sunday
		}
	}

	while (intDayCounter < 7) {								// filler cells after the last day of the month
		top.frames['fraCalBottom'].document.write("<TD class=dayEmpty></TD>");
		intDayCounter++;
	}

	document.write("</TR>");
}

