var globIdx = null;

function getField(frm, fieldName) {
	if (document.getElementById) return document.getElementById(fieldName)
	else if (document.all) return eval("frm.elements('" + fieldName + "')")
	else return eval("frm.elements['" + fieldName + "']");
}

function saveSettings(frm, flag) {
	mediaEl = getField(frm, '1389');
	mediaStr = mediaEl.options[mediaEl.selectedIndex].value;
	fontEl = getField(frm, '1385');
	fontStr = fontEl.options[fontEl.selectedIndex].value;
	familyEl = getField(frm, '1387');
	familyStr = familyEl.options[familyEl.selectedIndex].value;
	lineEl = getField(frm, '1388');
	lineStr = lineEl.options[lineEl.selectedIndex].value;

	if (flag == 'change') {
		url = replaceQuery('setmedia', mediaStr);
		setCookie('MEDIA', mediaStr);
		setCookie('FONT', fontStr);
		if (familyStr == 'Standard') deleteCookie('FAMILY')
		else setCookie('FAMILY', familyStr);
		setCookie('LINEHEIGHT', lineStr);
		window.location.href = url;
	}
	else if (flag == 'default') {
		url = replaceQuery('setmedia', 'WEBBROWSER_ALL');
	//	setCookie('MEDIA', 'WEBBROWSER_ALL');
	//	setCookie('FONT', '80%');
	//	setCookie('FAMILY', 'Verdana');
	//	setCookie('LINEHEIGHT', '120%');
		deleteCookie('MEDIA');
		deleteCookie('FONT');
		deleteCookie('FAMILY');
		deleteCookie('LINEHEIGHT');
		window.location.href = url;
	}
	else {
		setCookie('MEDIA', mediaStr);
		setCookie('FONT', fontStr);
		if (familyStr == 'Standard') deleteCookie('FAMILY')
		else setCookie('FAMILY', familyStr);
		setCookie('LINEHEIGHT', lineStr);
		window.location.href = '/?page=42&setmedia=' + mediaStr;
	}
}

function addToBasket(id, num, bUpdate) {
	idStr = id + '=';
	if (num != null) n = num
	else n = 1;
	cookieName = "fs_basket";
	cookieStr = getCookie(cookieName);
	if ((cookieStr != null) && (cookieStr != 'undefined')) {
		i = cookieStr.indexOf(';' + idStr);
		if (i != -1) {
			lStr = cookieStr.substring(0, i + idStr.length + 1);
			rStr = cookieStr.substring(lStr.length, cookieStr.length);
			n = parseInt(rStr.substring(0, rStr.indexOf(';')));
			if (num != null) num = num
			else num = n+1;
			cookieStr = lStr + num + rStr.substring(n.toString().length, rStr.length);
		}
		else cookieStr += idStr + n + ';';
	}
	else cookieStr = ';' + idStr + n + ';';
	setCookie(cookieName, cookieStr, true);
	if (bUpdate) {
	}
	else {
		alert('Boken har nu lagts till i din varukorg!');
		window.location.reload();
	}
}

function removeFromBasket(id) {
	idStr = id + '=';
	cookieName = "fs_basket";
	cookieStr = getCookie(cookieName);
	if ((cookieStr != null) && (cookieStr != 'undefined')) {
		i = cookieStr.indexOf(';' + idStr);
		if (i != -1) {
			lStr = cookieStr.substring(0, i);
			rStr = cookieStr.substring(lStr.length + idStr.length+1, cookieStr.length);
			n = parseInt(rStr.substring(0, rStr.indexOf(';')));
			cookieStr = lStr + rStr.substring(n.toString().length, rStr.length);
			setCookie(cookieName, cookieStr, true);
		}
	}
	window.location.reload();
}

function updateBasket(frm, flag) {
	for (j=0; j<frm.elements.length; j++) {
		fieldEl = frm.elements[j];
		fieldName = fieldEl.name;
		if (fieldName.indexOf('flowItem_') != -1) {
			id = fieldName.substring(9, fieldName.length);
			if (parseInt(fieldEl.value) > 0) {
				addToBasket(id, fieldEl.value, true);
			}
			else if ((parseInt(fieldEl.value) == 0) || (fieldEl.value == '')) {
				removeFromBasket(id);
			}
		}
	}
	if (flag) checkOutBasket(frm)
	else window.location.reload();
}

function checkOutBasket(frm) {
	window.location.href = '/?page=240';
/*

	for (j=0; j<frm.elements.length; j++) {
		fieldEl = frm.elements[j];
		fieldName = fieldEl.name;
		if (fieldName.indexOf('flowItem_') != -1) {
			id = fieldName.substring(9, fieldName.length);
			if (parseInt(fieldEl.value) > 0) {
				addToBasket(id, fieldEl.value, true);
			}
			else if ((parseInt(fieldEl.value) == 0) || (fieldEl.value == '')) {
				removeFromBasket(id);
			}
		}
	}
*/
}

function browserCheck(str) {
	userAgent = navigator.userAgent.toLowerCase();
	idx = userAgent.indexOf(str);
	if (idx != -1) {
		globVersion = parseFloat(userAgent.charAt(str.length + idx + 1));
		return true;
	}
	return false;
}

function browserObject(str) {
	if (browserCheck("opera")) {
		this.version = globVersion;
		this.name = "Opera";
		this.alias = this.name;
	}
	else if (browserCheck("msie")) {
		this.version = globVersion;
		this.name = "Internet Explorer";
		this.alias = "IE";
	}
	else if (navigator.appName == "Netscape") {
		this.version = parseFloat(navigator.appVersion);
		this.name = "Netscape Navigator";
		this.alias = "NS";
	}
	else {
		this.version = 0;
		this.name = "Unknown";
		this.alias = "Unknown";
	}
}

function writeBrowser() {
	fontSize = getCookie('FONT');
	fontFamily = getCookie('FAMILY');
	lineHeight = getCookie('LINEHEIGHT');

	var s = "<style>";
	if (fontSize != null) s += "td {font-size: " + fontSize + ";}";
	if (fontFamily != null) s += "td, SELECT, TEXTAREA, INPUT, #divPrint, #divNavigation {font-family: " + fontFamily + ";}";
	if (lineHeight != null) s += ".tblPage td {line-height: " + lineHeight + ";}";
	if ((b.alias == "IE" && b.version >= 4) || (b.alias == "NS") && (b.version >= 5)) {
		if (b.alias == "IE") s += "#divContents {position: relative; height: 1px;}";
	//	s += "#divNavigation {position: relative; top: -30px; left: 0px;}";
	//	s += "#search {padding-left: 3px; width: 150px; border: 1px solid #000000;}";
	}
	s += "</style>";
	document.writeln(s);
}

function checkForm() {
	return true;
}

function checkEmail(email) {
	if ((email.indexOf('@', 0) == -1) || (email.indexOf('.', 0) == -1)) return false;
	return true;
}

function getReal(el, type, value) {
	temp = el;
	while ((temp != null) && (temp.tagName != "BODY")) {
		if (eval("temp." + type) == value) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
	}
	return null;
}

function getRealEl(el, type, value, n) {
	i = 0;
	temp = el;
	while ((temp != null) && (temp.tagName != "BODY") && (i < n)) {
		if (eval("temp." + type + ".indexOf('" + value + "')") != -1) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
		i++;
	}
	return null;
}

function getRealNS(tempEl, typ, value, n) {
	i = 0;
	while ((tempEl != null) && (tempEl.tagName != "BODY") && (i < n)) {
		if (tempEl.className != null) {
			if ((tempEl.className.indexOf(value) != -1) || (tempEl.className.indexOf("tblMenu") != -1)) return tempEl;
		}
		tempEl = tempEl.parentNode;
		i++;
	}
	return null;
}

function getRealOffset(el, str) {
	temp = el;
	i = 0;
	while ((temp != null) && (temp.tagName != "BODY")) {
		i += eval("temp." + str);
		temp = temp.offsetParent;
	}
	return i;
}

function handleOver(e) {
}

function handleOut() {
}

function handleClick() {
}

function handleScroll() {
}

function handleResize() {
}

function hover(img, flag, layerEl) {
}

function dhtmlCalendar() {
	if (typeof(bCalendar) == "undefined") return;
	if (!bCalendar) {
		document.write('<flowserver:calendar id="calendar" />');
		document.write('<link rel="stylesheet" type="text/css" href="/css/calendar.css" />');
		document.write('<script language="JavaScript" src="scripts/calendar.js"></script>');
	}
}

function getQueryString(str) {
	var str = str.search.replace(/%20/g, " ");
	var index = str.indexOf("?");
	if (index != -1) return str.substring(index+1,str.length);
	return "";
}

function openWindow(url) {
	if (url != "") window.open(url);
}

function printPage() {
	url = replaceQuery('media', 'PRINT');
	url = replaceQuery('setmedia', '', url);
	window.open(url);
}

function replaceQuery(s, v, str) {
	if (str == null) str = window.location.href;
	aStr = "";
	idx = str.indexOf("#");
	if (idx != -1) {
		aStr = str.substring(idx, str.length);
		str = str.substring(0, idx);
	}
	s1 = '?' + s + '=';
	l = s1.length;
	lIdx = str.indexOf(s1);
	if (lIdx == -1) {
		s1 = '&' + s + '=';
		lIdx = str.indexOf(s1);
	}
	if (lIdx == -1) {
		str += s1 + v;
		str += aStr;
		return str;
	}
	lStr = str.substring(0, lIdx+l);
	rStr = str.substring(lStr.length, str.length);
	rIdx = rStr.indexOf("&");
	if (rIdx != -1) rStr = rStr.substring(rIdx, rStr.length)
	else rStr = '';
	str = lStr + v + rStr;
	str += aStr;
	return str;
}

function togglePart(contEl, flag) {
	if (typeof(contEl) == "string") contEl = document.getElementById(contEl);
	if (contEl != null) {
		srcEl = event.srcElement;
		if (srcEl.tagName == "A") srcEl = srcEl.children[0];
		if ((contEl.currentStyle.display == "none") || flag) {
			contEl.runtimeStyle.display = "block";
			srcEl.src = (srcEl.src).replace("maximize", "minimize");
			setCookie(contEl.id, "1");
		}
		else {
			contEl.runtimeStyle.display = "none";
			srcEl.src = (srcEl.src).replace("minimize", "maximize");
			setCookie(contEl.id, "0");
		}
	}
}

function setPartQuery(partid) {
	str = window.location.href;

	args = arguments;
	argStr = "";
	if (args.length > 2) {
		aStr = "";
		idx = str.indexOf("#");
		if (idx != -1) {
			aStr = str.substring(idx, str.length);
			str = str.substring(0, idx);
		}
		for (j=1; j<args.length; j=j+2) {
			s = args[j];
			v = args[j+1];

			if (s.indexOf("#") == 0) {
				str = str + s;
			}
			else {
				qry = s + "=" + partid + ":";

				idx = str.indexOf(qry);
				if (idx == -1) {
					idx = str.indexOf("?");
					if (idx != -1) flag = "&"
					else flag = "?";
					str = str + flag + qry + v;
				}
				else {
					lStr = str.substring(0, idx + qry.length);
					rStr = str.substring(lStr.length, str.length);
					idx = rStr.indexOf("&");
					if (idx != -1) rStr = rStr.substring(idx, rStr.length)
					else rStr = '';
					str = lStr + v + rStr;
				}
			}
		}
		if (str.indexOf("#") == -1) str = str + aStr;
	//	alert(str);
		if (document.all) window.location.replace(str)
		else window.location.href = str;
	}
}

function replaceLocation(s1, v1, s2, v2) {
	url = replaceQuery(s1, v1);
	if (s2 != null) url = replaceQuery(s2, v2, url);
	window.location.href = url;
}

function gotoPage(num) {
	if (num != null) window.location.href = replaceQuery("currpage", num);
}

function setCookie(cookieName, cookieValue, bExpires) {
	if (bExpires) {
	//	document.cookie = cookieName + "=" + escape(cookieValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT; path=/;"
		document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/;"
	}
	else {
		var expireDate = new Date();
		expireDate.setDate(365 + expireDate.getDate());
		document.cookie = cookieName + "=" + escape(cookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/;"
	}
}

function deleteCookie(cookieName) {
	document.cookie = cookieName + "=0; expires=Fri, 31 Dec 1999 23:59:59 GMT; path=/;";
}

function getCookie(cookieName) {
	var cookieList = document.cookie.split("; ");
	for (var i = 0; i < cookieList.length; i++) {
		var name = cookieList[i].split("=");
		if (unescape(name[0]) == cookieName) return unescape(name[1]);
	}
	return null;
}
