function editContent(id){
	window.open("/plugins/cms/editor.php?type=content&id="+id, "editWin", "width=600, height=700, left=20,top=20");
	return;
}

function _gel(id){return document.getElementById(id); }

function call(url){
	script = document.createElement( 'script' );
	script.src = url;
	document.getElementsByTagName( 'head' )[0].appendChild( script );
}

var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}


function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
	endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
	}

function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}

function bigImage(id){
	var x = activeImg != undefined ? activeImg : 0;
	TB_show("Stor bild", "/images/produkter/original/"+id+(x > 0 ? "_"+x : "")+".jpg", "Stor bild");
	
	//App.showBig("<a href='#' onclick='App.closeBig();return false;' style='color:#fff;'>Stäng förstoringen</a><br><img src='/images/produkter/original/"+id+(x > 0 ? "_"+x : "")+".jpg' style='cursor:pointer;margin-top:5px;border:5px solid #ccc;' onclick='App.closeBig()'>");
	// window.open("/big.image.php?id="+id+"&img="+x, "", "top=0,left=0,width=100,height=100");
}

function addToCart(id, sub){
	if(sub !== false && sub == ""){
		alert("Denna produkt är slutsåld och kan därför inte handlas.")
		return;
	}
	call("/plugins/shop/cart.process.php?action=add&id="+id+"&sub="+(sub == undefined || sub == false ? "" : sub));
}

function deleteItem(id){
	alert2("Ta bort denna vara?", id);
}

function doDeleteItem(id){
	call("/plugins/shop/cart.process.php?action=delete&id="+id);
	row = _gel("cart_row_"+id);
	row.parentNode.removeChild(row);
}

function itemUnits(id, type){
	antal = _gel("units_"+id).innerHTML;
	if(antal == 1 && type == "less"){
		deleteItem(id);
	}else{
		call("/plugins/shop/cart.process.php?action=units&id="+id+"&type="+type);
	}
}

function searchArtnr(){
	art = _gel("artnr");
	if(art.value.length == 0){
		alert2("Ange artikelnr");
	}else{
		call("/plugins/shop/search.process.php?str="+art.value);
	}
}

function printPage(){
	window.open("/print.php", "", "top=20, left=20, width=550, height=500");
}

function checkPersnr(persnr){
	persnr = persnr.replace("-","");
	
	if(persnr.length != 10){
		return false;
	}
	sum = 0;
	for(i = 0; i < 10; i++){
		tmp = persnr.substring(i,i+1);
		if(i % 2 == 0){
			tmp *= 2;
			if(tmp > 9){
				tmp = tmp - 10 + 1;
			}
		}
		sum += +tmp;
	}
	if(sum % 10 == 0){
		return true;
	}else{
		return false;
	}
}

App = {};
App.resizeTimer = null;
App.resizes = [];
App.scrollTimer = null;
App.scrolls = [];
App.opacity = .8;
App.showBig = function(html){
	App.bigDiv = true;
	var div1 = document.createElement("DIV");
	div1.className = "bigWinBg";
	
	// App.fade(div1,0,App.opacity);
	div1.style.filter = "alpha(opacity="+(App.opacity*100)+")";
	div1.style.opacity = App.opacity;
	
	// div1.title = "Klicka i den tonade bakgrunden för att stänga detta fönster";
	div1.innerHTML = "";
	div1.id = "bigDivBG";
	div1.onclick = App.closeBig;
	var div2 = document.createElement("DIV");
	div2.className = "bigWinContent";
	div2.innerHTML = html;
	div2.id = "bigDiv";
	div2.onclick = function(e){if(e){if(e.target == div2){App.closeBig();}}else{if(event.srcElement == div2){App.closeBig();}}}
	document.getElementsByTagName("BODY")[0].appendChild(div1);
	document.getElementsByTagName("BODY")[0].appendChild(div2);
	App.sizeBigDiv();
	// App.addResizeEvent(App.sizeBigDiv, 1);
}

App.closeBig = function(){
	if(!App.bigDiv){return;}
	App.bigDiv = false;
	var d = _gel("bigDiv");
	while(d.firstChild){d.removeChild(d.firstChild);}
	d.parentNode.removeChild(_gel("bigDiv"));
	_gel("bigDivBG").parentNode.removeChild(_gel("bigDivBG"));
	App.bigDivFrame = false;
}

App.sizeBigDiv = function(x){
	if(!App.bigDiv){return;}
	
	y = document.body.scrollTop < document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	h = document.body.clientHeight;
	w = document.body.clientWidth;
	var bg = _gel("bigDivBG");
	bg.style.width = w+"px";
	bg.style.height = h+"px";
	bg.style.top = y+"px";
	var div = _gel("bigDiv");
	// var y2 = (h - div.offsetHeight) / 2;
	div.style.top = (y+30)+"px";
	div.style.width = (w-40)+"px";
	if(App.bigDivFrame){
		App.bigDivFrame.style.width = (w-40)+"px";
		App.bigDivFrame.height = (h-60)+"px";
	}
	if(!x){App.sizeBigDiv(1);return;}
}

App.addResizeEvent = function(func, scrl){
	if(scrl != undefined){
		App.addScrollEvent(func);
	}
	
	for(var x=0;x<App.resizes.length;x++){
		if(func == App.resizes[x]){return;}
	} 
	App.resizes[App.resizes.length] = func;
}

App.execOnResize = function(){
	for(var x=0;x<App.resizes.length;x++){
		App.resizes[x]();
	} 
}
App.onResize = function(){
	if(App.resizeTimer){clearTimeout(App.resizeTimer);App.resizeTimer=null;}
	App.resizeTimer = setTimeout("App.execOnResize();",200);
}

App.addScrollEvent = function(func){
	for(var x=0;x<App.scrolls.length;x++){
		if(func == App.scrolls[x]){return;}
	} 
	App.scrolls[App.scrolls.length] = func;
}

App.execOnScroll = function(){
	for(var x=0;x<App.scrolls.length;x++){
		App.scrolls[x]();
	} 
}
App.onScroll = function(){
	if(App.scrollTimer){clearTimeout(App.scrollTimer);App.scrollTimer=null;}
	App.scrollTimer = setTimeout("App.execOnScroll();",200);
}

window.onresize = function(){App.onResize();}
window.onscroll = function(){App.onScroll();}



function call2(url, id, ret){
	var myConn = new XHConn();
	if(id){var ret = function (oXML) {if(_gel(id)){_gel(id).innerHTML = oXML.responseText;}};
	}else if(!ret){var ret = function (oXML) {eval(oXML.responseText);};
	}
	var c = "";
	var a = url.split("?"); 
	if(a[1]){
		var b = a[1].split("&");
		for(var x=0;x<b.length;x++){
			var d = b[x].split("=");
			c += (c != "" ? "&" : "")+d[0]+"="+escape(d[1]);
		}
	}else{
		var c = "";
	}

	var cc = 0;
	myConn.connect(a[0], cc ? "POST" : "GET", c, ret);
}

function XHConn(){
	var xmlhttp;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e){try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch(e){try{ xmlhttp = new XMLHttpRequest(); }
	catch(e){xmlhttp = false; }}}
	if(!xmlhttp) return null;
	this.connect = function(sURL, sMethod, sVars, fnDone){
		if (!xmlhttp) return false;
		sMethod = sMethod.toUpperCase();
		try {
			if (sMethod == "GET"){
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			}else{
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4) { fnDone(xmlhttp);}};
			xmlhttp.send(sVars);
		}
		catch(z) { return false; }
		return true;
	}
	return this;
}
