// javascript for display/hide the checkboxes
// on the whoIsGoing page
// based on the user selection of # of travellers

	// the number of travellers going has changed...
	function numberGoingChanged() {
	
		var numAdultsElem = document.getElementById("numAdults");
		var numSeniorsElem = document.getElementById("numSeniors");
		var numChildrenElem = document.getElementById("numChildren");
		
		if (numAdultsElem && numSeniorsElem && numChildrenElem) { 
			
			//alert("adults:" + numAdultsElem.value);
			//alert("seniors:" + numSeniorsElem.value);
			//alert("children:" + numChildrenElem.value);

			// counts
			var numAdults = parseInt(numAdultsElem.value) + parseInt(numSeniorsElem.value);
			var numChildren = parseInt(numChildrenElem.value);
			
			// fields
			var coupleRowElem = document.getElementById("rowCouple");
			var friendsRowElem = document.getElementById("rowFriends");
			var familyRowElem = document.getElementById("rowFamily");
			var groupRowElem = document.getElementById("rowGroup");
			var childTableElem= document.getElementById("childTable");
			
			// show/hide the couple/friends rows
			if (coupleRowElem && friendsRowElem) {
				//alert('numAdults: ' + numAdults);
				//var groupDisplay = (numAdults>1) ? "table-row" : "none";
				//alert('groupdisplay: ' + groupDisplay);
				
				var displayState = (numAdults>1) ? "" : "none";
				coupleRowElem.style.display = displayState;
				friendsRowElem.style.display = displayState;
			}
			else {
				alert('internal error: coupleRowElem or friendsRowElem elements not found');
			}

			// show/hide the family and group rows
			if (familyRowElem && groupRowElem) {
				//alert('numAdults: ' + numAdults);
				//var groupDisplay = (numAdults>2) ? "table-row" : "none";
				//alert('groupdisplay: ' + groupDisplay);
				
				var displayState = (numAdults>2) ? "" : "none";
				groupRowElem.style.display = displayState;
				
				// wait, any adults and any children could be family
				if (numChildren>0) {
					//alert('more than one child, overriding family');
					displayState = "";
				}
				familyRowElem.style.display = displayState;
			}
			else {
				alert('internal error: familyRowElem or groupRowElem elements not found');
			}
			
			// show/hide the children-related fields
			if (childTableElem) {
				var displayState  = (numChildren>0) ? "" : "none";
				childTableElem.style.display = displayState;
			}
			else {
				alert('internal error: childCol elem not found');
			}

		}
		else {
			alert('internal error, traveller count dropdowns not found');
		}
		
		
	}
	
// global script



function mapWindow(url) {

	var height = 800;
	var width = 950;
	if(screen.width == 800) {
		width = 800;
		height = 600;
	}
	//var launch = "menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,height=" + height + ",width=" + width;
	
	var launch = "menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=no,height=" + height + ",width=" + width;
	
    	var mapWin = window.open(url, 'mapPopup', launch);
    	if (window.focus) { mapWin.focus() }
}

function partner(url,linkCode) {
 	var newWin = window.open(url + "&linkCode=" + linkCode,"mywindow","toolbar=1,resizable=1,menubar=1,titlebar=1,location=1,status=1,scrollbars=1,width=790,height=400");
 	if (window.focus) newWin.focus();
}

function popup(url) {
 	var newWin = window.open(url,"mywindow","toolbar=1,resizable=1,menubar=1,titlebar=1,location=1,status=1,scrollbars=1,width=790,height=400");
 	if (window.focus) newWin.focus();
}

function popupMaximized(url) {
 	var newWin = window.open(url,"mywindow","toolbar=1,resizable=1,menubar=1,titlebar=1,location=1,status=1,scrollbars=1");
 	if (window.focus) {
 		newWin.focus();
		newWin.resizeTo (screen.availWidth, screen.availHeight);
		newWin.moveTo (0, 0) 		
	}
}


/*
This javascript deals with showing and hiding the widget layers
and making sure that only one is open at a time.
*/
//These vars track which widgets are open.
var isWidgetOpen=false;
var lastHiddenObject=null;
var lastShowObject = null;
var disableWidgetChecking = false;//When validation returns, a widget is open, so just disable checking completely.

function setVisibility(objectToHideID, objectToShowID) {
	
	
	if (disableWidgetChecking) return doHideShow(objectToHideID, objectToShowID);//Short Circuit.
	if (isWidgetOpen) {
	
	    if (lastHiddenObject == objectToShowID) { //Are we dealing with the same widget layer that was just hidden?
	    	isWidgetOpen = false;
	    	lastHiddenObject = null;//Alright, we closing the same one so let's reset everything.
	    	//alert("reset lastHiddenObject to null: "+lastHiddenObject);
	    	return doHideShow(objectToHideID, objectToShowID);
	    
	    }
	} else { //No widget was open so lets open one and track which one it was.
		lastHiddenObject = objectToHideID;
		lastShownObject = objectToShowID;
		isWidgetOpen = true;
		return doHideShow(objectToHideID, objectToShowID);
	}
}

function doHideShow(objectToHideId, objectToShowId) {
	hide(objectToHideId);
	show(objectToShowId);
    return true;
}

function changeVisibility(objId, state) {
    var object = document.getElementById(objId);
    if (object) {
	    object.style.display = state;
    }
}

function hide(objectToHideId) {
	changeVisibility(objectToHideId, "none");
	return true;
}

function show(objectToShowId) {
	changeVisibility(objectToShowId, "block");
	return true;
}

function countSelected(frm) {

	try {

		var selectCount = 0;

		for(i=0;i<frm.elements.length;i++) {

			var elem = frm.elements[i];
			if(elem.type == 'checkbox' && elem.checked)	 
				selectCount++;		

		}

      return selectCount;

	} catch(e) {
		return true;
	}


}

function checkForSelection(frm) {

	var selectCount = countSelected(frm);
	if(selectCount != 0) return true;
	
	alert('Selection required');
	
	return false;

}

function checkSingleSelect(frm) {


	try {

		var selectCount = countSelected(frm);

		if(selectCount == 1) return true;

		if(selectCount == 0) {
			alert('Select 1 item');
		} else if(selectCount > 1) {
			alert('Only 1 selection is permitted');				
		}		
		return false;


	} catch(e) {
		return true;
	}
}

function selectAll(frm) {

	try {

		for(i=0;i<frm.elements.length;i++) {

			var elem = frm.elements[i];
			if(elem.type == 'checkbox')	 
				elem.checked = !elem.checked;
		}

	} catch(e) {
		alert('ERROR: ' + e);
	}

}

function swapImage(imageId, src) {
	var img = document.getElementById(imageId);
	if (img) img.src = src;
}



function selectHotelImage(newImage) {

	var img = document.getElementById("smallHotelImage");
	var img2 = document.getElementById(newImage);
	img.innerHTML = img2.innerHTML;

}

function checkExpediaAvailability(frm) {
	

	var indate = frm.indate.value;
	var outdate = frm.outdate.value;

	if(indate == '' || indate == 'mm/dd/yyyy' || outdate == '' || outdate == 'mm/dd/yyyy') {
		alert('Select valid dates to check availability');
		return false;
	}
	
	var url = frm.url.value + '&indate=' + indate + '&outdate=' + outdate + '&type=r';
	partner(url,frm.linkCode);
	return false;
	
}

function Partner(name, displayName, emailDomain, partnerAppId, privateLabel) {
	this.name = name;
	this.displayName = displayName;
	this.emailDomain = emailDomain;
	this.partnerAppId = partnerAppId;
	this.privateLabel = privateLabel;
}

