//window.onload = initialise;
addEvent( window, "load", initialise, false );

/*
	initialise
	Performs any code required at page load
*/
function initialise()
{
	document._initialised = false;
	setupExternalTargets();
	
	if( typeof setupAllTabs == "function" )
	{
		//debugger;
		setupAllTabs();
	}
}

/*
	Array.prototype.push
	...
*/
if( typeof Array.push == "undefined" )
{
	Array.prototype.push = function()
	{
		for( var i = 0; i < arguments.length; i++ )
		{
			this[ this.length ] = arguments[ i ];
		}
		return this.length;
	}
}

/*
	resolveUrl
	...
*/
function resolveUrl( url )
{
	if( typeof document._root == "string" )
	{
		url = document._root + url;
	}
	return url;
}

/*
	removeDuplicateDelimeters( string, delimiter )
	Takes a delimited string and normalises its delimiters, 
	removing start, end and duplicates
*/
function removeDuplicateDelimeters( value, delimeter )
{
	if( typeof delimeter == "undefined" ) delimeter = ",";
	value = value.replace( eval( "/" + delimeter + "{2,}/gi" ), delimeter );
	while( value.charAt(0) == delimeter ) value = value.substring( 1 );
	while( value.charAt( value.length-1 ) == delimeter ) value = value.substring( 0, value.length-1 );
	return value;
}

/*
	setupExternalTargets
	Add's targets to external relevance links for XHTML compliance
*/
function setupExternalTargets( target )
{
	var anchors = !target ? document.getElementsByTagName( "a" ) : target.getElementsByTagName( "a" );
	for( var i = 0; i < anchors.length; i++ )
	{
		var a = anchors[i];
		var rel = a.rel ? a.rel : "";
		if( rel == "external" )
		{
			a.target = "_blank";
			a.onmouseover = function( e )
			{
				window.status = "External link: " + ( this.title ? this.title : this.href );
				return true;
			};
		}
		else if( rel == "email" )
		{
			a.onmouseover = function( e )
			{
				window.status = "Contact " + ( this.title ? this.title : this.innerHTML.replace( /<[^>]+>/gi, "" )) + " <" + this.href.substring( 7 ) + ">";
				return true;
			};
		}
		else
		{
			a.onmouseover = function( e )
			{
				window.status = ( this.title ? this.title : this.innerHTML.replace( /(<[^>]+>)|[\r\n\t]+/gi, "" ) ).replace( /&amp;/gi, "&" );
				return true;
			};
		}
		
		a.onmouseout = function( e )
		{
			window.status = "";
		};
	}
	
	document._initialised = true;
}

/*
	addEvent
	Attaches an event handler to the target element
*/
function addEvent( target, type, delegate, capture )
{
	if( target.addEventListener )
	{
		target.addEventListener( type, delegate, capture );
		return true;
	} 
	else if( target.attachEvent )
	{
		var r = target.attachEvent( 'on' + type, delegate );
		return r;
	}
	else
	{
		target[ 'on' + type ] = delegate;
	}
}

/*
	setOpacity
	Sets the opacity of an object
*/
function setOpacity( target, opacity )
{
	opacity = (opacity == 100)? 99.999: opacity;
	var alternateOpacity = opacity / 100;

	if( target.style.filter )
		target.style.filter = "alpha(opacity:" + opacity + ")";		// IE/Win
	if( target.style.KHTMLOpacity )
		target.style.KHTMLOpacity = alternateOpacity;				// Safari<1.2, Konqueror
	if( target.style.MozOpacity )
		target.style.MozOpacity = alternateOpacity;					// Older Mozilla and Firefox
	if( target.style.opacity )
		target.style.opacity = alternateOpacity;					// Safari 1.2, newer Firefox and Mozilla, CSS3
}

/* 
	getElementsByClassName()
	...
*/
function getElementsByClassName( className, parent, tagName, delegate )
{
	var elements = new Array();
	var re = new RegExp( '\\b' + className + '\\b', 'i' );
	
	if( !parent ) parent = document;
	if( !tagName ) tagName = '*';
	
	var list = parent.getElementsByTagName( tagName );
	for( var i = 0; i < list.length; ++i )
	{
		if( list[ i ].className && list[ i ].className.search( re ) != -1 )
		{
			elements[ elements.length ] = list[ i ];
			if( delegate )
			{
				delegate( list[ i ] );
			}
		}
	}
	return elements;
}

/* 
	popupImage()
*/
function popupImage( image )
{
	var win = window.open( image,"winImage","status=1,toolbar=0,scrollbars=1,top=50,left=50,width=620,height=620,resizable=1" );
	win.focus();
	return true;
}

/* 
	popupEmailSupplier()
*/
function popupEmailSupplier( productList )
{
	url = resolveUrl( "EmailSupplier.aspx?ProductList=" + productList );
	var win = window.open( url,"winEmail","status=1,toolbar=0,scrollbars=1,top=100,left=50,width=650,height=620,resizable=1" );
	win.focus();
	return true;
}

/* 
	processEmailSupplier()
*/
function processEmailSupplier()
{
	var cbList = document.getElementsByName( 'emailCheckbox' );
	var productList = "";
	
	// Loop through checkboxes
	for( i = 0; i < cbList.length; i++ )
	{
		if( cbList[i].checked )
		{
			if( productList.length > 0 ) productList += ",";
			productList += cbList[i].value;
		}
	}
	
	// Send emails to suppliers
	if( productList.length > 0 ) {
		popupEmailSupplier( productList );
	}
	else {
		alert( "Please select the suppliers you would like to contact from the list above." );
	}
}