﻿function remove_cart_item(key) {
	document.cartForm.Items.value=key;
	document.cartForm.Action.value='Delete';
	document.cartForm.submit();
}
function add_to_cart(a) {
	a = a || [];
	var b = [], c = [];
	a.push("ProductId");
	$.each(a, function(i, n){
		b[0] = escape(n);
		b[1] = escape($("#"+n).val());
		c.push(b.join('='));
	});
	var id = escape(c.join("&"));
	location = "cart.asp?Action=Append&Items="+id+"&"+id+"="+$("#Quantity").val()+"&ReturnURL="+$("#ReturnUrl").val();
}
$(function(){
	var validateElement = function() {
		var msg = [];
		var e = $(this);
		var v = e.val();
		var q = e.attr("required");
		var c = e.attr("compare");
		var r = e.attr("regex");
		if (q) {
			var pass = false;
			if (this.type == "radio" || this.type == "checkbox" ) {
				$("input[name="+this.name+"]").each(function(ei, en) {
					if (en.checked) pass = true;
				});
			} else if ($.trim(v) != "") {
				pass = true;
			}
			if (!pass) msg.push(e.attr("requiredText"));
		}
		if (r) {
			var regex;
			switch(r.toLowerCase()) {
				case "email":
					regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
					break;
				case "number":
					regex = /^\d+$/;
					break;
				default:
					regex = new RegExp(r);
					break;
			}
			if (!regex.test(v)) {
				msg.push(e.attr("regexText"));
			}
		}
		if (c && v != $("#"+c).val()) {
			msg.push(e.attr("compareText"));
		}
		if (q || r || c) {
			$("input[name="+this.name+"]").each(function(a,b){
				$(b)[msg.length?"addClass":"removeClass"]("highlight");
			});
		}
		return msg;
	}
	var validateForm = function() {
		var msg = [];
		$.each(this.elements, function(i, n) {
			$.each(n.validate(), function(i,n){msg.push(n)});
		});
		return msg;
	}
	$.each(document.forms, function(i, n){
		n.validate = validateForm;
		$.each(n.elements, function(j, m){
			m.validate = validateElement;
			if (m.type == "radio" || m.type == "checkbox") {
				var firstone;
				$("input[name="+this.name+"]").each(function(ei, en) {
					if (en.checked) pass = true;
					var $en = $(en);
					if (ei > 0) {
						$en.removeAttr("required").removeAttr("requiredText");
					} else {
						firstone = en;
					}
					$en.click(function(){firstone.validate()});
				});
			}
			$(m).change(function(){this.validate()});
		});
		n.validated = function() {
			var msg = this.validate();
			if (msg.length) {
				if (!this.messageContainer && this.getAttribute("container"))
					this.messageContainer = $("#"+this.getAttribute("container"));
				if (this.messageContainer)
					this.messageContainer.html("<ul><li>"+msg.join("</li><li>")+"</li></ul>");
				else
					alert(msg.join("\r\n"));
				return false;
			}
			return true;
		}
		n.validateSubmit = function() {if (this.validated()) this.submit()}
		$(n).submit(function(){return this.validated()});
	});
	$('a[@rel*=lightbox]').lightBox();
})