/*
 * GetAFreeQuote.js
 * 4596 - Get A Free Quote
 *
 * CONTENTS:
 * - Campaign code extraction from querystring
 * - Main form validation rules + initialization
 * - Online quote form show/hide
 * - Online quote form validation
 *
 * BEHAVIOR DEPENDENCIES (see resources/jslib):
 *      ncnu-jquery.js, ncnu-lightbox-tqs.js (for inline message script)
 * PRESENTATION DEPENDENCIES:
 *      CSS (see resources/css/ncnu-global-national.css):
 *           Alerts, Error Messages, Lightbox - TQS
 *
 * Created: 2009-02-12
 * Created By: Dave Wolfe
 * Last Updated: 2009-04-28
 * Last Updated By: Dave Wolfe
 */




/* *** Campaign code extraction script *** */
function passCampaignCode(formFieldId) {
	var querystringVariables = window.location.search.substring(1);
	var querystringValues = querystringVariables.split("&");
	for (var i=0; i < querystringValues.length; i++) {
		var thisValue = querystringValues[i].split("=");
		if (thisValue[0] == 'tid') {
			jQuery('#' + formFieldId).val(thisValue[1]);
			i = querystringValues.length; // 
		} else if (thisValue[0] == 'CMP') {
			jQuery('#' + formFieldId).val(thisValue[1]);
		}
	}
}




/* *** Validation script configuration *** */
jQuery.validator.setDefaults({
   	errorElement: "li",
	focusCleanup: false
});




/* *** Initialize/execute on pageload *** */
jQuery(document).ready(function(){




	passCampaignCode('00N40000001rFWK'); // Get campaign code from querystring/URL




	// ****************************
	// | BEGIN MAIN FORM VALIDATION

	// Add a container for the error summary
	jQuery('#form-ScheduleACall h4').after('<div class="ncnu-error-summary" id="form-ScheduleACall-errorContainer"><p class="ncnu-error-inline">Please correct the following errors:</p><ul id="form-ScheduleACall-errorLabelContainer"></ul></div>');

	// Configure validation
	jQuery('#form-ScheduleACall').validate({
		errorClass: 'ncnu-error-inline',
		errorContainer: jQuery("div#form-ScheduleACall-errorContainer"),
		errorLabelContainer: jQuery("ul#form-ScheduleACall-errorLabelContainer"),
		// Need highlight/unhighlight functions to add error class to labels
		highlight: function(element, errorClass) {
			jQuery(element).addClass(errorClass);
			jQuery(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
			jQuery(element).removeClass(errorClass);
			jQuery(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
		},
		messages: {
			'00N40000001rFX4': "Product of Interest is required",
			'first_name': "First Name is required",
			'last_name': "Last Name is required",
			'street': "Address is required",
			'state': "State/Province is required",
			'zip': {
				digits: "Please use only numbers for the ZIP code",
				maxlength: "Length of ZIP code should not be greater than 5 digits",
				minlength: "Length of ZIP code should not be less than 5 digits",
				required: "Zip Code is required",
				ZipcodeInFootprint: "Did you enter your Zip Code correctly? If so, it appears that you are outside our club\'s Northern California, Nevada & Utah territory. Please <a href=\"http://www.aaa.com?ZipCodeEdit=\">visit your local AAA club</a> to discover what benefits AAA has to offer in your area."
			},
			'phone': {
				required: "Phone is required",
				phone: "Please enter a valid phone number, ex. 415-555-1212"
			},
			'email': {
				required: "Email is required",
				email: "Email must be in the format of name@domain.com"
			},
			'00N40000001rFWI': "Best Time to Call is required"
		},
		rules: {
			'00N40000001rFX4': "required",
			'first_name': "required",
			'last_name': "required",
			'street': "required",
			'state': "required",
			'zip': {
				digits: true,
				maxlength: 5,
				minlength: 5,
				required: true,
				ZipcodeInFootprint: true
			},
			'phone': { required: true, phone: true },
			'email': { required: true, email: true },
			'00N40000001rFWI': "required"
		}
	});

	// | END MAIN FORM VALIDATION
	// **************************




	// ***********************************
	// | BEGIN ONLINE QUOTE FORM SHOW/HIDE

	// Hide the online quote form by default
	$onlineQuote = jQuery('#onlineQuote');
	$onlineQuote.hide();

	// Add links to expand the form
	jQuery('div#onlineQuote-toggle').wrapInner('<a class="onlineQuote-toggle"></a>');
	jQuery('div#onlineQuote-toggle').prepend('<a class="onlineQuote-toggle"><img alt="[Expand Form]" height="11" id="onlineQuote-toggle-icon" src="../../resources/images/icons/ico_Expand.gif" width="11" /></a>');

	// Enable the links to toggle the form open/closed on click
	jQuery('a.onlineQuote-toggle').hover(
		function(){
			jQuery('body').css({ cursor: "pointer" });
		},
		function(){
			jQuery('body').css({ cursor: "default" });
		}
	).click(function(){
		if ($onlineQuote.is(':visible')) {
			jQuery('#onlineQuote-toggle-icon').attr({ src: "../../resources/images/icons/ico_Expand.gif" });
			$onlineQuote.hide();
		} else {
			jQuery('#onlineQuote-toggle-icon').attr({ src: "../../resources/images/icons/ico_Collapse.gif" });
			$onlineQuote.show();
			jQuery('input#onlineZipCode').blur();
		}
	});
	// | END ONLINE QUOTE FORM SHOW/HIDE
	// *********************************




	// ************************************
	// | BEGIN ONLINE QUOTE FORM VALIDATION

	jQuery('#form-OnlineQuote').submit(function(){

		if (tqs_validateZipcode('onlineZipCode')) {
			var submitZipCode = jQuery('#onlineZipCode').val();
			// CA is the default form action
			if (parseInt(submitZipCode) > 84000 && parseInt(submitZipCode) < 84792) { // UT
				jQuery(this).attr({ action: 'https://www.aaa4insurance.com/quote/default.aspx?clubcode=204036' });
			} else if (parseInt(submitZipCode) > 88900 && parseInt(submitZipCode) < 89884) { // NV
				jQuery(this).attr({ action: 'https://www.aaa4insurance.com/quote/default.aspx?clubcode=204720' });
			}
			return true;
		} else {
			return false;
		}

	});

	// | END ONLINE QUOTE FORM VALIDATION
	// **********************************




});
