<!--
	/*
		Program: Calendar.js
		Programmer: Marcos Gonzalez
		Date Written: March 3, 2001
		Description:  This program generates a popup calendar window.  The calendar is dynamic; you can
			select the month and or year.  The calendar style can be changed from standard to european.
		  On the standard calendar the week starts on Sunday; on the european calendar the week starts 
			on Monday.  The calendar color are defined in the Calendar.css (cascading style sheet).
		Note:  This calendar has only been tested On Microsoft IE 5.5 and Netscape 4.75.
		
		Parameters
		Month -  The month the calendar will display.  The default is current month.
		Year -  The year the calendar will display.  The default is current year.
		Field -  This is the form.field name that will be populated when a date is selected.  There is no default.
	*/
	
	var border = 1;  // The size of the table border
	var cellspacing = 0;  // The size of the table cell spacing
	var cellpadding = 0;  // The size of the table cell padding
	var language = 1; 	// language (0 = spanish, 1 = english, 2 = portuguese)
	var style = 0; // style (0 = Standard, 1 = Europeon)
	var dateField = null;  // this will contain the form field object to which the date value will be return to
	//var isIE = (document.all) ? true : false;
	//var isNetscape6 = (document.getElementById&&!document.all) ? true : false;
	//var isNetscape4 = (document.layers) ? true : false;
	var CalendarWindow = null;  // The Calendar Window

	function returnValue(month, year, day) {
	
		day = (day < 10) ? '0' + day : day;
		month = (++month < 10) ? '0' + month : month;

		switch (style) {
			case 0 :
				eval('document.' + dateField + '.value = "' + year + '-' + month + '-' + day + '"');
				//eval('document.' + dateField + '.value = "' + ++month + '/' + day + '/' + year + '"');
			break;
			case 1 :
				eval('document.' + dateField + '.value = "' + day + '/' + month + '/' + year + '"');
			break;
		}
		CalendarWindow.close();
		CalendarWindow = null;
		return;
	}
	
	function setLanguage(value) {
	// Set the language 
	
		language = parseInt(value);
	}

	function setStyle(value, month, year, day) {
	// Set the style of the calendar

		style = parseInt(value);
		setCalendar(month, year);
	}
	
	function setCalendar() {

		var now = new Date();  // Todays date
		
		//  If all the arguments are null show the help screen
		if (arguments[0] == null && arguments[1] == null && arguments[2] == null) {
			helpScreen();
			return;
		}
		
		var item = arguments[2];  // The name of the field to populate
		if (item != null)
			dateField = item;
		
		if (arguments[0] == null)
			var month = now.getUTCMonth();  // This month
		else
			var month = arguments[0];  // Selected month
		
		if (arguments[1] == null)
			var year = now.getUTCFullYear();  // This year
		else
			var year = arguments[1];  // Selected year

		// number of days in current month
		var days = getNumberOfDaysInMonth(month, year);

		if (month == now.getUTCMonth() & year == now.getUTCFullYear())
			var date = now.getUTCDate();
		var now = null;
		
		// create instance of first day of the month and extract the day on which it occurs
		var firstDayInstance = new Date(year, month, 1);
		var firstDay = firstDayInstance.getUTCDay();
		var firstDayInstance = null;

		// if window not yet opened, open window and in which the calendar will be displayed
		windowOpen();

		// call function ot draw the calendar
		drawCalendar(firstDay + 1, days, date, month, year);
	}
	
	// build the calendar
	function drawCalendar(firstDay, lastDate, date, month, year) {
		// variables initialization
		var html = ""; // initialize variable
		var dayOfMonth = 1; // first day of month
		var currentCell = 0; // current table cell

		html += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">\n';
		html += '<html>\n';
		html += '<head>\n';
		html += '\t<title>Calendar</title>\n';
		html += '\t<link rel="stylesheet" type="text/css" href="Includes/Calendar.css">\n';
		html += '</head>\n';
		html += '<body leftmargin=0 topmargin=0 marginheight="0" marginwidth="0">\n';
		html += '<div align="center" id="XYZ">\n';
		html += '<form action="" method="post" name="Calendar">\n';
		html += '<table border="' + border + '" cellpadding="' + cellpadding + '" cellspacing="' + cellspacing + '" class="calendar" align="center" width="100%">\n';
		html += '\t<tr align="center" valign="middle">\n';
		html += '\t\t<td colspan="7" class="monthheading">\n';
		html += '\t\t\t<select name="Month" size="1" onchange="Javascript:window.opener.parent.setCalendar(this[this.selectedIndex].value, document.forms[0].Year.options[document.forms[0].Year.selectedIndex].value)">\n';
		for (var months = 0; months <= 11; months++) {
			if (months == month)
				html += '\t\t\t\t<option value="' + months + '" class="monthheading" selected>' + getMonthName(months) + '</option>\n';
			else		
				html += '\t\t\t\t<option value="' + months + '" class="monthheading">' + getMonthName(months) + '</option>\n';
		}
		html += '\t\t\t</select>&nbsp;\n';
		html += '\t\t\t<select name="Year" size="1" onchange="Javascript:window.opener.parent.setCalendar(document.forms[0].Month.options[document.forms[0].Month.selectedIndex].value, this[this.selectedIndex].value)">\n';
		for (var years = parseInt(year) - 10; years <= parseInt(year) + 10; years++) {
			if (years == year)
				html += '\t\t\t\t<option value="' + years + '" class="monthheading" selected>' + years + '</option>\n';
			else		
				html += '\t\t\t\t<option value="' + years + '" class="monthheading">' + years + '</option>\n';
		}
		html += '\t\t\t</select>\n';
		html += '\t\t</td>\n';
		html += '\t</tr>\n';

		//create the days of the week row
		html += '\t<tr align="center" valign="middle">\n';
		for (var dayNum = 0; dayNum < 7; ++dayNum) {
			html += '\t\t<td class="daysofweekheading" width="20">' + getDaysOfWeekName((dayNum+style)%7) +  '</td>\n';
		}
		html += '\t</tr>\n';
		if (style == 1)
			var startOfWeek = (((firstDay - 1) % 7) == 0) ? 7 : ((firstDay - style) % 7);
		else
			var startOfWeek = firstDay - 1;
		for (var row = 1; row <= Math.ceil((lastDate + startOfWeek) / 7); ++row) {
			html += '\t<tr align="right" valign="top">\n';
	    for (var column = 0; column < 7; ++column) {
  	    if ((currentCell < startOfWeek-style) || (dayOfMonth > lastDate)) {
    	    html += '\t\t<td class="emptycell">&nbsp;</td>\n';
      	  currentCell++;
	      }
				else {
					if (dayOfMonth == date)
            html += '\t\t<td class="today"><a href="#" onclick="Javascript:window.opener.parent.returnValue(document.forms[0].Month.options[document.forms[0].Month.selectedIndex].value, document.forms[0].Year.options[document.forms[0].Year.selectedIndex].value,' + dayOfMonth + ')">' + dayOfMonth + '</a></td>\n';
          else 
            html += '\t\t<td class="day"><a href="#" onclick="Javascript:window.opener.parent.returnValue(document.forms[0].Month.options[document.forms[0].Month.selectedIndex].value, document.forms[0].Year.options[document.forms[0].Year.selectedIndex].value,' + dayOfMonth + ')">' + dayOfMonth + '</a></td>\n';
         dayOfMonth++;
        }
   	 }
			html += '\t</tr>\n';
		} 
		
		// close table and body
		html += '</table>\n';
		html += '<table border="0" cellpadding="' + cellpadding + '" cellspacing="' + cellspacing + '" align="center">\n';
		html += '\t<tr>\n';
		html += '\t\t<td class="radio">';
		if (style == 0)
			html += '\t\t\t<input type="radio" name="style" value="0" checked onclick="Javascript:window.opener.parent.setStyle(this.value, document.forms[0].Month.options[document.forms[0].Month.selectedIndex].value, document.forms[0].Year.options[document.forms[0].Year.selectedIndex].value)"> ' + getStyleText(0) + '&nbsp;\n';
		else
			html += '\t\t\t<input type="radio" name="style" value="0" onclick="Javascript:window.opener.parent.setStyle(this.value, document.forms[0].Month.options[document.forms[0].Month.selectedIndex].value, document.forms[0].Year.options[document.forms[0].Year.selectedIndex].value)"> ' + getStyleText(0) + '&nbsp;\n';
		if (style == 1)
			html += '\t\t\t<input type="radio" name="style" value="1" checked onclick="Javascript:window.opener.parent.setStyle(this.value, document.forms[0].Month.options[document.forms[0].Month.selectedIndex].value, document.forms[0].Year.options[document.forms[0].Year.selectedIndex].value)"> ' + getStyleText(1) + '\n';
		else
			html += '\t\t\t<input type="radio" name="style" value="1" onclick="Javascript:window.opener.parent.setStyle(this.value, document.forms[0].Month.options[document.forms[0].Month.selectedIndex].value, document.forms[0].Year.options[document.forms[0].Year.selectedIndex].value)"> ' + getStyleText(1) + '\n';
		html += '\t\t</td>\n';
		html += '\t</tr>\n';
		html += '</table>\n'; 
		html += '</form>\n'
		html += '</div>\n';
		html += '</body>\n';
		html += '</html>\n';

		// write the html code string
		CalendarWindow.document.write(html);
		CalendarWindow.document.close();
		return;
	}

	function windowOpen(windowFeature) {
	 	/*	toolbar[=yes|no]|[=1|0] 		location[=yes|no]|[=1|0] 	directories[=yes|no]|[=1|0]
			status[=yes|no]|[=1|0] 			menubar[=yes|no]|[=1|0] 	scrollbars[=yes|no]|[=1|0]
			resizable[=yes|no]|[=1|0]	 width=pixels 							height=pixels 
		*/
	
		// default popup window settings
		if (windowFeature == null)
			windowFeature = "width=180,height=165,status=0,resizable=1,top=200,left=200,menubar=0,toolbar=0,scrolbars=1";

		// if window not yet opened, open window and in which the calendar will be displayed
		if (CalendarWindow == null || CalendarWindow.closed) {
			CalendarWindow = window.open("", "Cal", windowFeature);
			if (CalendarWindow.opener == null)
				CalendarWindow.opener = self;
		}
		else {
			// clear document
			CalendarWindow.document.open();
			CalendarWindow.document.page = null;
			CalendarWindow.document.close();
		}
	}

	function getNumberOfDaysInMonth(month, year) {
		// create an array to hold the number of days in each month
		var daysInMonth = new Array(12);

		daysInMonth[0] = 31;  // January
		daysInMonth[1] = (isLeapYear(year)) ? 29 : 28;  // February
		daysInMonth[2] = 31;  //  March
		daysInMonth[3] = 30;  // April
		daysInMonth[4] = 31;  // May
		daysInMonth[5] = 30;  // June
		daysInMonth[6] = 31;  // July
		daysInMonth[7] = 31;  // August
		daysInMonth[8] = 30;  // September
		daysInMonth[9] = 31;  // October
		daysInMonth[10] = 30;  // November
		daysInMonth[11] = 31;  // December

		// return the number of days in the specified month
		return daysInMonth[month];
	}
	
	function isLeapYear(year) {
		// Check if leap year
		return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	}
	
	function getMonthName(month) {
		// create an array to hold the names of the months
		var monthName = new Array(12);
		
		switch (language) {
			case 0:
				monthName[0] = "enero";
				monthName[1] = "febrero";
				monthName[2] = "marzo";
				monthName[3] = "abril";
				monthName[4] = "mayo";
				monthName[5] = "junio";
				monthName[6] = "julio";
				monthName[7] = "agosto";
				monthName[8] = "septiembre";
				monthName[9] = "octubre";
				monthName[10] = "noviembre";
				monthName[11] = "diciembre";
			break;
			case 1:
				monthName[0] = "January";
				monthName[1] = "February";
				monthName[2] = "March";
				monthName[3] = "April";
				monthName[4] = "May";
				monthName[5] = "June";
				monthName[6] = "July";
				monthName[7] = "August";
				monthName[8] = "September";
				monthName[9] = "October";
				monthName[10] = "November";
				monthName[11] = "December";
			break;
			case 2:
				monthName[0] = "janeiro";
				monthName[1] = "fevereiro";
				monthName[2] = "mar&ccedil;o";
				monthName[3] = "abril";
				monthName[4] = "maio";
				monthName[5] = "junho";
				monthName[6] = "julho";
				monthName[7] = "agosto";
				monthName[8] = "setembro";
				monthName[9] = "outubro";
				monthName[10] = "novembro";
				monthName[11] = "dezembro";
			break;
		}
		
		// return the name of the month in the specified language
		return monthName[month];
	}
	
	function getDaysOfWeekName(day) {
		// create an array to hold the days of the week in the specified language
		var weekDay = new Array(7);

		switch (language) {
			case 0:
				weekDay[0] = 	"domingo";
				weekDay[1] = 	"lunes";
				weekDay[2] = 	"martes";
				weekDay[3] = 	"mi&eacute;rcoles";
				weekDay[4] = 	"jueves";
				weekDay[5] = 	"viernes";
				weekDay[6] = 	"s&aacute;bado";
			break;
			case 1:
				weekDay[0] = 	"Sun";
				weekDay[1] = 	"Mon";
				weekDay[2] = 	"Tue";
				weekDay[3] = 	"Wed";
				weekDay[4] = 	"Thur";
				weekDay[5] = 	"Fri";
				weekDay[6] = 	"Sat";
			break;
			case 2:
				weekDay[0] = 	"domingo";
				weekDay[1] = 	"segunda-feira";
				weekDay[2] = 	"ter&ccedil;a-feira";
				weekDay[3] = 	"quarta-feira";
				weekDay[4] = 	"quinta-feira";
				weekDay[5] = 	"sexta-feira";
				weekDay[6] = 	"saacute;bado";
			break;
		}	
		
		// return the day of the week in the specified language
		return weekDay[day];
	}
	
	function getStyleText(style) {
		// create an array to hold the style text specified language
		var styleText = new Array(2);

		switch (language) {
			case 0:
				styleText[0] = 	"Normal";
				styleText[1] = 	"Euro";
			break;
			case 1:
				styleText[0] = 	"Standard";
				styleText[1] = 	"Euro";
			break;
			case 2:
				styleText[0] = 	"Normal";
				styleText[1] = 	"Euro";
			break;
		}	
		
		// return the style text in the specified language
		return styleText[style];
	}

	function helpScreen() {

		// if window not yet opened, open window and in which the calendar will be displayed
		windowOpen("width=600,height=340,status=0,resizable=1,top=200,left=200,menubar=0,toolbar=0,scrolbars=1");
		
		var html = ""; // initialize variable
		html += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">\n';
		html += '<html>\n';
		html += '<head>\n';
		html += '\t<title>Calendar</title>\n';
		html += '\t<link rel="stylesheet" type="text/css" href="Calendar.css">\n';
		html += '</head>\n';
		html += '<body leftmargin=0 topmargin=0 marginheight="0" marginwidth="0">\n';
		html += '<table border="0" cellpadding="' + cellpadding + '" cellspacing="' + cellspacing + '" align="center">\n';
		html += '\t<tr>\n';
		html += '\t\t<th>Help Screen</th>\n';
		html += '\t</tr>\n';
		html += '\t\t<td>\n';
		html += '\t\t\t<b>Program:</b> Calendar.js<br>\n';
		html += '\t\t\t<b>Programmer:</b> Marcos Gonzalez<br>\n';
		html += '\t\t\t<b>Date Written:</b> March 3, 2001<br>\n';
		html += '\t\t\t<b>Description:</b>  This program generates a popup calendar window.  The calendar is dynamic; you can\n';
		html += '\t\t\tselect the month and/or year.  The calendar style can be changed from standard to european.  On the standard calendar\n';
		html += '\t\t\tthe week starts on Sunday; on the european calendar the week starts on Monday.\n';
		html += '\t\t\tThe calendar color are defined in the Calendar.css (cascading style sheet).<br>\n';
		html += '\t\t\t<b>Note:</b>This calendar has only been tested On Microsoft IE 5.5 and Netscape 4.75.\n';
		html += '\t\t\t<p><b>Parameters</b><br>\n';
		html += '\t\t\t<li><b>Month</b> -  The month the calendar will display.  The default is current month.</li>\n';
		html += '\t\t\t<li><b>Year</b> -  The year the calendar will display.  The default is current year.</li>\n';
		html += '\t\t\t<li><b>Field</b> -  This is the form.field name that will be populated when a date is selected.  There is no default.</li>\n';
		html += '\t\t</td>\n';
		html += '\t</tr>\n';
		html += '</table>\n'; 
		html += '</body>\n';
		html += '</html>\n';

		// write the html code string
		CalendarWindow.document.write(html);
	}

