

<!-- Original:  Kedar R. Bhave (softricks@hotmail.com) -->
<!-- Web Site:  http://www.softricks.com -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

var DatePicker=new Calendar();

function Calendar(p_item, p_WinCal, p_month, p_year, p_format) {
	this.CurDate=new Date();
	this.Param=new Object();
	this.Param.Months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];	
	this.Param.WeekDay=["Sun", "Tue", "Wed", "Thu", "Fri", "Sat", "Mon"];
	this.Param.weekend=[0,6];
	this.Param.WeekStartDay=1;
	this.Param.dateformat="DD/MM/YYYY";
	// Non-Leap year Month days..
	this.DOMonth=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	// Leap year Month days..
	this.lDOMonth=[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	this.ShowYearButton=true;
		
	this.get_daysofmonth=function(monthNo, p_year) {
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return this.DOMonth[monthNo];
	
		return this.lDOMonth[monthNo];
	} else
		return this.DOMonth[monthNo];
	}
	
	this.calc_month_year=function (p_Month, p_Year, incr) {
		/* 
		Will return an 1-D array with 1st element being the calculated month 
		and second being the calculated year 
		after applying the month increment/decrement as specified by 'incr' parameter.
		'incr' will normally have 1/-1 to navigate thru the months.
		*/
		var ret_arr = new Array();
	
		if (incr == -1) {
			// B A C K W A R D
			if (p_Month == 0) {
				ret_arr[0] = 11;
				ret_arr[1] = parseInt(p_Year) - 1;
			}
			else {
				ret_arr[0] = parseInt(p_Month) - 1;
				ret_arr[1] = parseInt(p_Year);
			}
		} else if (incr == 1) {
			// F O R W A R D
			if (p_Month == 11) {
				ret_arr[0] = 0;
				ret_arr[1] = parseInt(p_Year) + 1;
			}
			else {
				ret_arr[0] = parseInt(p_Month) + 1;
				ret_arr[1] = parseInt(p_Year);
			}
		}
	
		return ret_arr;
	}
	this.isHightlight=function(dt,mm,yy) {return false;}
}



Calendar.prototype.buf="";

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	
	// Begin Table Drawing code here..
	vCode+='<div class="daytable">';
	vCode = vCode + '<TABLE width="183" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">';
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	
	vCode = vCode + "</TABLE>";
	vCode+='</div>';
	return vCode;
}

Calendar.prototype.hidewin=function() {this.Win.closewin();}

Calendar.prototype.show = function(p_item, p_month, p_year) {
			
	if ((p_month == null) || (p_year == null))	{
		var cdt=new Date();
		this.gMonth=parseInt(cdt.getMonth());
		this.gYear=new String(cdt.getFullYear().toString());
	} else {
		this.gYear=p_year
		this.gMonth = new Number(p_month);
	}
	
	this.gMonthName = this.Param.Months[this.gMonth];
	this.gReturnItem=p_item;
	this.p_item=p_item;
	if (!this.Win) {this.Win=new BlockContainer;} //else {

	
	var ps=new Object();
	var u=new CElementUntil();
	ps=u.getObjectPosition(document.getElementById(this.p_item));
	var sz=u.getObjectSize(document.getElementById(this.p_item));
	ps.y=sz.y+ps.y;
	this.Win.moveTo(ps);


	this.Win.show(this.getcalendarstring());
}

Calendar.prototype.getcalendarstring=function() {
	this.buf="";
		// Show navigation buttons
	var prevMMYYYY = this.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = this.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	this.wwrite("<div class='datepicker'>");
	this.wwrite("<TABLE width='100%' border='0' cellpadding='2' cellspacing='0' class='datepicker'>");	
	this.wwrite("<TR><TD class='date'>");
	if (this.ShowYearButton) {
		this.wwrite("<A HREF=\"" +
			"javascript: DatePicker.show(" + 
			"'" + this.p_item + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1) + "'" +
			");" +
			"\"><<<\/A>");
	}
	this.wwrite("</TD><TD class='date'>");
	this.wwrite("<A HREF=\"" +
		"javascript:DatePicker.show(" + 
		"'" + this.p_item + "', '" + prevMM + "', '" + prevYYYY + "'" +
		");" +
		"\"><<\/A></TD><td class='datepicker_monthname'>");
	this.wwriteA(this.gMonthName + ", " + this.gYear.substr(2,2));
	this.wwrite("</TD><TD class='date'>");
	
	this.wwrite("<A HREF=\"" +
		"javascript: DatePicker.show(" + 
		"'" + this.p_item + "', '" + nextMM + "', '" + nextYYYY + "'" +
		");" +
		"\">><\/A>");	
	this.wwrite("</TD><TD class='date'>");
	if (this.ShowYearButton) {
		this.wwrite("<A HREF=\"" +
			"javascript:DatePicker.show(" + 
			"'" + this.p_item + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1) + "'" +
			");" +
			"\">>><\/A>");
	}	
		
	this.wwrite("</TD></TR></TABLE>");

	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	this.wwrite(vCode);
	this.wwrite("<div><a href='javascript: DatePicker.hidewin();'>close</a></div>");
	this.wwrite("</div>");
	//var op=window.open("");
	//op.document.body.innerText=this.buf;
	return this.buf;
}

Calendar.prototype.wwrite = function(wtext) {this.buf+=wtext;}

Calendar.prototype.wwriteA = function(wtext) {this.buf+=wtext;}

Calendar.prototype.cal_header = function() {
	var vCode = "";
	vCode += "<TR>";
	for(var i=0; i<7; i++) {
		wd=((i+this.Param.WeekStartDay) % 7)
		vCode += "<td class='datepicker_dayname'>"+this.Param.WeekDay[wd]+"</TD>";
	}
	vCode = vCode + "</TR>";
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vLastDay=this.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/
	var dcount=0-((vDate.getDay()+7-this.Param.WeekStartDay) % 7)+1;
	while (dcount<=vLastDay) {
		vCode +="<tr>";
		for(var col=0; col<7; col++) {			
			if ((dcount>0) && (dcount<=vLastDay)) {
				vCode += "<TD WIDTH='14%' class='" + this.getDateClass(dcount, this.gMonth, this.gYear) + "'>" + 
				 this.getFormatedDate(dcount, this.gMonth, this.gYear)+"</TD>";
			} else {
				wd=((col+this.Param.WeekStartDay) % 7);
				vCode += "<TD class='" + this.getWeekendClass(wd) + "'>&nbsp;</TD>";
			}
			dcount++;
		}
		vCode +="</tr>";
	}

	return vCode;
	
}


Calendar.prototype.getFormatedDate = function(dt, mm, yy) {
	if (this.isDisable(dt, mm, yy)) {
		return dt;
	}else {
	 return "<A HREF=\"javascript: document.getElementById('"+this.gReturnItem+"').value='" + 
				this.format_data(dt) + "';DatePicker.hidewin();\">" + 
				dt + 
			"</A>";	
	}
}

Calendar.prototype.isDisable=function(dt, mm, yy) {return false;}

Calendar.prototype.getDateClass=function(dt, mm, yy) {
	
	var tmp
	var dd=new Date(yy, mm, dt);
	tmp=this.getAdditinalStyle(dt, mm, yy); 	
	return tmp+" " + this.getWeekendClass(dd.getDay());
}

Calendar.prototype.getAdditinalStyle=function(dt, mm, yy) {
		if (window.datepicker_isHightlight) {
			if (window.datepicker_isHightlight(dt,mm,yy)) {return ("datepicker_hightlight");}
		}
		if (dt==this.CurDate.getDate() && mm==this.CurDate.getMonth() && yy==this.CurDate.getFullYear()) {return "datepicker_currentday";}
}

Calendar.prototype.getWeekendClass=function(weekday) {
	for (var i=0; i<this.Param.weekend.length; i++) { if (weekday == this.Param.weekend[i]) return ("datepicker_nonworkdate");}
	return ("datepicker_date");
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = this.Param.Months[this.gMonth].substr(0,3).toUpperCase();
	var vFMon = this.Param.Months[this.gMonth].toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (this.Param.dateformat) {
		case "MM\/DD\/YYYY" :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
			break;
		case "MM\/DD\/YY" :
			vData = vMonth + "\/" + vDD + "\/" + vY2;
			break;
		case "MM-DD-YYYY" :
			vData = vMonth + "-" + vDD + "-" + vY4;
			break;
		case "MM-DD-YY" :
			vData = vMonth + "-" + vDD + "-" + vY2;
			break;

		case "DD\/MON\/YYYY" :
			vData = vDD + "\/" + vMon + "\/" + vY4;
			break;
		case "DD\/MON\/YY" :
			vData = vDD + "\/" + vMon + "\/" + vY2;
			break;
		case "DD-MON-YYYY" :
			vData = vDD + "-" + vMon + "-" + vY4;
			break;
		case "DD-MON-YY" :
			vData = vDD + "-" + vMon + "-" + vY2;
			break;

		case "DD\/MONTH\/YYYY" :
			vData = vDD + "\/" + vFMon + "\/" + vY4;
			break;
		case "DD\/MONTH\/YY" :
			vData = vDD + "\/" + vFMon + "\/" + vY2;
			break;
		case "DD-MONTH-YYYY" :
			vData = vDD + "-" + vFMon + "-" + vY4;
			break;
		case "DD-MONTH-YY" :
			vData = vDD + "-" + vFMon + "-" + vY2;
			break;

		case "DD\/MM\/YYYY" :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
			break;
		case "DD\/MM\/YY" :
			vData = vDD + "\/" + vMonth + "\/" + vY2;
			break;
		case "DD-MM-YYYY" :
			vData = vDD + "-" + vMonth + "-" + vY4;
			break;
		case "DD-MM-YY" :
			vData = vDD + "-" + vMonth + "-" + vY2;
			break;

		default :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
	}

	return vData;
}

function BlockContainer() {
		//this.obj=document.getElementById("dp_holder");
		this.show=function(txt) {
				this.dv.style.display="block";
				this.dv.innerHTML=txt;
				svn=document.getElementsByTagName("SELECT");
				for (a=0;a<svn.length;a++){svn[a].style.visibility="hidden";}
		}
		this.moveTo=function(pos) {
			var Util=new CElementUntil();
			Util.moveTo(this.dv, pos);
		}

		this.dv=document.createElement("div");
		document.body.appendChild(this.dv);		
		this.dv.style.position="absolute";
		this.dv.style.zIndex=10000;
		this.closewin=function() {
				this.dv.style.display="none";
				for (a=0;a<svn.length;a++){svn[a].style.visibility="visible";}
		}
}


function CElementUntil() {

	this.isCSS=(document.body && document.body.style) ? true : false;
	
	this.isW3C = (this.isCSS && document.getElementById) ? true : false;
	this.isIE4 = (this.isCSS && document.all) ? true : false;
	this.isNN4 = (document.layers) ? true : false;
	this.isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? 
            true : false;


	this.getRawObject=function (obj) {
    	if (typeof obj == "string") { return document.getElementById(obj);
    		} else { return obj;}
	}

	// Retrieve the x coordinate of a positionable object
	this.getObjectPosition=function (obj)  {
    	var elem = this.getRawObject(obj);
		var pos=new Object();    
    	var offsetTrail = elem;
    	var offsetLeft = 0;
    	var offsetTop = 0;
    	// account for IE 6 CSS compatibility mode
   		while (offsetTrail) {
        	offsetLeft += offsetTrail.offsetLeft;
        	offsetTop += offsetTrail.offsetTop;
        	offsetTrail = offsetTrail.offsetParent;
    	}
   		if (navigator.userAgent.indexOf("Mac") != -1 && 
        	typeof document.body.leftMargin != "undefined") {
        	offsetLeft += document.body.leftMargin;
        	offsetTop += document.body.topMargin;
    	}
		pos.x=offsetLeft;
		pos.y=offsetTop;
    	return pos;
	}
   
   
	// Retrieve the rendered width of an element
	this.getObjectSize=function (obj)  {
    	var elem = this.getRawObject(obj);
		//alert(elem.name);
    	var size=new Object();
    	if (elem.offsetWidth) {
        	size.x = elem.offsetWidth;
			size.y = elem.offsetHeight;
    	} else if (elem.clip && elem.clip.width) {
        	size.x = elem.clip.width;
			size.y = elem.clip.height;
    	} else if (elem.style && elem.style.pixelWidth) {
        	size.x = elem.style.pixelWidth;
			size.y = elem.style.pixelHeight;
    	}
    	return size;
	}
   
   this.moveTo=function(obj, pos) {
   		var elem = this.getObject(obj);		
    	if (elem) {
        	if (this.isCSS) {
            elem.left = pos.x+"px";
            elem.top = pos.y+"px";
        	} 
    	}		
   }
   this.getObject=function (obj) {
    	var theObj = this.getRawObject(obj);
    	if (theObj && this.isCSS) {theObj = theObj.style;}
    	return theObj;
	}

   

}

