var gNow = new Date();
var ggWinCal;

Calendar.Months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];

Calendar.Days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];

// 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];

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

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;
	
	this.gMonthName = Calendar.get_month(p_month);
	this.gHour = p_hour;
	this.gMinute = p_minute;
	this.gMonth = new Number(p_month);
	this.gYear = p_year;
	this.gReturnItem = p_item;
	this.gAction = p_action;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;

function Calendar_get_month(monthNo) {
	return Calendar.Months[monthNo];
}

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];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	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;
}

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";

	vCode = vCode + "<TABLE ALIGN=CENTER BORDER=0 CELLSPACING=1 CELLPADDING=0 BGCOLOR=#000000>";
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	
	vCode = vCode + "</TABLE>";
	
	return vCode;
}

Calendar.prototype.show = function() {

	// 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];
	
	var vCode = "";
	
	this.gWinCal.document.open();

	// Setup the page...
	this.wwrite("<html>");
	this.wwrite("<head><title>Calendar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
		+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
		+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</title>");
	this.wwrite("<script language='javascript'>\n" + 
			"function kdwn(pthis) {" + 
			"if (event.keyCode == 13) pthis.onchange(); else return false;}\n" + 
			"</script>");
	this.wwrite("<style type=\"text/css\"><!--\n" 
		+ "BODY, TD {font-size:12px;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;}\n"
		+ "INPUT, SELECT {font-size:10px;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;}\n"
		+ "A {text-decoration:none; color:#000000;}\n"
		+ "A:hover {text-decoration:underline;}\n"
		+ ".title {font-size:14px; font-family:Verdana, Geneva, Arial, Helvetica, sans-serif; color:#444444; font-weight:bold;}\n"
		+ ".heading {background-color: #444444; color:#AAAAAA;}\n"
		+ ".data {background-color: #DDDDDD; color:#000000}\n"
		+"</style>");		
	this.wwrite("</head>");
	this.wwrite("<body onLoad=\"document.frmCal.hh.focus()\"><form name=frmCal>");
	this.wwrite("<div class=\"title\">2ms Datepicker</div><hr width=90%>");
	
	var navBar;
	navBar = "<table align=center border=0><tr>";
	navBar += "<td>year<br><input name=\"cy\" size='4' maxlength='4' value=\"" + this.gYear + 
		"\" onchange=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" 
		+ this.gAction + "', '" 
		+ this.gMonth + "', " 
		+ "document.frmCal.cy.value, '" 
		+ this.gHour + "', '" 
		+ this.gMinute + "');\" onkeydown=\"javascript:kdwn(this);\"><\/td>\n";

	navBar += "<td>month<br><select name='selMonth' onchange=\"window.opener.Build('"
		+ this.gReturnItem + "', '"
		+ this.gAction + "', " 
		+ "document.frmCal.selMonth.selectedIndex, '"
		+ this.gYear + "', '"
		+ this.gFormat +"', '" 
		+ this.gHour + "', '" 
		+ this.gMinute + "');\">\n";
	for (i=0; i<12; i++) {
		navBar += "<option value=\"" + i + "\"";
		if (parseInt(this.gMonth) == i)
			navBar += "selected";
		navBar += ">" + Calendar.Months[i] + "</option>\n";
	}
	navBar += "<\/select>\n<\/td>";

	navBar += "<td>hour<br><input name=\"hh\" size='2' maxlength='2' value=\"" + this.gHour + 
		"\" onchange=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" 
		+ this.gAction + "', '" 
		+ this.gMonth + "','" 
		+ this.gYear + "', " 
		+ "document.frmCal.hh.value,'" 
		+ this.gMinute + "');\" onkeydown=\"javascript:kdwn(this);\"><\/td>\n";
	navBar += "<td>min<br><input name=\"mm\" size='2' maxlength='2' value=\"" + this.gMinute + 
		"\" onchange=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" 
		+ this.gAction + "', '" 
		+ this.gMonth + "','" 
		+ this.gYear + "', '" 
		+ this.gHour + "', " 
		+ "document.frmCal.mm.value);\" onkeydown=\"javascript:kdwn(this);\"><\/td>\n";
	navBar += "<\/tr><\/table>";
	this.wwrite(navBar);
	
	this.wwrite("<table align=center width='100%' border=0><tr><td align=center>");
	this.wwrite("<input type='button' value='<<' onClick=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" + this.gAction + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1)
		+ "', '" + this.gHour + "', '" + this.gMinute + "');\">");
	this.wwrite("<input type='button' value='<' onClick=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" + this.gAction + "', '" + prevMM + "', '" + prevYYYY
		+ "', '" + this.gHour + "', '" + this.gMinute + "');\">");
	this.wwrite("<input type='button' value='today' onClick=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" + this.gAction + "', '" + gNow.getMonth().toString() + "', '" + gNow.getFullYear().toString()
		+ "', '" + this.gHour + "', '" + this.gMinute + "');\">");
	this.wwrite("<input type='button' value='>' onClick=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" + this.gAction + "', '" + nextMM + "', '" + nextYYYY
		+ "', '" + this.gHour + "', '" + this.gMinute + "');\">");
	this.wwrite("<input type='button' value='>>' onClick=\"window.opener.Build('" 
		+ this.gReturnItem + "', '" + this.gAction + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1)
		+ "', '" + this.gHour + "', '" + this.gMinute + "');\"><\/td><\/tr><\/table>");

	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	this.wwrite(vCode);

	this.wwrite("</form></body></html>");
	this.gWinCal.document.close();
}

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

Calendar.prototype.cal_header = function() {
	var vCode = "";
	vCode = vCode + "<TR>";
	for (i=0; i<7; i++)
		vCode += "<TD WIDTH='14%' CLASS=\"heading\">" + Calendar.Days[i] + "</TD>";
	vCode = vCode + "</TR>";
	
	return vCode;
}

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 + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD CLASS=\"data\" ALIGN='center' WIDTH='14%'>&nbsp;</TD>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		vCode = vCode + "<TD CLASS=\"data\" ALIGN='center' WIDTH='14%'>" + 
			"<A HREF='#' onClick=\"self.opener.document." + this.gReturnItem + ".value='" + this.format_data(vDay) + "';" + 
			"self.opener.document." + this.gReturnItem + this.gAction + ";window.close();\">" +
			this.format_day(vDay) + "</A>" + "</TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=0; j<7; j++) {
			vCode = vCode + "<TD CLASS=\"data\" ALIGN='center' WIDTH='14%'>" +
			"<A HREF='#' onClick=\"self.opener.document." + this.gReturnItem + ".value='" + this.format_data(vDay) + "';" + 
			"self.opener.document." + this.gReturnItem + this.gAction + ";window.close();\">" +
			this.format_day(vDay) + "</A>" + "</TD>";
			vDay=vDay + 1;

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

		if (j == 6)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++) {
		vCode = vCode + "<TD CLASS=\"data\" WIDTH='14%'>&nbsp;</TD>";
	}
	
	return vCode;
}

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 ("<B>" + vday + "</B>");
	else
		return (vday);
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	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.toString().length < 2) ? "0" + p_day : p_day;
	var vHH = (this.gHour.toString().length < 2) ? "0" + this.gHour : this.gHour;
	var vMM = (this.gMinute.toString().length < 2) ? "0" + this.gMinute : this.gMinute;
	
	vData = vY4 + "-" + vMonth +  "-" + vDD + " " + vHH + ":" + vMM;
	return vData;
}

function Build(p_item, p_action, p_month, p_year, p_hour, p_minute) {
	var reg;
	var result;
	reg = /^(\d{4})$/;
	result = p_year.match(reg);
	if (result == null)
		p_year = gNow.getFullYear().toString();

	reg = /^(\d{1,2})$/;
	result = p_hour.match(reg);
	if (result != null) {
		if (result[1] > 23)
			p_hour = "00";
	} else {
		p_hour = gNow.getHours().toString();
	}
	p_hour = (p_hour.toString().length < 2) ? "0" + p_hour : p_hour;
	
	reg = /^(\d{1,2})$/;
	result = p_minute.match(reg);
	if (result != null) {
		if (result[1] > 59)
			p_minute = "59";
	} else {
		p_minute = gNow.getMinutes().toString();
	}
	p_minute = (p_minute.toString().length < 2) ? "0" + p_minute : p_minute;
	
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_action, p_WinCal, p_month, p_year, p_hour, p_minute);
	gCal.show();
}

function isFieldEmpty(s){
	if (s == null)
		return true;
	for (var i=0; i<s.length; i++){
		if (s.charAt(i) != ' ')
			return false;
	}
	return true;
}

function show_calendar() {
	var p_item = arguments[0];
	var p_action = arguments[1];
	var set=0;
	if (!isFieldEmpty(eval("document." + p_item + ".value"))) {
		field = eval("document." + p_item + ".value");
		var reg = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/;
		var result = field.match(reg);
		if (result != null) {
			set=1;
			p_year = result[1];
			p_month = result[2]-1;
			p_hour = result[4];
			p_minute = result[5];
		}
	}
	if (set==0) {
		p_year = new String(gNow.getFullYear().toString());
		p_month = new String(gNow.getMonth().toString());
		p_hour = new String(gNow.getHours().toString());
		p_minute = new String(gNow.getMinutes().toString());
	}
	vWinCal = window.open("", "Calendar","width=250,height=250,status=no,resizable=no,top=200,left=200");
	vWinCal.opener = self;
	ggWinCal = vWinCal;

	Build(p_item, p_action, p_month, p_year, p_hour, p_minute);
}