function upcd()
{
// calculate first and 2nd forward slashes
firstloc = document.Fsearch_120x600.checkindate.value.indexOf("/");
secloc = document.Fsearch_120x600.checkindate.value.lastIndexOf("/");
var len = document.Fsearch_120x600.checkindate.value.length;
document.Fsearch_120x600.CM.value = document.Fsearch_120x600.checkindate.value.substring(0,firstloc);
document.Fsearch_120x600.CD.value = document.Fsearch_120x600.checkindate.value.substring(firstloc+1,secloc);
// need to check that day is not starting with zero, or month not starting with zero
document.Fsearch_120x600.CY.value = document.Fsearch_120x600.checkindate.value.substring((len-4),len);
}
// Calendar global declarations and initialization
function init (monthAhead)
{
initStrCal();

today 			= new Date();
currDay         = today.getDate();
currMonth       = today.getMonth();
currYear	    = today.getYear();
if (currYear < 1000) currYear += 1900;

today			= new Date(currYear, currMonth, currDay);
nextYear	    = new Date(currYear, currMonth, currDay);
nextYear.setDate (nextYear.getDate() + 365);

// Get the date that is selected on the pull down menus if any.
var d;
var m;
var y;

eval("d=document.forms['" + formName + "']." + dayFld  + ".selectedIndex");
eval("m=document.forms['" + formName + "']." + monthFld  + ".selectedIndex");
eval("y=document.forms['" + formName + "']." + yearFld  + ".selectedIndex");

if (d == 0 && m == 0 && y == 0)
	displayMonth = currMonth;
else
	{
    if (d == 0) d = 1;		// set defaults
    if (m == 0) m = 0;		//Sven Buechsenschuetz 2002-12-06: changed from 1 to 0 to not get February displayed when January is selected in dropdown
    if (y == 0) y = currYear;
    else     y = currYear + y - 1;	// translate year index */

	if (monthAhead) m+=monthAhead;

    var tempDate = new Date (y, m - 1, d);
    if (tempDate.getTime() < today.getTime())
		tempDate.setDate(today.getDate());
    else if (tempDate.getTime() >= nextYear.getTime())
		tempDate.setDate(nextYear.getDate() - 1);

    displayMonth = tempDate.getMonth();
     var  displayYear = tempDate.getYear();


              if (displayYear < 1000) displayYear += 1900;

             tempDate = new Date(displayYear, displayMonth + 1, 1);

             if (tempDate.getTime() >= nextYear.getTime()) displayMonth -= 1;

	         // new code
			 if (dayFld == "CD_temp"){
	                 // check in
					      if ((document.Fsearch_120x600.CY_temp.selectedIndex + baseYear) > currYear) {
						  displayMonth += 12;

						  }
					 }
              else {
	                 // check out
					 if ((document.Fsearch_120x600.year_out.selectedIndex + baseYear) > currYear) {
						  displayMonth += 12;

						  }
               }
			 // end new code
	}
}
/*
-----------------------------
General date functions
They are used in the dropdowns (check-in and check-out dates)
-----------------------------
*/
// Adds some days, months or years to a given date
// returns the new date
function dateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date();
	returnDate.setTime(startDate.getTime());

	var yearsToAdd = numYears;
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
		{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;

		}
	returnDate.setMonth(month);
    returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	returnDate.setTime(returnDate.getTime()+(86400000*numDays));

// minor bug fix, 27 September 2002, Alex Bainbridge - for when drop down is more than 30 days 1 hour is lost, changing the day by 1 day.
if (returnDate.getHours() =="23"){returnDate.setTime(returnDate.getTime() + 3600000);}


	return returnDate;
}

// Returns a date in US format (mm/dd/yyyy)
function formatDate(date)
{
	day=date.getDate();
	day=((day<10)?"0":"")+day;
	month=date.getMonth()+1;
	month=((month<10)?"0":"")+month;
	yy=date.getYear();
	year = (yy < 1000) ? yy + 1900 : yy;
	str=month+"/"+day+"/"+year;
	return str;
}

// Changes the number of days visible in the dropdown - when the month changes.
function computeDays(sSelectMonth, sSelectDay, sSelectYear)
{
	var nMonthValue = sSelectMonth.options[sSelectMonth.selectedIndex].value;
	var sYear = sSelectYear.value;
	var nDays = 31;
	switch (nMonthValue)
		{
	    case "4":
	    case "6":
	    case "9":
	    case "11":
	    	nDays = 30;
			if (sSelectDay.selectedIndex==30) sSelectDay.selectedIndex=29;
	        sSelectDay.options[30] = null;

	        break;
		case "2":


	    var tempyear = parseInt(baseYear)+parseInt(sYear);

	        nDays = (tempyear%4 == 0 ? 29: 28);
			if (sSelectDay.selectedIndex>=nDays) sSelectDay.selectedIndex=nDays-1;
			for (var less = 30; less >= nDays; less --)
				{
	            sSelectDay.options[less] = null;
 				}
		    break;
	    }
	nbVisibleDays=sSelectDay.options.length;
	for ( var count =0; count <nbVisibleDays; count ++)
		{
	    sSelectDay.options[count].value = count + 1;
	    sSelectDay.options[count].text = count + 1;
	    }
	var index=sSelectDay.selectedIndex;
	for ( var count =nbVisibleDays; count <nDays; count ++)
		{
		newDay = new Option(count+1,count+1);
	    sSelectDay.options[count]=newDay;
	    }
	sSelectDay.selectedIndex=index;
}
// Returns true if date1 is strictly before date2
function isEarlier(date1, date2)
{
	if (date1.getTime()<date2.getTime() && !(date1.getDate()==date2.getDate() && date1.getMonth()==date2.getMonth() && date1.getYear()==date2.getYear()))
		return true;
	else return false;
}

// this function is directly called when changing the number of nights
// or the check in date
// it will calculate the new check-out date
function updateCheckOut()
{
	var nights=parseInt(document.Fsearch_120x600.numofnights.selectedIndex)+1;
	var date_in=new Date(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear,parseInt(document.Fsearch_120x600.CM_temp.selectedIndex),parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1 );
	var date_out=dateAdd(date_in, nights,0,0);

	document.Fsearch_120x600.month_out.selectedIndex = date_out.getMonth();
	var yy=date_out.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	document.Fsearch_120x600.year_out.selectedIndex = year-baseYear;
	computeDays(document.Fsearch_120x600.month_out, document.Fsearch_120x600.day_out, document.Fsearch_120x600.year_out);

	document.Fsearch_120x600.day_out.selectedIndex = date_out.getDate()-1;
	getDateInfo();
}
// Changes the check-in displayed date
function updateCheckIn(date)
{
	var day_in  = date.getDate();
	var month_in = date.getMonth();
	var yy = date.getYear();
	var year_in = (yy < 1000) ? yy + 1900 : yy;
	document.Fsearch_120x600.CM_temp.selectedIndex=month_in;
	document.Fsearch_120x600.CY_temp.selectedIndex=year_in-baseYear;
	document.Fsearch_120x600.CD_temp.selectedIndex=day_in-1;

	computeDays(document.Fsearch_120x600.CM_temp, document.Fsearch_120x600.CD_temp, document.Fsearch_120x600.CY_temp);
    getDateInfo();
}
// called when the day is changed in the check-in date
function changeCheckIn_day()
{
	var date_in=new Date(1,0,1);
	date_in.setYear(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear);
	date_in.setMonth(parseInt(document.Fsearch_120x600.CM_temp.selectedIndex));
	date_in.setDate(parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1);
	if (isEarlier(date_in, today))
		{
		date_in=dateAdd(date_in, 0, 1, 0);
		}
	updateCheckIn(date_in);
	document.Fsearch_120x600.checkindate.value=formatDate(date_in);

upcd();
	updateCheckOut();
	getDateInfo();
}
// called when the month is changed in the check-in date
function changeCheckIn_month()
{
	computeDays(document.Fsearch_120x600.CM_temp, document.Fsearch_120x600.CD_temp, document.Fsearch_120x600.CY_temp);
	var date_in=new Date(1,0,1);
	date_in.setYear(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear);
	date_in.setMonth(parseInt(document.Fsearch_120x600.CM_temp.selectedIndex));
	date_in.setDate(parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1);
	if (isEarlier(date_in, today))
		{
		date_in=dateAdd(date_in, 0, 0, 1);
		}
	updateCheckIn(date_in);
	document.Fsearch_120x600.checkindate.value=formatDate(date_in);

   	upcd();
	updateCheckOut();
	getDateInfo();
}
// called when the year is changed in the check-in date
function changeCheckIn_year()
{
	computeDays(document.Fsearch_120x600.CM_temp, document.Fsearch_120x600.CD_temp, document.Fsearch_120x600.CY_temp);
	var date_in=new Date(1,0,1);
	date_in.setYear(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear);
	date_in.setMonth(parseInt(document.Fsearch_120x600.CM_temp.selectedIndex));
	date_in.setDate(parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1);
	if (isEarlier(date_in, today))
		{
	    alert(checkinbeforetodaytext);
		var yy=date_in.getYear();
		var year = (yy < 1000) ? yy + 1900 : yy;
		date_in.setYear(year+1);
		}
	updateCheckIn(date_in);
	document.Fsearch_120x600.checkindate.value=formatDate(date_in);
upcd();
	updateCheckOut();
	getDateInfo();
}
function verifNbNights(date_in,date_out)
{
	var nMillsec  = date_out.getTime() - date_in.getTime();
	var nNights   = Math.floor(nMillsec /1000/60/60/24);

    document.Fsearch_120x600.numofnights.selectedIndex = nNights-1;
	getDateInfo();
}
// called when the day is changed in the check-out date
function changeCheckOut_day()
{
	var date_in=new Date(1,0,1);
	date_in.setYear(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear);
	date_in.setMonth(parseInt(document.Fsearch_120x600.CM_temp.selectedIndex));
	date_in.setDate(parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1);
	var date_out=new Date(1,0,1);
	date_out.setYear(parseInt(document.Fsearch_120x600.year_out.selectedIndex)+baseYear);
	date_out.setMonth(parseInt(document.Fsearch_120x600.month_out.selectedIndex));
	date_out.setDate(parseInt(document.Fsearch_120x600.day_out.selectedIndex)+1);

// this is the roll section - where if you are on the 7th of August and you select 3rd from checkout - will move month to Sept.
if (date_out<=date_in)
	{
	date_out=dateAdd(date_out, 0, 1, 0);
	document.Fsearch_120x600.year_out.selectedIndex = date_out.getYear()-baseYear;
	document.Fsearch_120x600.month_out.selectedIndex = date_out.getMonth();
	document.Fsearch_120x600.day_out.selectedIndex = date_out.getDate()-1;
	}
	verifNbNights(date_in,date_out);

getDateInfo();
}
// called when the month is changed in the check-out date
function changeCheckOut_month()
{
	var date_in=new Date(1,0,1);
	date_in.setYear(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear);
	date_in.setMonth(parseInt(document.Fsearch_120x600.CM_temp.selectedIndex));
	date_in.setDate(parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1);
	var date_out=new Date(1,0,1);
	date_out.setYear(parseInt(document.Fsearch_120x600.year_out.selectedIndex)+baseYear);
	date_out.setMonth(parseInt(document.Fsearch_120x600.month_out.selectedIndex));
	date_out.setDate(parseInt(document.Fsearch_120x600.day_out.selectedIndex)+1);

	if (date_out<=date_in)
		{
		date_out=dateAdd(date_out, 0, 0, 1);
		var nMillsec  = date_out.getTime() - date_in.getTime();
		var nNights   = Math.floor(nMillsec /1000/60/60/24);
		if (nNights<=99)
			{
			document.Fsearch_120x600.year_out.selectedIndex = date_out.getYear()-baseYear;
			document.Fsearch_120x600.month_out.selectedIndex = date_out.getMonth();
			document.Fsearch_120x600.day_out.selectedIndex = date_out.getDate()-1;
			}
		}
	verifNbNights(date_in,date_out);
	getDateInfo();
}
// called when the year is changed in the check-out date
function changeCheckOut_year()
{
	var date_in=new Date(1,0,1);
	date_in.setYear(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear);
	date_in.setMonth(parseInt(document.Fsearch_120x600.CM_temp.selectedIndex));
	date_in.setDate(parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1);
	var date_out=new Date(1,0,1);
	date_out.setYear(parseInt(document.Fsearch_120x600.year_out.selectedIndex)+baseYear);
	date_out.setMonth(parseInt(document.Fsearch_120x600.month_out.selectedIndex));
	date_out.setDate(parseInt(document.Fsearch_120x600.day_out.selectedIndex)+1);

	if (date_out<=date_in)
		{
		alert(checkoutearliertext);
		updateCheckOut();
		}
	else
		{
		verifNbNights(date_in,date_out);
		}

	getDateInfo();
}
// Set the check in date for today
// Set numofnights to 1
function set_defaults()
{
	var today=new Date();
	var yy=today.getYear();
	var baseYear=(yy < 1000) ? yy + 1900 : yy;

	document.Fsearch_120x600.CY_temp[0]=new Option(baseYear,0);
	document.Fsearch_120x600.CY_temp[1]=new Option(baseYear+1,1);

	document.Fsearch_120x600.year_out[0]=new Option(baseYear,0);
	document.Fsearch_120x600.year_out[1]=new Option(baseYear+1,1);
	document.Fsearch_120x600.year_out[2]=new Option(baseYear+2,2);

	var nights = 1;
	var date_in=today;
    updateCheckIn(date_in);
	document.Fsearch_120x600.checkindate.value=formatDate(date_in);

upcd();

    document.Fsearch_120x600.numofnights.selectedIndex = nights-1;
    updateCheckOut();
}
// scripts for days of week display on homepage

function getDateInfo() {
var yout = document.Fsearch_120x600.year_out.selectedIndex+baseYear;
var mout = document.Fsearch_120x600.month_out.selectedIndex;
var dout = document.Fsearch_120x600.day_out.selectedIndex+1;

var yin = document.Fsearch_120x600.CY_temp.selectedIndex+baseYear;
var moin = document.Fsearch_120x600.CM_temp.selectedIndex;
var din = document.Fsearch_120x600.CD_temp.selectedIndex+1;
var hlprin = mtend[moin];

if (moin == 1 && yin % 4 == 0) { hlprin++; }

var cin = new Date(yin,moin,din);
var dayOfWeekin = cin.getDay();
var hlprout = mtend[mout];

if (mout == 1 && yout % 4 == 0) { hlprout++; }

var cout = new Date(yout,mout,dout);
var dayOfWeekout = cout.getDay();
}
// VALIDATE DATE ENTRY, this function is run when the begin search graphic is selected

function validateForm(theForm)
{
var date_in=new Date(parseInt(document.Fsearch_120x600.CY_temp.selectedIndex)+baseYear,parseInt(document.Fsearch_120x600.CM_temp.selectedIndex),parseInt(document.Fsearch_120x600.CD_temp.selectedIndex)+1 );
var date_out=new Date(parseInt(document.Fsearch_120x600.year_out.selectedIndex)+baseYear,parseInt(document.Fsearch_120x600.month_out.selectedIndex),parseInt(document.Fsearch_120x600.day_out.selectedIndex)+1 );
var country=document.Fsearch_120x600.city_name.value;
var oktocontinue = true;

// cannot enter more than 99 nights
var nMillsec  = date_out.getTime() - date_in.getTime();
var nNights   = Math.floor(nMillsec /1000/60/60/24);

	if ( nNights > 99)
		{
	             alert(maxnightstext);
                 document.Fsearch_120x600.numofnights.selectedIndex = 0;
                 updateCheckOut();
				 oktocontinue = false;
		}
	if  (country == "")
		{
			alert(nocountry);
			document.Fsearch_120x600.city_name.focus();
			oktocontinue = false;
		}
// cannot book more than 365 days in advance
var nMillsecadvance  = date_in.getTime() - today.getTime();
var nNightsadvance   = Math.floor(nMillsecadvance /1000/60/60/24);

if (nNightsadvance > 364)
    {
    alert(atext);
	oktocontinue = false;
 }
// cannot have check out before check in
var nMillsecbetween = date_out.getTime() - date_in.getTime();
if (date_out.getTime() <= date_in.getTime())
{
    alert(checkoutearliertext);
	oktocontinue = false;
}
	if ( oktocontinue == true)
	{ return true;}
	else
	{ return false;}
}
/*
--------------------------------------
init stuff (will execute all the time)
--------------------------------------
*/
var mtend = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var today=new Date();
var yy=today.getYear();
var baseYear=(yy < 1000) ? yy + 1900 : yy;

var calendarWinOpen = false;
var calendarWindow = null;

var ftday = "";
var ftmonth = "";
var ftcheckin = "";

if (document.Fsearch_120x600)
	{
	if (location.href.search("day") != -1)
		{
		pos = location.search.indexOf("day") + 4;
		while ((location.search.charAt(pos) != "&") & (pos < location.search.length))
			{
			ftday = ftday + location.search.charAt(pos);
			pos++;
			}
		}
	if (location.href.search("month") != -1)
		{
		pos = location.search.indexOf("month") + 6;
		while ((location.search.charAt(pos) != "&") & (pos < location.search.length))
			{
			ftmonth = ftmonth + location.search.charAt(pos);
			pos++;
			}
		}
	if (location.href.search("numnights") != -1)
		{
		pos = location.search.indexOf("numnights") + 10;
		while ((location.search.charAt(pos) != "&") & (pos < location.search.length))
			{
			ftcheckin = ftcheckin + location.search.charAt(pos);
			pos++;
			}
		}
	else ftcheckin = 1;
	}

var str = "";
if (location.href.search("searchname") != -1)
	{
	pos = location.search.indexOf("searchname") + 11;
	while ((location.search.charAt(pos) != "&") & (pos < location.search.length))
		{
		str = str + location.search.charAt(pos);
		pos++;
		}
	str = str + ',';
	if (document.layers)
		{			// NS4
		this.find(str,false,false);
		}
	else
		{				// IE4
		txt = this.document.body.createTextRange();
		if (txt.findText(str))
			{
			txt.moveStart("character", -1);
			txt.findText(str);
			txt.select();
			txt.scrollIntoView();
			}
		}
	}
