/**************************************
*                Main                 *
**************************************/
//Used on category page. Sets licence button background
function catBtnBg(obj, state) {
	direction = obj.name == 'less' ? 'left' : 'right';
	color = state == 0 ? 'grey' : 'blue';
	obj.style.background = 'url("images/buttons/' + color + '-' + direction + '.gif")';
}

//Used on category page. Sets Adds or Subtracts from licence num
function incCatVal(id, amount) {
	obj = gEBI('txtNum'+id);
	obj.value = Math.min(Math.max(parseInt(obj.value)+amount, 1), 9);
	updateCat(id);
}

//Used on category page. Sets total price
function updateCat(id) {
	valObj = gEBI('txtNum'+id);
	totObj = gEBI('catTotal'+id);
	price = parseInt(gEBI('catPrice'+id).innerHTML*100);
	
	//Ensure valid number
	if (isNaN(parseInt(valObj.value)) || valObj.value<1)
		valObj.value=1;
	
	//Calculate price
	totObj.innerHTML = number_format(price*valObj.value/100, 2);
}


/**************************************
*             Generic                 *
**************************************/

//Enable redirections by default. This is to combat a drop down list bug
enableRedirs=1;

//Handles drop down menu actions
startList = function() {
	ddRoot = document.getElementById("dropdown");
	for (x=0; x<ddRoot.childNodes.length; x++) {
		node = ddRoot.childNodes[x];
		if (node.nodeName=="LI") {
			node.onmouseover=function() { this.className+=" over"; }
			node.onmouseout=function() { this.className=this.className.replace(" over", ""); }
			node.onclick=function() { gotoPage(this) }
		}
	}
	
	//Now handle submenu onclick events
	ddSubs = getElementsByClassName(ddRoot, "UL", 'ddSub');
	//For each submenu
	for (j=0; j<ddSubs.length; j++) {
		ddSub = ddSubs[j];
		for (x=0; x<ddSub.childNodes.length; x++) {
			node = ddSub.childNodes[x];
			if (node.nodeName=="LI") {
				node.onmouseover=function() { this.className="subOver"; }
				node.onmouseout=function() { this.className=''; }
				node.onclick=function() { gotoSubPage(this) }
			}
		} //for each sublist node
	} //for each sublist
}

//Main menus use 1, subs use 0
function gotoPage(node)
{
	if (enableRedirs)
		window.location=node.childNodes[1].href;
	enableRedirs=0;
}
function gotoSubPage(node)
{
	//alert(node.childNodes[0]);
	if (enableRedirs)
		window.location = node.childNodes[0].toString();
	enableRedirs=0;
}

function setImg(obj,imgName)
{
	obj.src='images/'+imgName;
}

function isDefined(variable)
{
	return (!(!( variable||false )));
}

/* Returns true if user is using IE */
function isIE() {
  var ua = navigator.userAgent.toLowerCase();
  return ((ua.indexOf('msie') != -1) && (ua.indexOf('opera') == -1) && 
          (ua.indexOf('webtv') == -1) &&
          (location.href.indexOf('seenIEPage') == -1));
}

function isIE6() {
  var ua = navigator.userAgent.toLowerCase();
  return ((ua.indexOf('msie 6.0') != -1) && (ua.indexOf('opera') == -1));
}

/* Shortened document.getElementById. */
function gEBI(a,b,c) {
	if(!b&&!c)return document.getElementById(a);
	if(b=="inn")if(c)gEBI(a).innerHTML=c; else return gEBI(a).innerHTML;
	if(b=="add"&&c)gEBI(a).innerHTML+=c;
}

/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
	Returns an array of all elements of a specified class name
*/
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.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);
		}
	}
	return (arrReturnElements)
}

//Converts a value to a currency with specified number of decimals
function number_format(val, decimalCount) {
	if (decimalCount == 0) return Math.ceil(val);

	var i = parseFloat(val);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

/* Creates a popup of specified url */
function popup(name, pg, width, height, scrollbars) {
	var popup = window.open(pg, name, "toolbar=0,scrollbars=" + scrollbars + ",location=0,statusbar=0," + 
									"menubar=0,resizable=0,width=" + width + ",height=" + height);
}