	var mywin,Month,Year,Day,weekDay,monthDays,activeMonth,currentDay;
	var objText,calMode,Date1;
	var MonthName = new Array(13);
	MonthName[1] = "January";
	MonthName[2] = "February";
	MonthName[3] = "March";
	MonthName[4] = "April";
	MonthName[5] = "May";
	MonthName[6] = "June";
	MonthName[7] = "July";
	MonthName[8] = "August";
	MonthName[9] = "September";
	MonthName[10] = "October";
	MonthName[11] = "November";
	MonthName[12] = "December";
	
	function changeCal(id)
	{
		switch(id)
		{
			case 1 :
					Year = Year - 1;
					break;
				
			case 2 :
					Month = Month - 1;
					if (Month < 1)
					{
						Month = 12;
						Year = Year - 1;
					}
					break;
			case 3 :
					Month = Month + 1;
					if (Month > 12)
					{
						Month = 1;
						Year = Year + 1;
					}
					break;
			case 4 :
					Year = Year + 1;
		}
		
			
			// creating new date to find no. of days in the month
			Date1 = new Date(Year,Month-1,1);
			var tMonth = Date1.getMonth();
			var tYear = Date1.getFullYear();
			if(tMonth == 11)
			{
				tMonth = 0;
				tYear = tYear + 1;
			}
			else
				tMonth++;
			var Date2= new Date(tYear,tMonth,Date1.getDate())
			monthDays = (Date2 - Date1) / 86400000;
			
			//finding whether the date is in current month
			Date2 = new Date()

			// assigning value to the global variables
			weekDay = Date1.getDay();

			if(Date2.getFullYear() == Date1.getFullYear() && Date2.getMonth() == Date1.getMonth())
				activeMonth = true;
			else
				activeMonth = false;
		
		
		winopen();
	}

	function winopen()
	{
				var i,j,CalendarBody = "",CalendarHeight=85 + 100;
				var colWidth = " width=\"14%\"";
				var toDay = new Date().getDate();
				CalendarBody = CalendarBody + ("<html><head><title>Calendar</title></head><body  marginheight=\"5\" marginwidth=\"5\" topmargin=\"5\" leftmargin=\"5\" rightmargin=\"5\" bgcolor=\"#FFFFFF\" text=\"#000000\" vLink=\"#000000\" Link=\"#000000\" ALink=\"#000000\">");
				CalendarBody = CalendarBody + ("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#8BA6D4\" bordercolor=\"#8BA6D4\" style=\"BORDER-COLLAPSE: collapse\">");
				CalendarBody = CalendarBody + ("<tr height=\"25\"> ");
				CalendarBody = CalendarBody + ("<td width=\"2%\">&nbsp;<a href=\"javascript:window.opener.changeCal(1)\"><img alt=\"previous year\" border=0 src=\"Images/LEFT_Extreme.gif\" width=\"17\" height=\"17\"></a>&nbsp;</td>");
				CalendarBody = CalendarBody + ("<td width=\"2%\"><a href=\"javascript:window.opener.changeCal(2)\"><img alt=\"previous month\" border=0 src=\"Images/LEFT.gif\" width=\"17\" height=\"17\"></a>&nbsp;</td>");
				CalendarBody = CalendarBody + ("<td width=\"92%\" align=\"center\"><b><font face=\"Arial, Helvetica, sans-serif\" color=\"#FFFFFF\">" + MonthName[parseInt(Month,10)] + " - " + Year + "</font></b></td>");
				CalendarBody = CalendarBody + ("<td width=\"2%\"><a href=\"javascript:window.opener.changeCal(3)\"><img alt=\"next month\" border=0 src=\"Images/Right.gif\" width=\"17\" height=\"17\"></a>&nbsp;</td>");
				CalendarBody = CalendarBody + ("<td width=\"2%\"><a href=\"javascript:window.opener.changeCal(4)\"><img alt=\"next year\" border=0 src=\"Images/Right_Extreme.gif\" width=\"17\" height=\"17\"></a>&nbsp;</td>");
				CalendarBody = CalendarBody + ("</tr><tr><td colspan=\"5\">");
				CalendarBody = CalendarBody + ("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#FFFFFF\" bordercolor=\"#8BA6D4\" style=\"BORDER-COLLAPSE: collapse\">");
				CalendarBody = CalendarBody + ("<tr height=\"20\" bgcolor=\"#D1DDE9\" style=\"font-family:Verdana, Arial, Helvetica, sans-serif;font-weight:bold;font-size:12;color:#000000;\" align=\"center\" valign=\"middle\">");
				CalendarBody = CalendarBody + ("<td" + colWidth + ">Sun</td><td" + colWidth + ">Mon</td><td" + colWidth + ">Tue</td><td" + colWidth + ">Wed</td><td" + colWidth + ">Thu</td><td" + colWidth + ">Fri</td><td" + colWidth + ">Sat</td></tr>");
				
				// code for the first row of the calendar
				CalendarBody = CalendarBody + ("<tr height=\"20\" style=\"font-family:Verdana, Arial, Helvetica, sans-serif;font-weight:normal;font-size:12;\" align=\"center\" valign=\"middle\">");

				currentDay = 1;
				for(i=0;i <= 6;i++)
				{
					if(i < weekDay) 
						CalendarBody = CalendarBody + ("<td>&nbsp;</td>");
					else if(activeMonth && currentDay == toDay)
						CalendarBody = CalendarBody + ("<td bgcolor=\"#E67300\">" + setLink(currentDay++) + "</td>");
					else if(i%7 == 0 || i%7 == 6) // code for weekend
						CalendarBody = CalendarBody + ("<td bgcolor=\"#D1DDE9\" >" + setLink(currentDay++) + "</td>");
					else if(activeMonth)	//code for current Month
						CalendarBody = CalendarBody + ("<td>" + setLink(currentDay++) + "</td>");
					else
						CalendarBody = CalendarBody + ("<td bgcolor=\"#C0C0C0\">" + setLink(currentDay++) + "</td>");
				}
//				CalendarBody = CalendarBody + ("</tr>");

				// code for the remaining days
				for(;currentDay <= monthDays;i++)
				{
					if(i%7 == 0)
					{
						CalendarHeight += 20;
						CalendarBody = CalendarBody + ("</tr><tr height=\"20\" style=\"font-family:Verdana, Arial, Helvetica, sans-serif;font-weight:normal;font-size:12;\" align=\"center\" valign=\"middle\">");
//						if (!activeMonth)				
//							CalendarBody = CalendarBody + (" \bgcolor=\"#C0C0C0\" ");				
//						CalendarBody = CalendarBody + (">");
					}
					if(activeMonth && currentDay == toDay)
						CalendarBody = CalendarBody + ("<td bgcolor=\"#FFA448\">" + setLink(currentDay++) + "</td>");
					else if(i%7 == 0 || i%7 == 6) // code for weekend
						CalendarBody = CalendarBody + ("<td bgcolor=\"#D1DDE9\">" + setLink(currentDay++) + "</td>");
					else if(activeMonth)	//code for current Month
						CalendarBody = CalendarBody + ("<td>" + setLink(currentDay++) + "</td>");
					else
						CalendarBody = CalendarBody + ("<td bgcolor=\"#C0C0C0\">" + setLink(currentDay++) + "</td>");
				}
				
				//filling up the last empty columns
				for(;i++ % 7 != 0;)
						CalendarBody = CalendarBody + ("<td>&nbsp;</td>");
				
				CalendarBody = CalendarBody + ("</tr></table></td></tr></table>");
				CalendarBody = CalendarBody + ("</body>");

				if (mywin)
					if (!mywin.closed) mywin.document.close();
				mywin = window.open("","myWindow111","height=" + CalendarHeight + ",width=250,resizable=yes,scrollbars=no,toolbar=no");
				if ((document.window) && (!mywin.opener))
						mywin.opener = document.window;
				mywin.document.write(CalendarBody);
				mywin.resizeTo(300,CalendarHeight);
				mywin.focus();
				setValue(Day,0);
	}
	
	function setLink(value)
	{
		return "<a href=\"javascript:window.opener.setValue(" + value + ",1)\" style=\"text-decoration:none\">" + value + "</a>"
	}
	
	function setValue(value,caller)
	{
		Day = parseInt(value,10);
		Month = parseInt(Month,10);
		if (Month <= 9)
			i = "0" + Month;
		else
			i = Month;
		if (Day <= 9)
			j = "0" + Day;
		else
			j = Day;
		if(calMode == 0)
			objText.value = i + "/" + j + "/" + Year;
		else
			objText.value = j + "/" + i + "/" + Year;
		if(caller == 1)
			mywin.close();
	}
	
	function windefault(obj,Mode)
	{
		// obj is the object to read and / or write the date value generated by this function
		// Mode is the value to set the date type
		// if Mode = 0, the date type is mm/dd/yyyy
		// if Mode is any value that 0, the date type is dd/mm/yyyy
		 
		objText = obj
		calMode = Mode
		var curDate,cal = objText.value;
		curDate = !isValidDate(objText);
		
		if (curDate)
		{
			// creating current date
			var myDate = new Date;

			// assigning value to the global variables
			Day = myDate.getDate();
			Month = myDate.getMonth();
			Year = myDate.getFullYear();

			// creating new date to find no. of days in the month
			myDate = new Date(Year,Month,1)
			var tMonth = myDate.getMonth();
			var tYear = myDate.getYear();
			if(tMonth == 11)
			{
				tMonth = 0;
				tYear = tYear + 1;
			}
			else
				tMonth++;
			var myDate1= new Date(tYear,tMonth,myDate.getDate())
			monthDays = (myDate1 - myDate) / 86400000;

			//finding the weekday of the first day of the month
			weekDay = myDate.getDay();
			
			// incrementing month value as it should between 1 to 12 for our code
			Month++;
			
			//setting, it is the active month
			activeMonth = true;
		}
		else
		{
			//finding whether the date in in current month
			Date2 = new Date()

			if(Date2.getFullYear() == Date1.getFullYear() && Date2.getMonth() == Date1.getMonth())
				activeMonth = true;
			else
				activeMonth = false;

			// assigning value to the global variables
			weekDay = Date1.getDay();

		}
		Year = parseInt(Year,10);
		winopen();
	}
	
	function isValidDate(obj)
	{
		var curDate,cal = obj.value;
		curDate = true;
		if (cal.length == 10)
		{
			if(calMode == 0)
			{
				Day = cal.substr(3,2)
				Month = cal.substr(0,2);
			}
			else
			{
				Day = cal.substr(0,2)
				Month = cal.substr(3,2);
			}
			Year = cal.substr(6,4);
			if (!isNaN(Day) && !isNaN(Month) && !isNaN(Year))
			{
					if((Month >= 1 && Month <= 12) && Year.length == 4)
					{
						Date1 = new Date(Year,Month-1,1);
						var tMonth = Date1.getMonth();
						var tYear = Date1.getFullYear();
						if(tMonth == 11)
						{
							tMonth = 0;
							tYear = tYear + 1;
						}
						else
							tMonth++;
						var Date2= new Date(tYear,tMonth,Date1.getDate())
						monthDays = (Date2 - Date1) / 86400000;
						if (Day >= 1 && Day <= monthDays)
							curDate = false;
					}
			}
					
		}
		return !curDate;
	}


