
// Keep track of region chooser
// since it is a .net control, can't rely on the name to access it
var mRegionChooser;

// called when region int region chooser drop-down list is selected
function _onRegionSelect(pRegionChooser) {
   mRegionChooser = pRegionChooser;
   var oLocationID;
   if (mRegionChooser) oLocationID = mRegionChooser.value;
   if (!oLocationID || oLocationID == '') {
     $('CityLocalChooser').options.length = 0;
	 Element.hide('CityLocalChooser')
      return;
   }
   Element.show('CityLocalChooser');
   // Clear out the dropdowns
   $('CityLocalChooser').options.length = 0;
   // Insert some text to show that we're Loading...   
   $('CityLocalChooser').options[0] = new Option('Loading...', '');
   // Make ajax call to get city and local deal locations based on selected region
   AjaxTravelDealsBO.getCityLocalDealLocations(oLocationID, _onRegionSelectCallback);
}

// call back function, loads CityLocalChooser 
// w/ response locations from AJAX call to AjaxTravelDealsBO.getCityLocalDealLocations
function _onRegionSelectCallback(pResponse) {
   // Clear it out city local chooser
   $('CityLocalChooser').options.length = 0;
   
   // Add Default Item
   $('CityLocalChooser').options[0] = new Option('Choose a City or Area', '');
   $('CityLocalChooser').selectedIndex = 0;
   
   // Add All Deals Item
   var SelectedRegionID = mRegionChooser.value;
   var SelectedRegion = '';
   if (mRegionChooser) {
      var oSelectedRegionIndex = mRegionChooser.selectedIndex;
      oSelectedRegion = mRegionChooser.options[oSelectedRegionIndex].text + ' ';
   }
   $('CityLocalChooser').options[$('CityLocalChooser').options.length] = 
      new Option("All " + oSelectedRegion + "Deals", SelectedRegionID);
      
   // Add the locations
   var oLocations = pResponse.value;
   var oLocation;
	for (var i = 0; i < oLocations.length; i++) {
		oLocation = oLocations[i];
	   $('CityLocalChooser').options[$('CityLocalChooser').options.length] = 
         new Option(oLocation.LocationName, oLocation.LocationID);
	}
}

// called when the CityLocalChooser is selected
// makes ajax call to fill DealTypeChooser
function _onCityLocalSelect() {
   //Reset DealTypeDropdown
      
   // get out selected location id
   var oLocationID = $('CityLocalChooser').value;
   if (!oLocationID || oLocationID == '') {
      return;
   }
   
   // Insert some text to show that we're Loading...
   
   //Make callt o backend
   //AjaxTravelDealsBO.getLocationDealTypes(oLocationID, _onCityLocalSelectCallback); 
    _searchDeals();
}

// fills the DealTypeChooser w/ response from ajax call in _onCityLocalSelect
function _onCityLocalSelectCallback(pResponse) {
   //Clear out DealTypeChooser
   $('DealTypeChooser').options.length = 0;
   
   // Add Default Item
   $('DealTypeChooser').options[0] = new Option('All Deal Types', '');
   $('DealTypeChooser').selectedIndex = 0;
   
   // DealTypes
   var oDealTypes = pResponse.value;
   var oDealTypeEntry;
	for (var i = 0; i < oDealTypes.length; i++) {
		oDealTypeEntry = oDealTypes[i];
	   $('DealTypeChooser').options[$('DealTypeChooser').options.length] = 
         new Option(oDealTypeEntry.Value, oDealTypeEntry.Key);
	}
}

// Submits the form on change of CityLocal Select box
function _onDealTypeSelect() {
   _searchDeals();
}

// does the search based on what's selected in the dropdowns when the button is clicked
function _onSearchDealsButtonClick() {
   _searchDeals();
}

// Does the actual searching
function _searchDeals() {
   var oRequest; 
   var oLocationID = $('CityLocalChooser').value;
   

   //if LocationID is not set in the city chooser, go with the one in the Region, which is always index 1
   if(!oLocationID || oLocationID == ''){
     oLocationID = $('CityLocalChooser').options[1].value;   
   }
   
   // If we have a deal type, we go to destinationDealTypeDeals w/ DealType as a param
   // Else we go to destinationDeals w/ just LocationID
   /*if (oDealType && oDealType != '') {
      oRequest = new IgoUgoJS.Util.Request('/traveldeals/destinationDealTypeDeals.aspx');
      oRequest.addParam('DealType', oDealType);
   } else {*/
      oRequest = new IgoUgoJS.Util.Request('/traveldeals/destinationDeals.aspx');
   //}
   // Both get LocationID
   oRequest.addParam('LocationID', oLocationID);
   var oUrl = oRequest.toString();
   IgoUgoJS.Util.Redirect(oUrl);
}

function _onSearchTravelDealsLoad() {
   var oCurrentUrl = new IgoUgoJS.Util.Request(IgoUgoJSConfigExt.currentUrl).toBaseURL();
   var oUrlSplit = oCurrentUrl.toString().split('/');
   if (oUrlSplit.length > 0) {
      oCurrentUrl = oUrlSplit[oUrlSplit.length - 1];
   }
    
   if($('CityLocalChooser').options.length == 0)
	Element.hide('CityLocalChooser');
}

_onSearchTravelDealsLoad();