var preSubmitProcessor = function() {};

jQuery.validator.addMethod("checkPnr", function(c, element) {
	c = c.replace("-", "");
	c = c.replace(" ", "");
	if (c.match(/[^\d]/)) return false;
	if (c.length < 10) return false;
	
	if (c.length == 12) {
		c = c.substring(2);
	}
	
	var sum=0, m = 1, d;
 
	var last = c.charAt(c.length - 1) * 1;
 
	for (var i = c.length - 1; i >= 0; i--) {
		d = c.charAt(i) * m;
		sum += d > 9 ? d - 9 : d;
		m = 3 - m;
	}
	if (!(sum % 10)) return true;

	return false;
}, "Felaktigt personnummer");


jQuery.validator.addMethod("checkEmail", function(value, element) {
  return /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i.test(value);
}, "Felaktig e-post");


/**
 * @created 2010-01-12
 * @author Christoffer Lejdborg
 * @author Jon Gotlin
 */
function User()
{
	this.user_id = null;
	this.shoppingcart = null;
	this.customer_details = {};

	/**
	 * Create validation rules for customer details form in the checkout area.
	 *
	 * @param form_rules hashmap
	 * @return void
	 */
	this.validateCustomerDetails = function(form_rules)
	{
		$("#order_form").validate({
			success: "valid",
			rules: form_rules,
			messages: {
				accept_terms: $('#error_terms').html()
			},
			submitHandler: function(form)
			{
			  if ($('.invalid_stock').length == 0)
			  {
					preSubmitProcessor();
			    form.submit();
			  }
			  else
			  {
			    $('#total_stock_error_message').show();
			  }
			}
		});
	}
}

