
// JS Calendar Control
// 08/24/2005 MyWebGrocer (JHJ)


	var arrDayNames = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
	var arrMonthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
	var arrMonthLen = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
	
	var curDate;
	var Month;
	var Day;
	var Year;
	var ctd=0;
	function ShowHideCalendar( which )
				{
					
					var d1 = document.getElementById( "Calendar1" );
					var a1 = document.getElementById( "CalendarLink1" );
					
					var divCC = "";
					var inpID = "";
					
					
					if( which == 1 )
					{
					
						divCC = "CC1";
						inpID = "txtPUD";
						
						if( d1.style.display == "none" )
						{
						
							d1.style.display = "block";
							a1.innerHTML = "Hide Calendar";
							
							DatesFromInput( divCC, inpID );
							
						}
						else
						{
							d1.style.display = "none";
							a1.innerHTML = "Show Calendar";
						}
						
					}
					
					
				}
				
				
				// Initialize the calendar control with dates if they already exist in the input boxes
				function DatesFromInput( divCC, inpID )
				{
				
					var val = document.getElementById( inpID );

					
					curDate = new Date();
					var m = curDate.getMonth();
					var d = curDate.getDate();
					var y = curDate.getFullYear();
					
					if( !isBlank( val.value ) && !isEmpty( val.value ) )
					{
						
						var arrVal = val.value.split( '/' );
						
						if( arrVal[0] != "" && (parseInt( arrVal[0] )) != 0 )
							m = (parseInt( arrVal[0] ) - 1);
						if( arrVal[1] != "" && (parseInt( arrVal[1] )) != 0 )
							d = parseInt( arrVal[1] );
						if( arrVal[2] != "" && (parseInt( arrVal[2] )) != 0 )
							y = parseInt( arrVal[2] );
							
					}
					
					InitCalendar( divCC, inpID, m, d, y );
				
				}
				
				
				function ShowHideExample( divID )
				{
					
					var d = document.getElementById( divID );
					
					if( d.style.display == "none" )
					{
						d.style.display = "block";
					}
					else
					{
						d.style.display = "none";
					}
				
				}
	
	function Rand()
	{
		var now = new Date()
		var num = ( now.getMilliseconds() ) % 100
		num = num + 1
		return num;
	}
	
	
	// Initialize the calendar
	function InitCalendar( divID, inpID, m, d, y )
	{
		Month = m;
		Day = d;
		Year = y;
		
		OutputCalendar( divID, inpID );
	}
	
	
	function IsLeapYear()
	{
		if( Year / 4 == parseInt( Year / 4 ) )
			arrMonthLen[1] = 29;
	}
	
	
	function GetFirstOfMonth()	// return what weekday first day of month falls on mon=1
	{
		var firstOfMonth = new Date( (Month+1) + "/1/" + Year );
		firstOfMonth = firstOfMonth.getDay();
		
		return firstOfMonth;
	}
	
	
	function AddToYear( val, divID, inpID )
	{
		Year = Year + val;
		OutputCalendar( divID, inpID );
	}
	
	
	function AddToMonth( val, divID, inpID )
	{
		Month = Month + val;
		if ( Month > 11 )	// Increment: December to January increment year, reset Month to 0
		{
			Month = Month - 12;
			Year++;
		}
		else if( Month < 0 )	// Decrement: January to December decrement year, reset Month to 11
		{
			Month = Month + 12;
			Year--;
		}
		
		OutputCalendar( divID, inpID );
	}
	
	function getDateObject(dateString,dateSeperator)
	{
		//This function return a date object after accepting 
		//a date string ans dateseparator as arguments
		var curValue=dateString;
		var sepChar=dateSeperator;
		var curPos=0;
		var cDate,cMonth,cYear;

		//extract day portion
		curPos=dateString.indexOf(sepChar);
		cDate=dateString.substring(0,curPos);
		
		//extract month portion				
		endPos=dateString.indexOf(sepChar,curPos+1);			cMonth=dateString.substring(curPos+1,endPos);

		//extract year portion				
		curPos=endPos;
		endPos=curPos+5;			
		cYear=curValue.substring(curPos+1,endPos);
		
		//Create Date Object
		dtObject=new Date(cYear,cMonth,cDate);	
		return dtObject;
	}
	
	function OutputCalendar( divID, inpID )
	{
		var today       = new Date();
		var strYear     = today.getFullYear();
		var iMonth      = today.getMonth() + 1; // +1, we do NOT want zero-based month index					
		var iDay        = today.getDate()+3;
		var strDateOut  = "";
		iMonth = (iMonth < 10)? "0" + iMonth : iMonth;
		iDay = (iDay < 10)? "0" + iDay : iDay;
		strDateOut =strYear+""+iMonth+""+iDay;
		IsLeapYear();
		
		var idPrefix = Rand();

		// output month and year
		var strCC = "<table cellspacing='0'><tr><td id='MonthNameCell' colspan='7'>" + arrMonthNames[Month] + " " + Year + "</td></tr>";
		
		strCC += "<tr>";
		
		var i,j;
		
		// output day names
		for( i = 0; i <= 6; i++ )
		{
			if( i == 0 )
				strCC += "<td id='DayNameLeftCell'>" + arrDayNames[i] + "</td>";
			else
				strCC += "<td class='DayNameCells'>" + arrDayNames[i] + "</td>";
		}
		
		strCC += "</tr><tr>"
		
		
		var dayCount = 1;
		var MonthLen = arrMonthLen[Month];	// what is the length of this month
		var firstOfMonth = GetFirstOfMonth();	// get the first day of this month
		var past;
		var rows = 5;
		
		if( (firstOfMonth + MonthLen) > 34 )
			rows = 6;
		
		
		
		for ( i = 0; i < rows; i++ )
		{
		
			strCC += "<tr>";
			
			for ( j = 0; j < 7; j++ )
			{
			
				// if before or after calendar days appear output blank cells
				if( ( i == 0 && j <= (firstOfMonth-1) ) || ( i == ( rows - 1 ) && dayCount > MonthLen ) )
				{
				
					if( j == 0 )
						strCC += "<td class='DayLeftCell' style='cursor: default;'>&nbsp;</td>";
					else
						strCC += "<td class='DayCells' style='cursor: default;'>&nbsp;</td>";
							
				}
				else
				{
			
						var newM = Month+1;
						newM = (newM < 10)? "0" + newM : newM;
						var newDC = (dayCount < 10)? "0" + dayCount : dayCount;
						var strDatePast = Year+""+newM+""+newDC;
					
			
					// is this day today or the selected day (use different style, don't output link)
					if (strDateOut > strDatePast)
					{
						var name = ( "c" + dayCount.toString() + idPrefix.toString() );

						if( j == 0 )
							strCC += "<td class='DayLeftCell' id='" + name + "' style='color:red;' >" + dayCount + "</td>";
						else
							strCC += "<td class='DayCells' id='" + name + "' style='color:red;' >" + dayCount + "</td>";
							
						dayCount++;
					
					past=true;
					}
					
					
					
					else if( dayCount == Day)
					{
					
						if( j == 0 )
							strCC += "<td id='TodayLeftCell' ondblclick=\"CloseCalendar( '" + divID + "' );\">" + dayCount + "</td>"
						else
							strCC += "<td class='TodayCells' ondblclick=\"CloseCalendar( '" + divID + "' );\">" + dayCount + "</td>"
						
						PickDate( dayCount, false, divID, inpID );	// Pick this day as current - false: don't call OutputCalendar
						
						dayCount++;
						
					}
					else	// output day cells normally with unselected style and PickDate link
					{
					
						var name = ( "c" + dayCount.toString() + idPrefix.toString() );

						if( j == 0 )
							strCC += "<td class='DayLeftCell' id='" + name + "' onmouseover=\"ChangeStyle( '" + name + "', 1 );\" onmouseout=\"ChangeStyle( '" + name + "', 0 );\" onclick=\"PickDate( " + dayCount + ", true, '" + divID + "', '" + inpID + "' );\" ondblclick=\"CloseCalendar( '" + divID + "' );\">" + dayCount + "</td>";
						else
							strCC += "<td class='DayCells' id='" + name + "' onmouseover=\"ChangeStyle( '" + name + "', 1 );\" onmouseout=\"ChangeStyle( '" + name + "', 0 );\" onclick=\"PickDate( " + dayCount + ", true, '" + divID + "', '" + inpID + "' );\" ondblclick=\"CloseCalendar( '" + divID + "' );\">" + dayCount + "</td>";
							
						dayCount++;
					
					}
					past=false;
				}
			
			}
			
			strCC += "</tr>";
			
		}
		
		strCC += "</table>";
		
		
		arrMonthLen[1] = 28;	// reset in the event that it is a leap year
		
		
		// write calendar
		var aDiv = document.getElementById( divID );
		aDiv.innerHTML = "<div>" + strCC + "</div>";

	}
	
	

	function CloseCalendar( id )
	{

		if( id == "CC1" )
			id = "Calendar1";
		else if( id == "CC2" )
			id = "Calendar2";
		else
			id = "";
		document.getElementById( id ).style.display = "none";
			
	}
	
	
	
	function ChangeStyle( id, val )
	{
		if( val == 1 )	// mouse over
			document.getElementById( id ).style.backgroundColor = "#F1F1F1";
		else
			document.getElementById( id ).style.backgroundColor = "#FFFFFF";
	}
	
	
	// pick a date on the calendar - select it and add to specified input textbox
	function PickDate( day, outputCal, divID, inpID )
	{
		var monDis, dayDis
		var inp = document.getElementById( inpID );
		
		monDis = Month + 1;
		dayDis = day;
		
		if( monDis < 10 )
			monDis = "0" + monDis;
		if( dayDis < 10 )
			dayDis = "0" + dayDis;

		inp.value = monDis + "/" + dayDis + "/" + Year; // set input box to date picked

		Day = day;

		if( outputCal )	// we don't want to output if we are picking the date on the refresh of the calendar
			OutputCalendar( divID, inpID );
	}