var cur_Div=""
function getDaysInMonth(iMonth, iYear) {
	var dPrevDate = new Date(iYear, iMonth, 0);
	return dPrevDate.getDate();
}

function buildCalCells(iYear, iMonth, iDayStyle) {
	for (x=0;x<7;x++) {
		this.aryCells[x] = new Array(7);
	}
	var dCalDate = new Date(iYear, iMonth-1, 1);
	var iDayOfFirst = dCalDate.getDay();
	var iDaysInMonth = getDaysInMonth(iMonth, iYear);
	var iVarDate = 1;
	var i, d, w;
	for (w=1;w < 7; w++) {
		for (d = 0; d < 7; d++) {
			iVarDate = (w==1 && d < iDayOfFirst)  ? 0 : iVarDate;
			if (iVarDate <= iDaysInMonth) {
				this.aryCells[w][d] = iVarDate;
			} else {
				this.aryCells[w][d] = 0;
			}
			iVarDate++;
		}
	} 
}

function WebCalendar(iYear, iMonth, iDayStyle, sObjName, objToFill) {
	
	if (iYear < 2000){
	   iYear += 1900;
	}
	this.objName = sObjName
	this.htmlToWrite = new String()
	this.aryCells = new Array()
	this.dDate = new Date()
	this.dSelYear = iYear
	this.dSelMonth = iMonth
	this.dSelDay
	this.dSelDate = new Date()
	this.iDayStyle = iDayStyle
	this.dCurMonth = this.dDate.getMonth()
	this.dCurDayOfMonth = this.dDate.getDate()
	this.dCurYear = this.dDate.getFullYear()
	this.objToFill = objToFill
	this.dayStyles = new Array(4)
	this.dayStyles[0] = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	this.dayStyles[1] = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	this.dayStyles[2] = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
	this.dayStyles[3] = new Array("S","M","T","W","T","F","S");
	this.months = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	this.createSelects = createSelects
	this.createCal = createCal
	this.createCells = buildCalCells
	this.setDate = setSelectedDate
	this.selectChange = selectChange
	this.setCurrentDate = setCurrentDate
	this.fillDateFld = fillDateFld

	this.createSelects()
	this.createCal(iYear, iMonth, iDayStyle)
	this.setCurrentDate()
}

function createSelects() {
	var htmlToWrite = new String()
	htmlToWrite += "<select id='selMonth' style='font : xx-small;' onchange='" 
		+ this.objName + ".selectChange()'>"
	for (m=0;m<12;m++) {
		htmlToWrite+= "<option value='" + (m+1) +"' >" 
			+ this.months[m] + "</option>"
	}
	htmlToWrite+= "</select>"
	htmlToWrite+= "<select id='selYear' style='font : xx-small;' onchange='" 
		+ this.objName + ".selectChange()'>"
	var tempYear = this.dCurYear
	for (y=-1;y<3;y++) {
		var optYear = tempYear + y
		htmlToWrite+= "<option value='" + optYear + "'>" 
			+ optYear + "</option>"
	}
	htmlToWrite+="</select>"
	this.htmlToWrite = htmlToWrite
}

function createCal(iYear, iMonth, iDayStyle) {
	var myMonth;
	var iDay;
	var htmlToWrite = new String()
	this.createCells(iYear, iMonth, iDayStyle);
	htmlToWrite = "<div id='calendar'><table border='1' bordercolor='gray' cellpadding='0' cellspacing='0'>"
	htmlToWrite += "<tr bgcolor='99CCFF'>";
	for (d = 0; d < 7; d++) {
		htmlToWrite += "<td class='body_cal' align='center'>" + 
			this.dayStyles[iDayStyle][d] + "</td>";
	}
	htmlToWrite += "</tr>";
	for (w = 1; w < 7; w++) {
		htmlToWrite += "<tr>"
		for (d = 0; d < 7; d++) {
			iDay = this.aryCells[w][d]
			htmlToWrite += "<td";
			if (iDay > 0) {
				htmlToWrite += " align='center'><a href='javascript:;' class='body_cal'> " +
					"<div " +
					"onclick='" + this.objName + 
					".setDate(this);return false;' " +
				"id='" + iDay + "calDate'>" + iDay +
				"</div></a>";
			} else {
				if (iDay < 31) htmlToWrite += " bgcolor='28428E' align='center'>&nbsp;"
			}
			htmlToWrite += "</td>"
		}
		htmlToWrite += "</tr>";
	}
	htmlToWrite += "</table></div>"
	this.htmlToWrite += htmlToWrite
	var calDiv = document.getElementById("calendar")
	if (calDiv) {
		calDiv.outerHTML = this.htmlToWrite
	} else {
		//document.write("<form id='frmCalendar'>")
		document.write(this.htmlToWrite)
		//document.write("</form>")
	}
}

function setCurrentDate() {
	var selYear = document.getElementById("selYear")
	var selMonth = document.getElementById("selMonth")
	selMonth.options[this.dSelMonth-1].selected = true;
	for (i = 0; i < selYear.length; i++) {
		var iYearVal = selYear.options[i].value
		if (parseInt(iYearVal) == this.dSelYear) {
			selYear.options[i].selected = true;
		}
	} 
}

function selectChange() {
	var objSelYear = document.getElementById("selYear")
	var objSelMonth = document.getElementById("selMonth")

	this.htmlToWrite = ""
	this.dSelDay = null
	this.dSelDate = new Date()
	this.dSelYear = objSelYear.value
	this.dSelMonth = objSelMonth.value
	this.createCal(this.dSelYear, this.dSelMonth, this.iDayStyle)
}

function setSelectedDate(obj) {
	var iDay = parseInt(obj.id)
	this.dSelDay = iDay
	this.dSelDate = new Date(this.dSelYear, this.dSelMonth-1, iDay)
	this.fillDateFld()
}

function fillDateFld() {
	var tmpMonth = this.dSelMonth
	if (tmpMonth < 10) {
		tmpMonth = "0" + tmpMonth
	}
	var tmpDay = this.dSelDay
	if (tmpDay < 10) {
		tmpDay = "0" + tmpDay
	}
	
	var sDispDate = new String(tmpMonth + "/" + tmpDay + "/" + this.dSelYear);
	this.objToFill.value = sDispDate;
	hidelayer();
}

function hidelayer() {
	cur_Div.style.visibility = 'hidden';
}