/*
Add functionality for setting table cell size and overall table width...later
*/
//##### set calendar colors here ######
var calColor = "#FFD6B9";
var dayColor = "#FFFFFF";
var weekendColor = "#FFFFFF";
var currDayColor = "red";
var weekend = [0,6];
//#####################################

//######## Gather date and info about system set date vars ############
var gNow = new Date();
var ggWinCal;
Calendar.Months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

//######################################################################


//Set display here
function show_calendar(whichMenu) {
	/* 
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		p_item	: Return Item.
	*/

	p_item = arguments[0];
	if (arguments[1] == null)
		p_month = new String(gNow.getMonth());
	else
		p_month = arguments[1];
	if (arguments[2] == "" || arguments[2] == null)
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[2];
	if (arguments[3] == null)
		p_format = "YYYY-MM-DD";
		//p_format = "MM/DD/YYYY";
	else
		p_format = arguments[3];
	
	//############ Format the pop-up window that will contain the calendar ##############
	vWinCal = window.open("", "Calendar", 
		"width=200,height=175,status=no,resizable=no,top=200,left=200");
	vWinCal.opener = self;
	ggWinCal = vWinCal;
	
	Build(p_item, p_month, p_year, p_format, whichMenu);
}

function Build(p_item, p_month, p_year, p_format, whichMenu) {
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year, p_format);

	// Choose appropriate show function
	if (gCal.gYearly){
		gCal.showY();
	} else {	
		gCal.show();
	}
}

function Calendar(p_item, p_WinCal, p_month, p_year, p_format) {
	if ((p_month == null) && (p_year == null))	return;

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;
	
	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
	this.gReturnItem = p_item;
}


//############## Set Methods ###################
Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;
Calendar.print = Calendar_print;
//##############################################


// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Build the calendar @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Calendar.prototype.show = function() {
	var vCode = "";
/*
The javascript below write the selected date to 3 drop-down form elements| Day : Month : Year
whichMenu is passed in to determine with menu from or to is calling the calendar
*/	
//var fillDay = this.gReturnItem + "day";
//var fillMonth = this.gReturnItem + "month";
//var fillYear = this.gReturnItem + "year"; 

//var module = this.gReturnItem + "-calendarModule.js";
var foo = this.gReturnItem;

//######### Header, title, and javascript functions for page #############
	this.wwrite("<html>\n");
	this.wwrite("<head>\n\t<title>Events Calendar</title>");
	//this.wwrite("\t<link href=\"expa.css\" rel=\"stylesheet\" type=\"text/css\">\n");
  	//this.wwrite("<script language='javascript' src='" +module+ "'><\/script>");
	this.wwrite("<script language='javascript'>");
	this.wwrite("function splitDate(pickDate){");
	this.wwrite("var target = opener.document.Form1;");
	this.wwrite("var dateNums = pickDate.split('-');");
	this.wwrite("for (var a=0; a<target.ddl_"+foo+"_year.options.length; a++){");
	this.wwrite("\tif (target.ddl_"+foo+"_year.options[a].value == dateNums[0])");
	this.wwrite("\t\ttarget.ddl_"+foo+"_year.options[a].selected = true; \n}");
	this.wwrite("for (var b=0; b<target.ddl_"+foo+"_month.options.length; b++){");
	this.wwrite("\t\tif (target.ddl_"+foo+"_month.options[b].value == dateNums[1])");
	this.wwrite("\ttarget.ddl_"+foo+"_month.options[b].selected = true; \n}");
	this.wwrite("for (var c=0; c<target.ddl_"+foo+"_day.options.length; c++){");
	this.wwrite("\t\tif (target.ddl_"+foo+"_day.options[c].value == dateNums[2])");
	this.wwrite("\ttarget.ddl_"+foo+"_day.options[c].selected = true; \n}");
	this.wwrite("self.close();\n}");
	this.wwrite("</script>");	
	this.wwrite("</head>\n");
	this.wwrite("<body>\n");
//#######################################################################
	

	// Show navigation buttons
	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	
	//Overall table, 2 rows - 2 cells  Table for bg coloring and border color
	this.wwrite("<TABLE WIDTH='148' BORDER='0' CELLSPACING='0' CELLPADDING='0' BGCOLOR='" + calColor + "' ALIGN='CENTER'>");
	this.wwrite("<TR>\n\t<TD>");
	
	
	//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% table that for month, year, previous and next stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	this.wwrite("\t<TABLE WIDTH='100%' BORDER=0 CELLSPACING=1 CELLPADDING=0 align='center'>\n\t<TR>");
	//############ Previous month arrow and function ###################	
	this.wwrite("\t\t<TD ALIGN=center class='calendar'><A HREF=\"" +
		"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +
		");" + "\" title='Previous Month' class='calArrow'>&laquo;<\/A></TD>\n");
	//##################################################################

	
	//############ Write the month and year ###########################
	this.wwrite("\t\t<TD ALIGN=center class='calendar' width='100%'><b>" + this.gMonthName + " " + this.gYear + "</b></TD>\n");
	//##################################################################

	
	//########### Next month arrow and function #######################
	this.wwrite("\t\t<TD ALIGN=center class='calendar'><A HREF=\"" +
		"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\" title='Next Month' class='calArrow'>&raquo;<\/A></TD>");
	//##################################################################
	this.wwrite("\t</TR>\n\t</TABLE>");
	//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End table that holds the month, previous and next %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	
	//############## new row for bg coloring table ####################
	this.wwrite("\t</TD>\n</TR>\n<TR>\n\t<TD>");
	//##################################################################
		
	//########## Begin table to hold the calendar code for the month ####
	this.wwrite("\t<TABLE width='148' align='center' BORDER=0 cellspacing='1' cellpadding='0'>");
	this.wwrite("\t<TR align='center'>\n");
	this.wwrite("\t\t<TD WIDTH='20' HEIGHT='17' bgcolor='" + dayColor + "' class='calendarHeader'><B>Su</B></TD>");
	this.wwrite("\t\t<TD WIDTH='20' HEIGHT='17' bgcolor='" + dayColor + "' class='calendarHeader'><B>Mo</B></TD>");
	this.wwrite("\t\t<TD WIDTH='20' HEIGHT='17' bgcolor='" + dayColor + "' class='calendarHeader'><B>Tu</B></TD>");
	this.wwrite("\t\t<TD WIDTH='20' HEIGHT='17' bgcolor='" + dayColor + "' class='calendarHeader'><B>We</B></TD>");
	this.wwrite("\t\t<TD WIDTH='20' HEIGHT='17' bgcolor='" + dayColor + "' class='calendarHeader'><B>Th</B></TD>");
	this.wwrite("\t\t<TD WIDTH='20' HEIGHT='17' bgcolor='" + dayColor + "' class='calendarHeader'><B>Fr</B></TD>");
	this.wwrite("\t\t<TD WIDTH='20' HEIGHT='17' bgcolor='" + dayColor + "' class='calendarHeader'><B>Sa</B></TD>");
	this.wwrite("\t</TR>");
	
	//##################### Begin writing out the days ##################
	this.wwrite(this.cal_data());
	this.wwrite("\t</TABLE>");
	//###################################################################
	
	this.wwrite("\t</TD>\n</TR>\n</TABLE>");
	
	this.wwrite("</body>\n</html>");
	this.gWinCal.document.close();
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


Calendar.prototype.wwrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}



Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary.*/
	vCode = vCode + "\t<TR align='center'>\n";
	for (i=0; i<vFirstDay; i++) {
			vCode = vCode + "\t\t<TD WIDTH='20' HEIGHT='17' " + this.write_daybg_string(i) + " class='calendar'>&nbsp;</TD>\n";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		vCode = vCode + "\t\t<TD WIDTH='20' HEIGHT='17' " + this.write_daybg_string(j) + " class='calendar'>" + 
			"<A HREF='#' " + 
					"onClick=\"splitDate('" + this.format_data(vDay) + "')" + 
					";\" class='calendar'>" + 
					this.format_day(vDay) + 
			"</A>" + 
			"\t</TD>\n";
		vDay=vDay + 1;
	}

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "\t</TR>\n\t<TR align='center'>\n";

		for (j=0; j<7; j++) {
			vCode = vCode + "\t\t<TD WIDTH='20' HEIGHT='17' " + this.write_daybg_string(j) + " class='calendar'>" + 
				"<A HREF='#' " + 
					"onClick=\"splitDate('" + this.format_data(vDay) + "')" + 
					";\" class='calendar'>" + 
					this.format_day(vDay) + 
				"</A>" + 
				"</TD>\n";
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "\t</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	// Fill up the rest of last week with blanks, so that we get square blocks use the incremented var to fill in next months days
	for (m=1; m<(7-j); m++) {
			vCode = vCode + "\t\t<TD WIDTH='20' HEIGHT='17' " + this.write_daybg_string(j+m) + " class='calendar'>&nbsp;</TD>\n";
	}
	
	return vCode;
}

//Mark the current day
Calendar.prototype.format_day = function(vday) {
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();

	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		return ("<FONT COLOR='" + currDayColor + "'><B>" + vday + "</B></FONT>");
	else
		return (vday);
}

//Color the day bg color depending on if it is a weekday or weekend
Calendar.prototype.write_daybg_string = function(vday) {
	var i;

	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i])
			return (" BGCOLOR='" + weekendColor + "'");
	}
	return (" BGCOLOR='" + dayColor + "'");
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = vMonth;
	//vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = p_day;
	//var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	//Choose a date format!!!!!
		//return date in the following format YYYY-MM-DD
		//rearrange vY4, vDD, vMonth for new date formats
		vData = vY4 + "-" + vMonth + "-" + vDD;
	return vData;
}


//################################### <Methods> ##################################
function Calendar_get_month(monthNo) {
	return Calendar.Months[monthNo];
}

//Returns days in a passed in month and year
function Calendar_get_daysofmonth(monthNo, p_year) {
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];
	
		return Calendar.lDOMonth[monthNo];
	} else
		return Calendar.DOMonth[monthNo];
}

//Move the month foreward and backward
function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-Dimensional array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

function Calendar_print() {
	ggWinCal.print();
}