/*
 * 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 ( /\/?quotebox_handler\.php$/.test( jQuery(e).attr('action') ) ) {
      // change the form action to the offers page and the method to GET
      jQuery(e).attr('action', 'offers.php'); // the offers page
      jQuery(e).attr('method', 'get');

		jQuery(e).submit(function(e) {
			var form = jQuery(e.target);
      var zip 		= form.find('input[name=zip]').val();
	    var insured = jQuery("#insured").val();
	    
	   // handle invalid zips
       if (!zip_pattern.test(zip)) {
         alert('Please provide a valid zip.');
         e.preventDefault();
         return;
       }

		    var pops    = "quotebox_handler.php" + "?zip=" + zip + "&insured=" + insured;  
				var params = "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,width="+screen.width+",height="+screen.height+",fullscreen=yes";
				window.open(pops, "", params);
      });
    }
  });
});
