/*
 * Add side-offers page to any quotebox that appears on the page
 */

jQuery(document).ready(function() {
  var zip_pattern = /^\d{5}$/;
  var forms = jQuery('form');
  forms.each(function(i, e) {
    if ( /\/?request-health-quote$/.test( jQuery(e).attr('action') ) ) {
      // change the form action to the offers page and the method to GET
      jQuery(e).attr('action', 'offers'); // the offers page
      jQuery(e).attr('method', 'get');

      // add a submit action that will open the form with the inputs as URL parameters
      jQuery(e).submit(function(e) {
        var form = jQuery(e.target);
        var zip  = form.find('input[name=zip]').val(); // input whose name is 'zip'

        // handle invalid zips
        if (!zip_pattern.test(zip)) {
          alert('That was an invalid zip code.\n\nPlease enter a valid zip code.');
          e.preventDefault();
          return;
        }

        // pop the form
        var params  = "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,width="+screen.width+",height="+screen.height+",fullscreen=yes";
        var pops    = "request-health-quote" + "?address1_zip=" + zip;// + "&over65=" + senior;
        window.open(pops, "", params);
      });
    }
  });
});
