/*
-----------------------------------------------
FlowerPowerFundraising.com
Script: vdwReg.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 10 Feb 2009
----------------------------------------------- */

vdwReg = {
	seasonStartDate : new Date(2012, 0, 10),
	seasonEndDate : new Date(2012, 3, 27),
	shipBillFieldMapping : [
		['billing_address_1', 'shipping_address_1'],
		['billing_address_2', 'shipping_address_2'],
		['billing_city', 'shipping_city'],
		['billing_state', 'shipping_state'],
		['billing_zip', 'shipping_zip']
	],
	fieldIds : {
		diffShipAd : 'different_shipping_address',
		campaignImg : 'campaign_image',
		campaignImgPreview : 'campaign-image-preview'
	},
	init : function() {
		// If the season start date is before the current date, set the start date to today
		var today = new Date();
		if (today > vdwReg.seasonStartDate) { vdwReg.seasonStartDate = today; }

		vdwReg.$regForm = $jq('#registration-wizard');

		// Progress Bar
		$jq("#nav-registration-wizard a").bind('click', function(e) {
			e.preventDefault();
			$jq('#mode').val($jq(this).attr('href').match(/mode=(.+?)$/)[1]);
			var currentIndex = $jq("#nav-registration-wizard li").index($jq("#nav-registration-wizard .current")[0]);
			var targetIndex = $jq("#nav-registration-wizard li").index($jq(this).parent('li')[0]);
			if (targetIndex > currentIndex) {
				vdwReg.validateZips();
			} else {
				vdwReg.$regForm[0].submit();
			}
		});

		// Prev and next step tabs
		$jq('.btn-next-step a').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			$jq('#mode').val(href.match(/mode=(.+?)$/)[1]);
			vdwReg.validateZips();
		});
		$jq('.btn-prev-step a').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			$jq('#mode').val(href.match(/mode=(.+?)$/)[1]);
			vdwReg.$regForm[0].submit();
		});

		// Page preview
		$jq('.btn-preview-landing-page').click(function(e) {
			e.preventDefault();
			var valid = vdwReg.$regForm.valid();
			if (valid) {
				var oldAction = vdwReg.$regForm.attr('action');
				$jq('#mode').val('landing_page_preview_multipage');
				vdwReg.$regForm
					.attr('action', '/campaign-landing-preview')
					.attr('target', '_blank')
					.eq(0)
					.submit();
				vdwReg.$regForm.attr('action', oldAction).attr('target','');
			}
		});

	},
	initAccount : function() {
		// Validation
		vdwReg.$regForm.validate({
			rules : {
				org_name : 'required',
				first_name : 'required',
				last_name : 'required',
				email : {
					required : true,
					email : true
				},
				confirm_email : {
					required : true,
					equalTo : '#email'
				},
				passwd : 'required',
				confirm_passwd : {
					required : true,
					equalTo : '#passwd'
				},
				profit_check_payee : 'required',
				profit_check_address : 'required',
				profit_check_city : 'required',
				profit_check_state : 'required',
				profit_check_zip : {
					required : true,
					digits : true,
					minlength: 5,
					maxlength: 5
				},
				profit_check_zip_valid : 'required',
				billing_address_1 : 'required',
				billing_city : 'required',
				billing_state : 'required',
				billing_zip : {
					required : true,
					digits : true,
					minlength: 5,
					maxlength: 5
				},
				billing_zip_valid : 'required',
				billing_phone : {
					required : true,
					phoneUS : true
				},
				shipping_address_1 : 'required',
				shipping_city : 'required',
				shipping_state : 'required',
				shipping_zip : {
					required : true,
					digits : true,
					minlength: 5,
					maxlength: 5
				}
			},
			messages : {
				org_name : 'Organization name is required',
				first_name : 'First name is required',
				last_name : 'Last name is required',
				email : 'Please enter a valid email',
				confirm_email : 'Please enter the same value for confirm email',
				passwd : 'Password is required',
				confirm_passwd : 'Please enter the same value for confirm password',
				profit_check_payee : 'Profit check payee is required',
				profit_check_address : 'Profit check address is required',
				profit_check_city : 'Profit check city is required',
				profit_check_state : 'Profit check state is required',
				profit_check_zip : {
					required : 'Profit check zip is required',
					digits : 'Profit check zip is required (digits only)',
					minlength : 'Profit check zip is required (5 digits)',
					maxlength : 'Profit check zip is required (5 digits)',
					remote : 'The profit check zip code you entered was not found in the state you selected'
				},
				profit_check_zip_valid : 'The profit check zip code you entered was not found in the state you selected',
				billing_address_1 : 'Billing address is required',
				billing_city : 'Billing city is required',
				billing_state : 'Billing state is required',
				billing_zip : {
					required : 'Billing zip is required',
					digits : 'Billing zip is required (digits only)',
					minlength : 'Billing zip is required (5 digits)',
					maxlength : 'Billing zip is required (5 digits)',
					remote : 'The Billing zip code you entered was not found in the state you selected'
				},
				billing_zip_valid : 'The Billing zip code you entered was not found in the state you selected',
				billing_phone : 'A valid billing phone is required',
				shipping_address_1 : 'Shipping address is required',
				shipping_city : 'Shipping city is required',
				shipping_state : 'Shipping state is required',
				shipping_zip : {
					required : 'Shipping zip is required',
					digits : 'Shipping zip is required (digits only)',
					minlength : 'Shipping zip is required (5 digits)',
					maxlength : 'Shipping zip is required (5 digits)',
					remote : 'The Shipping zip code you entered was not found in the state you selected'
				},
				shipping_zip_valid : 'The Shipping zip code you entered was not found in the state you selected'
			},
			errorLabelContainer : "#error-container",
			wrapper : "li",
			onkeyup : false,
			showErrors : function(errorMap, errorList) {
				this.defaultShowErrors();
				if (errorList.length) {
					window.location.hash = 'content';
				}
			}
		});
		// Campaign Manager Info
		$jq('#billing-address input').change(function() {
			$jq("input#profit_check_address").val($jq("input#billing_address_1").val());
			$jq("input#profit_check_city").val($jq("input#billing_city").val());
			$jq("input#profit_check_zip").val($jq("input#billing_zip").val());
		});
		$jq('#campaign-manager-information input').change(function() {
			$jq("input#profit_check_address_2").val($jq("input#first_name").val()  + ' ' + $jq("input#last_name").val());
			$jq("input#profit_check_payee").val($jq("input#org_name").val());
		});
		$jq('select#billing_state').change(function() {
			$jq("select#profit_check_state").val($jq("select#billing_state option:selected").val());
		});
	},
	initWebPage : function() {
		// Validation
		vdwReg.$regForm.validate({
			rules : {
				campaign_name : 'required',
				fundraising_goal : {
					required : true,
					number : true
				},
				tracking_graphic : 'required',
				start_date : 'required',
				end_date : 'required',
				custom_campaign_image : {
					required : function(element) { return (!$jq("#campaign_image").val()); },
					accept : 'jpg|jpeg'
				}
			},
			messages : {
				campaign_name : 'Campaign name is required',
				fundraising_goal : 'Please enter a number for fundraising goal',
				tracking_graphic : 'Please select a tracking graphic',
				start_date : 'Select a start date',
				end_date : 'Select an end date',
				custom_campaign_image : 'Please provide an image in JPG format via upload or choose a clip art option'
			},
			errorLabelContainer : "#error-container",
			wrapper : "li",
			onkeyup : false,
			showErrors : function(errorMap, errorList) {
				this.defaultShowErrors();
				if (errorList.length) {
					window.location.hash = 'content';
				}
			}
		});

		// Clear Photo/Logo upload if an icon is selected
		$jq('#campaign_image').bind('change', function() {
			$jq('#custom_campaign_image').val('');
		});

		// Campaign Image Preview
		$jq('#' + vdwReg.fieldIds.campaignImg).change(function() {
			var val = ($jq(this).val() != '') ? $jq(this).val() : 'placeholder';
			var re = /stock_.+?_thumb.gif/;
			var old_src = $jq('#' + vdwReg.fieldIds.campaignImgPreview + ' img').attr('src');
			var new_src = old_src.replace(re, 'stock_' + val + '_thumb.gif');
			$jq('#' + vdwReg.fieldIds.campaignImgPreview + ' img').attr('src',new_src);
		}).trigger('change');

		// Campaign Start Dates
		vdwReg.initStartDatePicker();
		vdwReg.initEndDatePicker();
	},
	initParticipantList : function() {
		// Validation
		vdwReg.$regForm.validate({
			rules : {
				member_first_name_1 : { required : function(element) { return !(Number($jq('#imported_contacts_count').val())); }},
				member_last_name_1 : { required : function(element) { return !(Number($jq('#imported_contacts_count').val())); }},
				member_email_1 : { required : function(element) { return !(Number($jq('#imported_contacts_count').val())); }, email : true }
			},
			messages : {
				member_first_name_1 : 'Please enter at least one participant first name',
				member_last_name_1 : 'Please enter at least one participant last name',
				member_email_1 : 'Please enter at least one valid participant email'
			},
			errorLabelContainer : "#error-container",
			wrapper : "li",
			onkeyup : false,
			showErrors : function(errorMap, errorList) {
				this.defaultShowErrors();
				if (errorList.length) {
					window.location.hash = 'content';
				}
			}
		});
		var member_fields_count = $jq('#member_fields_count').val();
		if (member_fields_count > 1) {
			for (var i = 2; i <=  member_fields_count; i++) {
				$jq('#member_email_'+i).rules('add', {
					email : true,
					messages : {
						email : 'Participant email '+i+' is invalid'
					}
				});
			}
		}

		// Participant emails
		$jq('.btn-remove-participant').click(vdwReg.removeParticipant);
		$jq('.btn-add-participant').click(function(e) {
			e.preventDefault();
			var rowNum = $jq('#participant-list tbody tr').length + 1;
			$jq('#member_fields_count').val(rowNum);
			$jq('#participant-list tbody').append('<tr><td>' + rowNum + '.</td><td><input type="text" id="member_first_name_' + rowNum + '" name="member_first_name_' + rowNum + '"/></td><td><input type="text" id="member_last_name_' + rowNum + '" name="member_last_name_' + rowNum + '"/></td><td><input type="text" id="member_email_' + rowNum + '" name="member_email_' + rowNum + '"/></td><td><a class="btn-remove-participant" href="#">Delete</a></td></tr>');
			$jq('#participant-list tbody tr').eq(rowNum - 1).find('.btn-remove-participant').click(vdwReg.removeParticipant);
			// Add the rule
			$jq('#member_email_' + rowNum).rules('add', {
				email : true,
				messages : {
					email : 'Participant email ' + rowNum + ' is invalid'
				}
			});
		});
	},
	initCustomizeEmails : function() {
		vdwReg.$regForm.validate({
			rules : {
				from_pe1 : { maxlength: 200 },
				subject_pe1 : { maxlength: 200 },
				salutation_pe1 : { maxlength: 200 },
				from_pe2 : { maxlength: 200 },
				subject_pe2 : { maxlength: 200 },
				salutation_pe2 : { maxlength: 200 },
				from_pe3 : { maxlength: 200 },
				subject_pe3 : { maxlength: 200 },
				salutation_pe3 : { maxlength: 200 },
				from_ffe1 : { maxlength: 200 },
				subject_ffe1 : { maxlength: 200 },
				salutation_ffe1 : { maxlength: 200 },
				from_ffe2 : { maxlength: 200 },
				subject_ffe2 : { maxlength: 200 },
				salutation_ffe2 : { maxlength: 200 },
				from_ffe3 : { maxlength: 200 },
				subject_ffe3 : { maxlength: 200 },
				salutation_ffe3 : { maxlength: 200 },
				from_ffe4 : { maxlength: 200 },
				subject_ffe4 : { maxlength: 200 },
				salutation_ffe4 : { maxlength: 200 }
			},
			messages : {
				from_pe1 : { maxlength: 'The maximum length for this field is 200 characters' },
				subject_pe1 : { maxlength: 'The maximum length for this field is 200 characters' },
				salutation_pe1 : { maxlength: 'The maximum length for this field is 200 characters' },
				from_pe2 : { maxlength: 'The maximum length for this field is 200 characters' },
				subject_pe2 : { maxlength: 'The maximum length for this field is 200 characters' },
				salutation_pe2 : { maxlength: 'The maximum length for this field is 200 characters' },
				from_pe3 : { maxlength: 'The maximum length for this field is 200 characters' },
				subject_pe3 : { maxlength: 'The maximum length for this field is 200 characters' },
				salutation_pe3 : { maxlength: 'The maximum length for this field is 200 characters' },
				from_ffe1 : { maxlength: 'The maximum length for this field is 200 characters' },
				subject_ffe1 : { maxlength: 'The maximum length for this field is 200 characters' },
				salutation_ffe1 : { maxlength: 'The maximum length for this field is 200 characters' },
				from_ffe2 : { maxlength: 'The maximum length for this field is 200 characters' },
				subject_ffe2 : { maxlength: 'The maximum length for this field is 200 characters' },
				salutation_ffe2 : { maxlength: 'The maximum length for this field is 200 characters' },
				from_ffe3 : { maxlength: 'The maximum length for this field is 200 characters' },
				subject_ffe3 : { maxlength: 'The maximum length for this field is 200 characters' },
				salutation_ffe3 : { maxlength: 'The maximum length for this field is 200 characters' },
				from_ffe4 : { maxlength: 'The maximum length for this field is 200 characters' },
				subject_ffe4 : { maxlength: 'The maximum length for this field is 200 characters' },
				salutation_ffe4 : { maxlength: 'The maximum length for this field is 200 characters' }
			},
			errorLabelContainer : "#error-container",
			wrapper : "li",
			onkeyup : false,
			showErrors : function(errorMap, errorList) {
				this.defaultShowErrors();
				if (errorList.length) {
					window.location.hash = 'content';
				}
			}
		});
		$jq('#participant-emails-personalization .btn-next-email a').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			vdwReg.$peTabs.tabs('select', href.substr(href.indexOf('#') + 1));
		});
		$jq('#participant-emails-personalization').hide();
		// Preview emails
		$jq('.btn-preview-email').click(function(e) {
			e.preventDefault();
			var href = $jq(this).find('a').attr('href');
			var re = /email_type=(.+)/;
			var email_type = href.match(re)[1];

			$jq('#email_type').val(email_type);
			$jq('#preview_from').val($jq(this).parents('.email-template-fields').find('.from').val());
			$jq('#preview_subject').val($jq(this).parents('.email-template-fields').find('.subject').val());
			$jq('#preview_salutation').val($jq(this).parents('.email-template-fields').find('.salutation').val());
			$jq('#preview_message').val($jq(this).parents('.email-template-fields').find('.message').val());
			var oldAction = vdwReg.$regForm.attr('action');
			vdwReg.$regForm.attr('action', '/campaign-email-preview')
				.attr('target', '_blank')
				.eq(0)
				.submit();
			vdwReg.$regForm.attr('action', oldAction).attr('target','');
		});
		$jq('.btn-view-standard-email').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			var re = /email_type=(.+)/;
			var email_type = href.match(re)[1];
			var re = /(#.+)\?/;
			var formId = href.match(re)[1];

			$jq('#email_type').val(email_type);
			$jq('#preview_from').val($jq(formId + ' .from').val());
			$jq('#preview_subject').val($jq(formId + ' .subject').val());
			$jq('#preview_salutation').val($jq(formId + ' .salutation').val());
			$jq('#preview_message').val($jq(formId + ' .message').val());
			var oldAction = vdwReg.$regForm.attr('action');
			vdwReg.$regForm.attr('action', '/campaign-email-preview')
				.attr('target', '_blank')
				.eq(0)
				.submit();
			vdwReg.$regForm.attr('action', oldAction).attr('target','');
		});
		$jq('.btn-personalize-emails').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			$jq('.email-template-fields').removeAttr('style');
			$jq('#' + href.substr(href.indexOf('#') + 1)).slideDown('fast');
			var tab_selected_id = $jq(this).attr('id');
			var tab_selected = tab_selected_id.replace('btnpe-', '') -1;
			$jq(this).parents('.email-personalization').find('.personalization-actions').hide();
			$jq(this).parents('.email-personalization').find('.btn-cancel-personalization').show();
			vdwReg.$peTabs = $jq('#nav-participant-emails').tabs( { selected : tab_selected } );
		});

		$jq('.btn-cancel-personalization a').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			$jq('#' + href.substr(href.indexOf('#') + 1)).slideUp('fast');
			$jq(this).parents('.email-personalization').find('.personalization-actions').show();
			$jq(this).parents('.email-personalization').find('.btn-cancel-personalization').hide();
			$jq(this).parents('.email-personalization').find('input').add('textarea').each(function (i) {
				$jq(this).val($jq(this).attr('defaultValue'));
			});
		});
	},
	initEmailCampaign : function() {
		// Validation
		vdwReg.$regForm.validate({
			rules : {
				member_first_name_1 : { required : function(element) { return !(Number($jq('#imported_contacts_count').val())); }},
				member_last_name_1 : { required : function(element) { return !(Number($jq('#imported_contacts_count').val())); }},
				member_email_1 : { required : function(element) { return !(Number($jq('#imported_contacts_count').val())); }, email : true }
			},
			messages : {
				member_first_name_1 : 'Please enter at least one participant first name',
				member_last_name_1 : 'Please enter at least one participant last name',
				member_email_1 : 'Please enter at least one valid participant email'
			},
			errorLabelContainer : "#error-container",
			wrapper : "li",
			onkeyup : false,
			showErrors : function(errorMap, errorList) {
				this.defaultShowErrors();
				if (errorList.length) {
					window.location.hash = 'content';
				}
			}
		});
		var member_fields_count = $jq('#member_fields_count').val();
		if (member_fields_count > 1) {
			for (var i = 2; i <=  member_fields_count; i++) {
				$jq('#member_email_'+i).rules('add', {
					email : true,
					messages : {
						email : 'Participant email '+i+' is invalid'
					}
				});
			}
		}

		// Participant emails
		$jq('.btn-remove-participant').click(vdwReg.removeParticipant);
		$jq('.btn-add-participant').click(function(e) {
			e.preventDefault();
			var rowNum = $jq('#participant-list tbody tr').length + 1;
			$jq('#member_fields_count').val(rowNum);
			$jq('#participant-list tbody').append('<tr><td>' + rowNum + '.</td><td><input type="text" id="member_first_name_' + rowNum + '" name="member_first_name_' + rowNum + '"/></td><td><input type="text" id="member_last_name_' + rowNum + '" name="member_last_name_' + rowNum + '"/></td><td><input type="text" id="member_email_' + rowNum + '" name="member_email_' + rowNum + '"/></td><td><a class="btn-remove-participant" href="#">Delete</a></td></tr>');
			$jq('#participant-list tbody tr').eq(rowNum - 1).find('.btn-remove-participant').click(vdwReg.removeParticipant);
			// Add the rule
			$jq('#member_email_' + rowNum).rules('add', {
				email : true,
				messages : {
					email : 'Participant email ' + rowNum + ' is invalid'
				}
			});
		});

		// Email tabs
		$jq('#participant-emails-personalization .btn-next-email a').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			vdwReg.$peTabs.tabs('select', href.substr(href.indexOf('#') + 1));
		});
		vdwReg.$ectiTabs = $jq("#email-campaign-tabs-interface").tabs( { selected : 0 } );
		$jq('#participant-emails-personalization').hide();
		// Preview emails
		$jq('.btn-preview-email').click(function(e) {
			e.preventDefault();
			var href = $jq(this).find('a').attr('href');
			var re = /email_type=(.+)/;
			var email_type = href.match(re)[1];
			$jq('#email_type').val(email_type);
			$jq('#preview_from').val($jq(this).parents('.email-template-fields').find('.from').val());
			$jq('#preview_subject').val($jq(this).parents('.email-template-fields').find('.subject').val());
			$jq('#preview_salutation').val($jq(this).parents('.email-template-fields').find('.salutation').val());
			$jq('#preview_message').val($jq(this).parents('.email-template-fields').find('.message').val());
			var oldAction = vdwReg.$regForm.attr('action');
			vdwReg.$regForm.attr('action', '/campaign-email-preview')
				.attr('target', '_blank')
				.eq(0)
				.submit();
			vdwReg.$regForm.attr('action', oldAction).attr('target','');
		});
		$jq('.btn-view-standard-email').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			var re = /email_type=(.+)/;
			var email_type = href.match(re)[1];
			var re = /(#.+)\?/;
			var formId = href.match(re)[1];
			$jq('#email_type').val(email_type);
			$jq('#preview_from').val($jq(formId + ' .from').val());
			$jq('#preview_subject').val($jq(formId + ' .subject').val());
			$jq('#preview_salutation').val($jq(formId + ' .salutation').val());
			$jq('#preview_message').val($jq(formId + ' .message').val());
			var oldAction = vdwReg.$regForm.attr('action');
			vdwReg.$regForm.attr('action', '/campaign-email-preview')
				.attr('target', '_blank')
				.eq(0)
				.submit();
			vdwReg.$regForm.attr('action', oldAction).attr('target','');
		});
		$jq('.btn-personalize-emails').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			$jq('#' + href.substr(href.indexOf('#') + 1)).slideDown('fast');
			$jq(this).parents('.email-personalization').find('.personalization-actions').hide();
			$jq(this).parents('.email-personalization').find('.btn-cancel-personalization').show();
			$jq('#nav-participant-emails').tabs( { selected : 0 } );
		});

		$jq('.btn-cancel-personalization a').click(function(e) {
			e.preventDefault();
			var href = $jq(this).attr('href');
			$jq('#' + href.substr(href.indexOf('#') + 1)).slideUp('fast');
			$jq(this).parents('.email-personalization').find('.personalization-actions').show();
			$jq(this).parents('.email-personalization').find('.btn-cancel-personalization').hide();
			$jq(this).parents('.email-personalization').find('input').add('textarea').each(function (i) {
				$jq(this).val($jq(this).attr('defaultValue'));
			});
		});
	},
	initReview : function() {
		var self = this;
		self.submitted = false;
		// Submit
		$jq('.btn-approve-launch-campaign a').click(function(e) {
			e.preventDefault();
			if (self.submitted) { return false; }
			self.submitted = true;
			$this = $jq(this);
			var mode = $this.attr('href').match(/mode=(.+?)$/)[1];
			var action = $this.attr('href').match(/(.+?)\?mode=.+/)[1];
			$jq('#mode').val(mode);
			vdwReg.$regForm
				.attr('action', action)
				.submit();
		});
	},
	removeParticipant : function(e) {
		e.preventDefault();
		$jq(this).parents('tr').find('input').val('');
	},
	initStartDatePicker : function() {
		maxDate = new Date(vdwReg.seasonEndDate);
		maxDate.setDate(maxDate.getDate() - 7);
		$jq("#start_date").datepicker({
			defaultDate : new Date(),
			minDate : vdwReg.seasonStartDate,
			maxDate : maxDate,
			dateFormat : "mm/dd/yy",
			buttonImage: "/assets/templates/main/images/icon-calendar.gif",
			buttonImageOnly : true,
			buttonText : "Choose",
			showOn : "both",
			mandatory : true,
			constrainInput : true,
			onSelect : function(dateText) {
				var myMinDate = $jq('#start_date').datepicker('getDate');
				myMinDate.setDate(myMinDate.getDate() + 7);
				$jq('#end_date').val('');
				$jq('#end_date').datepicker('change', { minDate : myMinDate });
			}
		});
	},
	initEndDatePicker : function() {
		var minDate = $jq('#start_date').datepicker('getDate');
		minDate.setDate(minDate.getDate() + 7);
		$jq("#end_date").datepicker({
			defaultDate : vdwReg.seasonEndDate,
			minDate : minDate,
			maxDate : vdwReg.seasonEndDate,
			dateFormat : "mm/dd/yy",
			buttonImage: "/assets/templates/main/images/icon-calendar.gif",
			buttonImageOnly : true,
			buttonText : "Choose",
			showOn : "both",
			mandatory : true,
			constrainInput : true
		});
	},
	copyFields : function(fieldMapping) {
		for (var i = 0; i < fieldMapping.length; i++) {
			$jq('#' + fieldMapping[i][1]).val($jq('#' + fieldMapping[i][0]).val());
		}
	},
	updateSummary : function() {
		$jq('#org_name_summary').text($jq('#org_name').val());
		$jq('#manager_name_summary').text($jq('#first_name').val() + ' ' + $jq('#last_name').val());
		$jq('#start_date_summary').text($jq('#start_date').val());
		$jq('#end_date_summary').text($jq('#end_date').val());
		$jq('#fundraising_goal_summary').text('$' + $jq('#fundraising_goal').val());
		$jq('#end_date_summary').text($jq('#end_date').val());
		$jq('#tracking_graphic_summary').html('<img width="52" height="52" alt="" src="/assets/templates/main/images/icon-tracker-' + $jq('input[name="tracking_graphic"]:checked').val() + '.gif"/>');
		// Calculate dates
		var start_date = new Date($jq("#start_date").datepicker('getDate'));
		var start_date_fmt = $jq.datepicker.formatDate('mm/dd/yy', start_date);
		$jq('#pe1_date_summary').text(start_date_fmt);
		$jq('#ffe1_date_summary').text(start_date_fmt);
		var end_date = new Date($jq("#end_date").datepicker('getDate'));
		var one_day=1000*60*60*24
		var fundraiser_length = Math.ceil(Math.ceil(end_date.getTime() - start_date.getTime()) / one_day);
		end_date.setDate(end_date.getDate()-7);
		var email3_date = $jq.datepicker.formatDate('mm/dd/yy', end_date);
		$jq('#pe3_date_summary').text(email3_date);
		$jq('#ffe3_date_summary').text(email3_date);
		if (fundraiser_length >= 14) {
			end_date.setDate(end_date.getDate()-7);
			var email2_date = $jq.datepicker.formatDate('mm/dd/yy', end_date);
		} else {
			var email2_date = 'This email will not be sent because your fundraiser is less than 2 weeks long';
		}
		$jq('#pe2_date_summary').text(email2_date);
		$jq('#ffe2_date_summary').text(email2_date);
		var email4_date = new Date($jq("#end_date").datepicker('getDate'));
		email4_date.setDate(email4_date.getDate()-1);
		email4_date = $jq.datepicker.formatDate('mm/dd/yy', email4_date);
		$jq('#ffe4_date_summary').text(email4_date);
	},
	validateZips : function() {
		if ($jq("#profit_check_state").length) {
			$jq.ajax({
				type : 'POST',
				url : '/assets/zip_lookup/zip_lookup.php',
				cache: false,
				data: {
					state: function() { return $jq("#profit_check_state").val(); },
					zip: function() { return $jq("#profit_check_zip").val(); }
				},
				success : function(data, textStatus) {
					if (data == 'true') {
						$jq('#profit_check_zip_valid').val('1');
					} else {
						$jq('#profit_check_zip_valid').val('');
					}
					$jq.ajax({
						type : 'POST',
						url : '/assets/zip_lookup/zip_lookup.php',
						cache: false,
						data: {
							state: function() { return $jq("#billing_state").val(); },
							zip: function() { return $jq("#billing_zip").val(); }
						},
						success : function(data, textStatus) {
							if (data == 'true') {
								$jq('#billing_zip_valid').val('1');
							} else {
								$jq('#billing_zip_valid').val('');
							}
							$jq.ajax({
								type : 'POST',
								url : '/assets/zip_lookup/zip_lookup.php',
								cache: false,
								data: {
									state: function() { return $jq("#shipping_state").val(); },
									zip: function() { return $jq("#shipping_zip").val(); }
								},
								success : function(data, textStatus) {
									if (data == 'true') {
										$jq('#shipping_zip_valid').val('1');
									} else {
										$jq('#shipping_zip_valid').val('');
									}
									vdwReg.$regForm.submit();
								}
							});
						}
					});
				}
			});
		} else {
			vdwReg.$regForm.submit();
		}
	}
};

$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(document).ready(vdwReg.init);

