var AryMonthsL = new Array('J','F','M','A','M','J','J','A','S','O','N','D');
var AryMonths = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var AryMonthsLengths = new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var AryDayNames =   new Array('S',  'M',  'T',  'W',  'T',  'F',  'S');

var bInOut = false;//set this to true if being displayed on the in out board
function FromInOut()
{
	bInOut = true;
}
function SetInOutValues(strDate)
{
	if  (bInOut)
	{
		var optionName = new Option(strDate, strDate, true, true);
	
		var length = window.document.frmInOut.sTime.length;
	
		window.document.frmInOut.sTime.options[length] = optionName;
	}
	bInOut = false;
}
var objTxtField = '';

//...................................................................................................


//...................................................................................................
function GetMonthNumber(m)
{
	for (var loop = 0; loop < AryMonths.length; loop++){
	  	if(m==AryMonths[loop]){
			return loop;
	  	}
	}		
}

//...................................................................................................
function GetMonthName(m)
{
	for (var loop = 0; loop < AryMonths.length; loop++){
		if(m==loop){
			return AryMonths[loop];
		}
	}
}

//...................................................................................................
function GetMonthsLength(m)
{        
	for (var loop = 0; loop < AryMonthsLengths.length; loop++){
		if(m==loop){
			return AryMonthsLengths[loop];
		}
    }
}

//...................................................................................................
function MonthsDayLength(dADate){

	// This function returns the length (in days) of the specified month:
        var AryMonthsLengths = new Array('31','28','31','30','31','30','31','31','30','31','30','31');
	var dLeapYr = new Date(dADate)

	if(dLeapYr.getMonth(dLeapYr)==1){

		// The month is Febuary - check for leapyear:
                var bLeapYear4 = new String('');
                var bLeapYear400 = new String('true');
                var sYear = new String(dLeapYr.getFullYear(dLeapYr));
                var iYear = new Number(dLeapYr.getFullYear(dLeapYr));

                // The calendar year is 365 days long - 
                // unless the year is exactly divisible by 4:
                var sDiv4 = new String((iYear / 4));
                if(sDiv4.indexOf(".") == -1){
                	bLeapYear4 = 'true';
                }else{
        		bLeapYear4 = 'false';
        	}

        	// If the year is the last year of a century (eg. 1900, 2000) - 
        	// AND is exactly divisible by 400, then it's still leap year, 
        	// otherwise it's not (even if it is exactly divisible by 4).
        	// Therefore 1900 wasn't a leap year but 2000 will be:
                var sDiv400 = new String((iYear / 400));
        	if((sYear.lastIndexOf("00")==2)){
        		bLeapYear400 = 'false';
                	if((sDiv400.indexOf(".") == -1)){
        			bLeapYear400 = 'true';
                	}
        	}

            if((bLeapYear4=='true') && (bLeapYear400=='true')){
                // It's a leap year.
                // Change the number of days in Feb (in the array 'AryMonthsLengths') to 29:
                AryMonthsLengths[1]=29;
        		return AryMonthsLengths[1]
            }else{
				AryMonthsLengths[1]=28; 
        		return AryMonthsLengths[1]
            }
	}else{
		return AryMonthsLengths[dLeapYr.getMonth(dLeapYr)];
	}
}

//...................................................................................................
// The date is intially set:
// The master date (as string):
var sDate = new String('28-Dec-2001');
var splitString = sDate.split("-");

// The master date (as date):
var d = new Date(splitString[2],GetMonthNumber(splitString[1]),splitString[0]);

// The first day of the month as set by 'd':
var dTemp = new Date(splitString[2],GetMonthNumber(splitString[1]),1);        
var i = -dTemp.getDay(d);	


//........................................................................................
function CalMonthView(){
	// Start constructing the HTML for the calendar (but does not set the dates - ResetCalendar() does that.):
        var s = "<table class='plain_box_cal' cellspacing='0' cellpadding='3'>";
        s += "<tr class='sub'>\n";
        for (var loop = 0; loop < AryDayNames.length; loop++)
        {
      		s += "<td align=\'center\'><b>" + AryDayNames[loop]; + "<\/b><\/td>"
        }
        s += "</tr>\n";

	// Loop through all the cells in the calendar and assign them date values, etc, as required:
	var c = 0;
	var sOutput = '<tr>';
	i = -dTemp.getDay(d);	
	// i is used to define in which cell the days of the current month start,
	// while 'loop' will represent the number of boxes required to build 
	// a table to hold all the days in a given month.

	for (var loop = 10; loop < 52; loop++){

		c = c + 1
		i = i + 1

		sOutput = sOutput + "<td align='center'>"
	  	if(d.getDate()==i){
			// This is the current value of the date:
			sOutput = sOutput + "<INPUT type='button' name='btn" + loop + "' value='" + i + "' id='cal_icon_val' class='cal_icon_val' onClick=\"CalSetDay(document.frmCal.btn" + loop + ".value); MM_showHideLayers('lyrJSCalendar','','hide'); SetInOutValues(window.document.frmCalDate.txtCalDate.value); objTxtField.value=window.document.frmCalDate.txtCalDate.value; objTxtField.fireEvent('onChange');\" onmouseover=\"this.className=this.id + '_mo'\" onmouseout=\"this.className=this.id\">";
	  	}else{
			// If i is Greater than 0...
			if(i > 0){
				// ...or less or equal to the number of days in this month:
				if(i <= MonthsDayLength(d)){
					sOutput = sOutput + "<INPUT type='button' name='btn" + loop + "' value='" + i + "' id='cal_icon' class='cal_icon' onClick=\"CalSetDay(document.frmCal.btn" + loop + ".value); MM_showHideLayers('lyrJSCalendar','','hide'); SetInOutValues(window.document.frmCalDate.txtCalDate.value); objTxtField.value=window.document.frmCalDate.txtCalDate.value; objTxtField.fireEvent('onChange');\" onmouseover=\"this.className=this.id + '_mo'\" onmouseout=\"this.className=this.id\">";
				}else{
					sOutput = sOutput + "<INPUT type='button' name='btn" + loop + "' value='' id='cal_icon_void' class='cal_icon_void' onClick=\"CalSetDay(document.frmCal.btn" + loop + ".value); MM_showHideLayers('lyrJSCalendar','','hide'); SetInOutValues(window.document.frmCalDate.txtCalDate.value); objTxtField.value=window.document.frmCalDate.txtCalDate.value; objTxtField.fireEvent('onChange');\" onmouseover=\"this.className=this.id + '_mo'\" onmouseout=\"this.className=this.id\">";
				}
			}else{
				sOutput = sOutput + "<INPUT type='button' name='btn" + loop + "' value='' id='cal_icon_void' class='cal_icon_void' onClick=\"CalSetDay(document.frmCal.btn" + loop + ".value); MM_showHideLayers('lyrJSCalendar','','hide'); SetInOutValues(window.document.frmCalDate.txtCalDate.value); objTxtField.value=window.document.frmCalDate.txtCalDate.value;\" onmouseover=\"this.className=this.id + '_mo'\" onmouseout=\"this.className=this.id\">";
			}
		}
		sOutput = sOutput + "</td>"

		// Starts a new HTML Table row every 7 cells:
	  	if(c==7){
			sOutput = sOutput + "</tr><tr>";
			c = 0;
	  	}
	}
	return s + sOutput + '</tr></table>';
}

//...................................................................................................
function CalSetDay(i){

	if(i!=''){
		d.setDate(i)
		dTemp.setDate(1)
		sDate = d.getDate(d) + '-' + GetMonthName(d.getMonth(d)) + '-' + d.getFullYear(d)
		ResetCalendar()
	}

}

//...................................................................................................
function CalSetMonth(i){
	//alert(i);

	// When going to a new month, select the 1st as the selected day. 
	// Reason: if the current date is the 31st, and try to go to a month with less 
	// than 31 days, you will end up on the 1st of the month after it.

	d.setDate(1)
	d.setMonth(i,d.getDate(d))

	dTemp.setMonth(i,1)
	//sDate = d.getDate(d) + '-' + GetMonthName(d.getMonth(d)) + '-' + d.getFullYear(d)
	sDate = d.getDate(d) + '-' + GetMonthName(d.getMonth(d)) + '-' + d.getFullYear(d)
	ResetCalendar()
}

//...................................................................................................
function CalSetYear(i){
	d.setFullYear(i,d.getMonth(d),d.getDate(d))
	dTemp.setFullYear(i,d.getMonth(d),1)
	sDate = d.getDate(d) + '-' + GetMonthName(d.getMonth(d)) + '-' + d.getFullYear(d)
	ResetCalendar()
}

//...................................................................................................
function CalSetDate(i,objReturnTxtField){

	objTxtField = objReturnTxtField


	// First - check to see if a date has been passed,
	// If not - use todays date:
	if(i==''){
		dToday = new Date()    //today's date
		i = dToday.getDate() + '-' + GetMonthName(dToday.getMonth()) + '-' + dToday.getFullYear()
	}


        var sNewDate = new String(i)
        var splitString = sNewDate.split("-");

	d.setFullYear(splitString[2],GetMonthNumber(splitString[1]),splitString[0])
	dTemp.setFullYear(splitString[2],GetMonthNumber(splitString[1]),1)

	d.setMonth(GetMonthNumber(splitString[1]),splitString[0])
	dTemp.setMonth(GetMonthNumber(splitString[1]),1)

	d.setDate(splitString[0])
	dTemp.setDate(1)

	window.document.frmYearMonth.txtYear.value=d.getFullYear(d);
	sDate = splitString[0] + '-' + splitString[1] + '-' + splitString[2]
	ResetCalendar()
}

//...................................................................................................
function ResetCalendar(){
	// Reset the calendar.
	// need to find the first day of the specified month in 'd':

	window.document.frmCalDate.txtCalDate.value=sDate;

	var s = '';
	i = -dTemp.getDay(d);	

	for (var loop=0; loop < document.frmCal.length; loop++){

		i = i + 1

	  	if(i==d.getDate()){
			// This is the current value of the date:
			if(i < 10){
				document.frmCal.elements[loop].value=' ' + i + ' ';
			}else{
				document.frmCal.elements[loop].value=i;
			}
			document.frmCal.elements[loop].className='cal_icon_val';
			document.frmCal.elements[loop].id='cal_icon_val';
	  	}else{
			// If i is Greater than 0...
			if(i > 0){
				// ...or less or equal to the number of days in this month:
				if(i <= MonthsDayLength(d)){
					if(i < 10){
						document.frmCal.elements[loop].value=' ' + i + ' ';
					}else{
						document.frmCal.elements[loop].value=i;
					}
					document.frmCal.elements[loop].className='cal_icon';
					document.frmCal.elements[loop].id='cal_icon';

				}else{
					document.frmCal.elements[loop].value='';
					document.frmCal.elements[loop].className='cal_icon_void';
					document.frmCal.elements[loop].id='cal_icon_void';
				}
			}else{
				document.frmCal.elements[loop].value='';
				document.frmCal.elements[loop].className='cal_icon_void';
				document.frmCal.elements[loop].id='cal_icon_void';
			}
		}
	}

	//for (var loop=0; loop < document.frmCal.length; loop++){
	//	s = s + document.frmCal.elements[loop].name + ' = ' + document.frmCal.elements[loop].value;
	//	if(d.getDate(d)==document.frmCal.elements[loop].value){
	//		s = s + ' <\n';
	//	}else{
	//		s = s + '\n';
	//	}
	//}
}

