// JavaScript Document
var j = jQuery.noConflict();

var signup_default = "email address";
var search_default = "Search";

function valid_email(email)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(email)) return true
	return false
}

function notify(msg)
{
	//alert(msg);	
	//j.alerts.dialogClass = "style1";
	jAlert(msg, 'Notification', function()
	{
		j.alerts.dialogClass = null; // reset to default
	});
}

/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

j(document).ready(function()
{
	j('img,input,.product-tag-best-seller,.product-tag-new').supersleight();
	/*var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
	if(IE6)
	{
		
		j("<div>")
			.css({
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				backgroundColor: 'black',
				'opacity': '0.75',
				'width': '100%',
				'height': j(window).height(),
				zIndex: 5000
			})
			.appendTo("body");
			
		j("<div><!--<img src='no-ie6.png' alt='' style='float: left;'/>--><p>We've detected that you are using Internet Explorer 6 which is an old unsupported browser. Our list of supported browsers include:<br/><br/><a href='http://www.mozilla-europe.org/en/firefox/'>Firefox</a><br/><a href='http://www.microsoft.com/windows/internet-explorer/ie7/'>Internet Explorer 7</a><br/><a href='http://www.microsoft.com/windows/internet-explorer/default.aspx'>Internet Explorer 8</a><br/><a href='http://www.apple.com/safari/'>Safari</a><br/><a href='http://www.opera.com/'>Opera</a><br/><br/>And many others...<br/></p>")
			.css({
				backgroundColor: 'white',
				'top': '50%',
				'left': '50%',
				marginLeft: -210,
				marginTop: -100,
				width: 410,
				paddingRight: 10,
				height: 200,
				'position': 'absolute',
				zIndex: 6000, 
				padding: "30px"
			})
			.appendTo("body");
	}*/
				
				
	
	// SET THE DEFAULT TEXT
	j("#semail").val(signup_default);
	
	// CHANGE TEXT ON CLICK
	j("#semail").focus(function()
	{
		if(j(this).val() == signup_default)
		{
			j(this).val("");
		}
	});
	
	// REVERT TEXT ON LEAVE
	j("#semail").blur(function()
	{
		if(j(this).val() == "")
		{
			j(this).val(signup_default);
		}
	});
	
	// SET THE DEFAULT TEXT
	//j("#shop-search .textbox").val(search_default);
	
	// CHANGE TEXT ON CLICK
	j("#shop-search .textbox").focus(function()
	{
		if(j(this).val() == search_default)
		{
			j(this).val("");
		}
	});
	
	// REVERT TEXT ON LEAVE
	j("#shop-search .textbox").blur(function()
	{
		if(j(this).val() == "")
		{
			j(this).val(search_default);
		}
	});
	
	// SHOP CATEGORY CLICK
	j("#shop-categories > ul > li:not(.selected) > a").live("click", function()
	{
		var index = j("#shop-categories > ul > li > a").index(this);
		j("#shop-categories > ul > li > ul:not("+index+")").slideUp().parent().removeClass("selected");
		j("#shop-categories > ul > li > ul:eq("+index+")").parent().addClass("selected");
		j("#shop-categories > ul > li > ul:eq("+index+")").slideDown();
	});
	
	
	
	j("div.rounded-box").wrap('<div class="rounded-corners">'+
        '<div class="bd">'+
        '<div class="c">'+
        '<div class="s">'+
        '</div>'+
        '</div>'+
        '</div>'+
        '</div>');
	
	j('div.rounded-corners').prepend('<div class="hd">'+
			'<div class="c"></div>'+
			'</div>')
	.append('<div class="ft">'+
			'<div class="c"></div>'+
			'</div>');
	
	/*j(".shadow").dropShadow({left:2,top:2,opacity:0.3});*/

	
	j(".newsletter-signup-submit").click(function()
	{
		var email = j("input[name=semail]").val();
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=newsletter-signup&semail="+email,
			dataType: "json",
			success: function(response)
			{
				notify(response.status_detail);
			}
		});
		j("input[name=semail]").val(signup_default);
		return false;
	});
		
		
	j(".brochure-form").submit(function()
	{
		var errors = [];
		var params = "";
		
		// VALIDATION
		j(".mandatory").each(function()
		{
			// CHECK FOR VALID EMAIL ADDRESS
			if(j(this).attr("class").indexOf("email") != -1)
			{
				if(!valid_email(j(this).val()))
				{
					errors.push("'"+j(this).prev().prev().text()+"' is not a valid email address");
				}
			}
			else
			{
				if(j(this).val() == "")
				{
					errors.push("Must specify '"+j(this).prev().prev().text()+"'");
				}
			}
		});
		
		if(errors.length > 0)
		{
			notify(errors.join("\n"));
			return false;
		}
		else
		{
			j(".brochure-form input,.brochure-form textarea").each(function()
			{
				params += "&"+j(this).attr("name")+"="+j(this).val();
			});
			
			j.ajax({
				type: "POST",
				url: "/lc-rpc.php",
				data: "action=brochure-request"+params,
				beforeSend : function(req)
				{
					//req.setRequestHeader('Authorization', "Basic "+j.base64Encode("oiadmin" + ':' + "offshore"));
				},
				success: function(response)
				{
					j(".brochure-form .status").fadeOut("slow").text("Thanks, we have received your request!").fadeIn("slow");
					j(".brochure-form input:not(.submit)").val("");
				}
			});
		}
		return false;
	});
	
	j(".search-form").submit(function()
	{		
		location.href = "/shop/search/?keywords="+j(".search-form input[name=keywords]").val();	
		return false;
	});
	
	j(".contact-form").submit(function()
	{
		var errors = [];
		var params = "";
		
		// VALIDATION
		j(".mandatory").each(function()
		{
			// CHECK FOR VALID EMAIL ADDRESS
			if(j(this).attr("class").indexOf("email") != -1)
			{
				if(!valid_email(j(this).val()))
				{
					errors.push("'"+j(this).prev().prev().text()+"' is not a valid email address");
				}
			}
			else
			{
				if(j(this).val() == "")
				{
					errors.push("Must specify '"+j(this).prev().prev().text()+"'");
				}
			}
		});
		
		if(errors.length > 0)
		{
			notify(errors.join("\n"));
			return false;
		}
		else
		{
			params = "";
			j(".contact-form input").each(function()
			{
				params += "&"+j(this).attr("name")+"="+j(this).val();
			});
			
			j.ajax({
				type: "POST",
				url: "/lc-rpc.php",
				data: "action=enquiry-submit"+params,
				beforeSend : function(req)
				{
					//req.setRequestHeader('Authorization', "Basic "+j.base64Encode("oiadmin" + ':' + "offshore"));
				},
				success: function(response)
				{
					j(".contact-form .status").fadeOut("slow").text("Thanks, we have received your enquiry!").fadeIn("slow");
					j(".contact-form input:not(.submit),.contact-form textarea").val("");
				}
			});
		}
		return false;
	});
	
	// REGISTER FORM USERNAME CHECK
	j(".register-form input[name=rusername]").blur(function()
	{	
		j(".register-form .status").hide().text("");
		
		var str = trim(j(this).val());
		var username_pattern = new RegExp("[^0-9a-zA-Z_]+");
		
		if(str.length < 5 || str.length > 12)
		{
			j(".register-form .status").text("Username must be between 5 and 12 characters").fadeIn();
			return false;
		}
		
		if(username_pattern.test(str))
		{
			j(".register-form .status").hide().text("Username must only contain letters, numbers and underscores").fadeIn();
			j(this).focus();
			return false;
		}
				
		params = "";
		j(".register-form input").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=register-username-exists"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					// USERNAME TAKEN
					j(".register-form .status").text(response.status_detail).fadeIn();
				}
			}
		});
	});
	
	j("#login-form").submit(function()
	{		
		j("#login-form .status").hide().text("");
		params = "";
		
		var rd = j("#login-form input[name=rd]").val();
		rd = Url.encode(rd);
		
		j("#login-form input").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=login-submit"+params,
			dataType: "json", 
			//dataType: "text", 
			success: function(response)
			{
				if(response.status == "true")
				{
					location.href = "/shop/account/?rd="+rd;
				}
				else
				{
					j(".login-form .status").text(response.status_detail).fadeIn();
					j(".login-form input:not(.submit)").val("");
				}
				
			}
		});
		return false;
		
	});
	
	j(".reminder-form").submit(function()
	{		
		j(".reminder-form .status").hide().text("");
		params = "";
		j(".reminder-form input").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=reminder-submit"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					notify(response.status_detail);
					setTimeout(function(){location.href="/shop/login-register/"}, 2000);
					//j(".reminder-form .status").text(response.status_detail).fadeIn();
					//j(".reminder-form input:not(.submit)").val("");
				}
				else
				{
					j(".reminder-form .status").text(response.status_detail).fadeIn();
				}
				
			}
		});
		return false;
		
	});
	
	
	j("form[name=shipping-form],form[name=payment-form]").submit(function()
	{		
		j(".mandatory").removeClass("mandatory-field");
		var mandatory_fields = j(".mandatory");
		var error = false;
		j(mandatory_fields).each(function()
		{
			if(this.tagName == "INPUT" || this.tagName == "TEXTAREA" || this.tagName == "SELECT")
			{
				if(j(this).val() == "" && error == false)
				{
					j(this).addClass("mandatory-field");
					alert("Must fill in mandatory fields");
					j(this).focus().select();
					error = true;
				}
			}
		});
		
		if(error)
			return false;
		return true;
		
		
	});
	
	/** REGISTER SUBMIT **/
	j("form[name=register-form]").submit(function()
	{		
		j(".mandatory").removeClass("mandatory-field");
		
		// CHECK EMAIL ADDRESSES ARE VALID AND MATCH
		if(!valid_email(j("input[name=billing_email]").val()))
		{
			j("input[name=billing_email]").addClass("mandatory-field");
			alert("Must supply a valid email address");
			return false;
		}
		
		/*
		if(j("input[name=billing_email]").val() != j("input[name=billing_email2]").val())
		{
			j("input[name=billing_email2]").addClass("mandatory-field");
			alert("Email addresses do not match");
			return false;
		}
		*/
		
		
		var pwd_pattern = new RegExp("[^0-9a-zA-Z_]+");
			
		if(j("input[name=billing_password]").val().length < 5 || j("input[name=billing_password]").val().length > 12)
		{
			j("input[name=billing_password]").addClass("mandatory-field");
			alert("Password must be between 5 and 12 characters");
			return false;
		}
		
		if(pwd_pattern.test(j("input[name=billing_password]").val()))
		{
			j("input[name=billing_password]").addClass("mandatory-field");
			alert("Password must only contain letters, numbers and underscores");
			return false;
		}
	
		if(j("input[name=billing_password]").val() != j("input[name=billing_password2]").val())
		{
			j("input[name=billing_password2]").addClass("mandatory-field");
			alert("Passwords do not match");
			return false;
		}
		
		var mandatory_fields = j(".mandatory");
		var error = false;
		j(mandatory_fields).each(function()
		{
			if(this.tagName == "INPUT" || this.tagName == "TEXTAREA" || this.tagName == "SELECT")
			{
				if(j(this).val() == "" && error == false)
				{
					j(this).addClass("mandatory-field");
					alert("Must fill in mandatory fields");
					j(this).focus().select();
					error = true;
				}
			}
		});
		
		if(error)
			return false;
			
			
		
		if(j("input[name=terms]").attr("checked") != true)
		{
			alert("Must agree to terms and conditions");
			return false;
		}
		
			
		return true;
		
		
	});
	
	// REGISTER FORM EMAIL CHECK
	j("form[name=register-form] input[name=billing_email]").blur(function()
	{			
		j(".register-form .status").hide().text("");
		
		j(".mandatory").removeClass("mandatory-field");
		
		// CHECK EMAIL IS VALID
		params = "";
		j("form[name=register-form] input").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=register-email-exists"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					// EMAIL TAKEN
					//j(".register-form .status").addClass("error").hide().text(response.status_detail).fadeIn();
					
					j("form[name=register-form] input[name=billing_email]").addClass("mandatory-field");
					alert("Email address is already taken");
					//j("form[name=register-form] input[name=billing_email]").focus();
				}
			}
		});
	});
	
	/** BILLING SUBMIT **/
	j("form[name=billing-form]").submit(function()
	{		
		j("input,select").removeClass("mandatory-field");
		
		
		
		var mandatory_fields = j(".mandatory");
		var error = false;
		j(mandatory_fields).each(function()
		{
			if(this.tagName == "INPUT" || this.tagName == "TEXTAREA" || this.tagName == "SELECT")
			{
				if(j(this).val() == "" && error == false)
				{
					j(this).addClass("mandatory-field");
					alert("Must fill in mandatory fields");
					j(this).focus().select();
					error = true;
				}
			}
		});
		
		if(!error)
		{
			// CHECK EMAIL ADDRESSES ARE VALID AND MATCH
			if(!valid_email(j("input[name=billing_email]").val()))
			{
				j("input[name=billing_email]").addClass("mandatory-field");
				alert("Must supply a valid email address");
				return false;
			}
			
			/*
			if(j("input[name=billing_email2]").length > 0)
			{
				if(j("input[name=billing_email]").val() != j("input[name=billing_email2]").val())
				{
					j("input[name=billing_email2]").addClass("mandatory-field");
					alert("Email addresses do not match");
					return false;
				}
			}
			*/
			
			if(j("input[name=billing_password]").length > 0)
			{
				var pwd_pattern = new RegExp("[^0-9a-zA-Z_]+");
					
				if(j("input[name=billing_password]").val().length < 5 || j("input[name=billing_password]").val().length > 12)
				{
					j("input[name=billing_password]").addClass("mandatory-field");
					alert("Password must be between 5 and 12 characters");
					return false;
				}
				
				if(pwd_pattern.test(j("input[name=billing_password]").val()))
				{
					j("input[name=billing_password]").addClass("mandatory-field");
					alert("Password must only contain letters, numbers and underscores");
					return false;
				}
			
				if(j("input[name=billing_password]").val() != j("input[name=billing_password2]").val())
				{
					j("input[name=billing_password2]").addClass("mandatory-field");
					alert("Passwords do not match");
					return false;
				}
			}
			
			if(j("input[name=terms]").length > 0)
			{
				if(j("input[name=terms]").attr("checked") != true)
				{
					alert("Must agree to terms and conditions");
					return false;
				}
			}
		}
		
		if(error)
			return false;
		return true;
		
		
	});
	
	// BILLING FORM EMAIL CHECK
	j("form[name=billing-form] input[name=billing_email]").blur(function()
	{			
		j(".register-form .status").hide().text("");
		
		j(".mandatory").removeClass("mandatory-field");
		
		// CHECK EMAIL IS VALID
		params = "";
		j("form[name=billing-form] input").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=register-email-exists"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					// EMAIL TAKEN
					//j(".register-form .status").addClass("error").hide().text(response.status_detail).fadeIn();
					
					j("form[name=billing-form] input[name=billing_email]").addClass("mandatory-field");
					alert("Email address is already taken");
					//j("form[name=register-form] input[name=billing_email]").focus();
				}
			}
		});
	});
	
	
	
	
	
	j(".account-form").submit(function()
	{		
		j(".account-form .status").hide().html("");
		
		params = "";
		j(".account-form input,.account-form textarea,.account-form select").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=account-submit"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					j(".account-form .status").html(response.status_detail).fadeIn();
				}
				else
				{
					j(".account-form .status").html(response.status_detail).fadeIn();
				}
				
			}
		});
		return false;
	});
	
	j(".subscriptions-form").submit(function()
	{		
		j(".subscriptions-form .subscriptions-status").hide().text("");
		
		params = "";
		j(".subscriptions-form input:checked").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=subscriptions-update"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					j(".subscriptions-form .subscriptions-status").text(response.status_detail).fadeIn();
				}
				else
				{
					j(".subscriptions-form .subscriptions-status").text(response.status_detail).fadeIn();
				}
				
			}
		});
		return false;
	});
	
	j(".add-to-cart-link").click(function()
	{
		j(this).closest("form").submit();
		return false;
	});
	
	j(".product-form").submit(function()
	{
		//j(".account-form .status").hide().text("");
		var index = j(".product-form").index(this);
		params = "";
		j(".product-form:eq("+index+") input,.product-form:eq("+index+") textarea,.product-form:eq("+index+") select").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
	
	
		
		//alert(params); return false;
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=add-to-cart"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					//j("#popup_content").css("");
					j.alerts.dialogClass = "style_1";
					j(".notify-add-to-cart-html .product-title").text(response.product_title);
					var notify_html = j(".notify-add-to-cart-html").html();
					notify(notify_html);
					update_cart();
				}
				else
				{
					var notify_html = j(".notify-insufficient-quantity-html").html();
					notify(notify_html);
					//j(".account-form .status").text(response.status_detail).fadeIn();
				}
			}
		});
		
		return false;
	});
	
	function update_cart()
	{
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=update-cart-data",
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					j(".shopping-cart-count").text((response.cart_count == 1)?("1 item"):(response.cart_count+" items"));
					j(".shopping-cart-total").text(response.cart_total);
				}
				else
				{
					//j(".account-form .status").text(response.status_detail).fadeIn();
				}
			}
		});
	}
	
	j(".product-form .add-to-wishlist").click(function()
	{
		//j(".account-form .status").hide().text("");
		//alert("TEST");
		params = "";
		j(".product-form input,.product-form textarea,.product-form select").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});
		
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=add-to-wishlist"+params,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					notify("Item added to wishlist!");
				}
				else
				{
					notify(response.status_detail);
				}
			}
		});
		return false;
	});
	
	j(".add-to-wishlist-link").click(function()
	{
		//j(".account-form .status").hide().text("");
		//alert("TEST");
		/*params = "";
		j(".product-form input,.product-form textarea,.product-form select").each(function()
		{
			params += "&"+j(this).attr("name")+"="+j(this).val();
		});*/
		
		params = j(this).attr("rel").split("|");
		
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=add-to-wishlist&product_id="+params[0]+"&customer_id="+params[1],
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					notify("Item added to wishlist!");
				}
				else
				{
					notify(response.status_detail);
				}
			}
		});
		return false;
	});
	
	
	j("input[name=billing-to-shipping]").click(function()
	{
		
		if(j(this).attr("checked"))
		{
			j("select[name=shipping_title] option").each(function()
			{
				if(j(this).val() == j("input[name=billing_title]").val())
				{
					j(this).attr("selected", true);	
				}
			});
			//j(".shipping-form select[name=shipping_title]").val(j(".shipping-form input[name=billing_title]").val());
			j("input[name=shipping_firstname]").val(j("input[name=billing_firstname]").val());
			j("input[name=shipping_lastname]").val(j("input[name=billing_lastname]").val());
			j("textarea[name=shipping_address_1]").val(j("input[name=billing_address_1]").val());
			j("input[name=shipping_city]").val(j("input[name=billing_city]").val());
			j("input[name=shipping_postcode]").val(j("input[name=billing_postcode]").val());
			j("input[name=shipping_county]").val(j("input[name=billing_county]").val());
			
			//j("select[name=shipping_country]").val(j("select[name=billing_country]").val());
			
			j("select[name=shipping_country] option").each(function()
			{
				if(j(this).val() == j("input[name=billing_country]").val())
				{
					j(this).attr("selected", true);	
				}
			});
			
			j("input[name=shipping_telephone]").val(j("input[name=billing_telephone]").val());
			j("input[name=shipping_email]").val(j("input[name=billing_email]").val());
		}
		else
		{
			j("select[name=shipping_title]").val("");
			j("input[name=shipping_firstname]").val("");
			j("input[name=shipping_lastname]").val("");
			j("textarea[name=shipping_address_1]").val("");
			j("input[name=shipping_city]").val("");
			j("input[name=shipping_postcode]").val("");
			j("input[name=shipping_county]").val("");
			j("select[name=shipping_country]").val("");
			j("input[name=shipping_telephone]").val("");
			j("input[name=shipping_email]").val("");
		}
	});
	
	/*
	j("input[name=shipping-to-billing]").click(function()
	{
		if(j(this).attr("checked"))
		{
			j(".checkout-form input[name=billing_name]").val(j(".checkout-form input[name=shipping_name]").val());
			j(".checkout-form textarea[name=billing_address_1]").val(j(".checkout-form textarea[name=shipping_address_1]").val());
			j(".checkout-form input[name=billing_city]").val(j(".checkout-form input[name=shipping_city]").val());
			j(".checkout-form input[name=billing_postcode]").val(j(".checkout-form input[name=shipping_postcode]").val());
			j(".checkout-form input[name=billing_county]").val(j(".checkout-form input[name=shipping_county]").val());
			j(".checkout-form select[name=billing_country]").val(j(".checkout-form input[name=shipping_country]").val());
			j(".checkout-form input[name=billing_telephone]").val(j(".checkout-form input[name=shipping_telephone]").val());
			j(".checkout-form input[name=billing_email]").val(j(".checkout-form input[name=shipping_email]").val());
		}
		else
		{
			j(".checkout-form input[name=billing_name]").val("");
			j(".checkout-form textarea[name=billing_address_1]").val("");
			j(".checkout-form input[name=billing_city]").val("");
			j(".checkout-form input[name=billing_postcode]").val("");
			j(".checkout-form input[name=billing_county]").val("");
			j(".checkout-form select[name=billing_country]").val("");
			j(".checkout-form input[name=billing_telephone]").val("");
			j(".checkout-form input[name=billing_email]").val("");
		}
	});
	*/
	
	j("#card_type").change(function()
	{
		
		if(j(this).val() == "MAESTRO" || j(this).val() == "SOLO")
		{
			j(".card-start-row").show();
			j(".card-issue-row").show();
		}
		else
		{
			j(".card-start-row").hide();
			j(".card-issue-row").hide();
		}
		
		
	});
	
	j(".shopping-bag-shipping-country").change(function()
	{
		location.href = "/shop/view-cart/?shipping_country="+j(this).val();
	});
	
	j("select[name=shipping_method]").change(function()
	{
		location.href = "/shop/view-cart/?shipping_method="+j(this).val();
	});
	
	j(".quantity input[name=qty]").change(function()
	{
		var basket_item_id = j(this).next().val();
		var basket_item_qty = j(this).val();
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=update-basket-item-qty&basket_item_id="+basket_item_id+"&basket_item_qty="+basket_item_qty,
			dataType: "json", 
			success: function(response)
			{
				location.reload(true);
			}
		});
		
		
	});
	
	j(".bag-update-link").click(function()
	{
		var basket_item_id = j(this).parent().prev().children("input[name=basket_item_id]").val();
		var basket_item_qty = j(this).parent().prev().children("input[name=qty]").val();
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=update-basket-item-qty&basket_item_id="+basket_item_id+"&basket_item_qty="+basket_item_qty,
			dataType: "json", 
			success: function(response)
			{
				if(response.status == "true")
				{
					location.reload(true);
				}
				else
				{
					var notify_html = j(".notify-insufficient-quantity-html").html();
					notify(notify_html);
					
					j("#view-cart-form")[0].reset();
					location.reload(true);
				}
				
			}
		});
		
		
	});
	
	j(".back-link").click(function()
	{
		history.back(true);
		return false;
	});
	
	j(".bag-remove-link").click(function()
	{
		var basket_item_id = j(this).parent().prev().children("input[name=basket_item_id]").val();
		var basket_item_qty = 0;
		j.ajax({
			type: "POST",
			url: "/lc-rpc.php",
			data: "action=update-basket-item-qty&basket_item_id="+basket_item_id+"&basket_item_qty="+basket_item_qty,
			dataType: "json", 
			success: function(response)
			{
				location.reload(true);
			}
		});
		
		
	});
	
	j("#signup .textbox").focus(function()
	{
		if(j(this).val() == "email address")
		{
			j(this).val("");
		}
	});
	
	j("#signup .textbox").blur(function()
	{
		if(j(this).val() == "")
		{
			j(this).val("email address");
		}
	});
	
	j("#search-form").submit(function()
	{
		if(j("#search-form input[name=keywords]").val() == "")
		{
			notify("<p>No search terms entered</p>");
			return false;
		}
	});
	
	j("#checkout-sagepay-form, #checkout-paypal-form").submit(function()
	{
		if(j("select#shipping_country").val() == "")
		{
			notify("<p>You must choose a shipping country first</p>");
			return false;
		}
	});
	
	
	j("input[name=gift-wrapping]").click(function()
	{
		if(j(this).attr("checked"))
		{
			location.href = "?gift-wrapping=1";
		}
		else
		{
			location.href = "?gift-wrapping=0";
		}
	});
	
	j(".product-additional-detail-box ul li a").click(function()
	{
		var index = j(".product-additional-detail-box ul li a").index(this);
		
		j(".product-additional-detail-box ul li a").removeClass();
		j(this).addClass("selected");
		
		
		j(".product-additional-detail").hide();
		j(".product-additional-detail:eq("+index+")").show();
		return false;
	});
	
	
	j(function(){j(".product-detail .image a").lightBox({fixedNavigation:true});});
	

});
