//Calendar
function calendar(caldivid){

	this.monthshortarr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	this.montharr = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	this.dayarr = new Array("S", "M", "T", "W", "T", "F", "S");
	this.daymedarr = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

	//Init vars
	this.startday = "Sun";

	this.curryear = undefined;
	this.currdate = undefined;
	this.currmonth = undefined;
	this.currday = undefined;
	this.currdaysinmonth = undefined;
	this.currdatevalid = false;

	this.caldivid = caldivid;
	this.onnewdayselected = "";

	this.yearfield = undefined;
	this.monthfield = undefined;
	this.dayfield = undefined;

	//Set object name
	this.setobjname = function(objname) {
		this.objname = objname;
	}

	//Set date fields
	this.setdatefield = function(yearfield, monthfield, dayfield) {
		this.yearfield = yearfield;
		this.monthfield = monthfield;
		this.dayfield = dayfield;
	}

	//Set on new day selected
	this.setonnewdayselected = function(onnewdayselected) {
		this.onnewdayselected = onnewdayselected;
	}

	//Set current date
	this.setcurrdate = function(year, month, day) {

		year = parseInt(year, 10);
		month = parseInt(month, 10);

		//Clear previous value
		//this.currdate = undefined;
		//this.curryear = undefined;
		//this.currmonth = undefined;
		//this.currday = undefined;
		//this.currdaysinmonth = undefined;
		//this.currdatevalid = false;

		//Default to current date
		var currdate = new Date();
		this.curryear = currdate.getFullYear();
		this.currmonth = currdate.getMonth()+1;
		this.currday = undefined;
		this.currdaysinmonth = this.daysinmonth(this.curryear, this.currmonth);
		this.currdatevalid = true;

		if ( (year > 1990) && (year < 2020) ) {

			if ( (month > 0) && (month <= 12) ) {

				var currdaysinmonth = this.daysinmonth(year, month);

				if (currdaysinmonth != false) {

					if (day != false) {
						day = parseInt(day, 10);
					}

					//alert(year + " " + month + " " + day);

					if ( (day == false) || ( (day > 0) && (day <= currdaysinmonth) ) ) {

						this.curryear = year;
						this.currmonth = month;
						this.currday = day;
						this.currdaysinmonth = currdaysinmonth;

						//alert(this.curryear + " " + this.currmonth + " " + this.currday);

						//var currdate = new Date();

						//if (day == false) {
						//	currdate.setFullYear(year, month-1);
						//} else {
						//	currdate.setFullYear(year, month-1, day);
						//}

						//this.currdate = currdate;

						this.currdatevalid = true;

						return true;

					} else {
						return false;
					}
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}

	}

	//Show hide / calendar
	this.showhide = function(linkobj, evtobj) {

		if (document.getElementById(this.caldivid).style.display == "none") {
			document.getElementById(this.caldivid).style.display = "";
			//linkobj.innerHTML == "Show";

			if (this.yearfield) {
				var year = document.getElementById(this.yearfield).value;
			} else {
				var year = false;
			}

			if (this.monthfield) {
				var month = document.getElementById(this.monthfield).value;
			} else {
				var month = false;
			}

			if (this.dayfield) {
				var day = document.getElementById(this.dayfield).value;
			} else {
				var day = false;
			}

			this.setcurrdate(year, month, day);
			this.generatehtml();

		} else {
			document.getElementById(this.caldivid).style.display = "none";
			//linkobj.innerHTML == "Hide";
		}

	}
	
	//Get days in set month
	this.daysinmonth = function(year, month) {

		var monarr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) monarr[1] = "29";

		if (monarr[month-1]) {
			return monarr[month-1];
		} else {
			return false;
		}

	}

	//Change day highlited / selected
	this.setcurrday = function(day) {

		//=== Day changed here ===

		//Update calendar display
		this.setcurrdate(this.curryear, this.currmonth, day);
		this.generatehtml();

		//Update field
		this.updatefield(this.yearfield, this.curryear);
		this.updatefield(this.monthfield, this.currmonth);
		this.updatefield(this.dayfield, day);

		//Hide calendar
		document.getElementById(this.caldivid).style.display = "none";

	}

	//Update field
	this.updatefield = function(field, fieldvalue) {

		if (field) {
			document.getElementById(field).value = fieldvalue;
		}

	}

	/*
	//Get field value
	this.getfieldvalue = function(field) {
	}
	*/

	/*
	//Change month selected
	this.setcurrmonth = function(month) {
		this.setcurrdate(this.curryear, month+1, false);
		this.generatehtml();
	}
	*/

	//Change month selected
	this.setcurrmonth = function(year, month) {
		this.setcurrdate(year, month, false);
		this.generatehtml();
	}

	//Change month selected
	this.setcurrmonthonly = function(month) {
		this.setcurrdate(this.curryear, month, false);
		this.generatehtml();
	}

	//Change year selected
	this.setcurryear = function(year) {
		this.setcurrdate(year, this.currmonth, false);
		this.generatehtml();
	}

/*
	//Change current month
	this.monthnav = function(navtype) {

		if (navtype == "prev") {

			if (this.currmonth == 1) {
				this.setcurrdate(this.curryear-1, 12, false);
			} else {
				this.setcurrdate(this.curryear, this.currmonth-1, false);
			}

		} else if (navtype == "next") {

			if (this.currmonth == 12) {
				this.setcurrdate(this.curryear+1, 1, false);
			} else {
				this.setcurrdate(this.curryear, this.currmonth+1, false);
			}

		}

		this.generatehtml();

	}
*/

	//Generate tables
	this.generatehtml = function() {

		////Retrieve calendar style info
		//var calstyle = new calendarstyle(false);

		var tablehtml = "";

		// border=\"1\" bordercolor=\"#cccccc\" 
		tablehtml += "<table class=\"calendar_tbl\" cellpadding=\"4\" cellspacing=\"0\" style=\"border-collapse: collapse;\">\n";

		//Month change nav
		//this.montharr[this.currmonth-1] + " " + this.curryear
		// bgcolor=\"#F7F7F7\"
		tablehtml += "\t<tr>\n";
		tablehtml += "\t\t<td class=\"calendar_hdr\" align=\"center\" colspan=\"7\"><div style=\"float: left\">"+this.generatemonthdropdown()+"</div><div style=\"float: right\">"+this.generateyeardropdown()+"</div></td>\n";
		tablehtml += "\t</tr>\n";

		//Resolve day nice name to index id
		var startdayindexid = this.resolvevaluetoindexid(this.startday, this.daymedarr);

		//Day titles
		// bgcolor=\"#F7F7F7\"
		tablehtml += "\t<tr align=\"center\">\n";
		for (i2=0; i2<7; i2++) {

			var daytitleindex = i2 + parseInt(startdayindexid);
			if (daytitleindex >= 7) {
				daytitleindex -= 7;
			}

			tablehtml += "\t\t<td class=\"calendar_dayhdr\">"+this.dayarr[daytitleindex]+"</td>\n";
		}
		tablehtml += "\t</tr>\n";

		var currday = 1;

		var startdays = false;

		var currdatenow = new Date();

		var currdate = new Date();
		currdate.setFullYear(this.curryear, this.currmonth-1, 1);
		var startdaynum = currdate.getDay();

		//Day row
		for (i=0; i<7; i++) {

			tablehtml += "\t<tr>\n";

			//Day column
			for (i2=0; i2<7; i2++) {

				//Complete end of table by putting empty cells
				if (currday > this.currdaysinmonth) {
					tablehtml += "\t\t<td></td>\n";
				} else {

					//Take into account, the start day chosen by the user
					var daytitleindex = i2 + parseInt(startdayindexid);
					if (daytitleindex >= 7) {
						daytitleindex -= 7;
					}

					//Actual table cell with date number in it
					if ( (startdaynum == daytitleindex) || (startdays == true) ) {

						//If curr day selected
						if (this.currday == currday) {
							//var coladdin = "background-color: #c0c0c0; ";
							var coladdin_selected = " calendar_dayselected";
						} else {
							var coladdin_selected = "";
						}

						//If current day is now
						if ( (currdatenow.getFullYear() == this.curryear) && ((currdatenow.getMonth()+1) == this.currmonth) && (currdatenow.getDate() == currday) ) {
							var coladdin_daycurr = " calendar_daycurr";
						} else {
							var coladdin_daycurr = "";
						}

						/*

						//Note: Other possible approach to use is leaving the for count along, and letting it run, then change the day name / number used in the compair / print only ???

						offset = (weekday >= this.weekStartDay) ? weekday-this.weekStartDay : 7-this.weekStartDay+weekday ;
						weekstartday = 3;
						var current_weekday = i2 - weekstartday;
						if (current_weekday < 0) {
						current_weekday += 7;
						}

						*/

						linkdata = this.objname + ".setcurrday("+currday+"); " + this.onnewdayselected + " return false;";

						tablehtml += "\t\t<td align=\"center\" class=\"calendar_day"+coladdin_selected+coladdin_daycurr+"\"><a href=\"#\" onclick=\""+linkdata+"\">"+currday+"</a></td>\n";
						currday++;
						startdays = true;

					} else {
						//Complete start of table empty cells
						tablehtml += "\t\t<td></td>\n";
					}

				}

			}

			tablehtml += "\t</tr>\n";

			//If have completed printing rows, and the last row holds the last day of the month, then no more rows needed
			if (currday > this.currdaysinmonth) {
				break;
			}

		}

		//Prev month link
		if (this.currmonth == 1) {
			prev_year = this.curryear-1;
			prev_month = 12;
		} else {
			prev_year = this.curryear;
			prev_month = this.currmonth-1;
		}

		//Next month link
		if (this.currmonth == 12) {
			next_year = this.curryear+1;
			next_month = 1;
		} else {
			next_year = this.curryear;
			next_month = this.currmonth+1;
		}

		prevlinkdata = this.objname + ".setcurrmonth("+prev_year+", "+prev_month+"); return false;";
		nextlinkdata = this.objname + ".setcurrmonth("+next_year+", "+next_month+"); return false;";

		//prevlinkdata = this.objname + ".monthnav('prev');";
		//nextlinkdata = this.objname + ".monthnav('next');";

		//Month change nav
		// bgcolor=\"#F7F7F7\"
		tablehtml += "\t<tr>\n";
		tablehtml += "\t\t<td colspan=\"7\" class=\"calendar_monthchange\"><div style=\"float: left\"><a href=\"#\" onclick=\""+prevlinkdata+"\">&lt;&lt; "+this.monthshortarr[prev_month-1]+"</a></div><div style=\"float: right\"><a href=\"#\" onclick=\""+nextlinkdata+"\">"+this.monthshortarr[next_month-1]+" &gt;&gt;</a></div></td>\n";
		tablehtml += "\t</tr>\n";

		tablehtml += "</table>\n";

		//tablehtml = tablehtml+"<pre>" + htmlentities(tablehtml) + "</pre>";

		document.getElementById(this.caldivid).innerHTML = tablehtml;

	}

	//Prepare month dropdown selection
	this.generatemonthdropdown = function() {

		var dropdownhtml = "";
		var dropdownoptions = "";

		for (i in this.montharr) {

			if (this.currmonth == parseInt(i)+1) {
				var selected = "selected=\"selected\"";
			} else {
				var selected = "";
			}
			dropdownoptions += "\t<option "+selected+" value=\""+i+"\">"+this.montharr[i]+"</option>\n";
		}

		dropdownhtml += "<select size=\"1\" onchange=\""+this.objname+".setcurrmonthonly(parseInt(this.value)+1)\" name=\""+this.objname+"_"+"monthselect"+"\">\n"+dropdownoptions+"\n</select>";

		return dropdownhtml;

	}

	//Prepare year dropdown selection
	this.generateyeardropdown = function() {

		var dropdownhtml = "";
		var dropdownoptions = "";

		var currdate = new Date();
		var year = currdate.getFullYear();

		for (i=year-10; i<year+10; i++) {
			if (this.curryear == i) {
				var selected = "selected=\"selected\"";
			} else {
				var selected = "";
			}
			dropdownoptions += "\t<option "+selected+" value=\""+i+"\">"+i+"</option>\n";
		}

		dropdownhtml += "<select size=\"1\" onchange=\""+this.objname+".setcurryear(this.value)\" name=\""+this.objname+"_"+"yearselect"+"\">\n"+dropdownoptions+"\n</select>";

		return dropdownhtml;

	}

	//Resolve value to indexid
	this.resolvevaluetoindexid = function(needle, haystack) {
		for (i in haystack) {
			if (needle == haystack[i]) {
				return i;
			}
		}
		return false;
	}

	//HTMLentities - escape data to html format
	//Used for debugging only
	function htmlentities(string) {

		string = string.replace(/\&/g,"&amp;");
		string = string.replace(/\</g,"&lt;");
		string = string.replace(/\>/g,"&gt;");
		string = string.replace(/\"/g,"&quot;");

		return string;

	}

}

/*

//http://www.webreference.com/programming/javascript/diaries/15/3.html
//http://www.zend.com/zend/trick/tricks-Oct-2002.php
//http://www.webreference.com/programming/javascript/diaries/15/2.html
//http://www.w3schools.com/js/js_obj_date.asp
//http://www.webreference.com/programming/javascript/diaries/15/4.html

*/

