var ContactUsForms = {
	display: 'Contact Us',
	forms: new Array(
		{type:'corporate', id: 'general', display: 'General question about Nyko'},
		{type:'support', id: 'problem', display: 'Problem with Nyko product'},
		{type:'corporate', id: 'comment', display: 'Comment on an existing or upcoming Nyko product'},
		{type:'corporate', id: 'idea', display: 'Idea for a new Nyko product'},
		{type:'support', id: 'purchaseQuestion', display: 'Question about purchasing Nyko products/order status/billing'},
		{type:'support', id: 'shippingQuestion', display: 'Question about shipping and returns'},
		{type:'support', id: 'registerProduct', display: 'Register a product'},
		{type:'corporate', id: 'newsletter', display: 'Nyko Newsletter'},
		{type:'corporate', id: 'review', display: 'Write a review of a Nyko product I own'},
		{type:'support', id: 'forgotPassword', display: 'Forgot Password'},
		{type:'support', id: 'other', display: 'Other'}
	),
	recipients: new Hash({
		'corporate':'marketing',
		'support':'customersupport'
	})
};

var SupportJumpFormManager = new Class({	
	initialize: function(form) {

		// populate selector with forms
		var selector = $(form).getElement("select[name=section]");
		ContactUsForms.forms.each(function(form) {
			if (form.type == 'support') {
				selector.options[selector.options.length] = new Option(form.display, form.id);
			}
		});
		
		// initialize the form validator
		new FormValidator.Inline($(form), {
			scrollToErrorsOnSubmit: false,
			evaluateFieldsOnBlur: false,
			evaluateFieldsOnChange: false
		});
	}
});

var ContactUsFormManager = new Class({

	platforms: new Hash(),
	addlRecipients: false,
	
	initialize: function(form, platforms) {
		this.form = $(form);
		if (platforms) {
			this.platforms = platforms;
		}
		this.initFormSelector();	
		this.initFormFields();
		this.initValidators();
		this.initFormHandler();
		this.showDefault();
	},

	initValidators: function() {
		this.validator = new FormValidator.Inline(this.form, {
			scrollToErrorsOnSubmit: false
		});
		$$('input').each(function(field) {
			field.addClass("msgPos:'errorMessages'");
		});
		$$('select').each(function(field) {
			field.addClass("msgPos:'errorMessages'");
		});
		$$('textarea').each(function(field) {
			field.addClass("msgPos:'errorMessages'");
		});
		$$('form .hiddenDiv').each(function(form) {
			form.addEvent('submit', function(event) {
				event.stop();
			});
		});
	},
	
	initFormFields: function() {		
		var platforms = this.platforms;
		$$('select[name=Platform]').each(function(field) {
			platforms.each(function(products, platform, index) {
				field.options[field.options.length] = new Option(platform, platform);
			});
			field.options[field.options.length] = new Option("Other", "Other");			
		});
		
		var countries = this.countries;
		$$('select[name=Country]').each(function(field) {
			for(var country in countries) {
				field.options[field.options.length] = new Option(country, country);
			}
			field.options[field.options.length] = new Option("Other", "Other");			
		});
	},
	
	initFormSelector: function() {

		// populate dropdown with forms
		this.selector = this.form.getElement("select[name=section]");
		
		ContactUsForms.forms.each(function(form) {
			this.selector.options[this.selector.options.length] = new Option(form.display, form.id);
		}.bind(this));
				
		this.selector.addEvent('change', function(event) {
			var target = event.target.value;
			if (target) {
				this.initForm(target);
			}
		}.bind(this));		
	},
	 
	initFormHandler: function() {
		var manager = this;
		var form = this.form;
		form.addEvent('submit', function(event) {
			
			// add hidden fields for all non-checked checkboxes
			form.getElements("input[type=checkbox]").each(function (item) {
				if (!item.checked) {
					form.adopt(new Element('input', {
						type: 'hidden',
						name: item.name,
						value: 'false'
					}));
				}
			});	
			
			// determine final email recipients
			var inquiryType = form.getElement("input[name=InquiryType]").value;
			var recipient = "";
			ContactUsForms.forms.each(function(form) {
				if (form.display == inquiryType) {
					recipient = ContactUsForms.recipients[form.type];
					if (recipient != null && recipient.trim() != "") {
						recipient = recipient + "@nyko.com";
					}
				}
			});
			var recipients = form.getElement("input[name=Recipients]");
			recipients.value = recipient;
			if (manager.addlRecipients) recipients.value += ";" + manager.addlRecipients;
			// debugging purposes
			//alert(form.getElement("input[name=Recipients]").value);
		});
	},

	showDefault: function() {
		var uri = new URI(window.location.href);
		var section = uri.getData('section');
		if(section) {
			this.selector.set('value',section);
			this.initForm(section);
		}
	}, 
	
	initForm: function(target) {
		this.validator.reset();
		$('formContent').set('html', $(target).get('html'));
		var mainform = this.form;
		
		// set the inquiry type and subject fields
		var inquiryType = mainform.getElement("input[name=InquiryType]");
		inquiryType.value = this.selector.options[this.selector.selectedIndex].text;
		var subject = mainform.getElement("input[name=Subject]");
		subject.value = "Email Us: " + inquiryType.value;
		
		// set the Platforms/Product dropdowns
		var platformSelect = mainform.getElement('select[name=Platform]');
		if (platformSelect) {
			var platforms = this.platforms;
			platformSelect.addEvent('change', function(event) {
				var productSelect = mainform.getElement('select[name=Product]');
				if (productSelect) {
					productSelect.options.length=1;
					var products = platforms[event.target.value];
					if (products) {
						products.sort();
						products.each(function(product) {
							productSelect.options[productSelect.options.length] = new Option(product, product);
						});
					}
					productSelect.options[productSelect.options.length] = new Option("Other", "Other");			
				}
			});
		}

		// set the Country/States dropdowns
		var countrySelect = mainform.getElement('select[name=Country]');
		if (countrySelect) {
			var countries = this.countries;
			countrySelect.addEvent('change', function(event) {
				var stateSelect = mainform.getElement('select[name=State]');
				if (stateSelect) {
					stateSelect.options.length=1;
					var states = countries[event.target.value];
					if (states) {
						states.sort(function(a,b){
							return a.display > b.display;
						});
						states.each(function(state) {
							stateSelect.options[stateSelect.options.length] = new Option(state.display, state.code);
						});
					}
				}
			});
		}
	},
	
	countries: {
		'United States' : new Array(
			{code:'AL',display:'Alabama'},
			{code:'AK',display:'Alaska'},
			{code:'AZ',display:'Arizona'},
			{code:'AR',display:'Arkansas'},
			{code:'CA',display:'California'},
			{code:'CO',display:'Colorado'},
			{code:'CT',display:'Connecticut'},
			{code:'DE',display:'Delaware'},
			{code:'FL',display:'Florida'},
			{code:'GA',display:'Georgia'},
			{code:'HI',display:'Hawaii'},
			{code:'ID',display:'Idaho'},
			{code:'IL',display:'Illinois'},
			{code:'IN',display:'Indiana'},
			{code:'IA',display:'Iowa'},
			{code:'KS',display:'Kansas'},
			{code:'KY',display:'Kentucky'},
			{code:'LA',display:'Louisiana'},
			{code:'ME',display:'Maine'},
			{code:'MD',display:'Maryland'},
			{code:'MA',display:'Massachusetts'},
			{code:'MI',display:'Michigan'},
			{code:'MN',display:'Minnesota'},
			{code:'MS',display:'Mississippi'},
			{code:'MO',display:'Missouri'},
			{code:'MT',display:'Montana'},
			{code:'NE',display:'Nebraska'},
			{code:'NV',display:'Nevada'},
			{code:'NH',display:'New Hampshire'},
			{code:'NJ',display:'New Jersey'},
			{code:'NM',display:'New Mexico'},
			{code:'NY',display:'New York'},
			{code:'NC',display:'North Carolina'},
			{code:'ND',display:'North Dakota'},
			{code:'OH',display:'Ohio'},
			{code:'OK',display:'Oklahoma'},
			{code:'OR',display:'Oregon'},
			{code:'PA',display:'Pennsylvania'},
			{code:'RI',display:'Rhode Island'},
			{code:'SC',display:'South Carolina'},
			{code:'SD',display:'South Dakota'},
			{code:'TN',display:'Tennessee'},
			{code:'TX',display:'Texas'},
			{code:'UT',display:'Utah'},
			{code:'VT',display:'Vermont'},
			{code:'VA',display:'Virginia'},
			{code:'WA',display:'Washington'},
			{code:'DC',display:'Washington D.C.'},
			{code:'WV',display:'West Virginia'},
			{code:'WI',display:'Wisconsin'},
			{code:'WY',display:'Wyoming'}
		),
		'Canada' : new Array(
			{code:'ON',display:'Ontario'},
			{code:'QC',display:'Quebec'},
			{code:'NS',display:'Nova Scotia'},
			{code:'NB',display:'New Brunswick'},
			{code:'MB',display:'Manitoba'},
			{code:'BC',display:'British Columbia'},
			{code:'PE',display:'Prince Edward Island'},
			{code:'SK',display:'Saskatchewan'},
			{code:'AB',display:'Alberta'},
			{code:'NL',display:'Newfoundland and Labrador'}
		)
	}
});

var PressRetailersUserFormManager = new Class({
	initialize: function(form) {
		this.form = $(form);
		this.validator = new FormValidator.Inline(this.form, {
			scrollToErrorsOnSubmit: false
		});
		$$('input').each(function(field) {
			field.addClass("msgPos:'errorMessages'");
		});
		$$('select').each(function(field) {
			field.addClass("msgPos:'errorMessages'");
		});
		$$('textarea').each(function(field) {
			field.addClass("msgPos:'errorMessages'");
		});
	}
});

FormValidator.add('required', {
    errorMsg: function(element, props) {
		if (element.title) 
			return "'" + element.title + "' is required.";
		return "Field is required.";
	},
    test: function(element, props){
		return (element.value.trim().length > 0);
    }
});

FormValidator.add('confirm', {
    errorMsg: function(element, props) {
		if ($type(props.confirm) && $type(props.form)) {
			var confirm = $(props.form).getElement('input[name='+props.confirm+']');
			return "'" + element.title + "' does not match '" + confirm.title + "'";
		}
		return 'Field does not match.';
	},
    test: function(element, props){
		if ($type(props.confirm) && $type(props.form)) {
			var confirm = $(props.form).getElement('input[name='+props.confirm+']');
			return (element.value == confirm.value);
		}
		return false;
    }
});

FormValidator.add('required-checked', {
    errorMsg: function(element, props) {
		if (element.title) 
			return element.title;
		return "Field must be checked.";
	},
    test: function(element, props){
		return (element.checked);
    }	
});

FormValidator.add('validate-datevalid', {
    errorMsg: function(element, props) {
		if (element.title) 
			return "'" + element.title + "' has an invalid date." ;
		return "Field has an invalid date.";
	},
    test: function(element, props){
		var date = Date.parse(element.value);
		if (date) {
			return ((new Date()).getTime() > date.getTime());
		}
		return true;
    }	
});
