//
// global declarations and initialization
//


function searchArnold() {

window1=window.open("","popupWindow", "width=640, height=480, left=0, top=0, menubar=yes, status=yes, location=no, toolbar=yes, scrollbars=yes, resizable=yes, directories=yes")
}


function setXPos(i) {
	xAdjust = +200;
	xPos = i-xAdjust;
}
function setYPos(i) {
	yAdjust = -235;
	yPos = i+yAdjust;
}
function init () {
	windowHeight	= 182;
	windowWidth	= 191;

	theDate         = new Date();
	today           = new Date();
	currMonth       = today.getMonth();
	msPerDay        = 24*60*60*1000;
	cookieString    = "dispMonth="+currMonth+";";
	document.cookie = cookieString;
	startOfString   = document.cookie.indexOf("dispMonth");
	countbegin      = document.cookie.indexOf("=",startOfString) + 1;
	countend        = document.cookie.indexOf(";",countbegin);
	if (countend == -1) {
		countend = document.cookie.length;
	}
	dispMonth     = eval ("document.cookie.substring(countbegin,countend)");

	year = today.getYear();
	if (year < 1900){
		year += 1900;
	}
	firstOfMonth  = new Date(year,dispMonth,1);
	monthName     = new makeArray(12);
	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";

	monthPrefix     = new Array(12);
	monthPrefix[1]  = "Jan";
	monthPrefix[2]  = "Feb";
	monthPrefix[3]  = "Mar";
	monthPrefix[4]  = "Apr";
	monthPrefix[5]  = "May";
	monthPrefix[6]  = "Jun";
	monthPrefix[7]  = "Jul";
	monthPrefix[8]  = "Aug";
	monthPrefix[9]  = "Sep";
	monthPrefix[10] = "Oct";
	monthPrefix[11] = "Nov";
	monthPrefix[12] = "Dec";	
}

function makeArray(n) {
	this.length = n;
	return this;
}




//
// entry point
//
function openCalendar(monthFldName,dateFldname,formNumber,positionField) {
	init();
	formNum  = formNumber;
	monthFld = monthFldName;
	dateFld  = dateFldname;
	posFld   = positionField;
	//
	windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=yes,width=180,height=173";
	calendarWindow = this.open("","calendarWindow",windowOptions);

	if (screen.width < xPos+windowWidth) {
		setXPos(xPos-windowWidth+5);
	}
	if (screen.height < yPos+windowHeight) {
		setYPos(yPos-windowHeight-45);
	}
	if (monthFld == "DEPMONTH")
		dispMonth = eval(document.getElementById("BBDepMon").value) + today.getMonth();
	else
		dispMonth = eval(document.getElementById("BBRetMon").value) + today.getMonth();
	if (dispMonth > 12)
  		dispMonth = dispMonth - 12;
	dispMonth -= 1;

	document.cookie="dispMonth="+dispMonth;
	calendarWindow.moveTo(xPos, yPos);
	calendarWindow.callingForm = this;
	redraw();
}

//
// repaint the calendar
//
function redraw() {

	calendarWindow.callingForm = this;

	year = today.getYear();
	currMonth=today.getMonth();
	if (currMonth > dispMonth)
		year++;	
	firstOfMonth = new Date(year, dispMonth,1);
	calendarWindow.document.open();
	calTitle = "<TITLE>Calendar</TITLE>";
	calendarWindow.document.write(calTitle);
	drawCalendar(firstOfMonth);
	calendarWindow.document.write(htmlBuffer);

	calendarWindow.document.write(" ");
	calendarWindow.document.close();
	calendarWindow.callingForm = this;
	calendarWindow.focus();
}

//
// fill the calling forms date and month
//
function fillDate(filler) {
	var m;
	var d;


	firstmonth = (new Date()).getMonth();

	if ( monthNum > firstmonth ){
		m = monthNum - firstmonth -1;
	} else {
		m = monthNum - firstmonth +11;
	}

	d = filler-1;

	eval("document.forms[" + formNum + "]." + monthFld + ".selectedIndex=" + m);
	eval("document.forms[" + formNum + "]." + dateFld  + ".selectedIndex=" + d);

	//CR 12655 - Selecting outbound date automatically updates the return date as the next day
	//Fill return date combo boxes with appropriate values
	refreshCombo(monthFld, dateFld);

	calendarWindow.close();
}

//
// set the month
//
//	function changeMonth (increment) {
//
//	nextMonth = dispMonth;
//	if (increment == 1) {
//		nextMonth++ ;
//	} else {
//		nextMonth-- ;
//	}
//	if ((nextMonth-currMonth >= 13) || (nextMonth < currMonth))  {
//		nextMonth = currMonth;
//	}
//	dispMonth = nextMonth;
//
//	document.cookie="dispMonth="+nextMonth;
//
//	redraw();
//}


function drawMonth (theMonth) {


	dispMonth = theMonth - 1;

	document.cookie="dispMonth="+dispMonth;

	redraw();
}

//
// generate the calendar document
//
function drawCalendar (theDate) {

	currMonth   = today.getMonth() + 1;
	monthNum    = theDate.getMonth() + 1;
	htmlBuffer  = "<HTML>";
	htmlBuffer += "<style type=\"text/css\">";
	htmlBuffer += "<!--";
	htmlBuffer += ".blue { color: #000080}";
	htmlBuffer += ".black { color: #000000}";
	htmlBuffer += ".white { color: #FFFFFF}";
	htmlBuffer += ".autowidth { width: 100% }";
	htmlBuffer += ".disabled { color: #808080}";
//	htmlBuffer += ".enabled { font-weight: bold;  }";
	htmlBuffer += "a:hover { text-decoration:none; color:#000000; }";
	htmlBuffer += "a:link { text-decoration:none; color:#000000; }";
	htmlBuffer += "a:visited { text-decoration:none; color:#000000; }";
	htmlBuffer += "a.white:hover { text-decoration:none; color:#FFFFFF; }";
	htmlBuffer += "a.white:link { text-decoration:none; color:#FFFFFF; }";
	htmlBuffer += "a.white:visited { text-decoration:none; color:#FFFFFF; }";
	htmlBuffer += "-->";
	htmlBuffer += "</style>";
	htmlBuffer += "<BODY BGCOLOR=#FFFFFF leftmargin=0 topmargin=0 marginheight=0 marginwidth=0>";
	//htmlBuffer += "<FORM>";
	htmlBuffer += "<CENTER><TABLE border=0 cellpadding=0 cellspacing=0>";
	htmlBuffer += "<TR bgcolor=#FFFFFF  align=left bordercolordark=white bordercolorlight=white>"
	htmlBuffer += "<TD>"

htmlBuffer += "<TABLE class=\"autowidth\" rows=2 cols=6 cellpadding=2  cellspacing=1 bordercolordark=white bordercolorlight=white bgcolor=#FFFFFF border=0 rules=ALL>";


//    	************ GRANT'S CHANGES adding months ******************************************************************
	
	htmlBuffer += "<TR bgcolor=#FFFFFF  align=left bordercolordark=white bordercolorlight=white>";
	
	for (m=0; m<12; m++) {
		if (currMonth>12) {
			currMonth = 1;			
		}			
		if (m == 5 || m==11) {
			
			if (dispMonth == currMonth-1) {
				htmlBuffer += "<TD ALIGN=CENTER bgcolor=#FF0000 ><a href=\"javascript:callingForm.drawMonth("+currMonth+")\"><FONT class=\"white\" FACE=\"Tahoma\" SIZE=\"1\">"+ monthPrefix[currMonth]+"</FONT></a></TD></TR>";
			} else {	
				htmlBuffer += "<TD ALIGN=CENTER bgcolor=#5CA4E4 ><a href=\"javascript:callingForm.drawMonth("+currMonth+")\"><FONT class=\"white\" FACE=\"Tahoma\" SIZE=\"1\">"+ monthPrefix[currMonth]+"</FONT></a></TD></TR>";
			}
		htmlBuffer += "<TR bgcolor=#FFFFFF  align=left bordercolordark=white bordercolorlight=white>";
		} else {

			if (dispMonth == currMonth-1) {
				htmlBuffer += "<TD ALIGN=CENTER bgcolor=#FF0000 ><a href=\"javascript:callingForm.drawMonth("+currMonth+")\"><FONT class=\"white\" FACE=\"Tahoma\" SIZE=\"1\">"+ monthPrefix[currMonth]+"</FONT></a></TD>";
			} else {
				htmlBuffer += "<TD ALIGN=CENTER bgcolor=#5CA4E4 ><a href=\"javascript:callingForm.drawMonth("+currMonth+")\"><FONT class=\"white\" FACE=\"Tahoma\" SIZE=\"1\">"+ monthPrefix[currMonth]+"</FONT></a></TD>";
			}
		}
		currMonth += 1;
	}	
	
	

//   ***********************END ******************************************************************

//	htmlBuffer += "<TD ALIGN=CENTER COLSPAN=1 WIDTH=21 BGCOLOR=#FF0000><FONT FACE=\"Verdana,Arial\" SIZE=\"1\"><a //href=\"javascript:callingForm.changeMonth(-1)\" class=white><B>&lt;&lt;</B></a></FONT></TD>";


	htmlBuffer += "<TD ALIGN=CENTER COLSPAN=7 BGCOLOR=#0077D3 ><FONT class=\"white\" FACE=\"Tahoma\" SIZE=\"1\">";

        // getFullYear sometimes returns the Year - 1900
        tempyr = theDate.getFullYear();
        if (tempyr < 1900) {
            tempyr += 1900;
        }

	htmlBuffer += "<B>"+monthName[monthNum] + " " + tempyr + "</B></FONT></TD></TR>";

//	htmlBuffer += "<TD ALIGN=CENTER COLSPAN=1 WIDTH=21 bgcolor=#FF0000><FONT FACE=\"Verdana,Arial\" SIZE=\"1\"><a //href=\"javascript:callingForm.changeMonth(1)\" class=white><B>&gt;&gt;</B></a></FONT></TD></TR>";


	htmlBuffer += "<TABLE  cellpadding=2  cellspacing=1 bordercolordark=white bordercolorlight=white bgcolor=#FFFFFF 	border=0 cols=5 rules=ALL>";

	htmlBuffer += "<TR bgcolor=#FFFFFF  align=left bordercolordark=white bordercolorlight=white>";
	
 
	htmlBuffer += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF ><FONT class=\"black\" FACE=\"Tahoma\" SIZE=\"1\"><B>Sun</B></FONT></TD>";
        htmlBuffer += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF ><FONT class=\"black\" FACE=\"Tahoma\" SIZE=\"1\"><B>Mon</B></FONT></TD>";
        htmlBuffer += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF ><FONT class=\"black\" FACE=\"Tahoma\" SIZE=\"1\"><B>Tue</B></FONT></TD>";
        htmlBuffer += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF ><FONT class=\"black\" FACE=\"Tahoma\" SIZE=\"1\"><B>Wed</B></FONT></TD>";
        htmlBuffer += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF ><FONT class=\"black\" FACE=\"Tahoma\" SIZE=\"1\"><B>Thu</B></FONT></TD>";
        htmlBuffer += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF ><FONT class=\"black\" FACE=\"Tahoma\" SIZE=\"1\"><B>Fri</B></FONT></TD>";
        htmlBuffer += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF ><FONT class=\"black\" FACE=\"Tahoma\" SIZE=\"1\"><B>Sat</B></FONT></TD></TR>";


	htmlBuffer += "<TR bgcolor=#FFFFFF align=left bordercolordark=white bordercolorlight=white>";

	drawBody(theDate);

//	htmlBuffer += "</TR><TR bgcolor=#FFFFFF  align=left border=white><TD COLSPAN=7 ALIGN=center bgcolor=#0000CD><FONT //class=\"blue\" FACE=\"Tahoma\" SIZE=\"1\" WIDTH=30><a href=# onClick=self.close() class=white><B>Close //Window</B></a></FONT></TD></TR></TD></TR></TABLE></TABLE>";

	htmlBuffer += "</BODY></HTML>";
}

//
// generate the calendar body
//
function drawBody (theDate) {
	date = 1;


	thisMonth = theDate.getMonth();
	startDay = today.getDate();
	startMonth = today.getMonth();
	
	// get last day for the previous month;
	previousMonth = new Date(theDate.getTime() - msPerDay);
	lastDateOfPreviousMonth = previousMonth.getDate();

	// get last day for this month.
	year = theDate.getYear();
	if (year < 1900){
		year += 1900;
	}
	nextMonth = new Date(year, thisMonth + 1, 1);
	lastDateOfThisMonth = new Date(nextMonth.getTime() - msPerDay);
	lastDate = lastDateOfThisMonth.getDate();

	// If the first day of this month is not Sunday, find first Sunday
	dayInWeek = theDate.getDay();
	if ( dayInWeek != 0 ){
		currentMonth = thisMonth - 1;
		firstDateToDisplay = lastDateOfPreviousMonth - dayInWeek + 1;

	} else {
		currentMonth = thisMonth;
	}

	for (w=0; w<6; w++) {
		for (d=0; d<7; d++) {

			if (d == 0 || d == 6) {
//				htmlBuffer   += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#B0C4DE>";
//				htmlBuffer   += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#DCDCDC>";
				htmlBuffer   += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#DBEAF5>";

			} else {
				htmlBuffer   += "<TD ALIGN=CENTER WIDTH=21 bgcolor=#F8F8FF>";
			}

			// skip previous month
			if (currentMonth != thisMonth) {
				htmlBuffer += "<image src=1pixel.gif height=12 width=1>";

				firstDateToDisplay +=1;

				// if exceed, increase current month
				if ( firstDateToDisplay > lastDateOfPreviousMonth ){
					currentMonth += 1;			
				}
				
			} else {
//				htmlBuffer += "<FONT FACE=\"Tahoma\" SIZE=\"1\">";
				if ((startMonth == thisMonth) && (date <= startDay)) {
					htmlBuffer += "<FONT CLASS=\"disabled\" FACE=\"Tahoma\" SIZE=\"1\">";
					htmlBuffer += "<S>" + date + "</S>";
					htmlBuffer += "</FONT>";

				} else {
					htmlBuffer += "<FONT CLASS=\"enabled\" FACE=\"Tahoma\" SIZE=\"1\">";
					htmlBuffer += "<a href=\"javascript:callingForm.fillDate(" + date + ")\">";
									
//					if ( date < 10 ) {
//						htmlBuffer += "<B>0</B>";
//						htmlBuffer += "0";
//					}
					
					htmlBuffer += date;
					htmlBuffer += "</a>";
					htmlBuffer += "</FONT>";
				}
				
	
				// increase the date;
				date += 1;

				if ( date > lastDate){
					currentMonth += 1;
					firstDateToDisplay = 1;
				}
			}

		}
		htmlBuffer += "<TR border=bordercolordark=white bordercolorlight=white>";


	}
	//htmlBuffer += "</FORM>";
}

    //CR 12655 - Selecting outbound date automatically updates the return date as the next day
    function refreshCombo(monthFld, dateFld){
        //'depmon','depday'

        if (document.layers)
        {
            /////
            ////
            ///  Proprietary Navigator 4.x code 
            //

            if (monthFld == "DEPMONTH" || dateFld == "DEPDAY")
            {
                var x=0;
                day = document.layers.DEPDAY;
                x = eval(day.value);
                if (x == "30")
                {
                    document.layers.RETDAY.value = x;
                }
                else
                {
                    document.layers.RETDAY.value = ((x++) + 1);
                }
                document.layers.RETMONTH.value = document.layers.DEPMONTH.value;
            }

            if (monthFld == "depmonschav" || dateFld == "depdayschav")
            {
                var y=0;
                day = document.layers.depdayschav;
                y = eval(day.value);
                if (y == "30")
                {
                    document.layers.retdayschav.value = y;
                }
                else
                {
                    document.layers.retdayschav.value = ((y++) + 1);
                }
                document.layers.retmonschav.value = document.layers.depmonschav.value;
            }
        }
        else if (document.all)
        {
            /////
            ////
            ///  Proprietary IE4
            // 

            if (monthFld == "DEPMONTH" || dateFld == "DEPDAY")
            {
                var x=0;
                day = document.all.DEPDAY;
                x = eval(day.value);
                if (x == "30")
                {
                    document.all.RETDAY.value = x;
                }
                else
                {
                    document.all.RETDAY.value = ((x++) + 1);
                }
                document.all.RETMONTH.value = document.all.DEPMONTH.value;
            }

            if (monthFld == "depmonschav" || dateFld == "depdayschav")
            {
                var y=0;
                day = document.all.depdayschav;
                y = eval(day.value);
                if (y == "30")
                {
                    document.all.retdayschav.value = y;
                }
                else
                {
                    document.all.retdayschav.value = ((y++) + 1);
                }
                document.all.retmonschav.value = document.all.depmonschav.value;
            }
        }
        else if (document.getElementById)
        {
            /////
            ////
            ///  The Standards Compliant Way .. recommended.
            // 
            if (monthFld == "DEPMONTH" || dateFld == "DEPDAY")
            {
                var x=0;
                day = document.getElementById("BBDepDay");
                x = eval(day.value);
                if (x == "30")
                {
                    document.getElementById("BBRetDay").value = x;
                }
                else
                {
                    document.getElementById("BBRetDay").value = ((x++) + 1);
                }
                document.getElementById("BBRetMon").value = document.getElementById("BBDepMon").value;
            }

            if (monthFld == "depmonschav" || dateFld == "depdayschav")
            {
                var y=0;
                day = document.getElementById("SCHEDDepDay");
                y = eval(day.value);
                if (y == "30")
                {
                    document.getElementById("SCHEDRetDay").value = y;
                }
                else
                {
                    document.getElementById("SCHEDRetDay").value = ((y++) + 1);
                }
                document.getElementById("SCHEDRetMon").value = document.getElementById("SCHEDDepMon").value;
            }
        }
    }
