var checkPanelActivatedOnce = false;
var availHotelRegistry = [];

function setDivInvisible(divid)
{
	document.getElementById(divid).style.visibility = "hidden";
}

function setDivVisible(divid, iwant, hid)
{
	window.location = "#login1";
	document.getElementById('iwant').value = iwant;
	document.getElementById('HotelID').value = hid;
	document.getElementById(divid).style.visibility = "visible";
}

function ViewAvailability(HID) {
	if(document.getElementById('AvailabilityDates'+HID).style.visibility=='hidden') {
		document.getElementById('AvailabilityDates'+HID).style.position = (HID=='All'?'relative':'relative');
		document.getElementById('AvailabilityDates'+HID).style.visibility = 'visible';
		document.getElementById('AvailabilityDates'+HID).style.height = 'auto';
		document.getElementById('AvailabilityDates'+HID).style.width = '95%';
		document.getElementById('view_avail_text'+HID).innerHTML = 'Hide availability options<br>';
		
		checkPanelActivatedOnce = true;
	} else {
		document.getElementById('AvailabilityDates'+HID).style.visibility = 'hidden';
		document.getElementById('AvailabilityDates'+HID).style.position = 'absolute';
		document.getElementById('AvailabilityDates'+HID).style.height = '0px';
		document.getElementById('AvailabilityDates'+HID).style.width = '0px';
		document.getElementById('view_avail_text'+HID).innerHTML = (HID=='All'?'You can check availability for some of the hotels in this list<br>':'Check availability');
	}
}

function ActivateViewAvailInit(HotelID) {
	arrival = document.getElementById('arrival_date'+HotelID).value;
	departure = document.getElementById('departure_date'+HotelID).value;

	if(arrival == '' || departure == '') {
		alert("Please fill in both dates.");
	} else if (HotelID == '') {
		document.getElementById('availCheckStatusAll').innerHTML = 'Checking availability based on dates, this may take a moment...';
		setTimeout("ActivateViewAvail('')",500);
	} else {
		document.getElementById(HotelID+'AvailLamp').innerHTML = 'Checking...';
		setTimeout('ActivateViewAvail('+HotelID+')',500);
	}
}

function ActivateViewAvail(HotelID) {
	var loop_a, is_available, arrival, departure;
	
	arrival = document.getElementById('arrival_date'+HotelID).value;
	departure = document.getElementById('departure_date'+HotelID).value;

	if(arrival == '' || departure == '') {
		alert("Please fill in both dates.");
	} else if (HotelID == '') {
		// Checks all hotels in registry
		
		// Looping through online hotel registry created during ASP retrieval
		for (loop_a=0; loop_a<availHotelRegistry.length; loop_a++) {
			
			// Update all date boxes with the dates we are searching with
			document.getElementById('arrival_date'+availHotelRegistry[loop_a][0]).value = arrival;
			document.getElementById('departure_date'+availHotelRegistry[loop_a][0]).value = departure;
			
			// Check availability for the hotels and update indicators
			if (availHotelRegistry[loop_a][3] == '') {
				// Wyndham Rewards
				is_available = CheckAvailability('WR1',arrival,departure,availHotelRegistry[loop_a][1],availHotelRegistry[loop_a][2],'');
				
				if (is_available == 1) {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:green">Yes</span>';
				} else {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:red">No</span>';
				}
				
			} else if (availHotelRegistry[loop_a][3] == 'BONLINE') {
				// HEI online booking
				is_available = CheckAvailability('BONLINE',arrival,departure,'','',availHotelRegistry[loop_a][0]);
				
				if (is_available == 1) {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:green">Yes</span>';
				} else {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:red">No</span>';
				}
			} else {
				// API
				is_available = CheckAvailability('API',arrival,departure,'','',availHotelRegistry[loop_a][3]);
				
				if (is_available == 1) {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:green">Yes</span>';
				} else {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:red">No</span>';
				}
			}
			
		}
		
		document.getElementById('availCheckStatusAll').innerHTML = 'See availability below for hotels that are available for online booking.';
		
	} else {
		// Check only a specific hotel
		
		// Looping through online hotel registry created during ASP retrieval
		for (loop_a=0; loop_a<availHotelRegistry.length; loop_a++) {
			
			// Check availability for the hotels and update indicators
			if (availHotelRegistry[loop_a][3] == '' && availHotelRegistry[loop_a][0] == HotelID) {
				// Wyndham Rewards
				is_available = CheckAvailability('WR1',arrival,departure,availHotelRegistry[loop_a][1],availHotelRegistry[loop_a][2],'');
				
				if (is_available == 1) {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:green">Yes</span>';
				} else {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:red">No</span>';
				}
				
			} else if (availHotelRegistry[loop_a][3] == 'BONLINE' && availHotelRegistry[loop_a][0] == HotelID) {
				// HEI Online Booking
				is_available = CheckAvailability('BONLINE',arrival,departure,'','',availHotelRegistry[loop_a][0]);
				
				if (is_available == 1) {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:green">Yes</span>';
				} else {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:red">No</span>';
				}
				
			} else if (availHotelRegistry[loop_a][3] != '' && availHotelRegistry[loop_a][0] == HotelID) {
				// API
				is_available = CheckAvailability('API',arrival,departure,'','',availHotelRegistry[loop_a][3]);
				
				if (is_available == 1) {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:green">Yes</span>';
				} else {
					document.getElementById(availHotelRegistry[loop_a][0]+'AvailLamp').innerHTML = 'Available: <span style="color:red">No</span>';
				}
			}
			
		}
		
	}
}

function CheckAvailability(checkType,arrival,departure,TRid,PBid,APIdata) {
	var xmlHttp, returnVal, arrDate, depDate;

	// Create date arrays
	arrDate = new Array();
	depDate = new Array();
	arrDate = arrival.split("/");
	depDate = departure.split("/");
	
	// Create a non-cache seed
	var currentTime = new Date();
	var nocacheseed = currentTime.getTime();

	// Prepare the AJAX
	if(window.XMLHttpRequest) {
		xmlHttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} else {
		alert("Your browser does not support this map system (AJAX powered).");
		return false;
	}
	
	if(checkType=='WR1') {
		// Wyndham Reward hotels
		
		// Get availability status
		xmlHttp.open("GET","hotel-map-search/CheckAvailability.asp?w=WR1&checkInDate="+arrDate[1]+"/"+arrDate[0]+"&checkOutDate="+depDate[1]+"/"+depDate[0]+"&id="+TRid+"&propBrandId="+PBid+"&nc="+nocacheseed,false);
		xmlHttp.send(null);
		returnVal = xmlHttp.responseText;
		
	} else if(checkType=='API') {
		// API hotels
		
		// Split up and assign APIdata values
		var ratecode,chain,citycode,hotel;
		ratecode = APIdata.substring(0,3);
		chain = APIdata.substring(4,6);
		citycode = APIdata.substring(6,9);
		hotel = APIdata.substring(9);
		
		// Fix ratecode when it's GDS
		if(ratecode=='gds' || ratecode=='GDS') {
			ratecode = 'HEI';
		}
		
		// Get availability status
		xmlHttp.open("GET","hotel-map-search/CheckAvailability.asp?w=API&chain="+chain+"&citycode="+citycode+"&hotel="+hotel+"&ratecode="+ratecode+"&arrivaldd="+arrDate[0]+"&arrivalmm="+arrDate[1]+"&arrivalyy="+arrDate[2]+"&departdd="+depDate[0]+"&departmm="+depDate[1]+"&departyy="+depDate[2]+"&nc="+nocacheseed,false);
		xmlHttp.send(null);
		returnVal = xmlHttp.responseText;
		
	} else if(checkType=='BONLINE') {
		// HEI online booking hotels, for hotels using hoteladmin.

		// Get availability status
		xmlHttp.open("GET","hotel-map-search/CheckAvailability.asp?w=BONLINE&hid="+APIdata+"&arrivaldd="+arrDate[0]+"&arrivalmm="+arrDate[1]+"&arrivalyy="+arrDate[2]+"&departdd="+depDate[0]+"&departmm="+depDate[1]+"&departyy="+depDate[2]+"&nc="+nocacheseed,false);
		xmlHttp.send(null);
		returnVal = xmlHttp.responseText;
	}
	
	if(returnVal==1 || returnVal==0) {
		return returnVal;
	} else {
		return 2;
	}
}

function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}



function priceInfoView(mouseState,which) {
	if(mouseState) {
		document.getElementById('priceInfoLayer'+which).style.visibility = 'visible';
		document.getElementById('priceInfoLayer'+which).style.height = 'auto';		
	} else {
		document.getElementById('priceInfoLayer'+which).style.visibility = 'hidden';
		document.getElementById('priceInfoLayer'+which).style.height = '0px';		
	}
}

function priceSeasonalView(mouseState, divId) {
	if(mouseState) {		
		document.getElementById('priceSeasonalLayer'+divId).style.visibility = 'visible';
		document.getElementById('priceSeasonalLayer'+divId).style.height = 'auto';		
	} else {		
		document.getElementById('priceSeasonalLayer'+divId).style.visibility = 'hidden';
		document.getElementById('priceSeasonalLayer'+divId).style.height = '0px';		
	}
}

function moreSearchOptions() {
	if(document.getElementById('moreSearchOptionsLayer').style.visibility == 'hidden') {
		document.getElementById('moreSearchOptionsLayer').style.visibility = 'visible';
		document.getElementById('moreSearchOptionsLayer').style.position = 'relative';
		document.getElementById('moreSearchOptionsLayer').style.height = 'auto';
		document.getElementById('moreSearchOptionsLayer').style.width = '99%';
		document.getElementById('searchOptionsLink').innerHTML = 'Disable advanced search';
		document.getElementById('fMoreSearchOptions').value = '1';
	} else {
		document.getElementById('moreSearchOptionsLayer').style.visibility = 'hidden';
		document.getElementById('moreSearchOptionsLayer').style.position = 'absolute';
		document.getElementById('moreSearchOptionsLayer').style.height = '0px';
		document.getElementById('moreSearchOptionsLayer').style.width = '0px';
		document.getElementById('searchOptionsLink').innerHTML = 'Enable advanced search';
		document.getElementById('fMoreSearchOptions').value = '0';
	}
}

function checkUSAJump() {
	if(document.CSCForm.CountrySelect.value == 132) {
		window.location = 'http://www.hotel-express.com/CC_USA.asp';
	} else {
		document.CSCForm.submit();
	}
}
