if (window != top) top.location.href = location.href;

function newWin(p, w, h) {
	win = window.open(p, 'ny', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + w + ',height=' + h);
	win.focus();
}

function closeWin() {
	if (window.win) {
		win.close();
	}
}

function preloadImages() {
	var d = document;
	if (d.images) {
		if (!d.imgArray) {
			d.imgArray = new Array();
		}
		var i;
		var j = d.imgArray.length;
		var a = preloadImages.arguments;
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf("#") != 0){ 
				d.imgArray[j] = new Image; 
				d.imgArray[j++].src = a[i];
			}
		}
	}
}

function swapImage(name, src) {
	document.images[name].src = src;
}

function changeLoc(address) {
	window.location.href = address;
}
function over(element) {
	if (document.getElementById || (document.all && !(document.getElementById))) {
		if (document.all) {
			element.style.cursor = "hand";
		}
		for (i = 0; i < element.childNodes.length; i++) {
			if (element.childNodes[i].tagName == 'A') { 
				el = element.childNodes[i];
				//el.style.textDecoration = "underline";
				el.className = "over";
			}
		}
	}
}
function out(element) {
	if (document.getElementById || (document.all && !(document.getElementById))) {
		for (i = 0; i < element.childNodes.length; i++) {
			if (element.childNodes[i].tagName == 'A') { 
				el = element.childNodes[i];
				//el.style.textDecoration = "none";
				el.className = "out";
			}
		}
	}
}

function setCookie (cookieNamn, cookieVarde, antalGiltigaMinuter) {
	var cookieStr = cookieNamn + '=' + escape(cookieVarde);
	if (antalGiltigaMinuter > 0) {
		var exp = new Date();
		var giltigTill = exp.getTime() + (antalGiltigaMinuter * 60 * 1000);
		exp.setTime(giltigTill);
		cookieStr += '; expires=' + exp.toGMTString();
	} else if (antalGiltigaMinuter < 0) {
		var exp = new Date();
		var giltigTill = exp.getTime() - (1 * 60 * 60 * 1000);
		exp.setTime(giltigTill);
		cookieStr += '; expires=' + exp.toGMTString();
	}
	document.cookie = cookieStr;
} 
function getCookie(cookieNamn) {
	var crumbs = document.cookie.split('; ');
	for (var i in crumbs) {
		crumbName = crumbs[i].split('=')[0];
		crumbValue = unescape(crumbs[i].split('=')[1]);
		if (crumbName == cookieNamn) {
			return crumbValue;
		}
	}
	return '';
}

function showMessage(msg) {
	//if (getCookie('msg_was_showed') != 'yes') {
	//	setCookie('msg_was_showed', 'yes', 10);
		alert(msg);
	//}	
}

function is_date(str) {
	var re = /^[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]$/;
	if (!re.test(str)) {
		return false;
	}
	return true;
}

function is_time(str) {
	var re = /^[0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/;
	if (!re.test(str)) {
		return false;
	}
	return true;
}

function is_hours(str) {
	var re = /^(\d+)(\.||\,)?(\d){0,2}$/;
	if ( isNaN(str.replace(/\,/g, '.')) || !re.test(str) ) {
		return false;
	}
	return true;
}

function is_email(str) {
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	if (!re.test(str)) {
		return false;
	}
	return true;
}

function is_phone(str) {
	var re = /^\+?([0-9\.\-\s\/])+$/;	
	if (!re.test(str)) {
		return false;
	}
	return true;
}

function is_url(str) {
	var re = /^(ht|f)tp(s)?:[\/]{2}[a-z]+(\.[a-z0-9_-]+)*[a-z]{2,4}(:[0-9]+)?(\/|\?)?.*$/i
	if (!re.test(str)) {
		return false;
	}
	return true;
}

function displayFlash (swf, w, h, img, lnk, trg, col, rtl, rtr, rbr, rbl, fr) { // corner color and radiuses, frames to fade in
	if (img) {
		var qstr = '?w=' + w + '&h=' + h + '&img=' + img + '&lnk=' + lnk + '&trg=' + trg + '&col=' + col;
		if (rtl && rtr && rbr && rbl) {
			qstr += '&rtl=' + rtl + '&rtr=' + rtr + '&rbr=' + rbr + '&rbl=' + rbl;
		} else if (rtl) {
			qstr += '&rtl=' + rtl + '&rtr=' + rtl + '&rbr=' + rtl + '&rbl=' + rtl;
		}
		if (fr) {
			qstr += '&fr=' + fr;
		} else {
			qstr += '&fr=1';
		}
	} else {
		var qstr = '';
	}
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' +
	'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' +
	'width="' + w + '" height="' + h + '" align="middle">');
	document.write('<param name="movie" value="' + swf + qstr + '" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="wmode" value="opaque" />');
	document.write('<param name="bgcolor" value="#000" />');
	document.write('<param name="scale" value="noscale" />');
	document.write('<embed src="' + swf + qstr + '" quality="high" scale="noscale" wmode="opaque"' +
	'bgcolor="#000" width="' + w + '" height="' + h + '" align="middle" type="application/x-shockwave-flash"' +
	'pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function displayFlash2 (w, h, params) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' +
	'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' +
	'width="' + w + '" height="' + h + '" align="middle">');
	document.write('<param name="movie" value="/swf/img2.swf?w=' + w + '&h=' + h + '&' + params + '" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="bgcolor" value="#fff" />');
	document.write('<param name="scale" value="noscale" />');
	document.write('<embed src="/swf/img2.swf?w=' + w + '&h=' + h + '&' + params + '" quality="high" scale="noscale"' +
	'bgcolor="#fff" width="' + w + '" height="' + h + '" align="middle" type="application/x-shockwave-flash"' +
	'pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('<\/object>');
}

function markupWord(node, word) {
	// depth-first, so first recurse down into children
	if (node.hasChildNodes) {
		for (var i = 0; i < node.childNodes.length; i++) {
			markupWord(node.childNodes[i], word);
		}
	}
	// massage this node (if needed)
	if (node.nodeType == 3) { // text node
		// lowercase both content and word to get a caseinsensitive match
		var lowercasedContent = node.nodeValue.toLowerCase();
		var lowercasedWord = word.toLowerCase();
	
		// can we find our word in this text blob ?
		if (lowercasedContent.indexOf(lowercasedWord) != -1) {
			// yep, it's there - check that we haven't massaged this one before, 
			// (due to insertions in the DOM we might see the same node twice)
			if (node.parentNode.className.indexOf("searchword") == -1) {
				// no, we need to fix it - need to insert <span> around the word
				var startIndex = lowercasedContent.indexOf(lowercasedWord);
	
				// create a text node for the content before the word
				var preContent = document.createTextNode(node.nodeValue.substr(0, startIndex));
				// create a node for the word
				var searchWord = document.createTextNode(node.nodeValue.substr(startIndex, word.length));
				// create a node for the content after the word
				var postContent = document.createTextNode(node.nodeValue.substr(startIndex + word.length));
		
				// create the span around the word
				var highlight = document.createElement("span");
				highlight.className = "searchword";
				highlight.appendChild(searchWord);
	
				// insert the new nodes before the old one
				node.parentNode.insertBefore(preContent, node);
				node.parentNode.insertBefore(highlight, node);
				node.parentNode.insertBefore(postContent, node);
	
				// remove the old content
				node.parentNode.removeChild(node);
			}
		}
	}
}

function chooseText(s) {
	/*
	if (document.all) { // ie
		var r, i;
		if (s) {
			r = document.body.createTextRange();	
			for( i = 0; r.findText(s); i++) {
				r.execCommand('BackColor', '', '#c0eb35');
				r.collapse(false);
			}
		}
	} else 
	*/
	if (document.getElementById) { // ff
		markupWord(document.getElementsByTagName("body")[0], s);
	}
}

function display_email(email, mailto, icon) {
	aParts = email.split("@");
	var user = aParts[0];
	var domain = aParts[1];
	if (mailto) {
		document.write('<a href="mailto:' + user);
		document.write('@' + domain + '">');
	}
	if (icon) {
		document.write('<img src="/img/mail.gif" width="16" height="16"');
		document.write(' alt="Send mail" title="Send mail" />');
	} else {
		document.write(user);
		document.write('@' + domain);
	}
	if (mailto) {
		document.write('</a>');
	}	
}

function payson_guarantee() {
	var f = document.checkoutFrm;
	var elmnt = document.getElementById('guarantee');
	if (f.payment_method.length != null) {
		// array...
		for (k = 0; k < f.payment_method.length; k++) {
			if (f.payment_method[k].checked) {
				if (f.payment_method[k].value == 'payson') {
					f.guarantee_offered.disabled = false;
					elmnt.className = '';
				} else {
					f.guarantee_offered.checked = false;
					f.guarantee_offered.disabled = true;
					elmnt.className = 'dimmed';
				}
				break;
			}
		}
	}
}

function check_payson() {
	var f = document.checkoutFrm;
	var elmnt = document.getElementById('payson');
	if (f.guarantee_offered.disabled == false) {
		if (f.guarantee_offered.checked) {
			f.payson.checked = true;
		}
	}
}

function number_format(num, decimals, decimalseparator, thousandseparator) {
	if (!decimals) { decimals = 0; }
	if (!decimalseparator) { decimalseparator = ','; }
	if (!thousandseparator) { thousandseparator = ' '; }
	num = num.toFixed(decimals);
	num += '';
	x = num.split('.');
	whole_numbers = x[0];
	decimal_numbers = x.length > 1 ? x[1] : '';	
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(whole_numbers)) {
		whole_numbers = whole_numbers.replace(rgx, '$1' + thousandseparator + '$2');
	}
	if (decimals > 0) {
		return whole_numbers + decimalseparator + decimal_numbers;
	} else {
		return whole_numbers;
	}
}

function getCurrency(amount, currency) {
	switch(currency) {
		case 'SEK' :
			return number_format(amount, 2) + ' kr';
			break;
		case 'GBP' :
			return '£ ' + number_format(amount, 2, ".", ",");
			break;
		case 'EUR' :
			return '€ ' + number_format(amount, 2);
			break;
		case 'USD' :
			return '$ ' + number_format(amount, 2, ".", ",");
			break;
		default :
			return number_format(amount, 2);
			break;
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function dec2hex(dec) {
	return dec.toString(16);
}
function hex2dec(hex) {
	return parseInt(hex,16);
} 

function rgb2Hex(str) {
	var begin = str.indexOf('(') + 1;
	var end = str.indexOf(')');
	var nums = str.slice(begin, end);
	var aNums = nums.split(',');
	var r = parseInt(trim(aNums[0]));
	var g = parseInt(trim(aNums[1]));
	var b = parseInt(trim(aNums[2]));
	return '' + dec2hex(r) + dec2hex(g) + dec2hex(b);
}
