// Regular expression to match the geo url parameter of the form -gXXXX-, where X is a digit
var geoRegExp = new RegExp("-g" + "[0-9]+-");

// When a filter is selected in the HAC form, this method changes the HAC_FORM action
// with this filter's value.
function setFilter (name, val)
{
	if (name == 't')
	{
		// For SEO reasons, "recommended for" is no longer part of the URL and is
		// instead tracked in a cookie.
		setRecommendedCookie(val);
	}
	else
	{
		// Everything else is tracked in the URL, so update the HAC_FORM form action
   		var re = new RegExp("-" + name + "[0-9]+-");
   		var action = $('HAC_FORM').action;
   		if (action)
   		{
   			if (action.search(re) >= 0)
   			{
   				// the parameter is already in the from action, just replace its value
   				$('HAC_FORM').action = action.replace(re, "-" + name + val + "-");
   			}
   			else
   			{
	   			// the parameter is not already in the form action, so insert it after the geo (-gXXXXX-) parameter
	   			var index = action.search(geoRegExp);
	   			if (index >= 0)
	   			{
	   				// make sure to position the new parameter after the geo parameter's last dash
	   				var dashIndex = action.substring(index + 1).indexOf("-");
	   				if (dashIndex >= 0)
	   				{
	   				   	// add the new parameter
	   					var endOfGeoIndex = index + dashIndex + 2;
	   					action = action.substring(0, endOfGeoIndex) + name + val + "-" + action.substring(endOfGeoIndex);
	   					$('HAC_FORM').action = action;
	   				}
	   			}
   			}
   		}
   		
   		//if currency, update associated price widget
   		if (name == "zfk") {fillRatesAndClear(val, true); setFilter("zfp", "0");}
   	}
   
	return true;
}

function updateLightningCurrency(name, val)
{
  setFilter(name, val);
  fillRatesLightning(val, true); 
  setFilter("l1price", $('l1price').value);
}

var recommendedCookieName = 'TARecommendedFor';

function setRecommendedCookie(val)
{
	if ($('geoId'))
	{
		var geoId = $('geoId').value;
		Cookie.set(recommendedCookieName, val + ',' + geoId);
	}
}

function removeRecommendedCookie()
{
	Cookie.remove(recommendedCookieName);
}

function getFormAction(forHAC, stripParams)
{
   if (forHAC) return "/HACSearch?" + $('HAC_FORM').toQueryString();
   if (stripParams) {stripParams += "|";}
   stripParams += "-a_[^-]+|-q[^-]+";
   var s = $('HAC_FORM').action.replace(new RegExp(stripParams,"g"),"");
   return s.replace (/-a_[^-]+/g, "").replace (/\/HACSearch-/g, "/Hotels-");
}

function clickMoreLink()
{
        new Ajax("/ActionRecord?action=TOURISM_MORE").request();
        $('TOGGLE').removeClass('off');
}

function clickLessLink()
{
        $('TOGGLE').addClass('off');
}

