function MM_swapImgRestore() { //v3.0 (formatted)
	var i, x, a = document.MM_sr;
	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) {
		x.src = x.oSrc;
	}
}

function MM_preloadImages() { //v3.0 (modified)
	var d = document;
	if (d.images) {
		if (!d.MM_p) {
			d.MM_p = new Array();
		}
		var i, j = d.MM_p.length, a = (arguments ? arguments : MM_preloadImages.arguments);
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
		}
	}
}

function MM_findObj(n, d) { //v4.01 (formatted)
	var p, i, x;
	if (!d) {
		d = document;
	}
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) {
		x = d.all[n];
	}
	for (i = 0; !x && i < d.forms.length; i++) {
		x = d.forms[i][n];
	}
	for (i = 0; !x && d.layers && i < d.layers.length; i++) {
		x = MM_findObj(n, d.layers[i].document);
	}
	if (!x && d.getElementById) {
		x = d.getElementById(n);
	}
	return x;
}

function MM_swapImage() { //v3.0 (modified)
	var i, j = 0, x, a = (arguments ? arguments : MM_swapImage.arguments);
	document.MM_sr = new Array;
	for (i = 0; i < (a.length - 2); i += 3) {
		if ((x = MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if (!x.oSrc) {
				x.oSrc = x.src;
			}
			x.src = a[i + 2];
		}
	}
}

function changeImages() {
	if (document.images) {
		var a = (arguments ? arguments : changeImages.arguments);
		for (var i = 0; i < a.length; i += 2) {
			document[a[i]].src = a[i + 1];
		}
	}
}

function scriptLoader() {
	MM_preloadImages('http://www.alfamax.se/bilder/nav_left_active.jpg',
		'http://www.alfamax.se/bilder/nav_home_active.jpg',
		'http://www.alfamax.se/bilder/nav_right_active.jpg',
		'/Webbutik/images/meny_03-over.jpg',
		'/Webbutik/images/meny_04-over.jpg',
		'/Webbutik/images/meny_07-over.jpg',
		'/Webbutik/images/meny_08-over.jpg',
		'/Webbutik/images/meny_09-over.jpg',
		'/Webbutik/images/meny_10-over.jpg',
		'/Webbutik/images/meny_11-over.jpg'
		);
}

/* HelpViewer script*/
function toggleInfo(id) {
	var obj = document.getElementById(id);
	obj.className = obj.className == "showInfo" ? "hideInfo" : "showInfo";
}
function jumpToAnchor(anchorName) {
	location.hash = anchorName;
}
/* Cart functions */
function handleResponse(message) {
	alert(message);
}
function callServer(cmd, itemId, quantityFieldName) {
	var url = "Cart.do" + "?" + "method=" + cmd + "&itemId=" + itemId;
	var id = "CartFrame";
	
	if (document.getElementById) { // Check if getElementById is supported
		if (typeof(quantityFieldName) != "undefined") {
			if (navigator.cookieEnabled) {
				var quantityField = document.getElementById(quantityFieldName);
				url += "&quantity=" + quantityField.value;
				quantityField.value = "";
			} else {
				displayIncompatibilityMessage();
				return false;
			}
		} else {
			displayIncompatibilityMessage();
			return false;
		}
		
		var iFrame = document.getElementById(id);
		if (iFrame.contentWindow) {  // Check if contentWindow is supported
			iFrame.contentWindow.location.replace(url);
		} else if (iFrame.contentDocument) { // Opera 7
			iFrame.contentDocument.location.replace(url);
		} else { // IE5.0
			displayIncompatibilityMessage();
		}
	} else { // IE4 or equivalent
		displayIncompatibilityMessage();
	}
	return false;
}
function displayIncompatibilityMessage() {
	alert("Din webbl\xe4sare har tyv\xe4rr inte alla funktioner som kr\xe4vs\nf\xf6r att du ska kunna anv\xe4nda Webbutiken.\n\nL\xe4s mer p\xe5 startsidan om vilka webbl\xe4sare som fungerar med Webbutiken.");
}
function loadCart() {
	var url = "Cart.do";
	var id = "CartFrame";
	
	if (!document.getElementById) {
		//displayIncompatibilityMessage();
		return;
	}
	
	var iFrame = document.getElementById(id);

	if (iFrame.contentWindow) {  // Check if contentWindow is supported
		iFrame.contentWindow.location.replace(url);
	} else if (iFrame.contentDocument) { // Opera 7
		iFrame.contentDocument.location.replace(url);
	} else { // IE5.0
		//displayIncompatibilityMessage();
		return;
	}
}

function set(fieldName, newValue) {
	if (document.getElementById) { // Check if getElementById is supported
		if (typeof(fieldName) != "undefined") {
			var field = document.getElementById(fieldName);
			field.value = newValue;
		} else {
			displayIncompatibilityMessage();
		}
	} else { // IE4 or equivalent
		displayIncompatibilityMessage();
	}
};

function move(dir, fieldName) {
	if (document.getElementById) { // Check if getElementById is supported
		if (typeof(fieldName) != "undefined") {
			var list = document.getElementById(fieldName);
			var index = list.selectedIndex;
			if (index == -1) {
				alert("Välj 1 rad som du vill flytta i listan.");
			} else {
			  var nextIndex = index + (dir == "up" ? -1 : 1);
			  if (nextIndex < 0) {
			  	nextIndex = list.length-1;
			  }
			  if (nextIndex >= list.length) {
			  	nextIndex=0;
			  }
			  
			  var oldVal = list[index].value;
			  var oldText = list[index].text;
			  list[index].value = list[nextIndex].value;
			  list[index].text = list[nextIndex].text;
			  list[nextIndex].value = oldVal;
			  list[nextIndex].text = oldText;
			  list.selectedIndex = nextIndex;
			}
		} else {
			displayIncompatibilityMessage();
		}
	} else { // IE4 or equivalent
		displayIncompatibilityMessage();
	}		
}

function selectAll(fieldName) {
	if (document.getElementById) { // Check if getElementById is supported
		if (typeof(fieldName) != "undefined") {
			var list = document.getElementById(fieldName);
		
			for (var i = 0; i < list.options.length; i++) {
				list.options[i].selected = true;
			}
		} else {
			displayIncompatibilityMessage();
		}
	} else { // IE4 or equivalent
		displayIncompatibilityMessage();
	}
}

function handleKeyPress(e, itemId, quantityFieldName) {
	var key = e.keyCode || e.which;
	if (key == 13) {
		callServer("add", itemId, quantityFieldName);
	}
}
/* Brick functions */
function brickOver(caller) {
	var id = caller.id;
	document.getElementById(id).className = "brickActive";
}
function brickOut(caller) {
	var id = caller.id;
	document.getElementById(id).className = "brick";
}
function brickClick(caller) {
	var id = caller.id;
	var itemId = id.substr(1, id.length - 1);
	self.location.href = "?itemId=" + itemId + "&piid=" + getPreviousItemId() + "&pgid=" + getPreviousGroupId();
	
	//alert("Öppna detaljsida. itemId=" + itemId);
}
function getPreviousItemId() {
	return getQuerystringVariable("itemId");
}
function getPreviousGroupId() {
	return getQuerystringVariable("groupId");
}
function getQuerystringVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return "";  // Not found
}

