/* ******************************************************************************************
	COMMON FUNCTIONS LIBARY
	
	Author: Gary Ott
	Version: 1.2.1
	Last Revision Date: 12th December 2010
	
	******************************************************************************************
	
	Functions:
	==========
	
	getElementById_s	Same function but wider compatiability than
						document.getElementById
	
	FilterOnMouseOut	Filters out OnMouseOut calls when the mouse hasn't
						actually left the calling element but rather entered
						an element contained within the calling element.
	
	getElementsByClass	Obtain array of HTML elements of the same class.
	
	GetTopLeft			Find real pixel position of any HTML element.
*/

function getElementById_s(id){
var obj = null;
if(document.getElementById)
	{
	// W3C DOM method, if available:-
	obj = document.getElementById(id);
	}
else if(document.all)
	{
	// Use document.all on document.all only browsers.
	obj = document.all[id];
	}
return obj;
}

function FilterOnMouseOut(EventSource, Evt, Action)
{
if (!Evt) var Evt=window.event;

var reltg = (Evt.relatedTarget || Evt.toElement);	/* relatedTarget for standards compliant browsers, toElement for that loathsome Internet Explorer */

while (reltg.nodeName!='BODY')
	{
	if (reltg==EventSource)
		{
		/* We have found that the mouse moved over a child of the source layer
			It therefore hasn't actually moved out of the layer. */
		return 0;
		}
	reltg = reltg.parentNode;
	}
	
eval(Action);
return 1;		/* It may be of interest to know if the event was fired or not. */

}


function getElementsByClass(searchClass, node, tag)
{

var classElements = new Array();

if (node == null) node = document;
if (tag == null) tag = '*';

var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');

for (i=0, j=0; i < elsLen; i++)
	{
	if (pattern.test(els[i].className))
		{
		classElements[j] = els[i];
		j++;
		}
	}

return classElements;
}

function GetTopLeft(Element)
{
var x = 0;
var y = 0;
if (Element.offsetParent)
	{
	do
		{
		//alert("ID:" + Element.id + "\nTag: " + Element.tagName + "\nClass:" + Element.className + "\noffsetLeft" + parseInt(Element.offsetLeft));
		x += parseInt(Element.offsetLeft);
		y += parseInt(Element.offsetTop);
	} while (Element = Element.offsetParent);	//set Element to its offsetParent
	}
//return object with two properties: Top and Left

return {Top:y, Left: x};
}


function ReLabelButton()
{
/*
	This function renames submit buttons on a given form to reflect changes made in the quantity
	text boxes on that form.
	
		This function must be passed the affected form as the first argument. A list of buttons may
	be passed as additional arguments to specify which button(s) to relabel. If no list of buttons
	is passed, the function will relabel all submit type buttons. If there are no submit buttons,
	it will relabel all buttons.
	
	The buttons are relabelled with 'Add Selected n Items To Basket' or 'No Items Chosen'.
	
	The function returns true if sucessful.
*/

if (arguments.length==0) return false;

var form = arguments[0];
var a=0;
var Count = 0;
var Success = false;
var NewLabel = "";

for(a=0; a < form.elements.length; a++)
	{
	if (form.elements[a].type=="text")
		{
		if (parseInt(form.elements[a].value) > 0)
			{
			Count += parseInt(form.elements[a].value);
			}
		}
	}

NewLabel = (Count > 0) ? ("Add Chosen " + Count + " Items To Basket") : "No Items Chosen";

for(a=1; a<arguments.length; a++)
	{
	if (arguments[a].type=="button")
		{
		arguments[a].value = NewLabel;
		Success = true;
		}
	}

if (!Success)
	{
	for(a=0; a < form.elements.length; a++)
		{
		if (form.elements[a].type=="submit")
			{
			form.elements[a].value = NewLabel;
			Success = true;
			}
		}

	}
	
if (!Success)
	{
	for(a=0; a < form.elements.length; a++)
		{
		if (form.elements[a].type=="button")
			{
			form.elements[a].value = NewLabel;
			Success = true;
			}
		}

	}

return Success;
}
