
var EarthBalance =  EarthBalance || {};

(function($, window, undefined) {
	
	EarthBalance.ContactForm = (function() {
		
		var form = "#contact-form";
		var overlay = "#overlay-form";
		var flash = "#swf-content";
		var trigger = ".close";
		var isValid = false;
		
		return {
			init : function() {
				// Setup overlay
				$('button[rel|="'+overlay+'"]').overlay({
					load : false,
					top : '5%',
					mask : {
						color : "#fff",
						opacity : 0.9
					},
					closeOnClick : false,
					onClose : this.clearForm
				});
				
				// Setup overlay trigger
				$('button[rel|="'+overlay+'"]').click(this.show);
				
				// Validation Engine
				$("#contact-form").validationEngine({
					openDebug: false
				});
				
				// Show/Hide form elements
				$("label[for=province]").hide();
				$("select[name=province]").hide();
				$("label[for=state]").show();
				$("select[name=state]").show();
				
				// Setup select(s) change handler
				$(form+" select").click(this.changeCountry);
				
				// Next & Back Button
				$("#next-button").click(this.changeStep);
				$("#back-button").click(this.changeStep);
				
				// Datepicker
				$("input[name=best_buy_code]").datepicker();
				$("input[name=date_purchased]").datepicker();
				
				// Setup from submit handler
				$("input[name=submit-button]").click(this.submitForm);
			},
			changeCountry : function( e ) {
				var t = $(this);
				var n = t.attr("name");
				var v = t.attr("value");
				if (n == "country") {
					if (v == "CN") {
						$("label[for=state]").hide(200);
						$("select[name=state]").hide(200);
						$("label[for=province]").show(200);
						$("select[name=province]").show(200);
					}
					else if (v == "US") {
						$("label[for=state]").show(200);
						$("select[name=state]").show(200);
						$("label[for=province]").hide(200);
						$("select[name=province]").hide(200);
					}
				}
			},
			changeStep : function( e ) {
				if (!$.validationEngine.loadValidation("#first_name")
						&& !$.validationEngine.loadValidation("#last_name")
						&& !$.validationEngine.loadValidation("#email")
						&& !$.validationEngine.loadValidation("#reason_for_contact")
						&& !$.validationEngine.loadValidation("#address_one")
						&& !$.validationEngine.loadValidation("#city")
						&& !$.validationEngine.loadValidation("#country")
						&& !$.validationEngine.loadValidation("#zip")) {
					
					var t = $(this);
					if (t.attr("id") == "next-button") {
						$("#step-one").fadeOut(500);
						$("#step-two").delay(400).fadeIn(500);
					}
					else if (t.attr("id") == "back-button") {
						$("#step-one").delay(400).fadeIn(500);
						$("#step-two").fadeOut(500);
					}
				}
			},
			show : function() {
				$('button[rel|="'+overlay+'"]').data('overlay').load();
				$('div#exposeMask').hover(function(){}, function(){});
			},
			close : function() {
				$('button[rel|="'+overlay+'"]').data('overlay').close();
			},
			submitForm : function( e ) {
				if ($("#contact-form").validationEngine({returnIsValid:true})) {
					var str = "Form Key: Earth Balance Contact Us Email";
						str += "\n\rFirst Name: " + $("input[name=first_name]").val();
						str += "\n\rLast Name: " + $("input[name=last_name]").val();
						str += "\n\rEmail: " + $("input[name=email]").val();
						str += "\n\rPhone: " + $("input[name=phone]").val();
						str += "\n\rReason For Contact: " + $("select[name=reason_for_contact]").val();
						str += "\n\rAddress1: " + $("input[name=address_one]").val();
						str += "\n\rAddress2: " + $("input[name=address_two]").val();
						str += "\n\rCity: " + $("input[name=city]").val();
						str += "\n\rCountry: " + $("select[name=country]").val();
						str += "\n\rState: " + $("select[name=state]").val();
						str += "\n\rProvince: " + $("select[name=province]").val();
						str += "\n\rZip Code: " + $("input[name=zip]").val();
						str += "\n\rUPC: " + $("input[name=upc_one]").val() + " " + $("input[name=upc_two]").val();
						str += "\n\rBest Buy Code: " + $("input[name=best_buy_code]").val();
						str += "\n\rStore Name: " + $("input[name=store_name]").val();
						str += "\n\rStore Intersection/City/State: " + $("input[name=store_city_state]").val();
						str += "\n\rDate Purchased: " + $("input[name=date_purchased]").val();
						str += "\n\rComments: " + $("textarea[name=comments]").val();
					
					$(".step-controls li p#response-field").text("Processing your submission.");
					
					$.ajax( { url:"includes/mailer.php",
						type:"POST",
						dataType:"json",
						data: "email="+str,
						cache: false,
						failure : function( data ) {
							$(".step-controls #response-field").text("Please try again.");
						},
						complete : function( request, status ) {
							if (status == "success") {
								$(".step-controls li p#response-field").text("Thank you for your submission.");
								
								setTimeout( function() {
									EarthBalance.ContactForm.clearForm();
									$(".step-controls li button.close").trigger("click");
								}, 2000);
							}
						}
					});
				} else {
					$(".step-controls li p#response-field").text("Please complete all required fields.");
				}
				return e.preventDefault();
			},
			clearForm : function() {
				// Clear out all visible validation prompts.
				$.validationEngine.closePrompt(".formError", true);
				
				// Clear out form values.
				$("#contact-form input[name!=submit-button]").val("");
				$("#contact-form textarea").val("");
				
				// Reset country / state select.
				$("#contact-form select[name=country] option[selected]").removeAttr("selected");
				$("#contact-form select[name=country] option[value=US]").attr("selected", "selected");
				$("#contact-form select[name=state] option[selected]").removeAttr("selected");
				$("#contact-form select[name=state] option[value='']").attr("selected", "selected");

				// Reset Province.
				$("#contact-form select[name=province] option[selected]").removeAttr("selected");
				$("#contact-form select[name=province] option[value='']").attr("selected", "selected");
				
				// Show state / hide province.
				$("label[for=state]").show(200);
				$("select[name=state]").show(200);
				$("label[for=province]").hide(200);
				$("select[name=province]").hide(200);
				
				// Reset reason for contact.
				$("#contact-form select[name=reason_for_contact] option[selected]").removeAttr("selected");
				$("#contact-form select[name=reason_for_contact] option[value='']").attr("selected", "selected");
				
				// Reset message field.
				$(".step-controls li p#response-field").text("");
				
				// Reset form page/state.
				$("#back-button").trigger("click");
			}
		}
	})();
	
	// Execute
	$(function() {
		EarthBalance.ContactForm.init();
		
		// Call from flash.
		//EarthBalance.ContactForm.show();
	});
	
})(jQuery, window);
