
/**
* Turboshop 4 Any browser functions and browser detection functions
* (c) Digivate Limited 2003-2006
**/

/**
* Get Pointer to document object in any browser
* @param string element id for hot/editable area
**/
function tsGetElementById(id)
{
	var ret = null;

	if (id!=null&&id!='')
	{
		try{
			if (document.getElementById)
			ret = document.getElementById(id);
			else
			if (document.layers) //Netscape 4
			ret = eval('document.' + id);
			else if (navigator.userAgent.indexOf("Opera") != -1) //Opera
			ret = eval("document.all."+id);
			else
			alert('browser not support by turboshop');
			//else // all others . . .	the dom
		}
		catch(e)
		{
		}
	}
	return ret;
}

/**
* Get Pointer to iframe document object in any browser
* @param string iframe id
**/
function getIFrameDocument(aID)
{
	// if contentDocument exists, W3C compliant (Mozilla)
	if (document.getElementById(aID).contentDocument)
	{
		return document.getElementById(aID).contentDocument;
	} else
	{
		// IE
		return document.frames[aID].document;
	}
}

/**
* Get Pointer to iframe object in any browser
* @param string iframe id
**/
function getIFrame(aID)
{
	if ((frames) && (frames[aID])) return frames[aID];
	else if (eval('window.'+aID)) return eval('window.'+aID);
	else if (eval('document.'+aID)) return eval('document.'+aID);
	else return false;
}



/**
* Get left postion of an element in the document
**/
function getOffsetLeft (el)
{
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null)
	ol += el.offsetLeft;
	return ol;
}

/**
* Get top postion of an element in the document, in Mozilla and Opera this returns the bottom so must use browserOffsetHeight() function
**/
function getOffsetTop (el)
{
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null)
	ot += el.offsetTop;
	return ot;
}

/**
* Get Pointer to a parents document object in any browser
* @param string element id for hot/editable area
**/
function tsGetParentElementById(id)
{
	if (parent.document.layers) //Netscape 4
	return eval('parent.document.' + id);
	else if (isOpera()) // Opera
	return eval("parent.document.all."+id);
	else // all others . . .
	return parent.document.getElementById(id);
}

/**
* Returns true if browser is Mozilla.org browser and not Mozilla compatible such as IE
**/
function isMozilla()
{
	return ((navigator.userAgent.indexOf('Mozilla')>-1) && (navigator.userAgent.indexOf('MSIE')==-1));
}

/**
* Returns true if browser is Opera
**/
function isOpera()
{
	return (navigator.userAgent.indexOf("Opera") != -1);
}

function isIE()
{
	//	if (document.frames) return true;
	//else return false;
	return (navigator.appName.indexOf('Internet Explorer') != -1)
}

// tsAddEventListener
//
// event parameter is one of one of:
//
// Covers: 'abort', 'blur', 'change', 'error', 'focus', 'load', 'reset', 'resize', 'scroll', 'select', 'submit', 'unload',
//         'keydown', 'keypress', 'keyup', 'click', 'mousedown', 'mousemove', 'mouseout', 'mouseover', and 'mouseup'.
function tsAddEventListener(id, event, eventHandler) {
	//	alert(id);
	var oDiv = tsGetElementById(id);

	if (oDiv!=null) {

		if( oDiv.addEventListener ) {
			oDiv.addEventListener(event,eval(eventHandler),false);
		} else if( oDiv.attachEvent ) {
			oDiv.attachEvent('on'+event,eval(eventHandler));
		}
		else
		{
			alert('turboshop: cannot add event:'+event)
		}
	}
}


function tsGetEventRecordTarget(e) {

	var theTarget = e.target ? e.target : e.srcElement;

	if (theTarget.id.indexOf('_')>-1)
	return theTarget;
	else if (theTarget.parentNode!=null && theTarget.parentNode.id.indexOf('_')>-1)
	return theTarget.parentNode;

	if( theTarget && ( theTarget.nodeType == 3 || theTarget.nodeType == 4 ) ) {
		theTarget = theTarget.parentNode;
	}
	return theTarget;
}

// Removes leading whitespaces
function LTrim( value ) {

	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim( value ) {

	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim( value ) {

	return LTrim(RTrim(value));

}

//set a div or layer in NS 4 display to a given state (none or block normallly)
function setLayerDisplay(layer_ref, state) {
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}