// Misc scripts
function reloadBy (id) {
	var sbox = document.getElementById(id);
	var index = sbox.selectedIndex;
	var ordercon = sbox.options[index].value;
//	Get rid of existing condition!
	var loc = location.href;
	var i = loc.indexOf('&orderby')
	if ( i > 0   ) {
		loc = loc.slice(0,i+1);
		}
	var newloc = loc+'&orderby='+ordercon;
	location.href=newloc;
	}
function doSearch () {
	var st = document.getElementById('searchterm').value;
	st = encodeURIComponent(st);
	loc = '../cgi-bin/shopsearch.cgi?searchterm='+st;
	top.location=loc;
	}
function showTrolley () {
	loc = '../cgi-bin/trolley.cgi?returnurl='+encodeURIComponent(top.location);
	top.location=loc;
	}

// 	Scripts for pop out menus
//	tries to keep menus on page
//
//	Next two functions courtesy of
//	http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
//
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
	}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}
function getWindowHeight() {
	var wx;
	var wy; 
	if (typeof window.innerWidth != 'undefined')  {
      		wx = window.innerWidth,
      		wy = window.innerHeight
 		}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
 		{
       		wx = document.documentElement.clientWidth,
       		wy = document.documentElement.clientHeight
 		}

// older versions of IE
	else  {
       		wx = document.getElementsByTagName('body')[0].clientWidth,
		wy = document.getElementsByTagName('body')[0].clientHeight
 		}
	return wy;
	}
//
//	Activate this for debug
//
function flag(str)  {
	return;
//	var bar = document.getElementById('flagbar');
//	bar.value = str;
	}
function hideContent (id) {
	var l = document.getElementById(id);
	l.style.visibility='hidden';
	}
function showContent (obj,id) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
	var l = document.getElementById(id);
	var xpos = curleft-l.offsetWidth;    // +obj.offsetWidth
	var ypos = curtop;
	var c = l;
	var wh = getWindowHeight() + f_scrollTop();
	var ch = c.offsetHeight
;
	var cb = ch + ypos;
	if ( cb > wh )	// is bottom of menu below window?
		{
		var wos = ypos;
		ypos = wh - ch;
		}  
	c.style.visibility = 'visible';

	c.style.position='absolute';
	c.style.top = ypos;
	c.style.left = xpos;
	}
function showDDMenu (id,tbl)  {
	var tb = document.getElementById(tbl+'-menu');
	var obj = tb;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
	var l = document.getElementById(id + '-ddcaller');
	l.style.backgroundColor = '#ffeeee';
  
	var xpos = curleft+l.offsetLeft+tb.offsetWidth; 
	var ypos = curtop+l.offsetTop;
	var c = document.getElementById(id + '-ddcontent');
	var wh = getWindowHeight() + f_scrollTop();
	var ch = c.offsetHeight
;
	var cb = ch + ypos;
	if ( cb > wh )	// is bottom of menu below window?
		{
		var wos = ypos;
		ypos = wh - ch;
		flag(ypos+" "+wos+" "+wh);
		}  
	c.style.visibility = 'visible';

	c.style.position='absolute';
	c.style.top = ypos;
	c.style.left = xpos;
	}
function showMenu (id)  {
	var c = document.getElementById(id + '-ddcontent');
  
	c.style.visibility = 'visible';

	var l = document.getElementById(id + '-ddcaller');
	l.style.backgroundColor = '#ffeeee';
  
	}
function hideMenu (id)  {
	var c = document.getElementById(id + '-ddcontent');
  
	c.style.visibility = 'hidden';
 
	var l = document.getElementById(id + '-ddcaller');
	l.style.backgroundColor = 'white';
  
	}

