/*

Copyright © 2009. Larry Staton Jr.
Version: 1.0
Version: 0.9 - IE sucks
Version: 0.5 - Fixed window resizing for non-tab users
Version: 0.2 - Script works on Firefox
Version: 0.1 - Event listeners attached and script works on Safari/WebKit

*/

function open_new_quote_window(e) {
  if (!e) { e = window.event; } // IE hack
  var insurance_type = document.getElementById('insurance_types').value;
  var zip = document.getElementById('zip').value;
  if (zip === "") {
    alert("Please enter a valid zip code in the zip code field.");
  } else {
    // Change the window.open address to alter the landing page
    var new_window = window.open('http://www.insuranceagents.com/' + insurance_type + '?a=' + zip + '&c=lowermyautorates');
    if (!document.addEventListener) { // Resize new window for folks who use new windows instead of tabs
      new_window.moveTo(0,0);
      new_window.resizeTo(1024,768); // Arbitrary window size
    }
  }
}

function attach_event_listeners() {
  document.getElementById('zip').focus();
  var quote_form = document.getElementById('get_a_quote');
  if (document.addEventListener) { // DOM Level 2
    quote_form.addEventListener('submit', open_new_quote_window, false);
  } else { // Forget IE 5+
    quote_form.setCapture();
    quote_form.attachEvent('onsubmit', open_new_quote_window);
  }
}


if (window.attachEvent) {
  window.attachEvent('onload', attach_event_listeners);
} else if (window.addEventListener) {
  window.addEventListener('load', attach_event_listeners, false);
} else {
  document.addEventListener('load', attach_event_listeners, false);
}
