// Set the form validator defaults
$.validator.setDefaults({
	highlight: function(input) {
		$(input).addClass("textInputOn");
	},
	unhighlight: function(input) {
		$(input).removeClass("textInputOn");
	}
});

// A custom method making the default value invalid
$.validator.addMethod("defaultInvalid", function(value, element) {
	return value != element.defaultValue;
}, "");

// Ready? Go!
$(document).ready(function() {
	// Load the autobox plugin
	$('.autobox').autobox();
	
	if ($.cookie('wholesale_cookie') == null) {
		$.cookie('wholesale_cookie', '0');
	}
	
	// Registration form validation
	$("#register-form").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			},
			Password: {
				required: true,
				minlength: 6
			}
		},
		messages: {
			FirstName: "",
			LastName: "",
			EmailAddress: "",
			Password: "",
			PasswordConfirm: {
				required: "",
				equalTo: "Passwords do not match"
			},
			ValidateBox: ""
		},
		submitHandler: function(form) {
			// Get the email address value and populate
			// username with that value
			var Email = $('#EmailAddress').val();
			$('#Username').val(Email);
			form.submit();
		},
		errorElement: "div"
	});
	
	// Wholesale application form validation
	$("#wholesale-application-form").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			},
			Password: {
				required: true,
				minlength: 6
			}
		},
		messages: {
			FirstName: "",
			LastName: "",
			EmailAddress: "",
			Company: "",
			CAT_Custom_264061: "",
			WorkPhone: "",
			ShippingAddress: "",
			ShippingCity: "",
			ShippingState: "",
			ShippingZip: "",
			BillingAddress: "",
			BillingCity: "",
			BillingState: "",
			BillingZip: "",
			Password: "",
			PasswordConfirm: {
				required: "",
				equalTo: ""
			},
			ValidateBox: ""
		},
		submitHandler: function(form) {
			// Get the email address value and populate
			// username with that value
			var Email = $('#EmailAddress').val();
			$('#Username').val(Email);
			form.submit();
		},
		errorElement: "div"
	});
	
	// Login form validation
	$("#login-form").validate({
		messages: {
			Username: "",
			Password: ""
		},
		errorElement: "div"
	});
	
	// My Account Info Form
	$("#update-info-form").validate({
		rules: {
			Password: {
				required: true,
				minlength: 6
			}
		},
		messages: {
			FirstName: "",
			LastName: "",
			EmailAddress: "",
			Username: "",
			Password: "",
			PasswordConfirm: {
				required: "",
				equalTo: "Passwords do not match"
			},
			HomePhone: "",
			HomeAddress: "",
			HomeCity: "",
			HomeState: "",
			HomeZip: ""
		},
		submitHandler: function(form) {
			form.submit();
		},
		errorElement: "div"
	});
	
	// Checkout Form
	$("#checkout_form").validate({
		rules: {
			CardName: {
				required: function(element) {
					return $("#PaymentMethodType_1").attr("checked") == "checked";
				}
			},
			CardNumber: {
				required: function(element) {
					return $("#PaymentMethodType_1").attr("checked") == "checked";
				}
			},
			CardCCV: {
				required: function(element) {
					return $("#PaymentMethodType_1").attr("checked") == "checked";
				}
			}
		},
		messages: {
			FirstName: "",
			LastName: "",
			EmailAddress: "",
			HomePhone: "",
			ShippingAddress: "",
			ShippingCity: "",
			ShippingState: "",
			ShippingZip: "",
			BillingAddress: "",
			BillingCity: "",
			BillingState: "",
			BillingZip: "",
			CardName: "",
			CardNumber: "",
			CardCCV: ""
		},
		submitHandler: function(form) {
			if($("#PaymentMethodType_3").attr("checked") == "checked")
			{
				$('#Amount').val('0.00');
			}
			$(form).Submit();
		},
		errorElement: "div"
	});
	

	// Newsletter form validation and ajax submission
	$("#newsletter-form").validate({
		messages: {
			FullName: "",
			EmailAddress: ""
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit();
			alert('Your request has been submitted successfully. Please check your email for confirmation.');
			$(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	// Search form validation
	$("#search-form").validate({
		messages: {
			CAT_Search: ""
		},
		errorElement: "div"
	});
	
	
/*	$('#rate-product-link').click(function() {
		$.scrollTo($('#rate-this-product'), 600);
		
		$('#comment-box').delay(1200).slideDown('slow');
	});*/
});

function SetBillingFromHome(checked) {
	if (checked) {
		document.getElementById('BillingAddress').value = document.getElementById('HomeAddress').value; 
		document.getElementById('BillingCity').value = document.getElementById('HomeCity').value; 
		document.getElementById('BillingState').value = document.getElementById('HomeState').value; 
		document.getElementById('BillingZip').value = document.getElementById('HomeZip').value; 
		document.getElementById('BillingCountry').value = document.getElementById('HomeCountry').value; 
	} else {
		document.getElementById('BillingAddress').value = ''; 
		document.getElementById('BillingCity').value = ''; 
		document.getElementById('BillingState').value = ''; 
		document.getElementById('BillingZip').value = ''; 
		document.getElementById('BillingCountry').value = ''; 
	}
}

function SetBillingFromShipping(checked) {
	if (checked) {
		document.getElementById('BillingAddress').value = document.getElementById('ShippingAddress').value; 
		document.getElementById('BillingCity').value = document.getElementById('ShippingCity').value; 
		document.getElementById('BillingState').value = document.getElementById('ShippingState').value; 
		document.getElementById('BillingZip').value = document.getElementById('ShippingZip').value; 
		document.getElementById('BillingCountry').value = document.getElementById('ShippingCountry').value; 
	} else {
		document.getElementById('BillingAddress').value = ''; 
		document.getElementById('BillingCity').value = ''; 
		document.getElementById('BillingState').value = ''; 
		document.getElementById('BillingZip').value = ''; 
		document.getElementById('BillingCountry').value = ''; 
	}
}

function SetShippingFromBilling(checked) {
	if (checked) {
		document.getElementById('ShippingAddress').value = document.getElementById('BillingAddress').value; 
		document.getElementById('ShippingCity').value = document.getElementById('BillingCity').value; 
		document.getElementById('ShippingState').value = document.getElementById('BillingState').value; 
		document.getElementById('ShippingZip').value = document.getElementById('BillingZip').value; 
		document.getElementById('ShippingCountry').value = document.getElementById('BillingCountry').value; 
	} else {
		document.getElementById('ShippingAddress').value = ''; 
		document.getElementById('ShippingCity').value = ''; 
		document.getElementById('ShippingState').value = ''; 
		document.getElementById('ShippingZip').value = ''; 
		document.getElementById('ShippingCountry').value = ''; 
	}
}

function SetBillingFromWork(checked) {
	if (checked) {
		document.getElementById('BillingAddress').value = document.getElementById('WorkAddress').value; 
		document.getElementById('BillingCity').value = document.getElementById('WorkCity').value; 
		document.getElementById('BillingState').value = document.getElementById('WorkState').value; 
		document.getElementById('BillingZip').value = document.getElementById('WorkZip').value; 
		document.getElementById('BillingCountry').value = document.getElementById('WorkCountry').value; 
	} else {
		document.getElementById('BillingAddress').value = ''; 
		document.getElementById('BillingCity').value = ''; 
		document.getElementById('BillingState').value = ''; 
		document.getElementById('BillingZip').value = ''; 
		document.getElementById('BillingCountry').value = ''; 
	}
}

// Pre-load images function
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
	
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

// Pre-load images
$.preLoadImages("/images/icons/bang.png");

