/*
	[TEMP] unify the NS4/IE4/IE5+/NS6+ code into a single model w/detection
	instead of this spaghetti in here - always, we don't want to support too many =)
	
	[Compatibility]
	All 4+ browsers should render the site in a 'more-or-less' correct way. IE5 needs more testing.
			
	PC (IE,NS,Opera)
	--
	IE4,NS4,NS6,OP6 : menus work
	IE4,NS6,OP6		: popups work
	IE4,NS6			: rollovers work
	IE4,NS4,OP6		: compare script works.
				
	MAC (IE,NS,iCab,Omniweb,Opera) 
	---
	IE4,NS4		: popups work
	IE4,iCab	: rollovers work
	IE4			: compare script bombs						

	NS4 appears to call closeMenu more than once.		
*/

/* NS4/IE4/Mac/Opera/DOM these are all that I care about, it's quite many */
var uA = navigator.userAgent.toLowerCase();
var isNS4 = ( document.layers!=null );
var isNS = ( uA.indexOf('mozilla') > -1 && uA.indexOf('compatible')==-1 && uA.indexOf('opera')==-1 && uA.indexOf('webtv')==-1 && uA.indexOf('spoofer')==-1 && uA.indexOf('hotjava')==-1);
var isIE4 = ( uA.indexOf('msie 4') > -1 );
var isIE5 = ( uA.indexOf('msie 5') > -1 );
var isMac = ( uA.indexOf('mac')  > -1 );
var isMacIE = ( uA.indexOf('ie ') > -1 && isMac);
var isDOM = ( document.getElementById!=null );
var isOpera = ( uA.indexOf('opera') > -1 );

	function windowPrint() { if (window.print) window.print(); }

	function toggleVisibility(obj) { if(isVisible(obj)) hideObject(obj); else showObject(obj); }

	function isVisible(obj)
	{	
		var o;
							
		if (isString(obj)) o = xx_getElementById(obj); else o = obj;
											
		if (o!=null)
		{
			if (isDOM)
				return (o.style.visibility == 'visible');	
			else if (isNS4) 
				return (o.visibility == 'show');
			else if (isIE4)				
				return (o.visibility == 'visible');
		}	

		return true;
	}

	function showObject(obj) {		
		var o;
		
		if (isString(obj)) o = xx_getElementById(obj); else o = obj;
				
		if (o!=null)
		{
			if (isDOM)
			{
				o.style.visibility='visible';
				/*o.style.display = 'block';*/
				o.style.display='';
			}
			else if (isNS4) o.visibility = 'show';
			else if (isIE4) o.visibility = 'visible';	
		}		
	}

	function hideObject(obj) {
		var o;
			
		if (isString(obj)) o = xx_getElementById(obj); else o = obj;

		if (o!=null)
		{
			if (isDOM)
			{				
				o.style.visibility='hidden';
				o.style.display = 'none';
			}
			else if (isNS4) o.visibility = 'hide';
			else if (isIE4) o.visibility = 'hidden';		
		}
	}

	function popup(URL) {
		day = new Date();
		id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,width=760,height=540');");
	}
	
	function helpPop(URL) { window.open(URL,'','width=800,height=300'); }
	
			
	function commentPop() {				
		var location = window.location.href;
							
		var p = escape(location.substring(0,1023));
		var t = escape(document.title.substring(0,1023));
		var w = 260;
		var h = 260;
				
		var left = (getScreenWidth() - w)/2;
		var top = (getScreenHeight() - h)/2;
				
		window.open('/misc/comment.aspx?p=' + p + '&t=' + t,'','width=' + w + ',height=' + h + ',top=' + top + ',left=' + left + ',toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable=0');
	}
	
	function getScreenWidth() {
		if (isIE4||isDOM)			
			return document.body.clientWidth;
		else
			window.innerWidth;
	}
	
	function getScreenHeight() {
		if (isIE4||isDOM)
			return document.body.clientHeight;
		else
			return window.innerHeight;
	}
					
	function pPop(url,w,h)
	{		
		var sW = getScreenWidth();
		var sH = getScreenHeight();

		if (isUndefined(w)||isUndefined(h)) {
			w = 760; h = sH - 80;
		}
													
		l = (sW - w)/2;
		t = (sH - h)/2;
				
		win = window.open(url,'','left=' + l + ',top=' + t + ',width=' + w + ',height=' + h + ',scrollbars=1,resizable=1,toolbar=1,location=1,menubar=1,status=1');
		win.window.focus();	
		return false;	
	}

	/* compare two or more items */
	function compare()
	{	
		var aC;
		var i;
			
		aC = xx_getElementsByName('chkCompare'); 

		if (!isNull(aC))
		{	
			var arr = new Array();
			
			for (i=0;i<aC.length;i++)
			{
				if (aC[i].checked)
					if (isIE5)
						arr = arr.concat(new Array(aC[i].value));	// IE5 isn't EMCA compliant, bozos
					else
						arr.push(aC[i].value);
			}
												
			if (arr.length>1)
 				pPop('/store/productcompare.aspx?p=' + arr.join('&p=')); 			 			
			else
				alert('Please choose 2 or more products to compare');
		}
	}

	/* uncheck the compare checkboxes */
	function uncheckCompare()
	{
		aC = xx_getElementsByName('chkCompare'); 	
		
		if (aC!=null)
		{
			for (i=0;i<aC.length;i++)
				aC[i].checked = false;	
		}
	}

	function checkedToCart()
	{
		aC = xx_getElementsByName('chkCompare'); 	

		if (aC!=null)
		{
			var arr = new Array();
			
			for (i=0;i<aC.length;i++)		
			{
				if (aC[i].checked)
					if (isIE5)
						arr = arr.concat(new Array(aC[i].value));	// IE5 isn't EMCA compliant			
					else
						arr.push(aC[i].value);
			}
															
			if (arr.length>0)
				window.location = '/store/checkout/cart.aspx?a=4&prodid=' + arr.join('&prodid=') + '&qty=1';		
			else
				alert('You must check a product first.');
		}
	}

	function rowlight(o) { o.bgColor='#eeeeee'; o.style.cursor='hand'; return; }
	function rowdim(o) { o.bgColor='#ffffff'; o.style.cursor='default'; return; }
	function rowclick(o) {	pPop('product/' + o + '.2.htm'); return false; }

	var oOpenMenu = null;	// global variable
			
	function popUpProperties(inobj) {
		op = window.open();
		op.document.open('text/plain');
		for (objprop in inobj) {
			op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
		}
		
		op.document.close();
	}
				 
	/* find the position of an element, doesn't work in Mac IE4, because it's lame*/
	function findPosX(obj)
	{
		var curleft = 0;
			
		if (isDOM||isIE4)
		{	
			while (obj.offsetParent!=null)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (isNS4)
			curleft += obj.x;

		return curleft;
	}


	function findPosY(obj)
	{
		var curtop = 0;

		if (isDOM||isIE4)
		{							
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			}									
		}

		else if (document.layers)
			curTop += obj.y;

		return curtop;
	}

	
		/* this is whacked just for the spec search don't trust it */
		function xx_inputType(o)
		{
			if (o.type)
			{
				if (o.type=='select-one')
					return 'select';
				
				if (o.type=='checkbox')
					return 'checkbox';		
						
				return o.type.toLowerCase();							
			}
			else if (o.getAttribute)	
			{
				var oT = o.getAttribute('type');
					if (oT)
						return oT.toLowerCase();
					else
						return null;						
			}
		}			
		
		function xx_tagName(o)
		{											
			if (o.tagName)
				return o.tagName.toLowerCase();				
			else
				if (document.all)
				{
					alert('to be implemented');
				}
				else
				{
					if (!(isUndefined(o.type)))
					{
						if (o.type=='select-one')
							return 'select';
						
						if (o.type=='checkbox')
							return 'input';
													
						return o.type;								
					}
																	
					return 'bogus';													
				}
		}
		
		//	NS4 support is very limited because it is very EViL					
		function xx_getElementById(name)
		{
			if (isDOM)			
  				return document.getElementById(name); // DOM
			else if (isIE4)				
				return document.all[name]; // IE4					
			else if (isNS4)					
			{		
					var i,j,o,tmp;	// NS4
					
					// just in the doc					
					if (document[name])
						return document[name];
												
					// recurse all layers
					tmp = getObjNN4(document,name);																									
					if (tmp) return tmp;
										
					// scan the forms														
					for (j=0;j<document.forms.length;j++)																																							
					{
						if (document.forms[j].name==name)
							return document.forms[j];
													
						for (i=0;i<document.forms[j].elements.length;i++)
						{						
							o = document.forms[j].elements[i];
							
							if (o.name==name||o.id==name)
								return o;
																							
							o = eval('document.forms[j].' + name);
							
							if (o)
								return o;
																						
						}
					}
					
					// scan the images
					for (j=0;j<document.images.length;j++)
					{
						if (document.images[j].name==name)
							return document.images[j];					
					}					
					
					// scan the anchors										
					for (j=0;j<document.anchors.length;j++)
					{						
						if (document.anchors[j].name==name)
							return document.anchors[j];						
					}
			}
		}

		function getObjNN4(obj,name)
		{				
			var tmp;
			var x;
				
			// scan this object's inner layers
			x = obj.layers;
			
			for (var i=0;i<x.length;i++)
			{									
				tmp = eval('document.layers["' + x[i].id + '"].' + name);	// this layer
																					
				if (tmp)
					return tmp;	// return the object
																			
				if (x[i].id == name)					
		 			return x[i]; // return the layer
		 		
				if (x[i].layers.length>0)
				{
					var tmp = getObjNN4(x[i],name);
					if (tmp) return tmp;
				}
			}																
		}

		/* okee */				
		function xx_getElementsByName(name)
		{
			if (isDOM)
				return document.getElementsByName(name);
			else
				if (isNS4)
				{												
					// attempt to traverse the document for this with this name, very, very BAD but a necessary EViL
					var aOut = new Array();
					var i,j,o;
						
					for (j=0;j<document.forms.length;j++)																																							
					for (i=0;i<document.forms[j].elements.length;i++)
					{
						o = document.forms[j].elements[i];
						if (o.name==name)
						{
							//alert(aOut.length + ' : ' + o.name);
							aOut.push(o);
						}							
					}			
					
					return aOut;
				}								  
		}	
			
		
	/* make a menu element appear */	
	function containsNN6 (container, containee)
	{
		var isParent = false;
		do {
			if ((isParent = container == containee))
				break;			
			if (containee !=null)
				containee = containee.parentNode;
			else
				break;
			}
		while (containee != null);
		
		return isParent;
	}

	// NS4 menu handlers, blarg
	function menu_bindNS4Handler(eSrc)
	{
		if (isNS4)
		{			
			var oMenu = xx_getElementById(eSrc);					
			oMenu.captureEvents(Event.MOUSEOUT);						
			oMenu.onMouseOut = menu_NS4Handler;
		}		
	}
			
	function menu_NS4Handler(e)
	{	
		if (e.target!=null)
			closeMenu(e.target)
	}
		
	// old fallback for NS4 browser, but I got the menus to wrok
	function menu_onmouseout(eSrc)
	{					
		return;
		
		var iPosition = parseInt(eSrc.replace('tdMenuBarItem',''));						// position
		var oImage = xx_getElementById(eSrc.replace('tdMenuBarItem','menuImage'));		// the menu image		
				
		if ((oImage!=null) && isNS4)
			oImage.src =  __roImages[iPosition * 2 + 1].src;
	}

	// process menu dropdown
	function menu_onmouseover(eSrc)
	{					
		var oMenu = xx_getElementById(eSrc.replace('tdMenuBarItem','divMenu'));			// the menu div
		var iPosition = parseInt(eSrc.replace('tdMenuBarItem',''));						// position
		var oImage = xx_getElementById(eSrc.replace('tdMenuBarItem','menuImage'));		// the menu image		
		
		// close the open menu						
		if ((oOpenMenu!=null) && (oOpenMenu!=oMenu)) 			
			closeMenu(oOpenMenu);		
			
		// rollover the image			
		if (oImage!=null)
		{			
			oImage.src = __roImages[iPosition * 2].src;
		}
												
		// process the drop down
		if (oMenu!=oOpenMenu) 					
		{			
			if ((isDOM||isIE4||isNS4) && !(isMacIE))					
			{					
				if (isNS4)
				{								
					oMenu.x= oImage.x + 1;	// why + 1 and -3 ? beats me but it works :)
					oMenu.y = oImage.y + oImage.height - 3;
				}
				else
				{	
					var oMenuBar = xx_getElementById('tblMenuBar');
					oMenu.style.left = findPosX(oImage);	
					oMenu.style.top = findPosY(oImage) + oMenuBar.offsetHeight;
				}
																									
				showObject(oMenu);
			}
												
			// set the open menu
			oOpenMenu = oMenu;							
		}				
		
		return;		
	} 

	/* close a menu element */
	function closeMenu(oMenu)
	{		
		if(oMenu)
		{		
			if (oMenu.id)
			{	
				var iPosition = parseInt(oMenu.id.replace('divMenu','')); // get the image name									
				var oImage = xx_getElementById(oMenu.id.replace('divMenu','menuImage'));
					
				// rollover
				if (oImage!=null)
					oImage.src =  __roImages[iPosition * 2 + 1].src;
			
																	
				hideObject(oMenu);							
				oOpenMenu = null;
			}
		}
				
	}

// I'm not sure where I got this from but it works well
function formatCurrency(num) { 
	var sign, cents; 
	
	num = num.toString().replace(/\$|\,/g,''); 
	
	if(isNaN(num)) 
		num = '0'; 
	
	sign = (num == (num = Math.abs(num))); 
	num = Math.floor(num*100+0.50000000001); 
	cents = num%100; 
	num = Math.floor(num/100).toString(); 
	
	if(cents<10) 
		cents = '0' + cents; 
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); 
	
	return (((sign)?'':'-') + '$' + num + '.' + cents); 
}

function writeContent(id,text)
{
	if (isNS4)
	{						
		var oDiv = xx_getElementById(id);
				
		if (!isNull(oDiv))
		{
			oDiv.document.open();
			oDiv.document.write(text);
			oDiv.document.close();
		}				
	}
	else if (isIE4||isDOM)
	{
		var oDiv = xx_getElementById(id);

		if (!isNull(oDiv))		
			oDiv.innerHTML = text;		
	}	
}
/*
 functions missing from the DOM 
 mostly from http://www.crockford.com/javascript/remedial.html
*/
function isArray(a) { return isObject(a) && a.constructor == Array; }	
function isFunction(a) { return typeof a == 'function'; }
function isNull(a) { return typeof a == 'object' && !a; }
function isNumber(a) { return typeof a == 'number' && isFinite(a); }
function isObject(a) { return (typeof a == 'object' && a) || isFunction(a); }			
function isUndefined(a) { return typeof a == 'undefined'; }

function isString() {
	if (arguments.length>0)
	{
		if (typeof arguments[0] == 'string')
			return true;
		
		if (typeof arguments[0] == 'object')
		{
			if (arguments[0].constructor!=null)
			{				
				var criterion = arguments[0].constructor.toString().match(/string/i);
				return (criterion != null);
			}
		}
	}

	return false;
} 

function getLinkText (link) { return link.text ? link.text : link.innerText ? link.innerText : link.firstChild.nodeType == 3 ? link.firstChild.nodeValue : ''; }
function getTime() { return new Date().getTime();}