
	//////////////////////////////////////////////////////////////////////////////
	//
	// Sofa Finder order by functions for ListSortBox drop down on sofasofa.com
	//
	// Copyright(c) Digivate 2007 - pAd
	//
	//////////////////////////////////////////////////////////////////////////////

	//////
	// Extract a parameters value
	///
	function extractQueryParameter(param, defaultValue) {
		// to do
		var sLocation = window.location.href;
		var i  = sLocation.indexOf('&'+param+'=');
		if (i==-1) {
			return defaultValue;
		}
		else {
			i = i+2+param.length;
			sLocation = sLocation.substr(i);
			var e = sLocation.indexOf('&');
			if (e==-1) e = sLocation.length;

			return sLocation.substr(0, e);
		}
	}

	//////
	// Replace a parameters value in a URL
	///
	function replaceQueryParameter(url, param, value) {
		var i = url.indexOf('&'+param+'=');
		if (i==-1) {
			return url+'&'+param+'='+escape(value);
		}
		else{
			 // locate and replace parameters values
			i = i+2+param.length;
			var sLocation = url.substr(i);
			var e = sLocation.indexOf('&');
			if (e==-1) e = url.length;
			else e = e+i;
			//alert( url.substr(i, e));
			var ret = url.substr(0, i)+escape(value)+url.substr(e, url.length);
			//alert(ret.substr(i-14));
			return ret;
		}
	}

	//////
	// Extract a parameters value, using mod rewrite URL's
	///
	function extractParameter(param, defaultValue) {
		var sLocation = window.location.href;
		if (sLocation.indexOf('=')>-1)
			return extractQueryParameter(param, defaultValue); // old type
		else {
			var i  = sLocation.indexOf('/'+param+'/');
			if (i==-1) {
				return defaultValue;
			}
			else {
				i = i+2+param.length;
				sLocation = sLocation.substr(i);
				var e = sLocation.indexOf('/');
				if (e==-1) e = sLocation.length;

				return sLocation.substr(0, e);
			}
		}
	}

	//////
	// Replace a parameters value in a URL
	///
	function replaceParameter(url, param, value) {
		if (url.indexOf('=')>-1)
			return replaceQueryParameter(url, param, value);
		else {
			var i = url.indexOf('/'+param+'/');
			if (i==-1) {
				var filename = url.substr(url.lastIndexOf('/')+1, 100);
				if (filename!='sofa-finder') {
					if (url.substr(-1,1) != '/') url += '/';
					return url += param+'/'+escape(value);
				}
				else {
				url = url.substr(0, url.lastIndexOf('/'));
				return url+'/'+param+'/'+escape(value)+'/'+filename;
				}
				/*if (url.substr(-1,1) != '/') url += '/';
				return url += param+'/'+escape(value);*/
			}
			else{
				 // locate and replace parameters values
				i = i+2+param.length;
				var sLocation = url.substr(i);
				var e = sLocation.indexOf('/');
				if (e==-1) e = url.length;
				else e = e+i;
				//alert( url.substr(i, e));
				var ret = url.substr(0, i)+escape(value)+url.substr(e, url.length);
				//alert(ret.substr(i-14));
				return ret;
			}
		}
	}

	//////
	// Order current lister by dropdown: obj
	///
	function onOrderByChange() {
		try {
			var obj = tsGetElementById('ListSortBox');
			//obj.disabled = true;
			var location = window.location.href;
			//location = location.replace(//, '&')
			var newLocation = '';
			var sOrderBy = '';
			var sOrderByType = obj.options[obj.selectedIndex].value;

			var sDirection = '';

			switch (sOrderByType) {
				case 'pricelow':
					sOrderBy = 'unitcost';
					sDirection = 'ASC'
					break;
				case 'pricehigh':
					sOrderBy = 'unitcost';
					sDirection = 'DESC'
					break;

				default:
					sOrderBy = sOrderByType;
					sDirection = 'ASC'
					break;
			}

			location = replaceParameter(location, 'orderby', sOrderBy);
			location = replaceParameter(location, 'direction', sDirection);
			location = replaceParameter(location, 'orderbytype', sOrderByType);

			if (extractParameter('page', '')!='-1')
			{
				location = replaceParameter(location, 'page', '1');
			}


			window.location.href = location;
		}
		finally {

		}
	}



	///////
	// initialise List Sort Box - set select index and attach onchange event
	////
	function initialiseListSortBox() {
		try {
			var obj = tsGetElementById('ListSortBox');
			try {
				var sOrderBy = extractParameter('orderbytype', 'itemdesc');
				for(var i=0; i<obj.options.length-1; i++)
				{
					if (obj.options[i].value == sOrderBy) {
						obj.selectedIndex = i;
						break;
					}
				}
			}
			finally {
				tsAddEventListener('ListSortBox', 'change', 'onOrderByChange');
			}
		}
		finally {
		}

	}

	////////
	//
	// Attach onLoad Event to current window
	//
	/////////////////////////////////////////////////////////////
	try {
		if( typeof(attachEvent) != "undefined" ) {
		  attachEvent('onload',initialiseListSortBox);
		} else if(typeof ( addEventListener ) != "undefined"  ) {
		  addEventListener('load',initialiseListSortBox,true);
		}
		else{
			setTimeout('initialiseListSortBox', 500);
		}
	}
	catch(err) {
		//alert(err.description);
	}
	/////////////////////////////////////////////////////////////


