// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var Request = {
  remote: function(update,url,args) {
    new Ajax.Updater(update, url, Request.get_args(args));
  },
  get: function(update,url) {
    Request.remote(update,url,{ method:'get' });
  },
  post: function(update,url) {
    Request.remote(update,url,{ method:'post' });
  },
  put: function(update,url) {
    Request.remote(update,url, { method:'put' });
  },
  destroy: function(update,url) {
    Request.remote(update,url,{ method:'delete' });
  },
  get_args: function(args) {
    var h = new Hash({asynchronous:true, evalScripts:true });
    h.merge(args);
    return h;
  }
}

var SearchForm = {
  validate: function() {
    if ($('hotel_input')) {
      var is_valid = false;
      var citynames = $('hotel_input').getInputs('radio','cityname');
      for (var i = 0; i < citynames.size(); i++) {
        var value = citynames[i].getValue();
        if (value != null && value != 'other') {
          is_valid = true;
        } else if (value == 'other' && $('city_name_select').getValue() != null ) {
          is_valid = true
        }
      }
      if ($('as_widget_value').getValue() == '1') {
        is_valid = true;
      }
      return is_valid;
    } else if ($('flight_input')) {
      return true;
    }
  },
  track_and_submit: function(form_id,tracker_id,advertiser_id) {
    if (SearchForm.validate()) {
      new Ajax.Request('/compare/track/' + advertiser_id, {asynchronous:false} );
      pageTracker._trackPageview(tracker_id);
      $(form_id).submit();
    } else {
      alert('Please select a city');
      return false;
    }
  }
}
