// definitions
var monthsArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
var weekArray	= new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
firstYear = 1995
lastYear  = 2030

var tmpStyleName
var CalendarPopUp
var myCalendar

validateDigits=new RegExp("^([0-9])$");
function filterDigits(tmp) {
	keyCode = (tmp.keyCode)?tmp.keyCode:(tmp.charCode)?tmp.charCode:(tmp.which)?tmp.which:0
	// Exclude navigation keys from digit filter
	// 8 = BACKSPACE; 9 = TAB; 13 = ENTER;  46 = DEL; 37 = <-; 39 = ->;
	if (keyCode==8 || keyCode == 9 ||  keyCode == 46 ||  keyCode == 39 ||  keyCode == 37 || keyCode == 13 ) return true
	tmpStr = String.fromCharCode(keyCode)
	return validateDigits.test(tmpStr)
}

function openCalendar(tmpObj) {
	var screenX,screenY
	if (document.all)
	{
		screenX = top.window.screen.availWidth;
		screenY = top.window.screen.availHeight - 60;
	}
	else
	{
		screenX = top.innerWidth - 16;
		screenY = top.innerHeight - 16;
	}

	myCalendar = new CalendarObject(tmpObj)
	myCalendar.getDateFromContainer()
	CalendarPopUp = window.open('','CalendarPopUp','width=300,height=200,left='+((screenX / 2) - 150)+',top='+((screenY / 2) - 60)+',status=no,dependent=yes,alwaysRaised=yes')
	
	CalendarPopUp.document.open()
	CalendarPopUp.document.write(myCalendar.bodyHTML())
	//alert(CalendarPopUp.document.styles)
	
	CalendarPopUp.document.close()
	
	//wait while browser converts string code into elements and fill array of elements
	setTimeout("myCalendar.drawCalendar()",100);
	CalendarPopUp.focus()
}


function CalendarObject(tmpRef){
	this.tmpDate = new Date()
	
	this.ContainerData = new Date() // date from opener page
	this.CalendarPopUpDate = new Date() // date in popup window (select box date)
	this.ContainerObject = tmpRef
	
	// 
	this.getDateFromContainer = function(){
		var tmpDate,tmpMonth,tmpYear
		tmpMonth = this.ContainerObject.getElementsByTagName("input")[0].value - 1
		tmpDate = this.ContainerObject.getElementsByTagName("input")[1].value
		tmpYear = this.ContainerObject.getElementsByTagName("input")[2].value
		
		if (tmpMonth>0 & tmpDate>0 & tmpYear > 0) {
			if ( tmpYear<firstYear || tmpYear>lastYear ) tmpYear = firstYear
		
			this.ContainerData.setMonth(tmpMonth)
			this.ContainerData.setDate(tmpDate)
			this.ContainerData.setFullYear(tmpYear)
		}
	}
	
	// 
	this.setDateInContainer = function(){
		var objInputs = this.ContainerObject.getElementsByTagName("input")
		if (objInputs) {
			objInputs[0].value = this.ContainerData.getMonth() + 1
			objInputs[1].value = this.ContainerData.getDate()
			objInputs[2].value = this.ContainerData.getFullYear()
		}
	}
	
	// 
	this.getDateFromPopup = function() {
		doc = CalendarPopUp.document
		allSelects = doc.getElementsByTagName("select")
		if (allSelects) {
			this.CalendarPopUpDate.setDate(1)
			this.CalendarPopUpDate.setMonth(allSelects[0].selectedIndex)
			this.CalendarPopUpDate.setFullYear(allSelects[1].options[allSelects[1].selectedIndex].value)
		}
	}
	
	this.tmpDateAdd = function (tmpDays) {
		this.tmpDate.setDate(this.tmpDate.getDate()+tmpDays)
	}
	
	// Build HTML code for popup calendar
	// return result in html code as a string
	this.bodyHTML = function() {
		var tmpResult ="<html><head><link rel='stylesheet' type='text/css' href='" + tmpRef.attributes["IncludeFilesFolder"].value + "/calendar.css'>"	
		tmpResult += "<title>Calendar</title></head>"
		tmpResult += "<body><div><table class=calendar>"
		tmpResult += "<tr><th>"
		
		// create Months box
		tmpResult += "<select onChange=opener.myCalendar.drawCalendar()>"
		for (tmp=0; tmp<monthsArray.length; tmp++) {
			tmpResult += "<option"
			if ( tmp == this.ContainerData.getMonth() ) tmpResult += " selected"
			tmpResult += ">" + monthsArray[tmp]
		}
		tmpResult += "</select>"
		tmpResult += ""
		
		// create Years box
		tmpResult += "<select onChange=opener.myCalendar.drawCalendar()>"
		for (tmp=firstYear;tmp<=lastYear;tmp++) {
			tmpResult += "<option value=" + tmp 
			if ( tmp==this.ContainerData.getFullYear() ) tmpResult += " selected"
			tmpResult += ">" + tmp
		}
		tmpResult += "</select>"
		
		tmpResult += "</th></tr>"
		tmpResult += "<tr><td id='calendarBody'></td></tr>"
		tmpResult += "</table></div>"
		tmpResult += "<div><a href='#' onclick='self.close();return false'>Close</a><div></body></html>"
		return tmpResult
	}
	
	this.drawCalendar = function() {
		CalendarPopUp.document.getElementById("calendarBody").innerHTML = this.calendarHTML()
	}
	
	this.calendarHTML = function(){

		tmpString = "<table cellspacing=0>"
		tmpString += "<tr>"
		for (tmp=0;tmp<weekArray.length;tmp++) {
			tmpString += "<th>"+weekArray[tmp]+"</th>"
		}
		tmpString += "</tr>"
		
		this.getDateFromPopup()
		this.tmpDate = new Date(this.CalendarPopUpDate)
		
		var nextMonth = this.tmpDate.getMonth()+1
		nextMonth = (nextMonth==12)? 0 :nextMonth
		
		// move counter date to first day of week from previous month
		this.tmpDateAdd(-this.tmpDate.getDay())
		
		while (this.tmpDate.getMonth() != nextMonth) {
			tmpString += "<tr>"
			tmpString += this.NextDay() 
			while (this.tmpDate.getDay()!=0) {
				tmpString += this.NextDay()
			}
			tmpString += "</tr>"
		}
		
		tmpString +="</tr>"
		tmpString += "</table>"
		return tmpString 
	}
	
	
	
	this.returnDate =  function(d,m){
		doc = CalendarPopUp.document
		allSelects = doc.getElementsByTagName("select")
		D = d
		M = m + 1
		Y = allSelects[1].options[allSelects[1].selectedIndex].value
		this.ContainerObject.getElementsByTagName("input")[0].value = M
		this.ContainerObject.getElementsByTagName("input")[1].value = D
		this.ContainerObject.getElementsByTagName("input")[2].value = Y //.substring(2)
		CalendarPopUp.close()
		return false
	}
	
	this.NextDay = function(){
		
		var tmpClassName
		if (this.tmpDate.getMonth() == this.CalendarPopUpDate.getMonth()) {
			if (this.tmpDate.getDay()==0 | this.tmpDate.getDay()==6) {
				tmpClassName = "clsWeekEnd"
			}else{
				tmpClassName = "clsWorkDay"
			}
		}else{
			tmpClassName = "clsWorkDayOtherMonth"
		}
		
		if ((this.tmpDate.getDate() == this.ContainerData.getDate()) & 
				(this.tmpDate.getFullYear() == this.ContainerData.getFullYear()) & 
					(this.tmpDate.getMonth() == this.ContainerData.getMonth())) {
			tmpClassName = "clsCurrentDay"
		}
		
		
		var tmpString2 = "<td class='"+tmpClassName+"'>"
		if(this.tmpDate.getFullYear() >= firstYear && this.tmpDate.getFullYear() <= lastYear)
		{
			tmpString2 += "<a href='#' onClick='opener.myCalendar.returnDate("+this.tmpDate.getDate()+","+this.tmpDate.getMonth()+")'>" + this.tmpDate.getDate() +"</a>"
		}
		else
		{
			tmpString2 += "<a href='#'>" + this.tmpDate.getDate() + "</a>"
		}
		
		tmpString2 += "</td>"
		this.tmpDateAdd(1)
		return tmpString2
	}
}