//###   Add custom method - this requires the value to be the same as the first parameter   ###
jQuery.validator.methods.notequal = function(value, element, param) {
	return value != param;
};

jQuery(document).ready(function($) {
	//###############   Form focus & default text   ###############
	$("form input.required:not([type=password]), form textarea.required").each(function(i) {
		$ParentForm = $(this).parents("form");
		if ($ParentForm.attr("name") == "search")
			DefaultValue = "Search products";
		else
			DefaultValue = "required";
		$(this).attr("notequal", DefaultValue);
		if ($(this).val() == "") {$(this).val(DefaultValue).addClass("empty");}
		$(this).focus(function () {
			if ($(this).parents("form").attr("name") == "search")
				DefaultValue = "Search products";
			else
				DefaultValue = "required";
			if ($(this).val() == DefaultValue) {
				$(this).val("").removeClass("empty");
			}
		}).blur(function () {
			if ($(this).val() == "") {
				if ($(this).parents("form").attr("name") == "search")
					DefaultValue = "Search products";
				else
					DefaultValue = "required";

				$(this).val(DefaultValue).addClass("empty");
			}
		});
	});


	//###   CONTACT US FORM   ###
	$("#contact-form").validate({
		submitHandler: function(form) {
			//###   Disable the Submit button   ###
			$("#contact-form button").fadeTo("slow", 0.20).attr("disabled","disabled").addClass("disabled");

			SubmitContactUs();
			return false;
		}
	});

	//###   SEARCHING   ###
	$("#sitesearch").validate();

	//###   NEWSLETTER   ###
	$("#newsletter-form").validate({
		submitHandler: function(form) {
			//###   Disable the Submit button   ###
			$("#newsletter-form button").fadeTo("slow", 0.20).attr("disabled","disabled").addClass("disabled");

			SubmitNewsletter();
			return false;
		}
	});

	//###   LOGIN   ###
	$("#login-form").validate({
		submitHandler: function(form) {
			//###   Disable the Submit button   ###
			$("#login-form button").fadeTo("slow", 0.20).attr("disabled","disabled");

			SubmitLogin();
			return false;
		}
	});

	//###   RESET PASSWORD   ###
	$("#forgot-password-form").validate({
		submitHandler: function(form) {
			//###   Disable the Submit button   ###
			$("#forgot-password-form button").fadeTo("slow", 0.20).attr("disabled","disabled");

			SubmitPasswordReset();
			return false;
		}
	});

	//###   REGISTRATION   ###
	$("#register-form").validate({
		submitHandler: function(form) {
			//###   Disable the Submit button   ###
			$("#register-form button").fadeTo("slow", 0.20).attr("disabled","disabled");

			SubmitRegistration();
			return false;
		}
	});

	//###   QUICK-CHECKOUT   ###
	$("#quick-checkout-form").validate({
		submitHandler: function(form) {
			//###   Disable the Submit button   ###
			$("#quick-checkout-form button").fadeTo("slow", 0.20).attr("disabled","disabled");

			SubmitQuickCheckout();
			return false;
		}
	});

	//###   USER DETAILS   ###
	$("#user-details-form").validate({
		submitHandler: function(form) {
			//###   Disable the Submit button   ###
			$("#user-details-form button").fadeTo("slow", 0.20).attr("disabled","disabled");
			form.submit();
		}
	});
}); //###   End of DOM Ready   ###


//###   CONTACT US SUBMITTED   ###
function SubmitContactUs() {
	//###   Send form via AJAX   ###
	$.ajax({
		type: "POST",
		data: $("#contact-form").serialize() + "&action=" + $("#contact-form").attr("action"),
		dataType: "html",
		success: function (html) {
			//alert(html);

			if ( html.search(/error/i) >= 0 ) {
				if (html.search(/captcha/i) >= 0) {
					$("#captcha").addClass('error').after('<label class="error" for="captcha" generated="true">' + $("#captcha").attr('title') + '</label>')
				}
				$("#contact-form button").fadeTo("slow", 1).removeAttr("disabled").removeClass('disabled');

			} else {
				$("#contact-form").hide();
				$("#content .form-success").css("display", "inline");
				$("#content .form-intro").hide();
				if ( $("#newsletter-signup").is(":checked") == true) {
					SubmitNewsletter("contact-form");
				}
				ClearForm("#contact-form");
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//alert(textStatus + errorThrown);
			$("#contact-form button").fadeTo("slow", 1).removeAttr("disabled").removeClass('disabled');

		},
		complete: function (XMLHttpRequest, textStatus) {
			//alert(textStatus);
		}
	});
}


//###   LOGIN FORM SUBMISSION   ###
function SubmitLogin() {
	//###   Send form via AJAX   ###
	$.ajax({
		type: "POST",
		data: $("#login-form").serialize() + "&action=" + $("#login-form").attr("action"),
		dataType: "html",
		success: function (html) {
			//###   Successfully received an html page from the server   ###
			if ( html.search(/error/i) >= 0 ) {
				if (html.search(/username you submitted was not found/i) >= 0) {
					$("#login-username").addClass('error');
					$("#login-username").after('<label class="error" for="login-username" generated="true">Email address doesn\'t exist<br />Re-check or register an account</label>');

				} else if (html.search(/password/i) >= 0) {
					$("#login-password").addClass('error');
					$("#login-password").after('<label class="error" for="login-username" generated="true">You have entered an incorrect password</label>');

				} else if (html.search(/already logged in/i) >= 0) {
					$("#login-butn").after('<label class="error" for="login-username" generated="true">User already logged in!</label>');

				} else if (html.search(/account has not been activated yet/i) >= 0) {
					$("#login-butn").after('<label class="error" for="login-username" generated="true">Account has not been activated yet<br />Check your email and activate</label>');

				} else {
				}

				$("#login-form button").fadeTo("slow", 1).removeAttr("disabled");

			} else {
				//###   Successfully Logged in   ###

				if ( $("#checkout .content #login-group").length > 0 ) {
					//###   We're not in PrettyPopup, but on the first page of checkout   ###
					top.location.href = "http://www.pallantbookshop.com/checkout/information";
				} else {
					$("#login-form").hide();
					$("#login .form-success").slideDown("slow");

					//###   Show/hide user nav links   ###
					//$("#user-nav li:gt(1)", window.parent.document).removeClass('hidden'); //###   RESULTS IN ERROR?!?! Code correct, but due to inside Ajax Call it seems to fail?!?
					//$("#user-nav li:lt(2)", window.parent.document).addClass('hidden');
					ClearForm("#login-form");

					//###   Close PrettyPhoto Popup Window   ###
					var currentURL = new Array();
					currentURL = top.location.href.split('#');
					top.location.href = currentURL[0] + "#close-pp-delay-signin";
				}
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//###   Error occurred, could be server, CMS or 404   ###
			//alert(textStatus + errorThrown);
			$("#login-form button").fadeTo("slow", 1).removeAttr("disabled")
		},
		complete: function (XMLHttpRequest, textStatus) {
			//alert(textStatus);
		}
	});
} //###   End of SubmitLogin function   ###


//###   RESET PASSWORD FORM SUBMISSION   ###
function SubmitPasswordReset() {
	//###   Send form via AJAX   ###
	$.ajax({
		type: "POST",
		data: $("#forgot-password-form").serialize() + "&action=" + $("#forgot-password-form").attr("action"),
		dataType: "html",
		success: function (html) {
			//###   Successfully received an html page from the server   ###
			if ( html.search(/error/i) >= 0 ) {
				if (html.search(/not found/i) >= 0) {
					$("#pwd-reset-email").addClass('error');
					$("#pwd-reset-email").after('<label class="error" for="login-username" generated="true">Email address doesn\'t exist<br />Re-check or register an account</label>');

				} else if (html.search(/already logged in/i) >= 0) {
					$("#forgot-password-form button").after('<label class="error" for="login-username" generated="true">User already logged in!</label>');

				} else if (html.search(/account has not been activated yet/i) >= 0) {
					$("#forgot-password-form button").after('<label class="error" for="login-username" generated="true">Account has not been activated yet<br />Check your email and activate</label>');

				} else {
				}

				$("#forgot-password-form button").fadeTo("slow", 1).removeAttr("disabled");

			} else {
				//###   Successfully sent reset request   ###
				//"Instructions for resetting your password have just been emailed to you."
				$("#forgot-password-form").hide();
				$("#password_reminder .form-success").slideDown("slow");

				//###   Show/hide user nav links   ###
				ClearForm("#forgot-password-form");

				//###   Close PrettyPhoto Popup Window   ###
				var currentURL = new Array();
				currentURL = top.location.href.split('#');
				top.location.href = currentURL[0] + "#close-pp-delay";
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//###   Error occurred, could be server, CMS or 404   ###
			//alert(textStatus + errorThrown);
			$("#forgot-password-form button").fadeTo("slow", 1).removeAttr("disabled")
		},
		complete: function (XMLHttpRequest, textStatus) {
			//alert(textStatus);
		}
	});
} //###   End of SubmitLogin function   ###


//###   REGISTER FORM SUBMISSION   ###
function SubmitRegistration() {
	//###   Send form via AJAX   ###
	$.ajax({
		type: "POST",
		data: $("#register-form").serialize() + "&action=" + $("#register-form").attr("action"),
		dataType: "html",
		success: function (html) {
			//###   Successfully received an html page from the server   ###
			if ( html.search(/error/i) >= 0 ) {
				if (html.search(/email you chose is not available/i) >= 0) {
					$("#reg-username").addClass('error').after('<label class="error" for="reg-username" generated="true">An account with this email already exists</label>');
					$("#reg-email").addClass('error');

				} else if (html.search(/captcha/i) >= 0) {
					$("#captcha").addClass('error').after('<label class="error" for="captcha" generated="true">' + $("#captcha").attr('title') + '</label>')
				}

				$("#register-form button").fadeTo("slow", 1).removeAttr("disabled");

			} else {
				//###   Successfully Registered   ###

				if ( $("#checkout .content #login-group").length > 0 ) {
					//###   We're not in PrettyPopup, but on the first page of checkout   ###
					top.location.href = "http://www.pallantbookshop.com/checkout/information";

				} else {
					$("#register-form").hide();
					$("#register .form-success").slideDown("slow");

					//###   Show/hide user nav links   ###
					//$("#user-nav li:gt(1)", window.parent.document).removeClass('hidden');
					//$("#user-nav li:lt(2)", window.parent.document).addClass('hidden');

					ClearForm("#register-form");
					//###   Close PrettyPhoto Popup Window   ###
					top.location.href = "http://www.pallantbookshop.com/#close-pp-delay-signin";
				}
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//###   Error occurred, could be server, CMS or 404   ###
			//alert(textStatus + errorThrown);
			$("#register-form button").fadeTo("slow", 1).removeAttr("disabled")
		},
		complete: function (XMLHttpRequest, textStatus) {
			//alert(textStatus);
		}
	});
} //###   End of SubmitRegistration function   ###


//###   NEWSLETTER SUBMISSION   ###
function SubmitNewsletter(formID) {

	if (formID == "contact-form") {
		var str = "cm-name=" + $("#contact-form #fullname").val() + "&";
		str = str + "cm-jkjudr-jkjudr=" + $("#contact-form #email").val();
		//###   Serialize form values to be submitted with POST
		//var str = $("#contact-form").serialize();
		//###   Grab form action
		var formAction = "http://boost.twobelowzero.com/t/r/s/jkjudr/";
	} else {
		//###   Serialize form values to be submitted with POST
		var str = $("#newsletter-form").serialize();
		//###   Grab form action
		var formAction = $("#newsletter-form").attr("action");
	}
			
	//###   Add form action to end of serialized data
	var final = str + "&action=" + formAction;

	//###   Submit the form via ajax
	$.ajax({
		url: "/themes/site_themes/anyoccasion/js/newsletter-proxy.php",
		type: "POST",
		data: final,
		success: function(html) {
			//alert(html);
			$("#newsletter-form").hide();
			$("#newsletter-form").parent().find(".form-success").slideDown("slow");

			//###   Set Session Variable   ###
			$.ajax({
				url: "/AjaxHandler/newsletter"
			});
		}
	});
} //###   End of SubmitNewsletter function   ###


//###   Clear any form - pass form element   ###
function ClearForm(FormID) {
	//###   Reset Contact Form   ###
	$(":input", FormID).each(function() {
		var type = this.type;
		var tag = this.tagName.toLowerCase();
		if (type == 'text' || type == 'password' || tag == 'textarea') {
			this.value = "";
		}
	});
} //###   End of ClearForm function   ###

