function AttachOnload(MyFunction) {
	if (window.attachEvent) { window.attachEvent("onload",MyFunction); }
	else if (window.addEventListener) { window.addEventListener("load",MyFunction,false); }
}

// attach functions to run on an object's (OBJ) event (EVT)
function AttachEvent(obj, evt, MyFunction) 
{
	if (typeof(obj) == "string") obj = document.getElementById(obj); 

	evt = evt.replace(/^on/i,"");

	if (obj.attachEvent)
		obj.attachEvent("on" + evt, MyFunction);
	else if (obj.addEventListener)
		obj.addEventListener(evt, MyFunction, false);
}

// Check for targetblank
function makeLinksPopup() {
	convertLinks("a");	// Links
	convertLinks("area");	// Image Maps
}


function convertLinks(tagType) {
	var targets = document.getElementsByTagName(tagType); 
	if (targets) {
		for (var i = 0; i < targets.length; i++) {
			if (targets[i].rel) {
				if ((targets[i].rel == "targetblank") || (targets[i].rel == "lightbox")) {
					targets[i].target = "_blank";
				}
			}
		}
	}
}

function splitBrowsers(id) {
	if (typeof(document.styleSheets[id].cssRules) == "object") {
		return document.styleSheets[id].cssRules;
	}
	else {
		return document.styleSheets[id].rules;
	}
}

function getStyleBySelector( selector ) {
   var sheetList = document.styleSheets;
   var ruleList;
   var i, j;

   /* look through stylesheets in reverse order that they appear in the document */
   for (i=sheetList.length-1; i >= 0; i--) {
	   ruleList = splitBrowsers(i);
	   for (j=0; j<ruleList.length; j++) {
		   if (ruleList[j].selectorText == selector) {
				return ruleList[j].style;
		   }   
	   }
   }
   return null;
}

// Alternative to m.offsetLeft as some browsers take their offsets from the body while others take it from the parent element
function FindPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) { curleft += obj.x; }
	return curleft;
}

// Alternative to m.offsetTop as some browsers take their offsets from the body while others take it from the parent element
function FindPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) { curtop += obj.y; }
	return curtop;
}

// get the height of the window
function GetWindowHeight() {
	if (window.innerHeight) 
		var window_height = window.innerHeight;
	else {
		if (document.documentElement.clientHeight)
			var window_height = document.documentElement.clientHeight;
		else
			var window_height = document.body.clientHeight;
	}
	return window_height;
}

// get the position of the scroll bar
function GetScrollTop() {
	if (window.pageYOffset)
		var window_top = window.pageYOffset;
	else {
		if (document.documentElement.scrollTop)
			var window_top = document.documentElement.scrollTop;
		else
			var window_top = document.body.scrollTop;
	}
	return window_top;
}

function writeThickboxCSS() {
	var obj1=getStyleBySelector("#TB_overlay");
	if (obj1!=null) {
		obj1.Opacity="0.6";
		obj1.MozOpacity="0.6";
		obj1.filter="alpha(opacity=60)";
	}

	var obj2=getStyleBySelector("#TB_HideSelect");
	if (obj2!=null) {
		obj2.Opacity="0";
		obj2.MozOpacity="0";
		obj2.filter="alpha(opacity=0)";
	}

	var obj3=getStyleBySelector(".clearfix");
	if (obj3!=null) obj3.Display="inline-block";
}

// Attaches a style to the page e.g. AttachStyle("#wrapper", "width:500px");
function AttachStyle(to, what) {

	// IE of course needs its own version
		if (document.createStyleSheet) {
			var obj = document.createStyleSheet();
			return obj.addRule(to, what);
		}

	// for the good browsers
		var head = document.getElementsByTagName("head")[0];
		var obj = document.createElement("STYLE");

		if (obj && head) {
			obj.setAttribute("type", "text/css");

			var entry = document.createTextNode(to + " { " + what + " }");

			if (obj.appendChild && head.appendChild) {
				obj.appendChild(entry);
				head.appendChild(obj);
			}
		}
}

// attaches a stylesheet to the page
function AttachStyleSheet(src) {

	var head = document.getElementsByTagName("head")[0];
	var obj = document.createElement("link");

	if (obj && head) {
		obj.setAttribute("type", "text/css");
		obj.setAttribute("rel", "stylesheet");
		obj.setAttribute("href", src);

		if (head.appendChild) head.appendChild(obj);
	}
}

// sorts out IE valigns, converts the DIVs (for validation) to SPANs
function GetValignDivs()
{
	var divs = document.getElementsByTagName("div");

	for (var i = 0; i < divs.length; i++) {
		var div = divs[i];
		var className = String(div.className);

		if (className.match(/ie_valign/i)) {
			var innerHTML = div.innerHTML;

			var span = document.createElement("span");
			span.className = "ie_valign";
			span.innerHTML = innerHTML;

			div.parentNode.replaceChild(span, div);
		}
	}
}

function doWidthFix()
{
	// get top nav width
		var nav = document.getElementById("top_list");
		if (!nav) return;
	
	// get/set up variables for width
		var nav_width = nav.offsetWidth;
		var nav_width_remaining = nav_width;
		var total_offset_width = 0;
		var item_widths = new Array();
		var item_widths_order = new Array();
		var this_item = 0;


	// get items in top nav
		var items = nav.getElementsByTagName("li");
		var num_of_items = items.length;
		if (!items) return;

		for (var i = 0; i < num_of_items; i++)
		{
			// get width of item
				var length = items[i].offsetWidth;
				length = Math.max(55, length);

			// remember length of string
				item_widths[i] = length;
				total_offset_width+= length;
			
			// put lengths in order
				if (!item_widths_order.length)
					item_widths_order[0] = i;
				else
				{
					var got_to_end = true;

					for (var k = 0; k < item_widths_order.length; k++) 
					{
						// get current smallest index
							var j = item_widths_order[k];

						// if the length of the item is smaller than the one we're searching
							if (length <= item_widths[j])
							{
								item_widths_order.splice(k,0,i);
								got_to_end = false;
								break;								
							}
					}

					// add it on the end if needs be
						if (got_to_end) item_widths_order.push(i);
				}

			// get which ever one we're styling at the moment
				if (this.firstChild == items[i].firstChild) this_item = i;
		}

	// lets go through each of the items and work out the width (and set it if its the current item)...
		for (var i = 0; i < item_widths_order.length; i++)
		{
			var k = item_widths_order[i];
			var width_portion = item_widths[k] / total_offset_width;
			var width = parseInt(nav_width * width_portion);

			// lets make it a minimum width
				width = Math.max(width, 66);

			// lets make sure this still fits in...
				width = Math.min(width, nav_width_remaining);

			// if its the last item then make it use up the remaining space
				if (item_widths_order.length - i <= 1) width = nav_width_remaining;
			
			// set width
				if (k > 0)
				{
					items[k].style.width = (width - 10) + "px";
				}
				else
				{
					items[k].style.width = width + "px";
				}

			// get rid of left and right padding which will just confuse things!
				items[k].firstChild.style.paddingLeft = "0px";
				items[k].firstChild.style.paddingRight = "0px";
				items[k].firstChild.style.whiteSpace = "normal";


			// if we get to here then subtract from total remaining
				nav_width_remaining-= width;
		}
}

function chkcontrol(j,t) 
{
	var total=0;

	for(var i=0; i < document.getElementById("prod_form").ckb.length; i++)
	{
		if(document.form1.ckb[i].checked)
		{
			total = total +1;
		}
			
		if(total > t)
		{
			alert("Please Select only " + t + " sensors before adding this product to basket")
			document.getElementById("prod_form").ckb[j].checked = false ;
			return false;
		}
	}
} 

AttachOnload(doWidthFix);
AttachOnload(makeLinksPopup);
AttachOnload(writeThickboxCSS);
if (false /*@cc_on || true @*/) AttachOnload(GetValignDivs);
