// Trent Blackwell
// Version 1.1
// Note: Only working for date and as a div at this stage

// Calendar style definition
var str_BgMain = "#003366";
var str_BgWeekday = "#99cccc";
var str_BgDay = "white";
var str_BgWeekend = "#cccccc";
var str_BgCurrent = "#DBEAF5";
var str_txtOtherDay = "red";
var str_head01face = "Comic Sans MS";
var str_dayface = "arial";

function show_calendar(str_dfield, str_tfield, str_date, str_time, b_inctime, b_showindiv, str_divname) {
	// if "b_showindiv" then will attempt to display in div "str_divname" else will create a popupwindow
	// if not "b_inctime" then time will be displayed

	var ary_months = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
	var ary_weekdays = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"];
	var n_weekstart = 1;

	if (!b_inctime){
		var str_datetime = str_date;
	}else{
		var str_datetime = str_date + " " + str_time;
	}

	//var dt_datetime = new Date();
	if (!b_inctime){
		var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : strToDate(str_datetime));
	}else{
		var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : strToDateTime(str_datetime));
	}

	var dt_fday = new Date(dt_datetime);
	dt_fday.setDate(1);
	dt_fday.setDate(1- (7 + dt_fday.getDay() - n_weekstart ) % 7);
	var dt_cday = new Date(dt_fday);
	var dt_pmonth = new Date(dt_datetime);
	dt_pmonth.setMonth(dt_datetime.getMonth() - 1);
	var dt_nmonth = new Date(dt_datetime);
	dt_nmonth.setMonth(dt_datetime.getMonth() + 1);

	// Next/Previous month links
	if (!b_showindiv) {
		var str_pmonthlink = "#";
		var str_nmonthlink = "#";
	}else{
		var str_pmonthlink = "javascript:show_calendar('"
			+ str_dfield + "', '" + str_tfield + "', '" + dateToString(dt_pmonth) + "', '"
			+ timeToString(dt_pmonth) + "', " + b_inctime + ", " + b_showindiv + ", '" + str_divname + "');";
		var str_nmonthlink = "javascript:show_calendar('"
			+ str_dfield + "', '" + str_tfield + "', '" + dateToString(dt_nmonth) + "', '"
			+ timeToString(dt_nmonth) + "', " + b_inctime + ", " + b_showindiv + ", '" + str_divname + "');";
	}

	if (!b_showindiv) {
		var str_html = new String (
			"<html>\n"
			+ "<head>\n"
			+ "	<title>Calendar</title>\n"
			+ "</head>\n"
			+ "<body bgcolor=\"White\">\n"
			+ "<table cellspacing=\"0\" border=\"0\" width=\"100%\">\n"
			+ "<tr><td bgcolor=\"" + str_BgMain + "\">\n"
			+ "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"
			+ "<tr>\n	<td bgcolor=\"" + str_BgMain + "\"><a href=\"" + str_pmonthlink + "\">"
			+ "<img src=\"prev.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"previous month\"></a></td>\n"
			+ "<td bgcolor=\"" + str_BgMain + "\" colspan=\"5\" align=center>"
			+ "<font color=\"white\" face=\"" + str_head01face + "\" size=\"2\">"
			+ ary_months[dt_datetime.getMonth()] + " " + dt_datetime.getFullYear() + "</font></td>\n"
			+ "<td bgcolor=\"" + str_BgMain + "\" align=\"right\"><a href=\"" + str_nmonthlink + "\">"
			+ "<img src=\"next.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"next month\"></a></td>\n</tr>\n"
		);
	}else{
		var str_html = new String (
			"<table cellspacing=\"0\" border=\"0\" width=\"100%\">\n"
			+ "<tr><td bgcolor=\"" + str_BgMain + "\">\n"
			+ "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"
			+ "<tr>\n	<td bgcolor=\"" + str_BgMain + "\"><a href=\"" + str_pmonthlink + "\">"
			+ "<img src=\"prev.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"previous month\"></a></td>\n"
			+ "<td bgcolor=\"" + str_BgMain + "\" colspan=\"5\" align=center>"
			+ "<font color=\"white\" face=\"" + str_head01face + "\" size=\"2\"><b>"
			+ ary_months[dt_datetime.getMonth()] + " " + dt_datetime.getFullYear() + "</b></font></td>\n"
			+ "<td bgcolor=\"" + str_BgMain + "\" align=\"right\"><a href=\"" + str_nmonthlink + "\">"
			+ "<img src=\"next.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"next month\"></a></td>\n</tr>\n"
		);

	}

	// weekday titles
	str_html += "<tr>\n";
	for (var n=0; n<7; n++) {
		str_html += "	<td bgcolor=\"" + str_BgWeekday + "\">"
		+ "<font color=\"white\" face=\"" + str_head01face + "\" size=\"2\">"
		+ ary_weekdays[(n_weekstart + n) % 7] + "</font></td>\n"; }
	str_html += "</tr>\n";

	// months days
	while (dt_cday.getMonth() == dt_datetime.getMonth() || dt_cday.getMonth() == dt_fday.getMonth()) {
		// row
		str_html += "<tr>\n";
		for (var n_cday = 0; n_cday < 7; n_cday++ ) {
				/*if (dt_cday.getDate() == dt_datetime.getDate() && dt_cday.getMonth() == dt_datetime.getMonth()) {
					// current date
					str_html += "	<td bgcolor=\"" + str_BgCurrent + "\" align=\"right\">";
				}else*/ if (dt_cday.getDay() == 0 || dt_cday.getDay() == 6) {
					// weekend days
					str_html += "	<td bgcolor=\"" + str_BgWeekend + "\" align=\"right\">";
				}else{
					// work days of current month
					str_html += "	<td bgcolor=\"" + str_BgDay + "\" align=\"right\">";
				}
				if (dt_cday.getMonth() == dt_datetime.getMonth()) {
					// day of current month
					str_html += "<a href=\"";
					if (!b_showindiv) {
						str_html += "#";
					}else{
						str_html += "#\" onClick=\"javascript:" + str_dfield +".value='" + dateToString(dt_cday) + "'";
					}
					str_html += "\"><font color=\"black\" face=\"" + str_dayface + "\" size=\"2\">" + dt_cday.getDate() + "</a>";
				}else{
					// day of other month
					str_html += "<a href=\"";
					if (!b_showindiv) {
						str_html += "#";
					}else{
						str_html += "#\" onClick=\"javascript:" + str_dfield +".value='" + dateToString(dt_cday) + "'";
					}
					str_html += "\"><font color=\"" + str_txtOtherDay + "\" face=\"" + str_dayface + "\" size=\"2\">" + dt_cday.getDate() + "</a>";
				}
				str_html += "</td>\n";
				dt_cday.setDate(dt_cday.getDate() + 1);
		}
		str_html += "</tr>\n";
	}

	// footer
	if (!b_showindiv) {
		if (b_inctime){
			str_html += "<form name=\"cal\">\n<tr><td colspan=\"7\" bgcolor=\"#87CEFA\">"
				+ "<font color=\"White\" face=\"" + str_head01face + "\" size=\"2\">"
				+ "Time: <input type=\"text\" name=\"time\" value=\"" + timeToString(dt_datetime)
				+ "\" size=\"8\" maxlength=\"8\"></font></td></tr>\n</form>\n"
		}
		str_html += "</table>\n"
			+ "</tr>\n</td>\n</table>\n"
			+ "</body>\n"
			+ "</html>\n";
	}else{
		if (b_inctime){
			str_html += "<tr><td colspan=\"7\" bgcolor=\"#87CEFA\">"
				+ "<font color=\"White\" face=\"" + str_head01face + "\" size=\"2\">" 
				+ "Time: <input type=\"text\" name=\"time\" value=\"" + timeToString(dt_datetime)
				+ "\" size=\"8\" maxlength=\"8\"></font></td></tr>\n";
		}
		str_html += "</table>\n"
			+ "</tr>\n</td>\n</table>\n";
	}

	if (!b_showindiv) {
		var obj_calwindow = window.open("", "Calendar", 
			"width=200,height=250,status=no,resizable=yes,top=200,left=200");
		obj_calwindow.opener = self;
		var str_windoc = obj_calwindow.document;
		str_windoc.write (str_html);
		str_windoc.close();
	}else{
		var obj_div = document.getElementById(str_divname);
		obj_div.innerHTML = str_html;
	}
}

// datetime parsing and formatting routimes. modify them if you wish other datetime format
function strToDateTime (str_datetime) {
	var re_datetime = /^(\d+)\/(\d+)\/(\d+)\s+(\d+)\:(\d+)$/;
	if (!re_datetime.exec(str_datetime))
		return alert("Invalid Datetime format: "+ str_datetime);
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5));
}
function strToDate (str_datetime) {
	var re_datetime = /^(\d+)\/(\d+)\/(\d+)$/;
	if (!re_datetime.exec(str_datetime))
		return alert("Invalid date format: "+ str_datetime);
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
}

function dateToString (dt_datetime) {
	return (new String (dt_datetime.getDate()+ "/" + (dt_datetime.getMonth() + 1 ) + "/" + dt_datetime.getFullYear()));
}

function timeToString (dt_datetime) {
	return (new String (dt_datetime.getHours() + ":" + dt_datetime.getMinutes()));
}

