/*

Copyright Ã‚Â© 2009. Larry Staton Jr.
Version: 2.0 - Remove variables from global namespace; passes JSLint tests
Version: 1.5 - Stop action from continuing
Version: 1.4 - Add validation
Version: 1.3 - IE still sucks
Version: 1.2 - Changes to help withd routing Rails and URLs
Version: 1.1 - Rails implementation for QuoTE
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

*/

var LS = {

  zip : 28078,
  age : 1,

  insurance_type : 1,

  validate_zip_code : function(e) {

    if (!e) {
      e = window.event;
      e.returnValue = true;
    }
    LS.zip = document.getElementById('target').value;
	LS.age = document.getElementById('age').value;
	
	allValid = true;
	var ageDiv = document.getElementById('ageError');
    if (LS.age == '') {
		ageDiv.style.display="inline";
		ageDiv.parentNode.parentNode.style.backgroundColor = "#ffcccc";
		//alert("Please select your Age from the Age drop down");
		allValid = false;
	} else {
		ageDiv.style.display="none";
		ageDiv.parentNode.parentNode.style.backgroundColor = "";
	}
	
	var zipDiv = document.getElementById('zipError');
	if ( !(/(^\d{5}$)/).test(LS.zip)) {
		zipDiv.style.display="block";
		zipDiv.parentNode.parentNode.style.backgroundColor = "#ffcccc";
		//alert("Please enter a valid ZIP code in the ZIP code field.");
		allValid = false;
	} else {
		zipDiv.style.display="none";
		zipDiv.parentNode.parentNode.style.backgroundColor = "";
	}
	
	if (!allValid) {
		if (e.returnValue) {
			e.returnValue = false;
		} else {
			e.preventDefault();
			return false;
		}
	} else {
	//	LS.open_new_quote_window(LS.zip);
	}
    return 0;

  },

  open_new_quote_window : function(zip) {
	LS.ref = document.getElementById('ref').value;
	LS.age = document.getElementById('age').value;
	LS.numIncidents = document.getElementById('numIncidents').value;
	LS.numDrivers = document.getElementById('numDrivers').value;

	//LS.insurance_kw = document.getElementById('subid3').value;
    for (index=0; index < document.q.yn.length; index++) {
        if (document.q.yn[index].checked) {
            LS.insurance_yn = document.q.yn[index].value;
            //break; remove this to stop popup
        }
    }
	
    for (index=0; index < document.q.homeowner.length; index++) {
        if (document.q.homeowner[index].checked) {
            LS.homeowner = document.q.homeowner[index].value;
            //break; remove this to stop popup
        }
    }
	
	window.open("http://www.auto-lnsurance.com/sb/submit.php?ref="+escape(LS.ref)
														+"&age="+LS.age
														+"&numIncidents="+LS.numIncidents
														+"&numDrivers="+LS.numDrivers
														+"&homeowner="+LS.homeowner
														+"&yn="+LS.insurance_yn
														+"&target="+zip
														+"&o=1",
				null, 'fullscreen=1, toolbars=1, scrollbars=1, menubar=1, location=1');
  },

  attach_event_listeners : function() {
    var quote_form = document.getElementById('get_a_quote');
    if (document.addEventListener) { // DOM Level 2
      quote_form.addEventListener('submit', function(e) { return LS.validate_zip_code(e); } , false);
    }
    else { // IE 5+ Event Model
      quote_form.attachEvent('onsubmit', function() { return LS.validate_zip_code(); });
    }
  }
};

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

