/*=========================================================================================
* Control:	oCalendar (version 2.0)
* Author:	Mike Sarieh
* Updated:	05-OCT-2005
*===========================================================================================*/

var oCalendar= function(id){
	this.id= id;
	this.displaySize= 0;
	this.baseElement= null;		//element to pop calendar in relation to (button)
	this.resultElement= null;	//element receiving the result (input.)
	this.initialDate= ""
	this.format= 1;
	this.lastDate= new Date()
	//User methods
	this.show= oCalendar_show
}
var oCalendarDays= new Array("Monday","Tuesday","Wednsday","Thursday","Friday","Saturday","Sunday");
var oCalendarMonths= new Array("January","February","March","April","May","June","July","August","September","October","November","December")
//---------------------------------------
function oCalendar_setArray(v1, v2, v3, v4, v5, v6, v7){
	this.d1= v1; this.d2= v2; this.d3= v3; this.d4= v4; this.d5= v5; this.d6= v6; this.d7= v7
}
//---------------------------------------
function oCalendar_onDateChange(inval){
// inval can be a date, 1 (next shown month) or -1 (prev shown month)
	inval= '' + inval
	if ((inval == null)||(inval.length == 0)){ 
		inval= new Date()
	}else if (isNaN(inval) == false){
		var d = new Date(oCalendar.lastDate)
		if (Math.abs(inval) == 10){		//year
			inval= new Date(d.getYear() + (parseInt(inval)/10), d.getMonth(), d.getDate())
		}else{
			inval= new Date(d.getYear(), d.getMonth() + parseInt(inval), d.getDate())
		}
	}
	oCalendar_show(oCalendar.baseElement, oCalendar.resultElement, oCalendar.displaySize, inval)
}
//---------------------------------------
function oCalendar_onClick(e, fmt){
	function fnRight(sz, n){
		return(sz.substr(sz.length-n))
	}
	var d= new Date(e.sdate);
	switch (fmt) {
		case 1:
			var sz= fnRight('0' + (d.getMonth()+1),2) 
				+ "/" + fnRight('0' + d.getDate(),2)
				+ "/" + d.getFullYear();
			break;
		case 2:
			var sz= d.toLocaleDateString();
		case 3:
			var n= ((d.getDay()-1) < 0)? 6: (d.getDay()-1)
			var sz= oCalendarDays[n].substr(0,3).toUpperCase() 
				+ ' ' + fnRight('0' + d.getDate(),2) 
				+ '-' + oCalendarMonths[d.getMonth()].substr(0,3).toUpperCase() 
				+ '-' + d.getFullYear();
			break;
	}
	
	var e2= oCalendar.resultElement
	if (e2.tagName == "INPUT"){
		e2.value= sz;
	}else if (oCalendar.resultElement.innerText){
		e2.innerText= sz;
	}
	try{
		eval(e2.id + "_onChange(\"" + e.sdate + "\")");
	}catch(err){	 
		try {
			e2.fireEvent("onchange");
		}catch(err){ 
			void(0);
		} 
	}
}
//---------------------------------------
function oCalendar_show(oBaseElement, oResultElement, nSize, indate){
	//size= 0(small), 1(medium), 2(large), 3(very large)
	
	//save user values
	oCalendar.displaySize= nSize
	oCalendar.baseElement= oBaseElement
	oCalendar.resultElement= oResultElement
	
	var hcolor= "#ff4500"
	var RowHeight= new Array("18px", "21px", "28px", "50px")
	var CellWidth= new Array("30px", "32px", "38px", "62px")

	var oInDate= new Date(indate);
	if (isNaN(oInDate.getMonth())) oInDate= new Date();

	var sInDate= (oInDate.getMonth()+1) + "/" + oInDate.getDate() + "/" + oInDate.getFullYear()
	this.initialDate= sInDate
	oCalendar.lastDate = sInDate
	var mm= oInDate.getMonth()
	var oADate=  new Date(oInDate.getMonth()+1 + "/1/" + oInDate.getYear())
	var oToday= new Date()
	var sToday= (oToday.getMonth()+1) + "/" + oToday.getDate() + "/" + oToday.getFullYear()

	var sz = "<table style=\"border:solid 1px #000033; background-color:#ffffff;\" border=0 cellpadding=0 cellspacing=0>"
			+ "<tr style=\"background-color:#003366; height:" + RowHeight[nSize] + "\"><td colspan=7>"
			+ "<table border=1 bordercolor=white cellpadding=0 cellspacing=0 style=\"width:100%; height:100%; border-collapse:collapse;\"><tr>"
			+ "<td style=\"width:15%; text-align:center; cursor:hand; color:white; font:bold 8pt arial;\" "
			+ "onclick=\"oCalendar_onDateChange(-10)\" onmouseover=\"this.style.color='" + hcolor + "';\" onmouseout=\"this.style.color='white';\" title='Previous Year'><<</td>"
			+ "<td style=\"width:15%; text-align:center; cursor:hand; color:white; font:bold 8pt arial;\" "
			+ "<td align=center style=\"width:15%;cursor:hand;color:white;font:bold 8pt arial;\" "
			+ "onclick=\"oCalendar_onDateChange(-1)\" onmouseover=\"this.style.color='" + hcolor + "';\" onmouseout=\"this.style.color='white';\" title='Previous Month'><</td>"
			+ "<td id=CurrentDate align=center style=\"width:40%;font:bold 8pt arial; color:white;\">" 
			+ oCalendarMonths[oInDate.getMonth()].substr(0,3).toUpperCase() + " " + oInDate.getFullYear() + "</td>"
			+ "<td style=\"width:15%; text-align:center; cursor:hand; color:white; font:bold 8pt arial;\" "
			+ "onclick=\"oCalendar_onDateChange(1)\" onmouseover=\"this.style.color='" + hcolor + "';\" onmouseout=\"this.style.color='white';\" title='Next Month'>></td>"
			+ "<td style=\"width:15%; text-align:center; cursor:hand; color:white; font:bold 8pt arial;\" "
			+ "onclick=\"oCalendar_onDateChange(10)\" onmouseover=\"this.style.color='" + hcolor + "';\" onmouseout=\"this.style.color='white';\" title='Next Year'>>></td>"
			+ "</tr></table></td></tr>"
			+ "<tr style=\"height:" + RowHeight[nSize] + ";background-color:#6699cc; cursor:default;font:normal 8pt arial;color:white;\">"

	var n= (nSize > 2)? 12: (nSize+1) 
	for (i= 0; i < 7; i++){
		sz += "<td style=\"width:" + CellWidth[nSize] + "\" align=center valign=middle>" 
			+ oCalendarDays[i].substr(0,n) + "</td>"
	}
	sz += "</tr></td></tr><tr><td>"	

	var dayNum= oADate.getDay()
	if (dayNum == 0) dayNum = 7
	oADate = new Date(oADate.getYear(), oADate.getMonth(), 1 - dayNum + 1)
	sADate = (oADate.getMonth()+1) + "/" + oADate.getDate() + "/" + oADate.getFullYear()
	var css= ""
	for (i = 0; i < 6; i++){
		sz += "<tr value='" + sADate + "' style=\"cursor:hand;\">"
		for (j = 0; j < 7; j++){
			css= (mm == oADate.getMonth())? "color:black;": "color:silver;"

			if (sADate == sToday){
				css += "font-weight:bold;"
			}else if (sADate == this.initialDate){
				css += "background-color:" + hcolor + ";color:white;"
			}
			sz += "<td align=center valign=middle style=\"" + css + "height:" + RowHeight[nSize] + ";\" "
				+ "onmouseover=\"this.style.border='1px solid " + hcolor + "'\" onmouseout=\"this.style.border='none'\" "
				+ "onclick=\"oCalendar_onClick(this," + this.format + ")\" sdate=\"" + sADate + "\">" 
				+ oADate.getDate() + "</td>"
			oADate= new Date(oADate.getYear(), oADate.getMonth(), oADate.getDate() + 1)
			sADate= (oADate.getMonth()+1) + "/" + oADate.getDate() + "/" + oADate.getFullYear()
		}
		sz += "</tr>"		
	}
	sz += "<tr style=\"height:" + RowHeight[nSize] + "\"><td colspan=\"7\" align=\"center\" valign=\"middle\" "
		+ "style=\"font:normal 8pt arial;border-top:solid 1pt #4682b4;\">"
	
	var n= ((oToday.getDay()-1) < 0)? 6: (oToday.getDay()-1)
	var sShortDate = oCalendarDays[n].substr(0,3) + " " + oToday.getDate() 
		+ "-" + oCalendarMonths[oToday.getMonth()].substr(0,3).toUpperCase() 
		+ "-" + oToday.getFullYear();

	sz += "<span style=\"cursor:hand;color:#000066\" onmouseover=\"this.style.color='" + hcolor + "';\" "
		+ "onmouseout=\"this.style.color='#000066';\" onclick=\"oCalendar_onDateChange('" + sToday + "')\" title=\"Go to today's date\">"
		+ "Today:&nbsp;" + sShortDate + "</span></td></tr></table>"
	
	oBaseElement.innerHTML= sz;
}
//---------------------------------------
