$(document).ready(function() {
	// functions for login form (login.ppf)
	$('#loginName, #loginPass').validate({
		empty: {},
		callback: function() {
			var result = $.Validate.resultsSimplify(this);
			if (result == null) return;

			if (!result) {
				$(this).parents("div[class*=row]:first").addClass("error");
				if (!$("#span_rf_" + $.Util.id(this)).length) {
					$(this).after("<span style='padding-left: 10px;' id='span_rf_" + $.Util.id(this) + "'>Required Field</span>");
				}
			}
			else {
				$(this).parents("div[class*=row]:first").removeClass("error");
				$("#span_rf_" + $.Util.id(this)).remove();
			}
		}
	});
	$('#loginSubmit').validate({
		callback: function() {
			var result = $.Validate.resultsSimplify(this);
			if (result !== true) return false;

			return true;
		}
	});

	// functions for "I forgot my username."
	$("#help_user_a").click(function() {
		if ($("#help_user_form").css("display") == "none") {
			$("#help_user_a").css("text-decoration", "underline");
			$("#help_user_form").show('fast');
			$("#help_user_text").focus();
		}
		else {
			$("#help_user_a").css("text-decoration", "none");
			$("#help_user_form").hide('fast');
			$("#help_user_text").blur();
		}

		return false;
	});
	$("#help_user_btn").click(function() {
		if ($('#help_user_text').val().length) {
			$('#help_user_text').attr('readonly', 'readonly');
			$.get(
				'ws.php?output=text&token=' + WS_TOKEN + '&module=core&ppf=login.ws', {
					help_user: $('#help_user_text').val()
				},
				function() {
					$('#help_user_text').blur();
					$('#help_user_form').children().slideUp('fast').end().append('<span style="display: none;">An email with the username has been sent to the address provided.</span>').children(':last').slideDown();
				}
			);
		}
	});

	// functions for "I forgot my password."
	$("#help_pass_a").click(function() {
		if ($("#help_pass_form").css("display") == "none") {
			$("#help_pass_a").css("text-decoration", "underline");
			$("#help_pass_form").show('fast');
			$("#help_pass_text").focus();
		}
		else {
			$("#help_pass_a").css("text-decoration", "none");
			$("#help_pass_form").hide('fast');
			$("#help_pass_text").blur();
		}

		return false;
	});
	$("#help_pass_btn").click(function() {
		if ($('#help_pass_text').val().length) {
			$('#help_pass_text').attr('readonly', 'readonly');
			$.get(
				'ws.php?output=text&token=' + WS_TOKEN + '&module=core&ppf=login.ws', {
					help_pass: $('#help_pass_text').val()
				},
				function() {
					$('#help_pass_text').blur();
					$('#help_pass_form').children().slideUp('fast').end().append('<span style="display: none;">An email with instructions on resetting your password has been sent.</span>').children(':last').slideDown();
				}
			);
		}
	});

	// functions for "I do not have an account."
	$("#help_noid_a").click(function() {
		if ($("#form_nologin_contact").parent().css("display") == "none") {
			$("#help_noid_a").css("text-decoration", "underline");
			$("#form_login").parent().hide('fast');
			$("#form_nologin_contact").parent().show('fast');
			$("#form_nologin_contact").find(":input:first").focus();
		}
		else {
			$("#help_noid_a").css("text-decoration", "none");
			$("#form_nologin_contact").parent().hide('fast');
			$("#form_login").parent().show('fast');
			$("#form_login").find(":input:first").focus();
		}
		return false;
	});
	$('#form_nologin_contact-name, #form_nologin_contact-email, #form_nologin_contact-subject').validate({
		empty: {},
		callback: function() {
			var result = $.Validate.resultsSimplify(this);
			if (result == null) return;

			if (!result) {
				$(this).parents("div[class*=row]:first").addClass("error");
				if (!$("#span_rf_" + $.Util.id(this)).length) {
					$(this).after("<span style='padding-left: 10px;' id='span_rf_" + $.Util.id(this) + "'>Required Field</span>");
				}
			}
			else {
				$(this).parents("div[class*=row]:first").removeClass("error");
				$("#span_rf_" + $.Util.id(this)).remove();
			}
		}
	});
	$("#form_nologin_contact-submit").validate({
		callback: function() {
			var result = $.Validate.resultsSimplify(this);
			if (result !== true) return false;

			return true;
		}
	});
});
