//User selected US city from drop down, so need to reset International City drop down to default
function us_onchange(usCity)
{
	if(document.forms["EventsActionForm"] != null)
	{
		if(!((usCity.value == "")||(usCity.value == null)))
		{
			document.forms["EventsActionForm"].intlCity.selectedIndex = 0;
		}
	}	
}

//User selected International city from drop down, so need to reset US City drop down to default
function intl_onchange(intlCity)
{
	if(document.forms["EventsActionForm"] != null)
	{
		if(!((intlCity.value == "")||(intlCity.value == null)))
		{
			document.forms["EventsActionForm"].usCity.selectedIndex = 0;
		}
	}		
}

// Sets the cityId to USCity or International City, which is passed to the URL
function setCityInUrl(){
	var selectedCity='';

	if (document.forms["EventsActionForm"].usCity != null && document.forms["EventsActionForm"].usCity.selectedIndex == 0 && document.forms["EventsActionForm"].intlCity != null && document.forms["EventsActionForm"].intlCity.selectedIndex == 0){
		return;
	}
	else if (document.forms["EventsActionForm"].intlCity != null && document.forms["EventsActionForm"].intlCity.selectedIndex == 0){
		selectedCity = document.forms["EventsActionForm"].usCity.value;
	}
	else if (document.forms["EventsActionForm"].usCity != null && document.forms["EventsActionForm"].usCity.selectedIndex == 0){
		selectedCity = document.forms["EventsActionForm"].intlCity.value;
	}
	
	//Checking that selectedCity is set to USCity or InternationalCity and there is cityId element
	if(selectedCity != '' && document.getElementById("cityId")){
		var hiddenInput = document.getElementById("cityId");
		hiddenInput.setAttribute("value", selectedCity);
	}
}

//User selected US State from drop down, so need to reset country drop down to default
function usstate_onchange(state)
{
	if(document.forms["CityInsidersActionForm"] != null)
	{
		if(!(document.forms["CityInsidersActionForm"].nation.selectedIndex == 0))
		{
			document.forms["CityInsidersActionForm"].nation.selectedIndex = 0;
		}	
	}	
}

//User selected country from drop down, so need to reset US State drop down to default
function nation_onchange(nation)
{
	if(document.forms["CityInsidersActionForm"] != null)
	{
		if(!(document.forms["CityInsidersActionForm"].state.selectedIndex == 0))
		{
			document.forms["CityInsidersActionForm"].state.selectedIndex = 0;
		}
	}
}

//Validates the City Insiders drop down
//If the user clicks on "Go" without selecting a US State or International Country, then error message is displayed
function validateCityInsidersForm()
{
	if(document.forms["CityInsidersActionForm"] != null)
	{
		if(document.forms["CityInsidersActionForm"].state != null && document.forms["CityInsidersActionForm"].state.selectedIndex == 0 && document.forms["CityInsidersActionForm"].nation != null && document.forms["CityInsidersActionForm"].nation.selectedIndex == 0)
		{
			var errorMessage = "We are sorry. We could not submit your search. Please correct the following and try again. \nPlease select a US State or Country from the drop down.";
			alert(errorMessage);
			return false;
		}
		else
		{
			return true;
		}	
	}
}

//Validates the Events form drop down
//If the user clicks on "Go" without selecting a US City or International Country, then error message is displayed
function validateEventsForm()
{
	if(document.forms["EventsActionForm"] != null)
	{
		var eventDateObj = {
			dateFormatPattern: document.forms['EventsActionForm'].dateFormatPattern.value.toLowerCase(),
			fromDate: document.forms['EventsActionForm'].fromDate,
			toDate: document.forms['EventsActionForm'].toDate,
			minDate: document.forms['EventsActionForm'].minDate,
			maxDate: document.forms['EventsActionForm'].maxDate
		}
		// Set the cityId in the URL for the selected city
		setCityInUrl();
		
		if(document.forms["EventsActionForm"].usCity != null && document.forms["EventsActionForm"].usCity.selectedIndex == 0 && document.forms["EventsActionForm"].intlCity != null && document.forms["EventsActionForm"].intlCity.selectedIndex == 0)
		{
			var errorMessage = "We are sorry. We could not submit your search. Please correct the following and try again. \nPlease select a US City or International City from the drop down.";
			alert(errorMessage);
			return false;
		}
		else
		{
			return validateEventsFormDates(eventDateObj);
		}	
	}
}

//Validates the AroundTownForm form drop down
//If the user clicks on "Go" without selecting a category, then error message is displayed
function validateAroundTownForm()
{
	if(document.forms["AroundTownForm"] != null)
	{
		if(document.forms["AroundTownForm"].category != null && document.forms["AroundTownForm"].category.selectedIndex == 0)
		{
			var errorMessage = "We are sorry. We could not submit your search. Please correct the following and try again. \nPlease select a category from the drop down.";
			alert(errorMessage);
			return false;
		}
		else
		{
			return true;
		}
	}
}

//Validates the dates selected on the Events form
function validateEventsFormDates(eventDateObj)
{	
	var errMessage = "We're sorry. We couldn't submit your search. Please correct the following and try again. \nYour ";
	var dateFormatPattern = eventDateObj.dateFormatPattern;

	var fromDateObj = eventDateObj.fromDate;	
	if(fromDateObj != null && (fromDateObj.value == null || fromDateObj.value == "") ){
		errMessage += "From date cannot not be null " ;
		alert(errMessage);
		fromDateObj.focus() ;
		return false ;
	}else if(!isDateValid(fromDateObj.value,dateFormatPattern)){
		errMessage += "From date is invalid. Please check the date. " ;
		alert(errMessage);
		fromDateObj.focus() ;
		return false; 
	}
	
	var toDateObj = eventDateObj.toDate;	
	if(toDateObj != null && (toDateObj.value == null || toDateObj.value	 == "") ){
		errMessage += "To date cannot not be null " ;
		alert(errMessage) ;
		toDateObj.focus() ;
		return false ;
	}else if(!isDateValid(toDateObj.value,dateFormatPattern)){
		errMessage += "To date is invalid. Please check the date. " ;
		alert(errMessage);
		toDateObj.focus() ;
		return false; 
	}

	//Validate that fromDate and toDate are not less than the min date
	var minDate = eventDateObj.minDate.value ;
	if(isDateLessThanDate1(fromDateObj.value,formatDate(minDate,dateFormatPattern),dateFormatPattern)){
		errMessage += "beginning search date cannot be less than "+formatDate(minDate,dateFormatPattern);
		alert(errMessage);
		fromDateObj.focus() ;
		return false;
	}

	if(isDateLessThanDate1(toDateObj.value,formatDate(minDate,dateFormatPattern),dateFormatPattern)){
		errMessage += "ending search date cannot be less than "+formatDate(minDate,dateFormatPattern);
		alert(errMessage);
		toDateObj.focus() ;
		return false;
	}

	//Validate that fromDate and toDate are not greater than the max date
	var maxDate = eventDateObj.maxDate.value ;
	if(isDateGreaterThanDate1(fromDateObj.value,formatDate(maxDate,dateFormatPattern),dateFormatPattern)){
		errMessage += "beginning search date cannot be more than "+formatDate(maxDate,dateFormatPattern);
		alert(errMessage);
		fromDateObj.focus() ;
		return false;
	}

	if(isDateGreaterThanDate1(toDateObj.value,formatDate(maxDate,dateFormatPattern),dateFormatPattern)){
		errMessage += "ending search date cannot be more than "+formatDate(maxDate,dateFormatPattern);
		alert(errMessage);
		toDateObj.focus() ;
		return false;
	}

	//Validate that fromDate is before toDate
	if(isDateLessThanDate1(toDateObj.value,fromDateObj.value,dateFormatPattern))
	{
		errMessage += " beginning search date must come before your ending search date."
		alert(errMessage);
		fromDateObj.focus() ;
		return false;
	}
	return true;
}

//this function is used for the printer friendly pop up window
function WinOpen(sURL, sName, intTop, intLeft, intWidth, intHeight)
{
	var windowprops;
	alert(sURL);
	if (null == sURL) {
		alert("ERROR: WinOpen() requires a URL");
		return;
	}
	if (null == sName)
		sName = '';
	if (null == intTop)
		intTop = 200;
	if (null == intLeft)
		intLeft = 200;
	if (null == intWidth)
		intWidth = 200;
	if (null == intHeight)
		intHeight = 200;
	windowprops = "location=no,scrollbars=yes,menubar=no,toolbar=no,locationbar=no,resizable=yes,alwaysRaised=yes,height="+intHeight+",width="+intWidth+",left="+intLeft+",top="+intTop;
	return window.open(sURL,sName,windowprops);
}

function getCategories() {
	var categoryString = "";

	for (i=0;i<12;i++){
		if (document.map.elements[i].checked){
			categoryString += document.map.elements[i].value;
		}
	}

	categoryString = categoryString.substring(0,categoryString.length-1);
	document.map.categoryList.value = categoryString;
	return true;

}

function doZoomIn() {
	getCategories();
	document.map.newZoomLevel.value = parseInt(document.map.newZoomLevel.value) - 1;
	document.map.submit();
	return true;
}

function doZoomOut() {
	getCategories();
	document.map.newZoomLevel.value = parseInt(document.map.newZoomLevel.value) + 1;
	document.map.submit();
	return true;
}

function doZoom(zoomIndex) {
	getCategories();
	document.map.newZoomLevel.value = parseInt(zoomIndex);
	document.map.submit();
	return true;
}

function doPan(direction) {
	getCategories();
	document.map.panDirection.value = direction;
	document.map.submit();
	return true;
}



//Show the attraction info container
function displayDetails(xPos, yPos, name, category, address, city, state, zip, phone, url){
	// Create a container which displays the info
	var info = document.createElement("div");
	info.className = "attractionInfo";
	
	if (xPos == 0 || yPos == 0){
		info.style.position = "relative";	
	}
	
	// Create a list
	var list = document.createElement("ul");

	var item1, item2, item3, item4, item5;
	item1 = document.createElement("li");
	item2 = document.createElement("li");
	item3 = document.createElement("li");
	item4 = document.createElement("li");
	item5 = document.createElement("li");
	
	// Piece the header tags together
	var header = document.createElement("p");
	var link = document.createElement("a");
	var image = document.createElement("img");
	
	image.setAttribute("src", "http://cache.marriott.com/images/icons/external_icon.gif");			
	link.setAttribute("href", url);
	link.setAttribute("target", "_blank");
	var attraction = document.createTextNode(name);
	link.appendChild(attraction);
	header.appendChild(link);
	header.appendChild(image);
	
	// Add the header to the container, then add the list to keep correct order
	info.appendChild(header);
	info.appendChild(list);
	
	// Create a new list item
	var category = document.createTextNode(category);
	item2.appendChild(category);
	list.appendChild(item2);
	
	// Create a new list item
	var address = document.createTextNode(address);
	item3.appendChild(address);
	list.appendChild(item3);
	
	// Create a new list item
	if (state != "") {
		if (zip !=  "" ) {
			var cityStateZip = document.createTextNode(city + ", " + state + ", " + zip);
		} else {
			var cityStateZip = document.createTextNode(city + ", " + state);
		}
	} else {
			var cityStateZip = document.createTextNode(city );
	}
	item4.appendChild(cityStateZip);
	list.appendChild(item4);
	
	// Create a new list item
	var phone = document.createTextNode(phone);
	item5.appendChild(phone);
	list.appendChild(item5);
	
	// Add the new node to the page body
	document.body.appendChild(info);
	
	// Continue displaying window if user navigates on info box
	info.onmouseover = function(){
		info.style.display = "block";
	}
	
	// Calculate popup height
	var divHeight;
    if(info.offsetHeight){
    	divHeight = info.offsetHeight;
    }
    else if(info.style.pixelHeight){
       	divHeight = info.style.pixelHeight;
    }
    
    // Determine the position of the info container
	info.style.left = (xPos + 0) + "px";
	info.style.top = (yPos - divHeight) + "px";
	
}
// Hide info when user navigates away from attraction icon
function hideDetails(){
	var divs = document.getElementsByTagName('div');
	
	for(var i = 0; i < divs.length; i++){
		if((divs[i].className) == "attractionInfo"){
			divs[i].style.display = "none";
		}
	}
}

// Hide roll-over popup containers on the map
function clearPopups(){
	var browserName = navigator.appName;
	var ie = "Microsoft Internet Explorer";
	
	if (browserName != ie){
		hideDetails();	
	}
		
}

// Resize image to fit required dimensions; This prevents layout from breaking;
function resize(pageContainerId, imageClass, x, y){
	
	// Perform check to prevent function from being loaded on non-relevant pages
	if (!document.getElementById(pageContainerId))
		return;
		
	var desiredWidth = x;
	var desiredHeight = y;
	var imageHeight, imageWidth, xFraction, yFraction, newWidth, newHeight, image, imgs;
	
	// get all img elements in the document
    imgs = document.getElementsByTagName('img');
	
     // iterate over all img elements in the document
     for(var i = 0; i < imgs.length; i++){
         
          // make collection with img elements with class attribute imageClass passed in function call
          if(imgs[i].className.match(imageClass)){
	        image = imgs[i];  
	          
			imageHeight = image.clientHeight;
			imageWidth = image.clientWidth;
			yFraction = imageHeight / desiredHeight;
			xFraction = imageWidth / desiredWidth;
			if (xFraction > 0 || yFraction > 0){
				if (xFraction > yFraction){
						newWidth = imageWidth / xFraction;
						newHeight = imageHeight / xFraction;
				}
				else{
						newWidth = imageWidth / yFraction;
						newHeight = imageHeight / yFraction;
				}
			}
			else{
				if (xFraction < yFraction){
						newWidth = imageWidth * xFraction;
						newHeight = imageHeight * xFraction;
				}
				else{
						newWidth = imageWidth * yFraction;
						newHeight = imageHeight * yFraction;
				}
			}
			image.style.width = newWidth + "px";
			image.style.height = newHeight + "px";
		}
	}
}
