/*
-----------------------------------------------
FlowerPowerFundraising.com
Script: vdwRequestInfo.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 2 Mar 2009
----------------------------------------------- */

vdwRequestInfo = {
	init : function() {
		$jq('#request-information-form').prepend('<ul id="error-container" class="hide"></ul>');
		$jq.validator.addMethod("phoneUS", function(phone_number, element) {
			phone_number = phone_number.replace(/\s+/g, "");
			return this.optional(element) || phone_number.length > 9 &&
				phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
		}, "Please specify a valid phone number");
		$jq('.submit input').click(function(e) {
			e.preventDefault();
			$jq.ajax({
				type : 'POST',
				url : '/assets/zip_lookup/zip_lookup.php',
				cache: false,
				data: {
					state: function() { return $jq("#state").val(); },
					zip: function() { return $jq("#postal_code").val(); }
				},
				success : function(data, textStatus) {
					$jq('#postal_code_valid').val((data == 'true') ? '1' : '');
					var valid = vdwRequestInfo.$val.form();
					if (valid) { $jq('#request-information-form').submit(); }
				}
			});
		});
		vdwRequestInfo.$val = $jq('#request-information-form').validate({
			onfocusout : false,
			onkeyup : false,
			onclick : false,
			rules : {
				company_name : 'required',
				first_name : 'required',
				last_name : 'required',
				street1 : 'required',
				city : 'required',
				state : 'required',
				postal_code : {
					required : true,
					digits : true,
					minlength: 5,
					maxlength: 5
				},
				postal_code_valid : 'required',
				day_phone : {
					required : true,
					phoneUS : true
				},
				lit_type_id : {
					required : '#sales_kit_qty:blank'
				},
				sales_kit_qty : {
					required : '#interest_both:unchecked'
				}
			},
			messages : {
				company_name : 'Please enter a group name',
				first_name : 'Please enter a first name',
				last_name : 'Please enter a last name',
				street1 : 'Please enter an address',
				city : 'Please enter a city',
				state : 'Please enter a state',
				postal_code : {
					required : 'Please enter a zip code',
					digits : 'Please enter a zip code (digits only)',
					minlength : 'Please enter a zip code (5 digits)',
					maxlength : 'Please enter a zip code (5 digits)'
				},
				postal_code_valid : 'The zip code you entered was not found in the state you selected',
				lit_type_id : 'Please indicate which program you are interested in',
				sales_kit_qty : 'Please enter a quantity for sales kits',
				source_code : 'Please tell us how you heard about us',
				email : 'Please enter a valid email',
				day_phone : 'A valid phone number is required'
			},
			errorLabelContainer : "#error-container",
			wrapper : "li"
		});
	},
	validateZip : function(state_fld, zip_fld, valid_fld) {
		$jq.ajax({
			type : 'POST',
			url : '/assets/zip_lookup/zip_lookup.php',
			cache: false,
			data: {
				state: function() { return $jq("#" + state_fld).val(); },
				zip: function() { return $jq("#" + zip_fld).val(); }
			},
			success : function(data, textStatus) {
				$jq('#' + valid_fld).val((data == 'true') ? '1' : '');
			}
		});
	}
}

$jq(document).ready(vdwRequestInfo.init);