
function Calendar_search(iYear, iMonth, iDay, ContainerId, ClassName, nth)
{
	MonthNames = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	//If no parameter is passed use the current date.
	oDate = new Date();
	Year = (iYear == null) ? oDate.getFullYear() : iYear;
	Month = (iMonth == null) ? oDate.getMonth() : iMonth - 1;
	curMonth = oDate.getMonth();
	curYear = oDate.getFullYear();
	while(Month < 0){Month += 12;Year--}
	while(Month >= 12){Month -= 12;Year++}
	Day = (iDay == null) ? 0 : iDay;
	oDate = new Date(Year, Month, 1);
	NextMonth = new Date(Year, Month + 1, 1);
	WeekStart = oDate.getDay();
	// Get the number of months in current month
	MonthDays = Math.round((NextMonth.getTime() - oDate.getTime()) / 86400000) + 1;
	// Check whether the Container Id is null


	if(ContainerId != null)
	{
		ContainerId = ContainerId;
		Container = document.getElementById(ContainerId);
		// If an element doesnot exists with the given ContainerId then create it
		if(!Container)
			document.write('<div id="' + ContainerId + '">&nbsp;</div>');
	}
	else
	{
		// Loop until a unique id is obtained for the container
		do
		{
			ContainerId = 'tblCalendar' + Math.round(Math.random() * 1000);
		}
		while(document.getElementById(ContainerId));
		// create an element with the new id
		document.write('<div id="' + ContainerId + '" style="border:0px;">&nbsp;</div>');
	}
	Container = document.getElementById(ContainerId);
	ClassName = (ClassName == null) ? 'tblCalendar' : ClassName;
	HTML = '<table class="' + ClassName + '" cellspacing="0" style="width:150px;border:0px;">';
	// Title bar
	val = 1;
	HTML += '<tr class="TitleBar"><td class="Nav">';
	if(nth==1)
	HTML +='<a style="color:#756BB0;font-weight:bold;" href="javascript:void(0)" onMouseDown="MakeDate(' + Year + ', ' + Month + ', ' + Day+', \''+ContainerId+'\', \''+ClassName+'\', \''+val+'\');">&laquo;</a>';
	
	HTML +='</td><td colspan="5" class="Title">' + MonthNames[Month] + ' ' + Year + '</td><td class="Nav"><a style="color:#756BB0;font-weight:bold;" href="javascript:void(0)" onMouseDown="MakeDate(' + Year + ', ' + (Month + 2) + ', ' + Day+', \''+ContainerId+'\', \''+ClassName+'\', \''+val+'\');">&raquo;</a>';
	
	if(nth==2)
	HTML +='<a style="color:#756BB0;font-weight:bold;" href="javascript:void(0)" onMouseDown="MakeDate(' + Year + ', ' + (Month + 1) + ', ' + Day+', \''+ContainerId+'\', \''+ClassName+'\', \''+val+'\');">&raquo;</a>';
	
	HTML +='</td></tr>';
	// Week Names
	
// 	alert(HTML);
	HTML += '<tr class="WeekName"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>';
	HTML += '<tr class="Days">';
	// Fill the previous month days with space
	for(DayCounter = 0; DayCounter < WeekStart; DayCounter++)
	{
		HTML += '<td>&nbsp;</td>';
	}
	// Populate current month	  
	for(DayCounter = 1; DayCounter < MonthDays; DayCounter++)
	{
		if((DayCounter + WeekStart) % 7 == 1) HTML += '<tr class="Days">';
		if(DayCounter == Day && Month==curMonth)
			HTML += '<td class="SelectedDay"><a href="javascript:ReturnDate(' + DayCounter + ',' + Month + ',' + Year + ')">' + DayCounter + '</a></td>';
		else if((Month<curMonth && Year <= curYear) || Year < curYear || (DayCounter < Day && Month<=curMonth && Year <= curYear) )
		{
			HTML += '<td class="PreviousDay">' + DayCounter + '</td>';
		}
		else HTML += '<td><a href="javascript:ReturnDate(' + DayCounter + ',' + Month + ',' + Year + ')">' + DayCounter + '</a></td>';
		if((DayCounter + WeekStart) % 7 == 0) HTML += '</tr>';
	}
	// Fill the next month days with space
	for(j = (42 - (MonthDays + WeekStart)), DayCounter = 0; DayCounter <= j; DayCounter++)
	{
		HTML += '<td>&nbsp;</td>';
		if((j - DayCounter) % 7 == 0) HTML += '</tr>';
	}
	HTML += '</table>';
	Container.innerHTML = HTML;
	// Returns Id of the element containing the calendar
	return ContainerId;
	
}
function ReturnDate(Day,Month,Year)
{
    if(textboxid.substr(0,8) == "end_date" ){
    	if(document.getElementById("start_date"+textboxid.substring(8)) ){
	    	sdate = document.getElementById("start_date"+textboxid.substring(8)).value ; 
			sdate = sdate.split("/");
			var d = sdate[1];
			var m = sdate[0]- 1;// in javascript Jan starts from 0
			var y = sdate[2];
		    sdate = new Date(y,m,d);
		    edate = new Date(Year,Month,Day);
		 //   alert("Starting Date ::" + sdate);
		 //   alert("Ending Date ::"+ edate);
		    if(edate <= sdate){ 	
		     alert("End Date should be greater than Start Date.");
	    	  return ; 
		    }
       }
    }	
	document.getElementById(textboxid).value =+ (Month + 1) + '/' + Day + '/' + Year;
	closeCal();
}
function closeCal()
{
	document.getElementById('YearCalendar').style.display='none';
}
function MakeDate(Year, Month, Day, ContainerId, ClassName, isNav)
{
	
      if (isNav == 0) document.write('<table id="YearCalendar" cellpadding="1" style="display:none;position:absolute;z-index:999999;" > <tr><td class="close" colspan="2"><a href="javascript:closeCal();" title="close">X</a></td></tr><tr><td>');
	
	id = Calendar_search(Year, Month, Day, 'tblCalendar1', ClassName ,1);//
	if (isNav == 0) document.write('</td>');
  // id = Calendar_search(Year, Month+1, Day, 'tblCalendar2', ClassName,2);//
	if (isNav == 0) document.write('</tr></table>');
}
//-->

posx = 0;

posy = 0;
	
function setcalendar(id)
{	 
    document.getElementById('YearCalendar').style.display='block';
	document.getElementById('YearCalendar').style.left = (getRealLeft(id) + 10)+'px';
	document.getElementById('YearCalendar').style.top = (getRealTop(id) + 20)+'px';
	textboxid = id;
}

function getmousexy(e) {
	
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}


function getRealLeft(el){
xPos = document.getElementById(el).offsetLeft;
tempEl = document.getElementById(el).offsetParent;
while (tempEl != null) {
xPos += tempEl.offsetLeft;
tempEl = tempEl.offsetParent;
}
return xPos;
}

function getRealTop(el){
yPos = document.getElementById(el).offsetTop;
tempEl = document.getElementById(el).offsetParent;
while (tempEl != null) {
yPos += tempEl.offsetTop;
tempEl = tempEl.offsetParent;
}
return yPos;
}

function dispTruePos(isID){
trueX = getRealLeft(isID);
trueY = getRealTop(isID);
alert('True Xpos is: '+trueX+'\nTrue Ypos is: '+trueY)
}


	document.onmousemove=getmousexy;


    Styles = new Array('CalendarRed', 'CalendarRed', 'CalendarRed', 'CalendarRed');
    D = new Date();
	Year = (typeof(iYear) != 'undefined') ? iYear : D.getFullYear();
	Month = (typeof(iMonth) != 'undefined') ? iMonth : D.getMonth();
	Day = (typeof(iDay) != 'undefined') ? iDay : D.getDate();
		
	MakeDate(Year, Month+1, Day, null, 'CalendarRed', 0);
	

