/*
 * If the page 'side-offers' exists for any given site, this functionality is added
 */

var QuoteBox = Class.create({
  initialize: function() {
    var that = this;
    this.offer_url = "/side-offers"
    this.QBs = this.findQBs();
    this.QBs.each(function(qb) {
      Event.observe(qb, 'submit', that.onQBSubmit.bindAsEventListener(that));
    });
  },

  findQBs: function() {
    return $$('form[action=/start-quote]');
  },

  onQBSubmit: function(e) {
    var form = Event.element(e);
    var zip  = form.select('input[name=zip]')[0].value;

    // don't do any thing if invalid zip
    if (!/^\d{5}$/.test(zip)) return;

    // change method and action
    //form.method = 'get';
    //form.action = this.offer_url;
    // pop open the page 'start-quote' with the zip appended
    var params  = "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,";
    params     += "width=998,height=768,left=250,top=175";
    var pops    = this.offer_url + "?zip=" + zip;
    window.open(pops, "", params).blur();
    window.focus();
  }
});

FastInit.addOnLoad(function() {
  var quote_box = new QuoteBox;
});