function setdate(){
	thisForm = document.hotelSearchCriteriaForm;
	if ( thisForm != null ) {
		var now = new Date();
		var checkinDate = new Date(now.getTime() + (14 * 24*60*60*1000));
		var checkoutDate = new Date(now.getTime() + (15 * 24*60*60*1000));
		thisForm.arrivalMonth.options[checkinDate.getMonth()].selected = true;
		thisForm.departureMonth.options[checkoutDate.getMonth()].selected = true;
		thisForm.arrivalDay.options[checkinDate.getDate()-1].selected = true;
		thisForm.departureDay.options[checkoutDate.getDate()-1].selected = true;
	}
}

function clearRadio() {
    // clear the city text box
    document.forms[0].city.value = '';
    var radios = document.forms[0].rCity;
    for (i = 0; i < radios.length; i++)
    radios[i].checked = false;
}



function OpenDates(file,input) {
    window.open(file, input, 'width=300,height=300,resizable=1');
}


function OpenCalendar(dayField, day, monthField, month, yearField, year)  {
    file = '/util/calendar.jsp?formName=frm_Search';
    if ( dayField != null )
      file += '&dayField=' + dayField + '&day=' + day;
    if ( monthField != null )
      file += '&monthField=' + monthField + '&month=' + month;
    if ( yearField != null )
      file += '&yearField=' + yearField + '&year=' + year;
    window.open( file, "dates", 'width=300,height=300,resizable=1');
}


function OpenLogWindow(file,input) {
    window.open(file, input, 'width=250,height=175,resizable=1,status=1');
}


function isBrowserSupp() {
    version =  parseFloat( navigator.appVersion );
	if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
    	return true;
    }
	return true;
}


function isLeapYear(yrStr) {
	var leapYear=false;
	var year = parseInt(yrStr, 10);
	if (year%4 == 0) {
	    leapYear=true;
    	if (year%100 == 0) {
	        leapYear=false;
        	if (year%400 == 0) {
	            leapYear=true;
            }
        }
    }
	return leapYear;
}

function getDaysInMonth(mthIdx, YrStr) {
	var maxDays=31
	if (mthIdx==1) {
	    if (isLeapYear(YrStr)) {
	        maxDays=29;
        }
    	else {
	        maxDays=28;
        }
    }
	if (mthIdx==3 || mthIdx==5 || mthIdx==8 || mthIdx==10) {
	    maxDays=30;
    }
	return maxDays;
}


function adjustDate(mthIdx, Dt) {
	var value=0;
	var today = new Date()
	var theYear = parseInt(today.getYear(),10)
	if (mthIdx < today.getMonth()) {
    	theYear = (parseInt(today.getYear(), 10) + 1)
	}
	if(theYear<100){
    	theYear = "19" + theYear
	}
	else {
    	if((theYear-100) < 10) {
        	theYear = "0" + (theYear-100)
    	}
    	else{
        	theYear = (theYear-100)+""
    	}
    	theYear = "20" + theYear
	}
	var numDays=getDaysInMonth(mthIdx, theYear);
	if (mthIdx==1) {
	    if (Dt.options.selectedIndex + 1 < numDays) {
	        return 0;
        }
    	else {
        	Dt.options.selectedIndex=numDays - 1;
        	if (numDays==29) {
	            return 99;
            }
        	else {
            	return 1;
            }
        }
    }
	if (Dt.options.selectedIndex + 1 < numDays) {
    	value=0;
    }
	else {
    	if (Dt.options.selectedIndex + 1 > numDays) {
	        Dt.options.selectedIndex ;
       	 	value=3;
        }
    	else {
        	value=2;
        }
    }
	return value;
}

function amadChange(inM,inD,outM,outD) {
    if (!isBrowserSupp()) {
    	return;
    }
	var res = adjustDate(inM.options.selectedIndex, inD);
	if (res != 0) {
		outD.options.selectedIndex=0;
        if (outM.options.selectedIndex==11){
        	outM.options.selectedIndex=0
		}
        else {
			outM.options.selectedIndex=inM.options.selectedIndex + 1;
        }
    } 
    else {
    	outM.options.selectedIndex = inM.options.selectedIndex;
    	outD.options.selectedIndex = inD.options.selectedIndex+2;
    }
	return ;
}

function dmddChange(outM,outD) {
    if (!isBrowserSupp()) {
	    return;
    }
	adjustDate(outM.options.selectedIndex,outD);
	return;
}

function hideProcess() {
    document.getElementById('process').style.visibility = "hidden";
	document.getElementById('overlay').style.visibility = "visible";
}

function setPriceRange(form, min, max) {
	if (min == null)
		$(form).priceRangeMin.value = "";
	else
		$(form).priceRangeMin.value = min;
		
	if (max == null)
		$(form).priceRangeMax.value = "";
	else
		$(form).priceRangeMax.value = max;
}

function setPriceRange(form) {
	if(form.priceRanges.options[1].selected) {
		form.priceRangeMin.value = null;
		form.priceRangeMax.value = 74;
	}
	else if(form.priceRanges.options[2].selected) {
		form.priceRangeMin.value = 75;
		form.priceRangeMax.value = 125;
	}
	else if(form.priceRanges.options[3].selected) {
		form.priceRangeMin.value = 125;
		form.priceRangeMax.value = 200;
	}
	else if(form.priceRanges.options[4].selected) {
		form.priceRangeMin.value = 200;
		form.priceRangeMax.value = null;
	}
	else {
		form.priceRangeMin.value = null;
		form.priceRangeMax.value = null;
	}
}

function showAmenities() {
	if (Element.visible($('priceRanges'))) {
		new Effect.SwitchOff($('priceRanges'));
		setTimeout("new Effect.Appear($('amenities'));", 1000);
	} else {
		new Effect.Appear($('amenities'));
	}
}

function clearAndHideAmenities() {
	var amenities = $('filterForm').amenity
	
	for (i = 0; i < amenities.length; i++) {
		amenities[i].checked = false;
	}
	
	new Effect.SwitchOff($('amenities'));
}

function clearAndHidePriceRanges() {
	// check the 'Any' radio button and update the form values
	$('filterForm').priceRange[0].checked = true;
	$('filterForm').priceRangeMin.value = '';
	$('filterForm').priceRangeMax.value = '';
	
	
	new Effect.SwitchOff($('priceRanges'));
}

function showPriceRanges() {
	// update the radio buttons with the appropriate value
	
	var min = (+$('filterForm').priceRangeMin.value)
	var max = (+$('filterForm').priceRangeMax.value)
	
	if (min == 0 && max == 0)
		$('filterForm').priceRange[0].checked = true;
	else if (min == 0)
		$('filterForm').priceRange[1].checked = true;
	else if (max == 0)
		$('filterForm').priceRange[4].checked = true;
	else {
		if ((+min) == 75)
			$('filterForm').priceRange[2].checked = true;
		else if ((+min) == 125)
			$('filterForm').priceRange[3].checked = true;
	}
	
	if (Element.visible($('amenities'))) {
		new Effect.SwitchOff($('amenities'));
		setTimeout("new Effect.Appear($('priceRanges'));", 1000);
	} else {
		new Effect.Appear($('priceRanges'));
	}
}

var currentDivLayer = 'photosDiv';
function showInfoDiv(divLayer) {
	gotoInfoDiv(divLayer);
	if(!Element.visible(divLayer)) {
		if(currentDivLayer != '')
			hideInfoDiv(currentDivLayer);
		if(divLayer != 'theMapDiv' || mapLoaded)
			new Effect.toggle(divLayer, 'slide', {queue: 'end'});
		else
			new Effect.toggle(divLayer, 'slide', {queue: 'end', afterFinish: loadGMap});
		currentDivLayer = divLayer;
	}
}

function hideInfoDiv(divLayer) {
	if (Element.visible(divLayer)) {
		new Effect.toggle(divLayer, 'slide', {queue: 'front'});
		currentDivLayer = '';
	}
}

function gotoInfoDiv(divLayer) {
	if(window.location.href.indexOf("#") > 0)
		window.location.href = window.location.href.substring(0, window.location.href.indexOf("#")) + "#" + divLayer.substring(0, divLayer.length - 3);
	else
		window.location.href += "#" + divLayer.substring(0, divLayer.length - 3);
}

var aCalendar, dCalendar; 
var holdAMonth, holdAYear, holdDMonth, holdDYear;
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var currentYear = currentDate.getFullYear();

function loadArrivalCalendar () {		
	var theYear = currentYear;
	//see if there is only one form
	if(typeof document.hotelSearchCriteriaForm[0].arrivalMonth == 'undefined') {
		if(document.hotelSearchCriteriaForm.arrivalMonth.selectedIndex < currentMonth  || (document.hotelSearchCriteriaForm.arrivalMonth.selectedIndex == currentMonth && document.hotelSearchCriteriaForm.arrivalDay.selectedIndex + 1 < currentDay))
			theYear++;	
			
		holdAMonth = document.hotelSearchCriteriaForm.arrivalMonth.selectedIndex;
		holdAYear = theYear;
			
		aCalendar = new Epoch('arrivalCal', 
			'popup', 
			document.getElementById('arrivalCalendarValue'), 
			document.getElementById('arrivalCalendarImgHeader'), 
			false,
			document.hotelSearchCriteriaForm.arrivalMonth.selectedIndex,
			document.hotelSearchCriteriaForm.arrivalDay.selectedIndex,
			theYear,
			function() {
				var sd = document.getElementById('arrivalCalendarValue').value.split("\/");
				document.hotelSearchCriteriaForm.arrivalMonth.options.selectedIndex = sd[0] - 1;
				document.hotelSearchCriteriaForm.arrivalDay.options.selectedIndex = sd[1] - 1;
				//document.forms[0].arrivalYear.value = sd[2];			
				amadChangeAll(document.hotelSearchCriteriaForm, false);
			});
	}
	else {//find the first form that has selectable dates and not just hidden inputs
		for(var i = 0; i < document.hotelSearchCriteriaForm.length; i++) 
			if(typeof document.hotelSearchCriteriaForm[i].arrivalMonth.selectedIndex != 'undefined') {
				if(document.hotelSearchCriteriaForm[i].arrivalMonth.selectedIndex < currentMonth  || (document.hotelSearchCriteriaForm[i].arrivalMonth.selectedIndex == currentMonth && document.hotelSearchCriteriaForm[i].arrivalDay.selectedIndex + 1 < currentDay))
					theYear++;	
					
				holdAMonth = document.hotelSearchCriteriaForm[i].arrivalMonth.selectedIndex;
				holdAYear = theYear;
					
				aCalendar = new Epoch('arrivalCal', 
					'popup', 
					document.getElementById('arrivalCalendarValue'), 
					document.getElementById('arrivalCalendarImgHeader'), 
					false,
					document.hotelSearchCriteriaForm[i].arrivalMonth.selectedIndex,
					document.hotelSearchCriteriaForm[i].arrivalDay.selectedIndex,
					theYear,
					function() {
						var sd = document.getElementById('arrivalCalendarValue').value.split("\/");
						document.hotelSearchCriteriaForm[i].arrivalMonth.options.selectedIndex = sd[0] - 1;
						document.hotelSearchCriteriaForm[i].arrivalDay.options.selectedIndex = sd[1] - 1;
						//document.forms[i].arrivalYear.value = sd[2];			
						amadChangeAll(document.hotelSearchCriteriaForm[i], false);
					});
				break;
			}
	}
}
function loadDepartureCalendar () {			
	var theYear = currentYear;
	//see if there is only one form
	if(typeof document.hotelSearchCriteriaForm[0].departureMonth == 'undefined') {
		if(document.hotelSearchCriteriaForm.departureMonth.selectedIndex < currentMonth  || (document.hotelSearchCriteriaForm.departureMonth.selectedIndex == currentMonth && document.hotelSearchCriteriaForm.departureDay.selectedIndex + 1 < currentDay))
			theYear++;	
			
		holdDMonth = document.hotelSearchCriteriaForm.departureMonth.selectedIndex;
		holdDYear = theYear;
		
		dCalendar = new Epoch('departureCal', 
			'popup', 
			document.getElementById('departureCalendarValue'), 
			document.getElementById('departureCalendarImgHeader'), 
			false,
			document.hotelSearchCriteriaForm.departureMonth.selectedIndex,
			document.hotelSearchCriteriaForm.departureDay.selectedIndex,
			theYear,
			function() {
				var sd = document.getElementById('departureCalendarValue').value.split("\/");
				document.hotelSearchCriteriaForm.departureMonth.options.selectedIndex = sd[0] - 1;
				document.hotelSearchCriteriaForm.departureDay.options.selectedIndex = sd[1] - 1;
				//document.forms[0].departureYear.value = sd[2];
				dmddChangeAll(document.hotelSearchCriteriaForm, false);
			});
	}
	else {//find the first form that has selectable dates and not just hidden inputs
		for(var i = 0; i < document.hotelSearchCriteriaForm.length; i++)
			if(typeof document.hotelSearchCriteriaForm[i].departureMonth.selectedIndex != 'undefined') {
				if(document.hotelSearchCriteriaForm[i].departureMonth.selectedIndex < currentMonth  || (document.hotelSearchCriteriaForm[i].departureMonth.selectedIndex == currentMonth && document.hotelSearchCriteriaForm[i].departureDay.selectedIndex + 1 < currentDay))
					theYear++;	
					
				holdDMonth = document.hotelSearchCriteriaForm[i].departureMonth.selectedIndex;
				holdDYear = theYear;
				
				dCalendar = new Epoch('departureCal', 
					'popup', 
					document.getElementById('departureCalendarValue'), 
					document.getElementById('departureCalendarImgHeader'), 
					false,
					document.hotelSearchCriteriaForm[i].departureMonth.selectedIndex,
					document.hotelSearchCriteriaForm[i].departureDay.selectedIndex,
					theYear,
					function() {
						var sd = document.getElementById('departureCalendarValue').value.split("\/");
						document.hotelSearchCriteriaForm[i].departureMonth.options.selectedIndex = sd[0] - 1;
						document.hotelSearchCriteriaForm[i].departureDay.options.selectedIndex = sd[1] - 1;
						//document.forms[i].departureYear.value = sd[2];
						dmddChangeAll(document.hotelSearchCriteriaForm[i], false);
					});		
				break;
			}
	}
}

function loadCalendars() {
	loadArrivalCalendar();
	loadDepartureCalendar();
}

function amadChangeAll(theForm, changeCalendars) {
    if (!isBrowserSupp())
       	return;
       	
	var newDepartureDay = adjustDates(theForm.arrivalMonth, theForm.arrivalDay);
	if (newDepartureDay != 0) {
		theForm.departureDay.options.selectedIndex = newDepartureDay - 1;
        if (theForm.departureMonth.options.selectedIndex == 11)
        	theForm.departureMonth.options.selectedIndex = 0			
        else 
			theForm.departureMonth.options.selectedIndex = theForm.arrivalMonth.options.selectedIndex + 1;	        
    } 
    else {
    	theForm.departureMonth.options.selectedIndex = theForm.arrivalMonth.options.selectedIndex;
    	theForm.departureDay.options.selectedIndex = theForm.arrivalDay.options.selectedIndex + 2;
    }
    
    if(changeCalendars)    	
		changeArrivalCalendar(theForm);

	if(typeof dCalendar != 'undefined')
		changeDepartureCalendar(theForm);
	

	for(var i = 0; i < document.hotelSearchCriteriaForm.length; i++) 
		if(document.hotelSearchCriteriaForm[i] != theForm && typeof document.hotelSearchCriteriaForm[i].arrivalMonth != 'undefined' && typeof document.hotelSearchCriteriaForm[i].arrivalMonth.options != 'undefined') {
			document.hotelSearchCriteriaForm[i].arrivalMonth.options.selectedIndex = theForm.arrivalMonth.options.selectedIndex;
			document.hotelSearchCriteriaForm[i].arrivalDay.options.selectedIndex = theForm.arrivalDay.options.selectedIndex;
			//in case of nights drop down instead of the normal departure date
			if(typeof document.hotelSearchCriteriaForm[i].departureMonth != 'undefined' && typeof document.hotelSearchCriteriaForm[i].departureDay != 'undefined') {
				document.hotelSearchCriteriaForm[i].departureMonth.options.selectedIndex = theForm.departureMonth.options.selectedIndex;
				document.hotelSearchCriteriaForm[i].departureDay.options.selectedIndex = theForm.departureDay.options.selectedIndex;
			}
		}
}

function dmddChangeAll(theForm, changeCalendars) {
    if (!isBrowserSupp()) 
	    return;
    
	adjustDates(theForm.departureMonth, theForm.departureDay);
	if(changeCalendars)
		changeDepartureCalendar(theForm);

	for(var i = 0; i < document.hotelSearchCriteriaForm.length; i++) 
		if(document.hotelSearchCriteriaForm[i] != theForm && typeof document.hotelSearchCriteriaForm[i].departureMonth != 'undefined' && typeof document.hotelSearchCriteriaForm[i].departureMonth.options != 'undefined') {
			document.hotelSearchCriteriaForm[i].departureMonth.options.selectedIndex = theForm.departureMonth.options.selectedIndex;
			document.hotelSearchCriteriaForm[i].departureDay.options.selectedIndex = theForm.departureDay.options.selectedIndex;
		}
}

function changeArrivalCalendar(theForm) {
	var needsRedrawn = false;
	//determine if the new month & year are the same as the old ones
	if(theForm.arrivalMonth.options.selectedIndex < currentMonth  || 
		(theForm.arrivalMonth.options.selectedIndex == currentMonth && theForm.arrivalDay.options.selectedIndex + 1 < currentDay)) {
		if(holdAYear != currentYear +1 || holdAMonth != theForm.arrivalMonth.options.selectedIndex)  {
			holdAYear = currentYear + 1;
			needsRedrawn = true;
		}
	}
	else if(holdAYear != currentYear || holdAMonth != theForm.arrivalMonth.options.selectedIndex) {
		holdAYear = currentYear;
		needsRedrawn = true;
	}
	
	holdAMonth = theForm.arrivalMonth.options.selectedIndex;
	aCalendar.selectedDates = new Array(new Date(currentDate.setFullYear(holdAYear,holdAMonth,theForm.arrivalDay.options.selectedIndex+1)));
	
	//if month or year has changed calendar needs to be recreated for the new month/year
	if(needsRedrawn) { 
		aCalendar.monthSelect.value = aCalendar.displayMonth = holdAMonth;
		aCalendar.yearSelect.value = aCalendar.displayYear = holdAYear;

		//and refresh the calendar for the new month/year
		aCalendar.deleteCells();
		aCalendar.calCellContainer.appendChild(aCalendar.createCalCells());
	}
	else 
		aCalendar.reDraw();	
}

function changeDepartureCalendar(theForm) {
	var needsRedrawn = false;
	//determine if the new month & year are the same as the old ones
	if(theForm.departureMonth.options.selectedIndex < currentMonth  || 
		(theForm.departureMonth.options.selectedIndex == currentMonth && theForm.departureDay.options.selectedIndex + 1 < currentDay)) {
		if(holdDYear != currentYear + 1 || holdDMonth != theForm.departureMonth.options.selectedIndex)  {
			holdDYear = currentYear + 1;
			needsRedrawn = true;
		}
	}
	else if(holdDYear != currentYear || holdDMonth != theForm.departureMonth.options.selectedIndex) {
		holdDYear = currentYear;
		needsRedrawn = true;
	}
	
	holdDMonth = theForm.departureMonth.options.selectedIndex;
	dCalendar.selectedDates = new Array(new Date(currentDate.setFullYear(holdDYear,holdDMonth,theForm.departureDay.options.selectedIndex+1)));
	
	//if month or year has changed calendar needs to be recreated for the new month/year
	if(needsRedrawn) { 
		dCalendar.monthSelect.value = dCalendar.displayMonth = holdDMonth;
		dCalendar.yearSelect.value = dCalendar.displayYear = holdDYear;

		//and refresh the calendar for the new month/year
		dCalendar.deleteCells();
		dCalendar.calCellContainer.appendChild(dCalendar.createCalCells());
	}
	else 
		dCalendar.reDraw();	
}

function adjustDates(monthField, dayField) {
	var numDays;
	if(monthField.options.selectedIndex < currentMonth  || (monthField.options.selectedIndex == currentMonth && dayField.options.selectedIndex + 1 < currentDay)) 
		numDays = getDaysInMonth(monthField.options.selectedIndex, currentYear+1);
	else
		numDays = getDaysInMonth(monthField.options.selectedIndex, currentYear);

    if (dayField.options.selectedIndex + 2 < numDays) 
    	return 0;
    else if(dayField.options.selectedIndex + 1 > numDays) {
		monthField.options.selectedIndex++; //dont have to worry about december since it has 31 days
		dayField.options.selectedIndex -= numDays;
		return 0;
	}
	else
		return dayField.options.selectedIndex - numDays + 3; //dayField.options.selectedIndex + 1 - numDays + 2; //returns 1 or 2
}