// Calendar Script

	var ie4 = navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) >= 4 ? 1 : 0;
	var ns5 = navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5 ? 1 : 0;

    var thisMonth = (new Date().getFullYear()*12) + new Date().getMonth();
    var thisMonth_b = (new Date().getFullYear()*12) + new Date().getMonth();
    var monthsArray = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var fullMonthsArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var daysArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var stndrdArray = ["","st","nd","rd","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","st","nd","rd","th","th","th","th","th","th","th","st"];
    var whichFields = "start";
    var fieldsToHide = new Array();
    var targetDay;
    var targetMonth;
    var targetYear;
    
    

	function PopulateCalendar( monthOffset, calendarId, defaultday, defaultmonth, defaultyear) {
		thisMonth += monthOffset;
		theDate = new Date( Math.floor( thisMonth / 12), thisMonth % 12);
		if ( defaultday > 0 )
			{
//			theDate.setDate(defaultday);
			theDate.setMonth(defaultmonth);
			theDate.setYear(defaultyear)
			thisMonth = ( defaultyear * 12 ) + defaultmonth;
			}
		
		targetMonth = theDate.getMonth( );
		targetYear = theDate.getFullYear( );

		daysInMonth = GetDaysInMonth( theDate);

		outHtml = '<table class="doublecal">';
		outHtml += '<tr>' + "\n";

		if (calendarId == 'calendar_a')
			{
			outHtml += '<td colspan="3" class="calhead" onclick="toggleCalendar();">' + "\n";
			}
		else
			{
			if (calendarId == 'calendar_a_ins')
				{
				outHtml += '<td colspan="3" class="calhead" onclick="toggleCalendar_ins();">' + "\n";
				}
			else
				{
				if (calendarId == 'calendar_b_ins')
					{
					outHtml += '<td colspan="3" class="calhead" onclick="toggleCalendar_b_ins();">' + "\n";
					}
				else
					{
					outHtml += '<td colspan="3" class="calhead" onclick="toggleCalendar_b();">' + "\n";
					}
				}				
			}

		
		outHtml += 'x Close' + "\n";
		outHtml += '</td>' + "\n";
		outHtml += '</tr>' + "\n";

		
		outHtml += '<tr><td valign="top" align="center">'; 
		outHtml += '<table class="contents" border="0px" cellspacing="0px" cellpadding="0px">';
		
		
		outHtml += '<tr>' + "\n";
		outHtml += '<td class="cell">' + "\n";
		outHtml += '<a href="Javascript: PopulateCalendar( -2, \'' + calendarId + '\');"><img class="arrow" src="assets/calendar/downmonth.gif"></a>' + "\n";
		outHtml += '</td>' + "\n";
		outHtml += '<td colspan="6" class="month">' + "\n"; // Start outputting the header
		outHtml += fullMonthsArray[theDate.getMonth()] + " " + theDate.getFullYear();
		outHtml += '</td>' + "\n";
		outHtml += '</tr>' + "\n";
		outHtml += '<tr class="weekdays">' + "\n";
		outHtml += '<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>' + "\n";
		outHtml += '</tr>' + "\n";

		// Now output the dates
		
		outHtml += '<tr>' + "\n"; //Start the first new row!


//		LastDateAllowed = new Date("2007","11","31","23","59","59");
		LastDateAllowed = new Date
//		LastDateAllowed.setYear(LastDateAllowed.getFullYear() + 1);
		LastDateAllowed.setDate(1);
		LastDateAllowed.setMonth(LastDateAllowed.getMonth() + 12);	
		
		FirstDateAllowed = new Date();
		FirstDateAllowed.setHours(23);
		FirstDateAllowed.setMinutes(59);
		FirstDateAllowed.setSeconds(59);
		for( idx = 0; idx < 42; idx++) {
			currentdate = (idx - new Date(Math.floor(thisMonth/12), thisMonth%12).getDay()) + 1;

			if( (idx % 7) == 0) 
				outHtml += '</tr><tr>' + "\n";
			
			if( currentdate > 0 && currentdate <= daysInMonth) 
                        {
				currDate = new Date(targetYear, targetMonth, currentdate,"23","59","59");
				
				
				if (currDate <= FirstDateAllowed)
				{
					outHtml += '<td class="dayx">' + currentdate + '</td>' + "\n";
				}
				else
				{
					if (currDate > LastDateAllowed)
					{
						outHtml += '<td class="dayx">' + currentdate + '</td>' + "\n";
					}
					else
					{
					
					if (defaultday == currDate.getDate() && defaultmonth == currDate.getMonth() && defaultyear == currDate.getFullYear() )
						{
						outHtml += '<td class="defday" onclick="SelectDate( '+currentdate+", "+targetMonth +", "+targetYear+", '"+calendarId+"'"+');">' + currentdate + '</td>' + "\n";
						}
					else
						{
						outHtml += '<td class="day" onclick="SelectDate( '+currentdate+", "+targetMonth +", "+targetYear+", '"+calendarId+"'"+');">' + currentdate + '</td>' + "\n";
						}
												
					}
				}
			} 
                        else
                        {
				outHtml += '<td>&nbsp;</td>' + "\n";
			}
		}
		outHtml += '</tr>' + "\n";
		
		outHtml += '</table>' + "\n";
		outHtml += '</td>'
//	divider
				
		outHtml += '<td class="monthdivider">';
//		outHtml += '<img src="assets/calendar/divider.gif" border="0">';
		outHtml += '</td>' + "\n";
//	Start of second month
		outHtml += '<td valign="top" align="center">';
		

		thisMonth += 1;
		theDate = new Date( Math.floor( thisMonth / 12), thisMonth % 12);
				
		targetMonth = theDate.getMonth( );
		targetYear = theDate.getFullYear( );

		daysInMonth = GetDaysInMonth( theDate);



		outHtml += '<table class="contents" border="0px" cellspacing="0px" cellpadding="0px">';
		outHtml += '<tr>' + "\n";
		outHtml += '<td colspan="6" class="month">' + "\n"; // Start outputting the header
		outHtml += fullMonthsArray[theDate.getMonth()] + " " + theDate.getFullYear();
		outHtml += '</td>' + "\n";
		

		outHtml += '<td class="cell">' + "\n";
		outHtml += '<a href="Javascript: PopulateCalendar( 0, \'' + calendarId + '\');"><img class="arrow" src="assets/calendar/upmonth.gif" border="0"></a>' + "\n";
		outHtml += '</td>' + "\n";
		outHtml += '</tr>' + "\n";
		outHtml += '<tr class="weekdays">' + "\n";
		outHtml += '<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>' + "\n";
		outHtml += '</tr>' + "\n";

		// Now output the dates
		
		outHtml += '<tr>' + "\n"; //Start the first new row!


//		LastDateAllowed = new Date("2007","11","31","23","59","59");
		LastDateAllowed = new Date
//		LastDateAllowed.setYear(LastDateAllowed.getFullYear() + 1);
		LastDateAllowed.setDate(1);
		LastDateAllowed.setMonth(LastDateAllowed.getMonth() + 12);	

		FirstDateAllowed = new Date();
		FirstDateAllowed.setHours(23);
		FirstDateAllowed.setMinutes(59);
		FirstDateAllowed.setSeconds(59);
		for( idx = 0; idx < 42; idx++) {
			currentdate = (idx - new Date(Math.floor(thisMonth/12), thisMonth%12).getDay()) + 1;

			if( (idx % 7) == 0) 
				outHtml += '</tr><tr>' + "\n";
			
			if( currentdate > 0 && currentdate <= daysInMonth) 
                        {
				currDate = new Date(targetYear, targetMonth, currentdate,"23","59","59");
				
				
				if (currDate <= FirstDateAllowed)
				{
					outHtml += '<td class="dayx">' + currentdate + '</td>' + "\n";
				}
				else
				{
					if (currDate > LastDateAllowed)
					{
						outHtml += '<td class="dayx">' + currentdate + '</td>' + "\n";
					}
					else
					{

					if (defaultday == currDate.getDate() && defaultmonth == currDate.getMonth() && defaultyear == currDate.getFullYear() )
						{
						outHtml += '<td class="defday" onclick="SelectDate( '+currentdate+", "+targetMonth +", "+targetYear+", '"+calendarId+"'"+');">' + currentdate + '</td>' + "\n";
						}
					else
						{
						outHtml += '<td class="day" onclick="SelectDate( '+currentdate+", "+targetMonth +", "+targetYear+", '"+calendarId+"'"+');">' + currentdate + '</td>' + "\n";
						}
												
					}
				}
			} 
                        else
                        {
				outHtml += '<td>&nbsp;</td>' + "\n";
			}
		}
		outHtml += '</tr>' + "\n";
		
		outHtml += '</table>' + "\n";
		outHtml += '</td></tr></table>';
		if( document.getElementById) 
			document.getElementById( calendarId).innerHTML = outHtml;
		else
			document.all[ calendarId].innerHTML = outHtml;
	}
	
	
	
	

	
	
	function SelectDate( theDay, theMonth, theYear, calId) {
	
	if (calId == "calendar_a")
		{
                document.login.dd.options[(theDay-1)].selected = true;
		changethemonthyeardropdown((monthsArray[ theMonth].toUpperCase()) + ("" + theYear).substring(0,4),document.login.dmy);
		
		if (document.login.rmy)
			{
			changethemonthyeardropdown((monthsArray[ theMonth].toUpperCase()) + ("" + theYear).substring(0,4),document.login.rmy);
                	}
		toggleCalendar( ); // Hide the calendar!
		}
	else
		{
		if (calId == "calendar_a_ins")
			{
                	document.login.dd.options[(theDay-1)].selected = true;
			changethemonthyeardropdown((monthsArray[ theMonth].toUpperCase()) + ("" + theYear).substring(0,4),document.login.dmy);
			changethemonthyeardropdown((monthsArray[ theMonth].toUpperCase()) + ("" + theYear).substring(0,4),document.login.rmy);
                	toggleCalendar_ins( ); // Hide the calendar!
			}
		else
			{
			if (calId == "calendar_b_ins")
				{
				document.login.rd.options[(theDay-1)].selected = true;
				changethemonthyeardropdown((monthsArray[ theMonth].toUpperCase()) + ("" + theYear).substring(0,4),document.login.rmy);
				toggleCalendar_b_ins( ); // Hide the calendar!	
				}
			else
				{		
				document.login.rd.options[(theDay-1)].selected = true;
				changethemonthyeardropdown((monthsArray[ theMonth].toUpperCase()) + ("" + theYear).substring(0,4),document.login.rmy);
				toggleCalendar_b( ); // Hide the calendar!	
				}

			}
	
		}
	
}

	function GetDaysInMonth(date)
    {
       temp = new Date(date.valueOf());

       return new Date(temp.setMonth(temp.getMonth()+1).valueOf() - (24*60*60*1000)).getDate();
    }





    
    function toggleCalendar( ) {
	    if( document.getElementById) 
	    	document.getElementById( "calendar_a").style.display = document.getElementById( "calendar_a").style.display == "none" ? "" : "none";
	    else
	    	document.all[ "calendar_a"].style.display = document.all[ "calendar_a"].style.display == "none" ? "" : "none";
	    	
	    return true;
    }


    function toggleCalendar_b( ) {
	    if( document.getElementById) 
	    	document.getElementById( "calendar_b").style.display = document.getElementById( "calendar_b").style.display == "none" ? "" : "none";
	    else
	    	document.all[ "calendar_b"].style.display = document.all[ "calendar_b"].style.display == "none" ? "" : "none";
	    	
	    return true;
    }
    function toggleCalendar_ins( ) {
	    if( document.getElementById) 
	    	document.getElementById( "calendar_a_ins").style.display = document.getElementById( "calendar_a_ins").style.display == "none" ? "" : "none";
	    else
	    	document.all[ "calendar_a_ins"].style.display = document.all[ "calendar_a_ins"].style.display == "none" ? "" : "none";
	    	
	    return true;
    }


    function toggleCalendar_b_ins( ) {
	    if( document.getElementById) 
	    	document.getElementById( "calendar_b_ins").style.display = document.getElementById( "calendar_b_ins").style.display == "none" ? "" : "none";
	    else
	    	document.all[ "calendar_b_ins"].style.display = document.all[ "calendar_b_ins"].style.display == "none" ? "" : "none";
	    	
	    return true;
    }


    function PadLeft(str)
    {
       while(str.length < 2)
          str = "0" + str;

       return str;
    }






function changethemonthyeardropdown(mycode,listname){

i = 0
      var itemFound = false
      while ((i < listname.options.length) && (!itemFound)) {
         if (listname.options[i].value == mycode) {
            listname.options[i].selected = true
            itemFound = true
         }
         i++
      }
   }


function togcal()
{
var months = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
var monthpos;
monthpos = months.indexOf(document.forms[0].dmy.value.substring(0,3));
monthpos = (( monthpos + 3 ) / 3 ) - 1;
PopulateCalendar( 0, "calendar_a",document.forms[0].dd.value, monthpos, document.forms[0].dmy.value.substring(3,7));
toggleCalendar( );
}

function togcal_b()
{
var months = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
var monthpos;
monthpos = months.indexOf(document.forms[0].rmy.value.substring(0,3));
monthpos = (( monthpos + 3 ) / 3 ) - 1;
PopulateCalendar( 0, "calendar_b",document.forms[0].rd.value, monthpos, parseInt(document.forms[0].rmy.value.substring(3,7)));
toggleCalendar_b( );
}


function togcal_ins()
{
var months = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
var monthpos;
monthpos = months.indexOf(document.forms[0].dmy.value.substring(0,3));
monthpos = (( monthpos + 3 ) / 3 ) - 1;
PopulateCalendar( 0, "calendar_a_ins",document.forms[0].dd.value, monthpos, document.forms[0].dmy.value.substring(3,7));
toggleCalendar_ins( );
}

function togcal_b_ins()
{
var months = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
var monthpos;
monthpos = months.indexOf(document.forms[0].rmy.value.substring(0,3));
monthpos = (( monthpos + 3 ) / 3 ) - 1;
PopulateCalendar( 0, "calendar_b_ins",document.forms[0].rd.value, monthpos, parseInt(document.forms[0].rmy.value.substring(3,7)));
toggleCalendar_b_ins( );
}
