String.prototype.RTrim = function() { return this.replace(/\s+$/, ""); }
String.prototype.LTrim = function() { return this.replace(/^\s+/, ""); }
String.prototype.Trim = function() { return this.RTrim().LTrim(); }

function Browse(categoryList, brandList, lang, resetbrand, direction)
{
	var url = "";
	
	//All Categories and All Brands selected
	if(categoryList.selectedIndex <= 1 && brandList.selectedIndex <= 1)
	{
		url = "/default";
	}
	//All Categories for a certain brand
	else if(categoryList.selectedIndex <= 1 && brandList.selectedIndex > 1)
	{
		url = "/brand/" + brandList[brandList.selectedIndex].value;
	}
	//Or Brand, Or Category, Or Both (with different possible BrowseOrder)
	else
	{
		//Set /category/* section of the URI
		if(categoryList.selectedIndex > 1)
		{
			url += "/category/" + categoryList[categoryList.selectedIndex].value;
		}
		else
		{
			//Reset it to the first item
			categoryList.selectedIndex = -1;
		}

		if(brandList.selectedIndex > 1 && !resetbrand)
		{
			//Depending on the BrowseOrder, place the brand behind the category, or alone
			if(direction == "BrandCategory")
			{
				url = "/brand/" + brandList[brandList.selectedIndex].value;
				categoryList.selectedIndex = -1;
			}
			else
			{
				url += "/brand/" + brandList[brandList.selectedIndex].value;
			}
		}
		else
		{
			//Reset it to the first item
			brandList.selectedIndex = -1;
		}
	}

	if(url != "")
	{
		url = "/" + lang + url + ".aspx";
		location.href = url;
	}
}

function Sort(direction)
{
	var args = getArgs();
	var path = location.pathname;
	
	args["d"]  = direction;

	path += "?";
	
	if(args["pg"] != null)
	{
		path += "pg=" + args.pg + "&";
	}
	
	if(args["q"] != null)
	{
		path += "q=" + args.q + "&";
	}
	
	var sort = document.forms[0][sortList].options[document.forms[0][sortList].selectedIndex].value
	
	path += "s=" + sort + "&";
	path += "d=" + direction;
	
	location.href = path;
}

function SetSortType(list)
{
	switch(Number(list.options[list.selectedIndex].value))
	{
		case 1:
			document.getElementById("sortup").className = "sortuptitle";
			document.getElementById("sortdown").className = "sortdowntitle";
			break;
		case 2:
			document.getElementById("sortup").className = "sortupprice";
			document.getElementById("sortdown").className = "sortdownprice";
			break;
		case 3:
			document.getElementById("sortup").className = "sortupstock";
			document.getElementById("sortdown").className = "sortdownstock";
			break;
	}
}

function Search(basepath,keyword,defaultValue)
{
	if(keyword.value.Trim() != defaultValue)
	{
		location.href = basepath + "search.aspx?q=" + keyword.value.Trim();
	}
}

function getArgs()
{
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++)
	{
		var pos = pairs[i].indexOf('=');
		if(pos == -1)
		{
			continue;
		}
		
		var argname = pairs[i].substring(0,pos);
		var argvalue = pairs[i].substring(pos+1);
		
		args[argname] = unescape(argvalue);
	}
	
	return args;
}

function ValidQuantity(quantityID,button)
{
	var quantity = document.getElementById(quantityID);
	if(quantity == null)
	{
		return;
	}

	if(isNaN(quantity.value.Trim()))
	{
		return false;
	}
	else
	{
		if(quantity.value.Trim() > 0)
		{
			return true;
		}
		else
		{
		return false;
		}
	}
}


function ValidQuantityBlur(quantityID)
{
	var quantity = document.getElementById(quantityID);
	if(quantity == null)
	{
		return;
	}

	if(quantity.value.Trim() == "" || isNaN(quantity.value.Trim()))
	{
		quantity.value = 1;
	}
	else
	{
		if(quantity.value.Trim() <= 0)
		{
			quantity.value = 1;
		}
	}
}

function ConfirmationHookupControl( confirmButton ) {
    var ev = confirmButton.onclick;
    if (typeof(ev) == "function" ) {            
        ev = ev.toString();
        ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
    }
    else {
        ev = "";
    }
    var func = new Function("if ( !ConfirmationOnClick( this ) ){return false;} " + ev);
    confirmButton.onclick = func;
}


function CheckConditions(check,confirm)
{
	if(document.forms[0][check].checked)
	{
		return true;
	}
	else
	{
		alert(confirm);
		return false;
	}
}


function ViewImage(sender,width,height)
{
	var NS=document.all;
	
	var w = (width+30);
	var h = (height+60);
	
	if(w > self.screen.width)
	{
		w = self.screen.width- 100;
	}
	
	if(h > self.screen.height)
	{
		h = self.screen.height- 100;
	}

	if(!NS)
	{
		var leftPos = (self.screen.width - self.outerWidth) / 2;
		var topPos = (self.screen.height - self.outerHeight) / 2;
	}
	else
	{
		var leftPos = (self.screen.width-w) / 2;
		var topPos = (self.screen.height-h) / 2;
	}
	
	winopts = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=auto,copyhistory=1,resizable=0,width=" + w + ",height=" + h;
	winopts += ",screenY=" + topPos + "screenY,=" + leftPos + ",top=" + topPos + ",left=" + leftPos;
	window.open(sender.href,"image",winopts);
}


function ResizeImageWindow(width,height)
{
	var NS=document.all;
	
	var w = (width+36);
	var h = (height+105);
	
	if(w > self.screen.width)
	{
		w = self.screen.width- 100;
	}
	
	if(h > self.screen.height)
	{
		h = self.screen.height- 100;
	}

	window.resizeTo(w,h);
}


function AntiSpam(args)
{
	var addresses = args.split(";");

	var output = "";

	for(var i=0;i < addresses.length;i++)
	{
		var params = addresses[i].split(",");
		output += Parse(params[0],params[1]);

		if(i != addresses.length - 1)
		{
			output += ";";
		}
	}

	location.href = "mailto:" + output;
}

function Parse(name,domain)
{
	var parsedDomain = domain.replace("NOSPAM_","");
	return name + "@" + parsedDomain;
}

var liveStockIsRunning = false;

function CheckStock(sender,id,wait,localeID,type)
{
	if(!liveStockIsRunning)
	{	
		liveStockIsRunning = true;
		
		sender.className = "hidden";
		
		var livestockDiv = document.getElementById("livestock_" + id);
		if(livestockDiv != null)
		{
			livestockDiv.className = "shown";
		}
		
		var livestockWaitNode = document.getElementById("livestockwait_" + id);
		if(livestockWaitNode != null)
		{
			livestockWaitNode.className = "shown";
			while(livestockWaitNode.hasChildNodes())
			{
				livestockWaitNode.removeChild(livestockWaitNode.firstChild);
			}
			
			var waitText = new String(livestockWait).replace(/waitfortime/,wait);
			
			var span = document.createElement("span");
			span.className = "loading";
			livestockWaitNode.appendChild(span);
			
			var waitTextNode = document.createTextNode(waitText);
			span.appendChild(waitTextNode);
		}
		
		var livestockResult = document.getElementById("livestockresult_" + id)
		if(livestockResult != null)
		{
			livestockResult.className = "hidden";
		}
		
		type.ServerSideCheckStock(id,localeID,ServerSideCheckStock_CallBack);

		return false;
	}
	
	return false;
}

function ServerSideCheckStock_CallBack(response)
{
	if(response.error != "" && response.value != null)
	{
		liveStockIsRunning = false;

		var livestockWaitNode = document.getElementById("livestockwait_" + response.value.productID)
		if(livestockWaitNode != null)
		{
			livestockWaitNode.className = "hidden";
		}
		
		var livestockResult = document.getElementById("livestockresult_" + response.value.productID)
		if(livestockResult != null)
		{
			livestockResult.className = "shown";
			while(livestockResult.hasChildNodes())
			{
				livestockResult.removeChild(livestockResult.firstChild);
			}
			
			
			//<span> (a)
			// <div class="livestocketa"> (b)
			//   livestockETA (c)
			//   <span class="livestockresult"> (d)
			//    (f) (g)
			//   </span>
			//		<span class="livestockshop"> (h)
			//    (i)
			//   </span>
			//   <span class="livestockquantity"> (h)
			//    (i)
			//   </span>
			//   <span class="livestockinfo"> (j)
			//    <a> (l) </a>(k)
			//   </span>
			// </div>
			//</span>
			
			//(a)
			var span = document.createElement("span");
			span.className = "livestockresponse";
			livestockResult.appendChild(span);
			
			if(response.value.StatusString == "ProductFound")
			{			
				//(b)	
				var diveta = document.createElement("div");
				diveta.className = "livestocketa";
				span.appendChild(diveta);

				//(c)
				var resultNode = document.createTextNode(livestockETA);
				diveta.appendChild(resultNode);
				
				//(d)
				var spanresult = document.createElement("span");
				spanresult.className = "livestockresult";
				diveta.appendChild(spanresult);
				
				//(f)
				var eta = document.createTextNode(response.value.ETAString);
				spanresult.appendChild(eta);
				
				if(response.value.ETADateString != '')
				{
					//(e)
					var etadate = document.createTextNode(" (" + response.value.ETADateString + ")");
					spanresult.appendChild(etadate);
				}
				
				//(d)
				var spanshop = document.createElement("span");
				spanshop.className = "livestockshop";
				diveta.appendChild(spanshop);
				
				//(g)
				var etaextra = document.createTextNode(livestockETAExtra);
				spanshop.appendChild(etaextra);
				
				//(g)
				var etashop = document.createTextNode(livestockShop);
				spanshop.appendChild(etashop);
				
				var qText = livestockQuantity;
				if(response.value.Quantity > 100)
				{
					qText += "> 100";
				}
				else if(response.value.Quantity <= 100 && response.value.Quantity > 50)
				{
					qText += "> 50";
				}
				else if(response.value.Quantity <= 50 && response.value.Quantity > 10)
				{
					qText += "> 10";
				}
				else
				{
					qText += "< 10";
				}
				
				qText += livestockQuantityExtra;
				
				//(h)
				var spanq = document.createElement("span");
				spanq.className = "livestockquantity";
				diveta.appendChild(spanq);
				
				//(i)
				var q = document.createTextNode(qText);
				spanq.appendChild(q);
				
				//(j)
				var spani = document.createElement("span");
				spani.className = "livestockinfo";
				diveta.appendChild(spani);
				
				//(k)
				var a = document.createElement("a");
				a.setAttribute("href","#");
				a.setAttribute("title",livestockMoreInfo);
				a.onclick = OpenLiveStockMoreInfo;
				spani.appendChild(a);
				
				//(l)
				var moreInfoNode = document.createTextNode(livestockMoreInfo);
				a.appendChild(moreInfoNode);
			}
			else if(response.value.StatusString == "ProductNotFound" || response.value.StatusString == "NoProductData" || response.value.StatusString == "SupplierNotFound")
			{
				var partOne = livestockNotFound.split('#')[0];
				var partTwo = livestockNotFound.split('#')[1];
				
				var a = document.createElement("a");
				a.setAttribute("href","/contact.aspx");
				a.setAttribute("title",partOne);
				span.appendChild(a);
				
				var partOneNode = document.createTextNode(partOne);
				a.appendChild(partOneNode);
				
				var partTwoNode = document.createTextNode(partTwo);
				span.appendChild(partTwoNode);
			}
			else if(response.value.StatusString == "Error")
			{				
				var errorNode = document.createTextNode(livestockError);
				span.appendChild(errorNode);
			}
			else if(response.value.StatusString == "TimeOut")
			{
				var resultNode = document.createTextNode(livestockTimeOut);
				span.appendChild(resultNode);
			}
		}
	}
	else
	{
		alert("Error occured. Please try again.\n\n" + response.error);
	}
	
	return false;
}


function OpenLiveStockMoreInfo()
{
	var NS=document.all;

	if(!NS)
	{
		var leftPos = (self.screen.width - self.outerWidth) / 2;
		var topPos = (self.screen.height - self.outerHeight) / 2;
	}
	else
	{
		var leftPos = (self.screen.width-500) / 2;
		var topPos = (self.screen.height-500) / 2;
	}
	
	winopts = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=auto,copyhistory=1,resizable=0,width=500,height=500";
	winopts += ",screenY=" + topPos + "screenY,=" + leftPos + ",top=" + topPos + ",left=" + leftPos;
	window.open("/nl/livestockmoreinfo.html","livestockmoreinfo",winopts);
}

function SpotlightSlideshow(refreshInterval)
{
	this.loadIntervalID;
	this.refreshInterval = refreshInterval;
	this.currentSlideID = '';
	this.currentSlideIndex = 0;
	this.slideIDs = new Array();

	var self = this;
	self.Init();
}

SpotlightSlideshow.prototype.Init = function()
{
	var self = this;
	this.loadIntervalID = setInterval(function() { self.ShowNext(); },  this.refreshInterval);
}

SpotlightSlideshow.prototype.ShowNext = function()
{
	var self = this;	
	if(self.currentSlideIndex == self.slideIDs.length-1)
	{
		self.currentSlideIndex = 0;
	}
	else
	{
		self.currentSlideIndex++;
	}
	
	var newSlide = document.getElementById(self.slideIDs[self.currentSlideIndex]);
	if(newSlide != null)
	{
		//Only hide the current one when we have found the next one
		var currentSlide = document.getElementById(self.currentSlideID);
		if(currentSlide != null)
		{
			currentSlide.className = currentSlide.className + " hidden";
		}
	
		self.currentSlideID = self.slideIDs[self.currentSlideIndex];
		newSlide.className = newSlide.className.replace(/ hidden/g, '');
	}
}


/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val) {
		if (document.getElementById && document.createElement && document.appendChild)
		{
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++)
			{
				oLink = arrLinks[i];
				if ((strAtt == 'class' && oRegExp.test(oLink.className)) || oRegExp.test(oLink.getAttribute(strAtt)))
				{
					oLink.onclick = JSTarget.openWin;
				}
			}
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn)
	{
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent)
		{
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
JSTarget.addEvent(window, 'load', function(){JSTarget.init("rel","external");});