/**
 * Validate the entered attraction city.  If valid then create and navigate to the URL for the specified attraction.
 */
function attractionFormOnSubmit()
{
    var url = $('attractionCityUrl').value;
    if (url == '') 
    {
        // TODO: Maybe we do have a match in the list and they typed it or did a copy/paste?
        alert(JS_choose_valid_city);
        return false;
    }
    
    // Find selected radio for category parameter.
    var radios = $$('input[name=c]');
    var category = "0";
    for(y=0;y<radios.length;y++){
        if (radios[y].checked) {
            category = radios[y].value;
            break;
        }
    }
    if (category != "0") {
        url = url.replace("-Activities-","-Activities-c"+category+"-");
    }

    document.location.href = url 
    return false;
}

/**
 * Selects 'all attractions' and disables radio buttons if broad, and enables all radio buttons if not broad.
 */
function updateAttractionCategory(broad)
{
    var radios = $$('input[name=c]');
    
    if (broad) {
        radios[0].checked = true;
        for(y=0;y<radios.length;y++){
            radios[y].disabled = true;
        }
    }
    else {
        for(y=0;y<radios.length;y++){
            radios[y].disabled = false;
        }
    }
}

/**
 * Validate the entered restaurant city.  If valid then create and navigate to the URL for the specified attraction.
 */
function restaurantFormOnSubmit()
{
    var url = $('restaurantCityUrl').value;
    if (url == '') 
    {
        // TODO: Maybe we do have a match in the list and they typed it or did a copy/paste?
        alert(JS_choose_valid_city);
        return false;
    }
    
    document.location.href = url;
    return false;
}

/**
 * Process the Cruise Critic Cruise Review form from the Plan the Perfect Trip Cruise Critic Tab
 *
 * expects a Cruise Critc CruiseLineId to be selected - redirect to the Cruise Critic site
 * with this Id.
 */
function cruiseCriticReviewFormOnSubmit()
{
    var CRUISE_CRITIC_DEFAULT_COUNTY = 'us';
    var CRUISE_CRITIC_URL_BASE_MAP = new Object();
    CRUISE_CRITIC_URL_BASE_MAP['us'] = "/ShowUrl-a_url.http%3A__2F____2F__www__2E__cruisecritic__2E__com__2F__reviews__2F__cruiseline__2E__cfm__3F__CruiseLineID%3D";
    CRUISE_CRITIC_URL_BASE_MAP['uk'] = "/ShowUrl-a_url.http%3A__2F____2F__www__2E__cruisecritic__2E__co__2E__uk__2F__reviews__2F__cruiseline__2E__cfm__3F__CruiseLineID%3D";
    var CRUISE_CRITIC_URL_TAIL = ".html";
    var cruiseLineSelectBox = $('cruiseLineReview');
    var cruiseCriticCountry = $('cruiseCriticCountry').value;
    if (cruiseLineSelectBox.selectedIndex == 0)
    {
        alert(JS_select_a_cruise_line);
        return false;
    }

    var cruiseLineId = cruiseLineSelectBox.options[cruiseLineSelectBox.selectedIndex].value;

    //Build the URL
    // the last 5 chars are the .html added by $util.encode() when the velocity file is processed
    var countryUrlBaseMap = CRUISE_CRITIC_URL_BASE_MAP[cruiseCriticCountry]
    if (countryUrlBaseMap == undefined)
    {
        countryUrlBaseMap = CRUISE_CRITIC_URL_BASE_MAP[CRUISE_CRITIC_DEFAULT_COUNTY];
    }
    var cruiseLineReviewUrl = countryUrlBaseMap + cruiseLineId + CRUISE_CRITIC_URL_TAIL;
    //alert("jump To:" + cruiseLineReviewUrl);
    window.open(cruiseLineReviewUrl);
    return false;
}

ajaxRules['input.attractionTypeAhead'] = function(elmt) {
  if (action = elmt.className.match(/\bact(\w+)\b/)) {
    new Autocompleter.Ajax.Json2(elmt, "/TypeAheadJson?action="+action[1], {
      ajaxOptions: {method:'get'},
      postVar: 'query',
      inheritWidth: false,
      onSelect: function(elmt, resObj) {
        $('attractionCityUrl').value = resObj.url;
        updateAttractionCategory(resObj.broad);
      }
    });
  }
}

ajaxRules['input.restaurantTypeAhead'] = function(elmt) {
  if (action = elmt.className.match(/\bact(\w+)\b/)) {
    new Autocompleter.Ajax.Json2(elmt, "/TypeAheadJson?action="+action[1], {
      ajaxOptions: {method:'get'},
      postVar: 'query',
      inheritWidth: false,
      onSelect: function(elmt, resObj) {
        $('restaurantCityUrl').value = resObj.url;
      }
    });
  }
}

rules['span.dsrc'] = function(elmt) 
{ 
  var elmtP = elmt.getParent();
  // Make AJAX call to get content -delay it 10ms to let IE process the rest of the JS first.
  (function () {
    new Ajax('/' + elmt.innerHTML + '.html', {
      update: elmtP,
      onComplete: function(txt, xml) { window.behavior.apply(elmtP);  } // apply any defined behavior
      }).request();
  }).delay(10);
}

ajaxRules['#ATTRACTION_FORM'] = function(elmt) {
  elmt.addEvent('submit', function(e) {
    if (!attractionFormOnSubmit()) { (new Event(e)).stop(); }
  });
}

ajaxRules['#RESTAURANT_FORM'] = function(elmt) {
  elmt.addEvent('submit', function(e) {
    if (!restaurantFormOnSubmit()) { (new Event(e)).stop(); }
  });
}

ajaxRules['#CRUISE_CRITIC_REVIEWS_FORM'] = function(elmt) {
  elmt.addEvent('submit', function(e) {
    if (!cruiseCriticReviewFormOnSubmit()) { (new Event(e)).stop(); }
  });
}

rules['#FLIGHT_FORM input.flightTypeAhead[type=text]'] = function(elmt) {
  var action = elmt.className.match(/\bact(\w+)\b/);
  if (action) {
    new Autocompleter.Ajax.Json2(elmt, "/TypeAheadJson?action="+action[1], {
      ajaxOptions: {method:'get'},
      postVar: 'query',
      className: 'autocompleter-choices flights',
      inheritWidth: false,
      selectOnBlur: true,
      onSelect: function(elmt, resObj) {
        elmt.value = resObj.value;
        elmt.fireEvent('updateairport');
      }
    });
  }
};

rules['#UNDER_13'] = function(elmt) {
	  var text ="<div class='coppa'><div class='alertIcon'><img src='img2/generic/site/alert.gif'></div>";
	  text+="<div class='alertText'>";
	  text+="<div class='title'>"+JS_coppa_sorry+"</div>";
	  text+=JS_coppa_privacy+"<br/><br/>";
	  text+=JS_coppa_deleted+"<br/><br/>";
	  text+="<img id='LIGHTBOX_CLOSE' alt="+JS_close+" src="+JS_close_image+"></div></div>";
    ta.overlays.showInLightbox(text);
};

// FFM FLIGHT_FORM_META
var HomeFFM = {
  makeAutocompleter: function(elmt) {
    new Autocompleter.Ajax.Flights(elmt, "/TypeAheadJson?action=AIRPORT", {
      ajaxOptions: {method:'get'},
      postVar: 'query',
      className: 'autocompleter-choices flights',
      inheritWidth: false,
      selectOnBlur: true,
      onSelect: function(elmt, resObj) {
        elmt.value = resObj.name;
        $(elmt.id + 'Code').value = resObj.value;
      }
    });
    elmt.addEvent('focus', function() {
      HomeFFM.hideCalendar();
      elmt.select();
    });
  },
  hideCalendar: function() {
    if (window.calendar && window.calendar.flyout) {
      window.calendar.flyout.hide(false);
    }
  },
  moreOptionsSubmit: function() {
      $('FLIGHT_FORM_META').action='/Flights';
      $('FLIGHT_FORM_META').submit();
      return false;
  },
  setLastDate: function(elmt) {
    var lastDate = new Date();
    lastDate.setHours(0,0,0,0);
    // 330 days in milliseconds: 330*24*60*60*1000 = 28512000000
    lastDate.setTime(lastDate.getTime() + 28512000000);
    elmt.lastDate = lastDate;
  }
};
rules['#metaFlightFrom'] = HomeFFM.makeAutocompleter;
rules['#metaFlightTo'] = HomeFFM.makeAutocompleter;
rules['#metaCheckInSpan'] = HomeFFM.setLastDate;
rules['#metaCheckOutSpan'] = HomeFFM.setLastDate;
