var MAX_STAY_LENGTH = 1000 * 60 * 60 * 24 * 30; // 30 days

// REPLACED WITH DUMMY COORDINATES FOR TESTING
var calendar; // Calendar;
var cr; // Check rates object

function selectImage(img_id, imgCount) {
	document.getElementById('mainImage').src = document.getElementById('tnImage_' + img_id).src;
	for(var i=0; i < imgCount; i++) {
		document.getElementById('tnCell_' + i).className = 'ImageTable';
	}
	document.getElementById('tnCell_' + img_id).className = 'SelectedImageTable';
}

function showCalendar(obj) {
	calendar.popup(obj, document.forms.CheckRatesForm.CheckInDate.value, document.forms.CheckRatesForm.CheckOutDate.value, calendarCallback);
}

function hideCalendar() {
	calendar.hide();
}

function calendarCallback(obj) {
	if(obj == document.forms.CheckRatesForm.CheckInDate) {
		showCalendar(document.forms.CheckRatesForm.CheckOutDate);
	}
	if(document.forms.HotelCheckRatesForm) {
		if(obj.form == document.forms.HotelCheckRatesForm) {
			document.forms.CheckRatesForm.CheckInDate.value = document.forms.HotelCheckRatesForm.CheckInDate.value;
			document.forms.CheckRatesForm.CheckOutDate.value = document.forms.CheckRatesForm.CheckOutDate.value;
		} 
		else {
			document.forms.HotelCheckRatesForm.CheckInDate.value = document.forms.CheckRatesForm.CheckInDate.value;
			document.forms.HotelCheckRatesForm.CheckOutDate.value = document.forms.CheckRatesForm.CheckOutDate.value;
		}
	}
}

function fixDateStr(obj) {
	var theDate = parseDate(obj.value);
	
	if(!theDate) {
		obj.value='';
	}
	else {
		obj.value = formatDate(theDate,"MM/dd/yyyy");
	}
}

function checkRates(theForm) {
	if(calendar) hideCalendar();
	var outDiv = document.getElementById("RatesDiv");
	var today = new Date();
	today = parseDate((today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear());
	
	if(!theForm.hotels_id) {
		if(theForm.city.value == '') {
			alert('Please enter a city name.');
			return;
		}
	}

	if(theForm.CheckInDate.value == ''  || theForm.CheckInDate.value == 'mm/dd/yyyy' || !calendar.isInitialized) {
		alert('Please enter a check-in date.');
		return;
	}
	
	if(theForm.CheckOutDate.value == '' || theForm.CheckOutDate.value == 'mm/dd/yyyy') {
		alert('Please enter a check-out date.');
		return;
	}

	if(parseDate(theForm.CheckInDate.value) == null) {
		alert('You have entered an invalid check-in date.');
		return;
	}

	if(parseDate(theForm.CheckOutDate.value) == null) {
		alert('You have entered an invalid check-out date. Please try again.');
		return;
	}
	
	if(parseDate(theForm.CheckInDate.value).getTime() >= parseDate(theForm.CheckOutDate.value).getTime()) {
		alert('Please select a check-out date that is after your check-in date');
		return;
	}
	
	if(parseDate(theForm.CheckInDate.value).getTime() < today.getTime()) {
		alert('Your selected dates are in the past. Please select a future date for your stay.');
		return;
	}
	
	if((parseDate(theForm.CheckOutDate.value).getTime() - parseDate(theForm.CheckInDate.value).getTime()) > MAX_STAY_LENGTH) {
		alert('Unfortunately we cannot book a stay longer than 30 days. Please select a shorter stay.');
		return;
	}
	
	for(var i=0; i < theForm.num_children.value; i++) {
		eval('var theInput = theForm.minorAge' + (i+1) + ';');
		if(theInput.value == 'N/A') {
			alert('Please specify the ages of all children');
			return;
		}
	}
	
	book(theForm); 
}

function checkRatesComplete() {
	hideTransition();
	selectTab(document.getElementById('RoomsAndRatesTab'));
	var rightAd = document.getElementById('RightAdDiv');
	if(rightAd) {
		rightAd.style.display='block';
	}
	theInput = document.getElementById('ResultsRadio_0');
	
	if(theInput != null) {
		theInput.click();
	}
}

function initPage() {
	updateWillOpen();
	showKidsAgeLayer(document.forms.CheckRatesForm,'kid');
}



function updateWillOpen() {
	var i=0;
	var count=0;
	var theInput = document.getElementById('CompareInput_' + i++);
	while(theInput != null) {
		if(theInput.checked) {
			count++;
		}
		theInput = document.getElementById('CompareInput_' + i++);
	}
	
	if(count > 1) {
		document.getElementById('WillOpenDiv').innerHTML = '(will open in ' + count + ' new windows)';
	}
	else if(count > 0) {
		document.getElementById('WillOpenDiv').innerHTML = '(will open in a new window)';
	}
	else {
		document.getElementById('WillOpenDiv').innerHTML = '';
	}
}

function launchCompareWindows() {
/*	var theForm = document.forms.CheckRatesForm;
	var today = new Date();
	var checkInDate = parseDate(theForm.CheckInDate.value);
	var checkOutDate = parseDate(theForm.CheckOutDate.value);
	
	var i=0;
	var theInput = document.getElementById('CompareInput_' + i++);
	var compareCounter = 0;
	while(theInput != null) {
		if(theInput.checked) {
			switch(theInput.value) {
				case "HotelsCom":
					window.open('http://www.hotels.com/processIndexSearch.do?usertypedcity=' 
					+ escape(city_name) 
					+ '%2C+' 
					+ escape(state_name) 
					+'%2C+' 
					+ escape(country_name) 
					+'&CIMonth=' 
					+ (checkInDate.getMonth()+1) 
					+ '&CIDay='
					+ (checkInDate.getDate()) 
					+ '&CIYear='
					+ (checkInDate.getFullYear())
					+'&COMonth=' 
					+ (checkOutDate.getMonth()+1) 
					+ '&CODay='
					+ (checkOutDate.getDate()) 
					+ '&COYear='
					+ (checkOutDate.getFullYear())
					+ '&numrooms='
					+ theForm.num_rooms.value
					+'&adults[0]='
					+ theForm.num_adults.value
					+'&child[0]='
					+ theForm.num_children.value
					+'&OIdx[0].CAIdx[0]=4', 'HotelsComWindow', 'height=undefined,,width=undefined,menubar=1,location=1,resizable=1,scrollbars=1,toolbar=1');
					compareCounter++;
					break;
				case "Hotwire":
					window.open('http://www.hotwire.com/hotel/search-options.jsp?inputId=hotel-results&destCity='
					+ escape(city_name) 
					+ ','
					+ escape(state_code)
					+ '&destAddressZipCode=&destAddressStreet=&destAddressCity='
					+ escape(city_name)
					+ '&destAddressState='
					+ escape(state_code)
					+ '&startDate='
					+ formatDate(checkInDate,"MM/dd/yyyy")
					+ '&endDate='
					+ formatDate(checkOutDate, "MM/dd/yyyy")
					+ '&noOfChildren='
					+ theForm.num_children.value
					+ '&noOfAdults='
					+ theForm.num_adults.value
					+'&numRooms='
					+ theForm.num_rooms.value, 'HotwireWindow', 'height=800,width=600,menubar=1,location=1,resizable=1,scrollbars=1,toolbar=1');
					compareCounter++;
					break;
				case "FHS":
					window.open('http://www.freehotelsearch.com/freehotelsearch_com/servlet/search?DoNewSearch=Y&CheckAvailability=Y&CityName=' 
					+ escape(city_name) 
					+ '&StateCode=' 
					+ escape(state_code) 
					+'&CountryCode=' 
					+ escape(country_code) 
					+'&CheckInDate=' 
					+ formatDate(checkInDate,"MM/dd/yyyy") 
					+'&CheckOutDate=' 
					+ formatDate(checkOutDate,"MM/dd/yyyy") 
					+ '&num_rooms='
					+ theForm.num_rooms.value
					+'&num_adults='
					+ theForm.num_adults.value
					+'&num_children='
					+ theForm.num_children.value
					+'&minorAge0='
					+ theForm.minorAge0.value
					+'&minorAge1='
					+ theForm.minorAge1.value
					+'&minorAge2='
					+ theForm.minorAge2.value
					+'&minorAge3='
					+ theForm.minorAge3.value, 'FHSWindow', 'height=undefined,,width=undefined,menubar=1,location=1,resizable=1,scrollbars=1,toolbar=1');
					compareCounter++;
					break;
			}
		}
		theInput = document.getElementById('CompareInput_' + i++);
	}
	window.focus();
	window.setTimeout(window.focus, 1000);
	return compareCounter;*/
}

function book(theForm) {
	var checkInDate = parseDate(theForm.CheckInDate.value);
	var checkOutDate = parseDate(theForm.CheckOutDate.value);
	theForm.doa_dd.value = checkInDate.getDate();
	theForm.doa_mm.value = checkInDate.getMonth() + 1;
	theForm.doa_yy.value = checkInDate.getFullYear();
	theForm.dod_dd.value = checkOutDate.getDate();
	theForm.dod_mm.value = checkOutDate.getMonth() + 1;
	theForm.dod_yy.value = checkOutDate.getFullYear();
	theForm.submit();
}

function getCompareHotelInputs() {	
	return '';
	var outStr = '<input type="checkbox" onclick="updateWillOpen()" value="HotelsCom" id="CompareInput_1" checked=""/> Hotels.com <input type="checkbox" onclick="updateWillOpen()" value="Hotwire" id="CompareInput_2" checked=""/> Hotwire';
	
	return outStr;
}

function generateCompareInput(theName, theText, num) {
	var tempDiv = document.createElement('div');
	var theInput = document.createElement('input');
	theInput.type = 'checkbox';
	theInput.checked='true';
	theInput.id="CompareInput_" + num;
	theInput.value=theName;
	theInput.onClick=function() {updateWillOpen();};
	tempDiv.appendChild(theInput);
	tempDiv.innerHTML += " " + theText + " ";
	return tempDiv.innerHTML;
}

function openPop(url)
{
   window.open(url,'ratings', 'width=550,height=600,titlebar=yes, scrollbars=yes');
  // window.open(url)
}

function makeAgeList(){
	document.write("<option value='N/A'>?</option>");
	for(i=0;i<18;i++){
		document.write("<option value='" + i +"'>"+ i + "</option>");
	}
}

function showKidsAgeLayer(frm, layerPrefix){
	 num = frm.num_children.options[frm.num_children.selectedIndex].value

  for(n=0;n<4;n++){
		hideLayer(layerPrefix+n)
	}

	for(i=0;i<num;i++){
		
		showLayer(layerPrefix+i);
	}

}

function toggleSearch() {
	if(document.getElementById('CheckDiv').style.display == 'none') {
		document.getElementById('HotelCheckDiv').style.display = 'none'
		document.getElementById('CheckDiv').style.display = 'block'
	} else {
		document.getElementById('CheckDiv').style.display = 'none'
		document.getElementById('HotelCheckDiv').style.display = 'block'
	}
}

calendar = new PopUpCal();
addEvent(window, 'load', initPage);