//order confirmation and order receipt page - split the email address on the RHN if too long
function splitEmailAdd(usemail) {
	var stringlist = new Array();
	while (usemail.length > 30) {
	   stringlist.push( usemail.slice(0,30));
	   usemail=usemail.substr(30);
	}
	if (usemail.length) {
	  stringlist.push(usemail);
	}
	document.write(stringlist.join( '<br>' ));
}

//dynamic nav script
turnonToggle = 1; //change this to 0 if you don't want to use toggling
// preload controls
// change path to the desired location
shown = new Image();
shown.src = "/venda-support/images/bulleton.gif";
hidden = new Image();
hidden.src = "/venda-support/images/bulletoff.gif";

function dynamicContent(where,what) {
	// find out what tag the function is called from so the correct value is passed for url
	identifyTag = where.tagName;
	if (identifyTag == "A"){
		ajaxFunction(where+'&layout=noheaders&temp=subcategories',what);
		if (turnonToggle == 1){toggle(where);}
	} else if (identifyTag == "INPUT" || identifyTag == "SELECT") {
		ajaxFunction(where.value,what);
	}
}
//Variable for checking free ticket
var allowfreeticket = true;

function setequalheight(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));

	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	//return (arrReturnElements)
	var divs = arrReturnElements;
	//alert (divs.length);
	var maxHeight=0;
	for(i=0;i<divs.length;i++){
		//alert(a[i].offsetHeight);
		maxHeight = Math.max(maxHeight,divs[i].offsetHeight);
	}
	for(i=0;i<divs.length;i++){
		if(navigator.appName == "Microsoft Internet Explorer"){
			var version = navigator.appVersion;
			var versnum = version.split("MSIE");
			var versnumber = versnum[1].substring(0,2);
			if(versnumber > 6){
				/*  IE7 only */
				divs[i].style.minHeight=maxHeight + "px";
			}
			else{
				/* IE6 and the less */
				divs[i].style.height=maxHeight + "px";
			}
		}else{
			divs[i].style.minHeight=maxHeight + "px";
		}
	}
	return maxHeight;
}
// ---
// Array support for the push method in IE 5
if(typeof Array.prototype.push != "function"){
            Array.prototype.push = ArrayPush;
            function ArrayPush(value){
                        this[this.length] = value;
            }
}

// pre-load all images into cache
function preloadImage(images) {
	if (images) {
		if (images.length) {
			for (i = 0; i < images.length; i++) {
				var tmpImage = new Image();
				tmpImage.src = images[i];
			}
		}
	}
}

ShopCart = function() {};
ShopCart.prototype = {
	items: {},
	qtys:  {},

	addItem: function(item) {
		if(!this.items[item.id])
			this.items[item.id] = [];
		this.items[item.id].push(item);
	},

	// This currently just resolves quantity based on parent sku.
	resolveData: function() {
		// Reset the quantities each time we resolve the cart.
		this.qtys = {};
		var cart = this;
		// This should be fine, but I am wary (see http://yuiblog.com/blog/2006/09/26/for-in-intrigue/).
		jq.each(this.items, function(id, items) {
			cart.qtys[id] = 0;
			jq.each(items, function(idx, item) {
				cart.qtys[id] += cart.getItemQty(item);
			});
		});
	},

	// This only checks whether the user has over-ordered.
	isItemValid: function(item) {
		return this.qtys[item.id] <= item.maxorder;
	},

	getItemQty: function(item) {
		return parseInt( document.getElementsByName('setqty-id-' + item.line)[0].value );
	},

	// Just checks the user hasn't over-ordered anything.
	isValid: function() {
		this.resolveData();
		var ok = true;
		var cart = this;
		jq.each(this.items, function(id, items) {
			jq.each(items, function(idx, item) {
				if(cart.isItemValid(item))
					return;

				alert("You can only order "+item.maxorder+" of "+item.desc);
				// Return false to break from the loop aka last, break, etc
				// XXX - This is broken from at least 1.2.1 and only works on arrays.
				return ok = false;
			});
		});
		return ok;
	}
};

function Loading() {
preloadImage(new Array("/content/ebiz/britishmuseumonlineshop/resources/images/shop_on.gif","/content/ebiz/britishmuseumonlineshop/resources/images/packages_on.gif","/content/ebiz/britishmuseumonlineshop/resources/images/tickets_on.gif","/content/ebiz/britishmuseumonlineshop/resources/images/membership_on.gif","/content/ebiz/britishmuseumonlineshop/resources/images/printandimage_on.gif"))}
// XXX - This is fragile and should be replaced with addEvent in future.
window.onload = Loading;

function popup(url,width,height,name) {
    if (width == '' || width == null) width = 400;
    if (height == ''|| height == null) height = 425;
    if (name == '' || name == null) name = "details";
    var props = "toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,titlebar=no,menubar=no,width="+width+",height="+height;
    w = window.open(url, name, props);
    if (w) w.focus();
}

function addOnloadEvent(fnc) {
    if ( typeof window.addEventListener != "undefined" )
        window.addEventListener( "load", fnc, false );
    else if ( typeof window.attachEvent != "undefined" ) {
        window.attachEvent( "onload", fnc );
    }
    else {
         if ( window.onload != null ) {
         var oldOnload = window.onload;
            window.onload = function ( e ) {
            oldOnload( e );
            window[fnc]();
            };
        }
        else
            window.onload = fnc;
    }
}