
$(function() {
	var FIRST_LABEL = "First";
	var LAST_LABEL = "Last";
	
	var SUCCESS_MESSAGE = 'Thanks! We\'ve sent you an email with next steps. If it doesn\'t arrive soon, please <a href="/company/contact/">contact us directly</a>.';
	var DUPLICATE_EMAIL_MESSAGE = 'An account already exists with this email address. If you have previously created an account, please <a href="http://my.pulseenergy.com/">log in directly</a>.';	
	var FAILURE_MESSAGE = 'There was a problem sending your verification email. Please check your email address or <a href="/company/contact/">contact us directly</a>.';
	
	$("#tryPulseCheckButton").click(function() {
		if ($("#pulseCheckForm").is(":visible")) {
			$("#pulseCheckForm").fadeOut("slow");
		} else {
			$("#pulseCheckForm").fadeIn("slow");
		}
	});
	
	$("#otherDataloggerLabel").click(function() {
		$("#otherDatalogger").focus();
	})
	$("#otherDatalogger").focus(function() {
		$(this).prev().find(":radio").attr("checked", true);
	})
	
	$("#pulseCheckSignup").click(submitForm);
	$("#pulseCheckCancel").click(function(event) {
		$("#pulseCheckForm").fadeOut("slow");
		resetForm();
		event.preventDefault();
	});
	
	$("#noDeviceType").click(function(event) {
		$(this).parent().next().fadeIn();
		event.preventDefault();
	});
	
	$("#first_name, #last_name").bind({
		focus: function() {
			if ($(this).val() == FIRST_LABEL || $(this).val() == LAST_LABEL) {
				$(this).val("");	
			}
		},
		blur: function() {
			if ($(this).val() == "") {
				var label = this.id == "first_name" ? FIRST_LABEL : LAST_LABEL;
				$(this).val(label);
			}
		}
	});
	
	function submitForm() {
		$("#pulseCheckForm .form_error").hide()
		$("#pulseCheckForm #flash").hide();
		var submitForm = validation();
		if (submitForm) {
			updateDataloggerField();
			ajaxSubmit($("#pulseCheckForm form"));
		}
		return false; // prevent traditional submit
	}
	
	function updateDataloggerField() {
		var datalogger = $(".radioLabel :checked").val();
		if (datalogger == "Other") {
			datalogger = $("#otherDatalogger").val();
		}
		$("#datalogger").val(datalogger);
	}
	
	function validation() {
		var submitForm = true;
		$("#first_name, #last_name, #pulseCheckEmail").each(function() {
			var that = $(this);
			if (that.val() == "" || that.val() == FIRST_LABEL || that.val() == LAST_LABEL) {
				that.focus().nextAll(".form_error").show();
				submitForm = false;
				return false; // break from each
			}
			if (that.attr("id") == "pulseCheckEmail" && that.val().indexOf("@") < 0) {
				that.focus().nextAll(".form_error").show();
				submitForm = false;
				return false; // break from each
			}
		});
		return submitForm;
	}
	
	function ajaxSubmit(form) {
		setLoading(true);
		$.jsonp({
			url: $(form).attr("action"),
			dataType: "jsonp",
			data: $(form).serialize(),
			timeout: 30000,
			callbackParameter: "callback",
			success: function(json,status) {
				if (json && json.invalidEmail) {
					$("#flash").html(DUPLICATE_EMAIL_MESSAGE).show();
				} else {
					$("#flash").html(SUCCESS_MESSAGE).show();
					$("#pulseCheckForm form").fadeOut("slow");
				}
			},
			error: function(options, status) {
				$("#flash").html(FAILURE_MESSAGE).show();
			},
			complete: function() {
				setLoading(false);
			}
		});
	}

	function setLoading(loading) {
		if (loading) {
			$("#pulseCheckSignup").attr("disabled","disabled").css("opacity",0.3);
		} else {
			$("#pulseCheckSignup").removeAttr("disabled").css("opacity",1);
		}
		$("#pulseCheckForm #loading").toggle(loading);
	}
	
	function resetForm() {
		$("#pulseCheckForm .form_error").hide()
		$("#pulseCheckForm #flash").hide()
		setLoading(false);
		$("#pulseCheckForm input").not("#pulseCheckSignup").not(":radio").val("");
		$("#first_name").val(FIRST_LABEL);
		$("#last_name").val(LAST_LABEL);
	}
	
});
