
function clear_recently_viewed() {
	$.ajax({ 
		type: "GET", 
		url: "/ajax/clear_recently_viewed.cfm", 
		data: "", 
		dataType: "html",
		success: function(msg){
			$("#RecentlyViewedWrap").hide("slow");
			$("#RecentlyViewedWrap").remove();
		}
	});
}


function trim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}



function setReviewLength(text1, text2, limit) {
	if (text1.value.length > limit) {
		text1.value = text1.value.substring(0, 1000);
	}
	text2.value = limit - text1.value.length;
}


function changeNumRows() {
	var box = document.getElementById("rows");	
	var val = box.options[box.selectedIndex].value;
	location.href = "";
}

function onAnimate(show) {
	//$(this).fadeIn('fast').show();
	if (show) {
		$(this)
			.css('visibility', 'hidden').show()
				.css('width', $(this).innerWidth())
			.hide().css('visibility', 'visible')
		.fadeIn('fast');
	} else {
		$(this).fadeOut('fast');
	}
}

var MENU_COUNTER = 1;
function loadMenu() {
	if (this.id == 'dynamicMenu') {
		$('> ul > li', this).remove();

		var ul = $('<ul></ul>');
		var t = MENU_COUNTER + 10;
		for (; MENU_COUNTER < t; MENU_COUNTER++) {
			$('> ul', this).append('<li>Item ' + MENU_COUNTER + '</li>');
		}
	}
}

function unloadMenu() {
	if (MENU_COUNTER >= 30) {
		MENU_COUNTER = 1;
	}
}

// We're passed a UL
function onHideCheckMenu() {
	return !$(this).parent().is('.LOCKED');
}

// We're passed a LI
function onClickMenu() {
	$(this).toggleClass('LOCKED');
	return true;
}

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}


function doFilter(type, e) {
	var myID = e.options[e.selectedIndex].value;	
	var qs = new Querystring();
	var catid = qs.get("catid", "");
	var manuf = qs.get("manuf", "");
	var gender = qs.get("gender", "");
	var location = "";
	if (type == "manufacturer") {
		location += "/ApplyFilterSettings/?";
		if (catid != "") location += "catid=" + catid + "&";
		if (gender != "") location += "gender=" + gender + "&";
		location += "manuf=" + myID;
	}
	if (type == "subcat") {
		location += "/?";
		if (manuf != "") location += "manuf=" + manuf + "&";
		if (gender != "") location += "gender=" + gender + "&";
		location += "catid=" + myID;
	}
	if (type == "gender") {
		location += "/?";
		if (catid != "") location += "catid=" + catid + "&";
		if (manuf != "") location += "manuf=" + manuf + "&";
		location += "gender=" + myID;
	}
	self.location.href = location;
}

function highlightMe(e, color) {
	e.style.backgroundColor = color;	
}
/*
function changeOption() {
	if (document.getElementById("selectOption") && $("input[name=prod_unavailable]").val() != 1) {
		var title = "[SELECT " + $("#OriginalOptionDesc").val().toUpperCase() + "]";
		var box = document.getElementById("selectOption");
		var val = box.options[box.selectedIndex].value;
		$("#selectOption option").each(function(i){
			if ($(this).attr("value") == val) {
				title = $(this).text();
				$("main_basket_option").val($(this).attr("value"));
				inStock = $(this).attr("class");
				inStock = inStock.replace("S_", "");
				inStock = parseInt(inStock);
				if (inStock < 1) {
					$("div.AddButton").hide();
					$("div.OutOfStock").remove();
					code = '<div class="OutOfStock">';
					code += 'Sorry, this item is out of stock.<br /><a href="/content/EmailWhenItemInStock/?prodid=' + $(".ajax_ProdID").val() + '&amp;optionid=' + $(this).attr("value") + '" target="_blank" id="EmailWhenItemInStock">Mail me when it\'s in!</a>';	
					code += '</div>';	
					$("div.AddButton").before(code);
					$("#EmailWhenItemInStock").bind("click", function(){
						window.open($(this).attr("href"), 'email_stock_window', 'location=yes,menubar=no,resizeable=yes,status=yes,toolbar=no,width=600,height=400');		
						return false;
					});
				} else {
					$("div.AddButton").show();
					$("div.OutOfStock").remove();
				}
			}
		});
		if (val == "") {
			$("div.AddButton").show();
			$("div.OutOfStock").remove();
		}
		$(".ajax_Option").html(title);	
	}
}
*/
function changeOption() {
	if (document.getElementById("selectOption") && $("input[name=prod_unavailable]").val() != 1) {
		var title = "[SELECT " + $("#OriginalOptionDesc").val().toUpperCase() + "]";
		var box = document.getElementById("selectOption");
		var val = box.options[box.selectedIndex].value;
		$("#selectOption option").each(function(i){
			if ($(this).attr("value") == val) {
				title = $(this).text();
				$("main_basket_option").val($(this).attr("value"));
				inStock = $(this).attr("class");
				inStock = inStock.replace("S_", "");
				inStock = parseInt(inStock);
				if (inStock < 1) {
					if (inStock == -10) {
						$("#ProductStockDisplay").html('<span class="OutOfStock">Temp. Unavailable</span>');
					} else if (inStock == -30) {
						$("#ProductStockDisplay").html('<span class="PreOrder">Pre-Order</span>');
					} else {
						$("#ProductStockDisplay").html('<span class="OutOfStock">Out of stock - Expected in 3-5 days</span>');
					}
				} else {
					$("#ProductStockDisplay").html('<span class="InStock">In stock</span>');
				}
			}
		});
		if (val == "") {
			$("#ProductStockDisplay").html('-');
		}
		$(".ajax_Option").html(title);	
	}
}
function showReviewForm(e) {
	$_element = $(e);
	$("#ReviewForm").css("display", "block");
	return true;
}

function setLoadingMessage() {

	("#LoadColour").ajaxSend(function(request, settings){
		$(this).show();									   
	});

	$("#LoadColour").ajaxStop(function(){
		$(this).hide();
	});
}


function changeColour(id) {
	
	var catid = $(".ajax_CatID").val();
	
	$.ajax({ 
		type: "POST", 
		url: "/xml/prodinfo.xml", 
		data: "prodid=" + id + "&catid=" + catid, 
		dataType: "xml",
		success: handleResponse
	});
	
	var returnval = false;
	
	function handleResponse(data) {
		if ($(data).find("error").text() != "") {
			self.location.href = '/?catid=' + catid + '&prodid=' + id;		
		}
	
		$_CompleteTitle = $(data).find("ManufacturerTitle").text() + " " + $(data).find("Title").text() + " - " + $(data).find("Colour").text();
		$_ProdID = $(data).find("Product").attr("id");
		$_Title = $(data).find("Title").text();
		$_ManufTitle = $(data).find("ManufacturerTitle").text();
		$_ProductDetails = $(data).find("ProductDetails").text();
		$_ImageStandard_URL = $(data).find("ImageStandard_URL").text();
		$_ImageLarge_URL = $(data).find("ImageLarge_URL").text();
		$_Colour = $(data).find("Colour").text();
		$_CompleteTitleWithColour = $_ManufTitle + " " + $_Title + " - " + $_Colour;
		$_ColourImageURL = $(data).find("ColourImageURL").text();
		$_OptionDescription = $(data).find("OptionDescription").text();
		$_Price = $(data).find("Price").text();
		$_RRP = $(data).find("RRP").text();
		$_StockCode = $(data).find("StockCode").text();
		
		if (document.getElementById("selectOption")) {
			// has multiple options	
			
			var sel_OptionTitle = "";
			var box = document.getElementById("selectOption");
			var val = box.options[box.selectedIndex].childNodes[0].nodeValue;
			
			$("#selectOption").empty();
			newVal = "";
			$("#selectOption").append('<option value="">-- SELECT ' + $_OptionDescription.toUpperCase() + ' --</option>');
			$(data).find("Option").each(function(i){
				id = $(this).attr("id");
				instock = $(this).attr("instock");
				title = $(this).text();
				if (title == val) {
					newVal = i + 1;	
				}
				$("#selectOption").append('<option value="' + id + '" class="S_' + instock + '">' + title + '</option>');
			});
			
			if (newVal != "") {
				newBox = document.getElementById("selectOption");
				newBox.selectedIndex = newVal;
			}
			
			$("#selectOption option").each(function(i){
				if ($(this).text() == val) {
					sel_OptionTitle = $(this).text();	
				}
			});
		} else {
			id = $(data).find("Option").attr("id");
			$("input[name=basketoption]").val(id);
		}
		
		
		if ($(data).find("Attributes").size() > 0) {
			// has attributes	
		}
		document.title = $_CompleteTitleWithColour;
		$(".ajax_ProdID").val($_ProdID);
		$(".ajax_ProdTitle").text($_Title + " - " + $_Colour);
		$(".ajax_Colour").text($_Colour);
		//if (($_RRP != 0) && ($_RRP != "")) {
		//	$(".ajax_RRP").html("RRP: &pound;" + $_RRP);
		//	$(".ajax_Price").html("Our price: &pound;" + $_Price);
		//} else {
			$(".ajax_Price").html("&pound;" + $_Price);
		//}
		$(".ajax_PriceSingle").html("&pound;" + $_Price);
		$(".ajax_Image").attr("src", $_ImageStandard_URL);
		$(".ajax_EnlargeLink").attr("href", $_ImageLarge_URL);
		$(".ajax_EnlargeLink").attr("title", $_CompleteTitle + " - Large");
		$("#ColourWrap a.Current").removeClass("Current");
		$("#ColourWrap a").each(function(i){
			if ($(this).attr("class") == $_Colour) {
				$(this).addClass("Current");	
			}
		});
		if ($(data).find("UpdateRecentlyViewed").text()) {
			$("#RecentlyViewedWrap").remove();
			$("#ShopByBrandWrap").after($(data).find("UpdateRecentlyViewed").text());
		}
		changeOption();
	}
	return returnval;
}

jQuery.fn.highlightBasket = function(){
	jQuery(this).each(function(){
	var elements = jQuery(this);
	elements
		.animate({ opacity: 'hide' }, 200)
		.animate({ opacity: 'show' }, 200)
		.animate({ opacity: 'hide' }, 200)
		.animate({ opacity: 'show' }, 200)
		.animate({ opacity: 'hide' }, 200)
		.animate({ opacity: 'show' }, 200);
	});
}


function addToBasket(e) {
	/*
	var optdesc = "";
	var prodid = $(".ajax_ProdID").val();
	var optionid = $(".basketoption").val();
	var catid = $(".ajax_CatID").val();
	*/
	var optdesc = "";
	var prodid = $(e).parents("form").find("*[name=basketprod]").val();
	var optionid = $(e).parents("form").find("*[name=basketoption]").val();
	var catid = $(e).parents("form").find("*[name=basketcat]").val();
	var button_offset = $(e).offset();
	
	if (optionid == "") {
		optdesc = $("span.ajax_OptionDescription").text().toLowerCase();
		optdesc_fl = optdesc.substring(0, 1);
		if (optdesc_fl == "a" || optdesc_fl == "e" || optdesc_fl == "i" || optdesc_fl == "o" || optdesc_fl == "u") {
			optdesc = "an " + optdesc;	
		} else {
			optdesc = "a " + optdesc;	
		}
		alert("You must select " + optdesc + " before adding to basket!");
		return false;
	}
	$.ajax({ 
		type: "GET", 
		url: "/basket.cfm", 
		data: "basketprod=" + prodid + "&basketoption=" + optionid + "&basketcat=" + catid + "&ajax=1" + "&action=add&random=" + new Date().getTime(), 
		dataType: "html",
		success: handleResponse
	});
	
	var returnval = false;
	
	/*
	function handleResponse(data) {
		if (trim(data) != "") $("#ajax_BasketUpdate").html(data);
		$('html, body').animate({scrollTop: '0px'}, 200);
		$("#BasketContainer").after('<div id="BasketArrow">New items added to basket!</div>');
		$("#ajax_BasketUpdate").highlightBasket();
	}
	temp = setTimeout('$("#BasketArrow").remove();', 2500);
	return returnval;
	*/
	
	function handleResponse(data) {
		if (trim(data) != "") {
			$("#BasketTable").remove();
			$("#Basket h2").after(data);
			evalme = '';
			$("body").append('<div id="ItemAddedPopup">Item Added to Basket</div>');
			//var winWidth = $(window).width() / 2;
			//var winHeight = $(window).height() / 2;
			//var popupLeft = winWidth - ($("#ItemAddedPopup").width() / 2);
			//var popupTop = winHeight - ($("#ItemAddedPopup").height() / 2);
			//$("#ItemAddedPopup").css("left", popupLeft + "px");
			//$("#ItemAddedPopup").css("top", popupTop + "px");
			
			
			$("#ItemAddedPopup").css("left", (button_offset.left - $("#ItemAddedPopup").width() - 50) + "px");
			$("#ItemAddedPopup").css("top", (button_offset.top - $("#ItemAddedPopup").height()) + "px");
			$("#ItemAddedPopup").show();
			//t = setTimeout('$("#BasketTable tr.ItemAddedImage").remove();', 2000);
			t = setTimeout('$("#ItemAddedPopup").remove();', 2000);
		}
	}
	return returnval;
}
