if (typeof(arrMonth) == "undefined") 
	var arrMonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

if (typeof(dateMask) == "undefined")
	var dateMask = "d|m|y|-"

if (typeof(arrShortDay) == "undefined")
	var arrShortDay = new Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa") 

//calendar object constructor
function Calendar(years, returnFunc) {
	//properties
	this.years = years  //number of years in drop-down box
	this.returnFunc = returnFunc  //function to return the seleted date to
	this.docStyles = GetDocStyles()  //link to external stylesheets

	//methods
	this.show = ShowCal  //show calendar
	this.draw = DrawCal  //draw calendar html
	this.returnDate = ReturnDate  //function to call when a date is selected in the calendar

	function GetDocStyles() {
		var docStyles = ""
		var sSheets = document.styleSheets

		for (var i=0; i < sSheets.length; i++) {
			if ((sSheets[i].id == "cal-css") && (sSheets[i].disabled == false)) {
				docStyles = "<link rel=stylesheet href=" + sSheets[i].href + " type=text/css>"

				break
			}

			if (sSheets[i].href != "") docStyles += "<link rel=stylesheet href=" + sSheets[i].href + " type=text/css>"
		}

		return docStyles
	}
}

//set selected date in the return controls
function ReturnDate(year, month, day) {
	this.returnFunc(year, month + 1, day, this.returnParam)  //call the calendar return function
	window.focus()  //hide calendar by moving focus to the main window
}

//show calendar
function ShowCal(posCtl, title, startYear, selDate, returnParam) {
	//do not show calendar in ie4
	if (navigator.appVersion.indexOf("MSIE 4") != -1) return
	
	this.title = title  //calendar titlebar text
	this.startYear = startYear  //first year in the dropdown box

	this.selDate = selDate  //date selected on the calender
	this.year = selDate.getFullYear()  //current calendar year
	this.month = selDate.getMonth()  //current calendar month

	this.returnParam = returnParam  //extra parameter to pass to the return function

	//open calendar window
	var calPos = CalcCalCoord()
	this.win = window.open("", "", "channelmode=0, directories=0, fullscreen=0, hotkeys=0, left=" + calPos.x + ", location=0, menubar=0, resizable=0, status=0, titlebar=1, toolbar=0, top=" + calPos.y + ", width=141, height=133")

	//draw calendar
	this.draw()

	//calculate screen coordinates of the calendar window
	function CalcCalCoord() {
		var coord = new Object()
		if (typeof(window.screenLeft) == "undefined") {
			coord.x = screen.width / 2
			coord.y = screen.height / 2
		} else {
			var docBody = document.body
			var posTmp = posCtl
			
			coord.x = screenLeft + docBody.clientLeft - docBody.scrollLeft - 1
			coord.y = screenTop + docBody.clientTop - docBody.scrollTop + posCtl.offsetHeight + 1

			while (posTmp != docBody) {
				coord.x += posTmp.offsetLeft
				coord.y += posTmp.offsetTop
				posTmp = posTmp.offsetParent
			}

			if (typeof(blnDisplayFlg) != "undefined") {
				if ((posCtl == getPageObject("inDay")) || (posCtl == getPageObject("outDay"))) {
					coord.x += 6
					coord.y += 8
				}
			}
		}

		return coord
	}
}

//reload calendar html
function DrawCal(monthShift) {
	//shift calendar's month and year if monthShift passed
	if (typeof(monthShift) != "undefined") {
		//year
		this.year += Math.floor((this.month + monthShift) / 12)

		//month
		this.month = (this.month + monthShift) % 12
		if (this.month < 0) this.month += 12
	}

	//get day of the week of the 1st day of the month (0 - sun, 1 - mon, etc)
	var firstMonthDay = new Date(this.year, this.month, 1).getDay()

	//get first monday before the 1st day of the month
	var day = 1 - (firstMonthDay + 6) % 7

	//get "previous" and "next" link html
	var prevLnk = (this.year == this.startYear && this.month == 0) ? "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" : "&nbsp;<a class=cal-nav href=javascript:oCal.draw(-1)>&lt;&lt;</a>"
	var nextLnk = (this.year == this.startYear + this.years - 1 && this.month == 11) ? "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" : "<a class=cal-nav href=javascript:oCal.draw(1)>&gt;&gt;</a>&nbsp;"

	//get calender html	
	var calHTML =
		"<html>" +
		"<head>" +
		"<title>" + this.title + "</title>" +
		this.docStyles +
		"<script language=javascript>" +
		"var oCal;" +
		"window.onload = ResizeCal;" +
	 	"function ResizeCal() {" +
			"if (document.all) {" +
				"var cTbl = document.all.calTbl;" +
				"var cBody = document.body;" +
				"cBody.style.border = 0;" +
				"window.resizeBy(cTbl.offsetWidth - cBody.clientWidth, cTbl.offsetHeight - cBody.clientHeight)" +
			"} else {" +
				"var cTbl = document.getElementById('calTbl');  var x=50; var y=300; if (navigator.appVersion.indexOf('Macintosh') != -1) {x=25;y=15;}" +
				"window.resizeTo(cTbl.offsetWidth + y, cTbl.offsetHeight + x);" +
				"if (navigator.appVersion.indexOf('Macintosh') == -1) window.sizeToContent();" +
			"}" +
		"}" +
		"</script>" +
		"</head>" +
		'<body style="margin: 0">' +
		"<table id=calTbl border=0 cellpadding=0 cellspacing=0>" +
		"<tr>" +
			"<th class=cal-top colspan=7>" +
				"<table border=0 cellpadding=2 cellspacing=0>" +
				"<tr>" +
					"<th class=cal-top nowrap>" + prevLnk + "</th>" +
					"<th class=cal-top width=100%>" + GetDropdownHTML(this.startYear, this.years, this.year, this.month) + "</th>" +
					"<th class=cal-top nowrap>" + nextLnk + "</th>" +
				"</tr>" +
				"</table>" +
			"</th>" +
		"</tr>" +
		"<tr>" +
			"<td>" +
				"<table border=0 cellpadding=3 cellspacing=0 width=100%>" +
				"<tr>" +
					"<th class=cal-day>" + arrShortDay[1] + "</th>" +
					"<th class=cal-day>" + arrShortDay[2] + "</th>" +
					"<th class=cal-day>" + arrShortDay[3] + "</th>" +
					"<th class=cal-day>" + arrShortDay[4] + "</th>" +
					"<th class=cal-day>" + arrShortDay[5] + "</th>" +
					"<th class=cal-day>" + arrShortDay[6] + "</th>" +
					"<th class=cal-day>" + arrShortDay[0] + "</th>" +
				"</tr>"

	var calYear, calMonth, calDay, tdClass, aClass
	var calDate = new Date(this.year, this.month, day)

	do {
		calHTML += "<tr>"

		do {
			calYear = calDate.getFullYear()
			calMonth = calDate.getMonth()
			calDay = calDate.getDate()

			//get cell and link class names
			if (calDate.getDay() == 0 || calDate.getDay() == 6) {  //weekend
				aClass = "cal-wend"
			} else {
				aClass = "cal-wday"
			}

			if (calYear == this.selDate.getFullYear() && calMonth == this.selDate.getMonth() && calDay == this.selDate.getDate() && day > 0) {
				tdClass = "cal-this"
				aClass = "cal-lthis"
			} else if (calMonth == this.month && day > 0) {
				tdClass = "cal-month"
			} else {
				tdClass = "cal-date"
			}

			calHTML += "<th class=" + tdClass + "><a class=" + aClass + " href=javascript:oCal.returnDate(" + calYear + "," + calMonth + "," + calDay + ")>" + calDay + "</a></th>"

			day++
			calDate = new Date(this.year, this.month, day)
		} while (calDate.getDay() != 1)  //1 - monday

		calHTML += "</tr>"
	} while (calDate.getMonth() == this.month)

	calHTML +=
				"</table>" +
			"</td>" +
		"</tr>" +
		"</table>" +
		"</body>" +
		"</html>"

	//write calender html into the window
	with (this.win.document) {
		open()
		write(calHTML)
		close()
	}

	//set reference inside calendar window to the calendar object in the parent window
	this.win.oCal = this

	//get html of the calendar's dropdown
	function GetDropdownHTML(startYear, years, year, month) {
		var sMask = GetDropdownDateMask()
		var loopMonth, loopYear
		var sName, sValue, sSelected

		var dropdownHTML = "<select class=cal-sel onchange=oCal.draw(parseInt(this[this.selectedIndex].value))>"

		for (loopYear = startYear; loopYear < startYear + years; loopYear++) {
			for (loopMonth = 0; loopMonth < 12; loopMonth++) {
				sValue = (loopYear - year) * 12 + loopMonth - month

				sName = sMask.replace(/y/, loopYear)
				sName = sName.replace(/m/, arrMonth[loopMonth])

				sSelected = ((loopYear == year && loopMonth == month) ? " selected" : "")

				dropdownHTML += "<option value=" + sValue + sSelected + ">" + sName + "</option>"
			}
		}

		dropdownHTML += "</select>"

		return dropdownHTML
	}

	//returns date mask for month-year drop-down
	function GetDropdownDateMask() {
		var arrMask = dateMask.split("|")
		var sSeparator = arrMask[3]
		var sTmpMask = ""

		for (var i=0; i < arrMask.length-1; i++) {
			if (arrMask[i].indexOf("d") == -1 )  //do not include day in the mask
				sTmpMask += arrMask[i] + sSeparator
		}

		if (sSeparator != "")
			sTmpMask = sTmpMask.substring(0, sTmpMask.length - 1)

		return sTmpMask
	}
}
