/*
-----------------------------------------------
general.js
$Id: general.js,v 1.7 2006/08/11 13:23:09 leoj Exp $
----------------------------------------------- */

/* Load events
---------------------------------------*/
addLoadEvent(doPopups)
addLoadEvent(checkForAnchor)
addLoadEvent(doHelpLinks)
addLoadEvent(ui)

/* UI
---------------------------------------*/
function ui() {
	uiSearchBox();
}

function uiSearchBox() {
	if (!document.getElementById) return false;
	
	var search = document.getElementById("input-search");
	
	//Verify Element Found.
	if (search != null)
	{
	    search.value = 'Search';
	    search.onclick = function() {
		    if (this.value == 'Search') this.value = '';
	    }
	    search.onkeypress = search.onclick;
	    search.onblur = function () {
		    if (this.value == '') this.value = 'Search';
	    }
	}
	
	var search = null;
}

/* Generic functions
---------------------------------------*/
function openLink(oururl,features,winname) { // SP2 friendly popups hopefully
	if (oururl) {
		if(!features) { features="" }
		if(!winname) { winname="" }
		try	{		    
			window.open(oururl,winname,features);		
			return false;
		}
		catch (e)	{
			return true; // Just use normal link if popup cannot open
		}
	}
}
function doPopups() { // Open links .'class' in new windows
	if (!document.getElementsByTagName) return false;		
	
	var links = document.getElementsByTagName("a");  
	for (var i=0; i < links.length; i++) {			
			
		if (links[i].className.match("newwin")) { // Open new window
			links[i].onclick = function() {
				openLink(this.href);
				return false;
			}
			links[i].title = "This link will open in a new window";
		}
		else if (links[i].className.match("popup")) {	// Open popup 
			links[i].onclick = function() {
				openLink(this.href,"width=550,height=490,resizable=0,scrollbars=1,navigation=0,menubar=0,location=0,status=0,toolbar=0");
				return false;
			}
			links[i].title = "This link will open in a new window";			
				
		}
	}  
}

/* FAQ highlighter functions
-----------------------------------*/
function doHelpLinks() { // setup highlighting from links
	if (!document.getElementById) return false;
	if (document.getElementById("faqs")) {
		var links = document.getElementById("faqs").getElementsByTagName("a")
		for (var i=0; i < links.length; i++) {
			links[i].onclick = function() {
				jump(this.href);
			}
		}
	}
	else {
		return false;
	}
}

var theID = "";
function highlighter(styleloop) {
	if (!document.getElementById) return false;
	var limit = 6;
	if (styleloop == 1) {
		document.getElementById(theID).className = "faq-step" + styleloop.toString();
		setTimeout("highlighter(" + (styleloop + 1) + ")", 1000);
	}
	else if (styleloop <= limit) {
		document.getElementById(theID).className = "faq-step" + styleloop.toString();
		setTimeout("highlighter(" + (styleloop + 1) + ")", 33);
	}
	else {
		document.getElementById(theID).className = "faq-step0";
	}
}

function jump(target) {
	if (!document.getElementById) return false;
	if (target)	{
		if ( target.indexOf("#") >= 0 ) {
			
			if ((theID != "") && document.getElementById(theID)) {
				document.getElementById(theID).className = "faq_off";
			}
			
			theID = target.substring(target.indexOf("#")+1);
			
			if (document.getElementById(theID)) {
				highlighter(0);
			}
			else {
				//alert("not found: "+theID);
			}
			
		}
	}
}

function checkForAnchor() {
	if (!document.getElementById("faqs")) return false;
	if (location.hash && location.hash!="#content") {
		jump(location.hash);
	}
}

/* Contact form show and hide function
---------------------------------------*/
function show_div(div_id) {
    // hide all the divs
    document.getElementById('health-and-safety').style.display = 'none';
    document.getElementById('investorrelations').style.display = 'none';
    document.getElementById('quality-systems').style.display = 'none';
    document.getElementById('sample-request').style.display = 'none';
    document.getElementById('product-inquiry').style.display = 'none';
    document.getElementById('website-issue').style.display = 'none';
    document.getElementById('general-inquiry').style.display = 'none';		
    document.getElementById('send-button').style.display = 'none';		
    // show the requested div
    document.getElementById(div_id).style.display = 'block';
}


/* Global onLoad function handler
---------------------------------------*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
	  		oldonload();
	  		func();
//			checkEqualise();
		}
	}
}

function changeHeight(pixelMeasure) {
	var obj = document.getElementById("basic-chemicals");
	obj.style.height = pixelMeasure+"px";
}

document.getElementsByClassName = function(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    arrElements = null;
    strClassName = null;
    oRegExp = null;
    oElement = null;
    return (arrReturnElements)
}


//Prevents Page PostBack when hit 'Enter' key.
function PreventPostBack()
{
    if (window.event.keyCode == 13) 
    {
        event.returnValue=false; 
        event.cancel = true;
    }
}