﻿/* SOME CONSTANTS */
var colHLInfo = '#FAD15B';  //Highlight colour for informational messages
var colHLError = '#E3352D'; //Highlight colour for error messages

/*
	Borrowed from http://www.bennolan.com/behaviour/behaviour.js		
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function getXHR() {
	var xmlhttp = false;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function formData(form) {
	var query = "";
	for (var i=0; i<form.elements.length; i++) {
		query += form.elements[i].name;
		query += "=";
		query += escape(form.elements[i].value);
		query += "&";	
	}
	return query;
}
				
function setTextSize(size) {
	if(size != null) {
		document.body.style.fontSize = size + "%";
		setCookie('textSize',size,364)
	}
}

function showTab(names) {
	// Shows namegiven tab(s) (e.g. "Basket")
	arrNames = names.split(',');
	var theTab = document.getElementById('tab' + arrNames[0])
	if(theTab != null) {
		var theTabs = theTab.parentNode.getElementsByTagName('li')
		for(i = 0;i < theTabs.length;i++) {
			if(theTabs[i].className != 'tabTitle') {
				theTabs[i].className = '';
			}
		}
				
		var thePanel = document.getElementById('pnl' + arrNames[0])
		var thePanels = theTab.parentNode.parentNode.parentNode.getElementsByTagName('div')
		for(i = 0;i < thePanels.length;i++) {
			if(thePanels[i].className.indexOf('panel') != -1) {
				thePanels[i].style.display = 'none';
			}
		}
		
		theTab.className = 'tabActive';
		theTab.getElementsByTagName('a')[0].blur();
		
		for(i = 0;i < arrNames.length;i++) {
			thePanel = document.getElementById('pnl' + arrNames[i])
			if(thePanel != null) {
				thePanel.style.display = 'block';
			}
		}
	}
	return false;
}

function setFormatGroup(groupID) {
	//This is now also used by the widget format selectors on product.aspx

	//reset format selector highlighting
	var fmtAllSelectors = getElementsByClassName('div','formatSelector');
	for(i = 0;i < fmtAllSelectors.length;i++){
		remClassName(fmtAllSelectors[i],'selected');
	}
	
	//check the formatgroup selector
	var fmtRadio = document.getElementById('fmtRadio_' + groupID);
	fmtRadio.checked = true; 
	
	//highlight current formatgroup selector
	var fmtSel = document.getElementById('fmtSel_' + groupID);
	addClassName(fmtSel,'selected');
	
	//hide all formatgroup prices
	var fmtAllGroups = getElementsByClassName('form','fmtGrp');
	for(i = 0;i < fmtAllGroups.length;i++){
		fmtAllGroups[i].style.display = 'none';
	}
	
	//only display prices and formats for the selected group
	var fmtGroups = getElementsByClassName('form','fmt_' + groupID);
	for(i = 0;i < fmtGroups.length;i++){
		fmtGroups[i].style.display = 'inline';
	}
}

function showToolTip(tooltip,location,left,top,pos, forceShow) {
	// Displays the specified tooltip at the specified object's location (adjusted with left & top)
	if((forceShow || getCookie(tooltip) != 0) && document.getElementById(location) && document.getElementById(tooltip)) {
		var objLocation = document.getElementById(location);
		var objTooltip = document.getElementById(tooltip);
		var arrPos = getPosition(objLocation);
		objTooltip.style.display = 'block';
		if(pos == 'R' || pos == 'BR' || pos == 'TR') {
			objTooltip.style.left = arrPos[0] + (objLocation.offsetWidth + left) + 'px';
		}
		if(pos == 'L' || pos == 'BL' || pos == 'TL') {
			objTooltip.style.left = arrPos[0] - (objTooltip.offsetWidth - left) + 'px';
		}
		if(pos == 'T' || pos == 'TR' || pos == 'TL') {
			objTooltip.style.top = arrPos[1] - (objTooltip.offsetHeight + top) + 'px';
		}
		if(pos == 'B' || pos == 'BR' || pos == 'BL') {
			objTooltip.style.top = arrPos[1] + (objLocation.offsetHeight + top) + 'px';
		}
		if(getScrollPos() > arrPos[1] - objTooltip.offsetHeight) {
			window.scrollTo(0,arrPos[1] - objTooltip.offsetHeight)
		}
	}
}

function closeTooltip(tooltip) {
	if(document.getElementById(tooltip)) {
		document.getElementById(tooltip).style.display = 'none';
		setCookie(tooltip,0);
	}
}

function setHistory(strTitle,strLink) {
	if(getCookie('history') != null) {
		var arrHistory = getCookie('history').split('|',9);
	}
	else {
		var arrHistory = new Array();
	}
	if(strLink.indexOf('404') < 0 && strLink.indexOf('500') < 0 && strTitle != decodeURIComponent(arrHistory[0]).split('`')[1]) {
		arrHistory.unshift(encodeURIComponent(strLink + '`' + strTitle));
	}				
	setCookie('history',arrHistory.join('|'));
}

function getHistory() {
	if(getCookie('history') != null) {
		var arrHistory = getCookie('history').split('|',9);
		var historyList = document.createElement('ol');
		historyList.className = 'chart item';
		historyList.id = 'historyList';
		historyList.start = 1;
		var historyPanel = document.getElementById('pnlHistory');
		historyPanel.appendChild(historyList);

		for(i = 0;i < arrHistory.length;i++){
			var strClass = (i % 2 == 0) ? "odd" : "even";
			var newLi = document.createElement('li');
			var arrLink = decodeURIComponent(arrHistory[i]).split('`');
			var trackerJS = 'onClick="historyEventTracker._trackEvent(\'HistoryClick\', \'' + arrLink[1].replace(/'/g, "") + '\', ' + i + '); return true;"';
			newLi.innerHTML = '<a href="' + arrLink[0] + '" ' + trackerJS + '>' + arrLink[1] + '</a>';
			newLi.className = strClass;
			historyList.appendChild(newLi);
		}
	}
	else {
		document.getElementById('pnlHistory').innerHTML = '<div class="item"></div>';
	}
}

function getPath() {
	var strURL = location.toString().replace('://','');
	return strURL.substring(strURL.indexOf("/"));
}

function setCookie(name,value,days) {
	// Needs no further introduction I think...
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	// Needs no further introduction I think...
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function delCookie(name) {
	// Needs no further introduction I think...
	setCookie(name,"",-1);
}

function getElementsByClassName(strTagName, strClassName){
	// Needs no further introduction I think...
	var arrElements = (strTagName == "*" && document.all)? document.all : document.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)
}

/*
	Minor improvement, based on http://ejohn.org/blog/getelementsbyclassname-speed-comparison/
	-Allows getElementsByClass within an elenment (rather than defaulting to document scope)
	-Can pass in a callback function to avoid looping over elements twice
*/
function foreachElementsByClass(searchClass, tag, node, callback) {
	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|$)");
  var retEls = new Array();
	for (i = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			retEls.push(els[i]);
			if (callback != null) {
				callback(els[i]);
			}
    }
  }    
  return retEls;
}

function getPosition(obj) {
	// Returns the x & y coordiantes of an object's top left corner relative to the page
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getScrollPos() {
	// Returns the current vertical scroll position
	var intPage = window.pageYOffset ? window.pageYOffset : 0; 
	var intElement = document.documentElement ? document.documentElement.scrollTop : 0;
	var intBody = document.body ? document.body.scrollTop : 0;
	var intScrollTop = intPage ? intPage : 0;
	if (intElement && (!intScrollTop || (intScrollTop > intElement)))
		intScrollTop = intElement;
	return intBody && (!intScrollTop || (intScrollTop > intBody)) ? intBody : intScrollTop;
}


function setOpacity(obj,opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	if (typeof(obj) == 'object' && obj.style) {
		if (obj.style.MozOpacity!=null) {  
			obj.style.MozOpacity = (opacity/100) - .001;
		} 
		else if (obj.style.opacity!=null) {
			obj.style.opacity = (opacity/100) - .001;
		} 
		else if (obj.style.filter!=null) {
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}
	
function getOpacity(obj) {
	var opactiy = 100;
	if (typeof(obj) == 'object' && obj.style) {
		if (obj.style.MozOpacity!=null) {  
			opactiy = parseInt((obj.style.MozOpacity*100) + .001);
		} 
		else if (obj.style.opacity!=null) {
			opactiy = parseInt((obj.style.opacity*100) + .001);
		} 
		else if (obj.style.filter!=null) {
			opactiy = obj.filters.item("alpha").opacity;
		}
	}
	return opactiy;
}


function hasClassName(objElement, strClass) {
	// Returns boolean indicating whether the object has the supplied class name
	if ( objElement.className ) {
		var arrList = objElement.className.split(' ');
		var strClassUpper = strClass.toUpperCase();
		for ( var i = 0; i < arrList.length; i++ ) {
			if ( arrList[i].toUpperCase() == strClassUpper ) {
				return true;
			}
		}
	}
	return false;
}

function addClassName(objElement, strClass, blnMayAlreadyExist) {
	// Adds a class to the class attribute of a DOM element
	if (objElement.className ) {
		var arrList = objElement.className.split(' ');
		if (blnMayAlreadyExist ) {
			var strClassUpper = strClass.toUpperCase();
			for (var i = 0; i < arrList.length; i++ ) {
				if (arrList[i].toUpperCase() == strClassUpper ) {
					arrList.splice(i, 1);
					i--;
				}
			}
		}
		arrList[arrList.length] = strClass;
		objElement.className = arrList.join(' ');
	}
	else {   
		objElement.className = strClass;
	}
}

function remClassName(objElement,strClass) {
	// Removes a class from the class attribute of a DOM element
	if (objElement.className ) {
		var arrList = objElement.className.split(' ');
		var strClassUpper = strClass.toUpperCase();
		for (var i = 0; i < arrList.length; i++ ) {
			if (arrList[i].toUpperCase() == strClassUpper ) {
				arrList.splice(i, 1);
				i--;
			}
		}
	objElement.className = arrList.join(' ');
	}
}


//tips javascript
var objFocus = 'undefined';
var bitTip = false;

function setTips() {
	var arrTools = getElementsByClassName('*','tool');
	for(var i = 0;i < arrTools.length;i++) {
		arrTools[i].onmouseover = function() {
			if(objFocus == 'undefined' &! bitTip) {
				showTip(this);
			}
		}
		arrTools[i].onfocus = function() {
			objFocus = this;
			if(!bitTip) {
				showTip(this);
			}
		}
		arrTools[i].onmouseout = function() {
			if(objFocus == 'undefined' && bitTip) {
				hideTip();
			}
		}
		arrTools[i].onblur = function() {
			objFocus = 'undefined';
			if(bitTip) {
				hideTip();
			}
		}
	}
}

function showTip(objTool) {
	var objTip = document.getElementById('divToolTip')
	var arrPos = findPos(objTool);
	objTip.style.left = arrPos[0] + 20 + 'px';
	objTip.style.top = arrPos[1] - 1 + objTool.offsetHeight + 'px';
	objTip.innerHTML = objTool.title;
	objTip.className = objTool.id;
	objTip.style.display = 'block';
	objTool.title = '';
	bitTip = true;
}

function hideTip() {
	var objTip = document.getElementById('divToolTip')
	document.getElementById(objTip.className).title = objTip.innerHTML;
	objTip.style.display = 'none';
	bitTip = false;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function buildGraph(arrNames,arrValues,objChart,bitNormaliseAll) {
	var intGraphWidth = objChart.offsetWidth;
	var intStepping = ((intGraphWidth / arrValues.length) / intGraphWidth) * 100;
	
	for(i=0;i<arrNames.length;i++) {
		var intMaxValue = 0;
		if(bitNormaliseAll) {
		//highest value of each individual set is always 100%
			for(j=0;j<arrValues.length;j++) {
				if(arrValues[j][i] > intMaxValue) {
					intMaxValue = arrValues[j][i];
				}
			}
		}
		else {
		//highest value of all sets combined is 100%
			for(h=0;h<arrNames.length;h++) {
				for(j=0;j<arrValues.length;j++) {
					if(arrValues[j][h] > intMaxValue) {
						intMaxValue = arrValues[j][h];
					}
				}
			}
		}
		for(j=0;j<arrValues.length;j++) {
			theBar = document.createElement('div');
			theBar.style.top = 100 - ((arrValues[j][i] / intMaxValue) * 100) + '%';
			theBar.style.left = j * intStepping + (intStepping / 2 * i) + '%';
			theBar.style.width = 50 / arrValues.length + '%';
			theBar.className = arrNames[i];
			objChart.appendChild(theBar);
		}
		objLegend = document.getElementById(objChart.id + '_legend')
		theKey = document.createElement('div');
		theKey.className = 'key ' + arrNames[i];
		theName = document.createElement('div');
		theName.innerHTML = arrNames[i];
		objLegend.appendChild(theKey);
		objLegend.appendChild(theName);
	}
}