/*  The  code  contained  in  this  distribution  is  copyrighted  by
www.jojoxx.net The file may be  used for none commercial applications
and distributed  as long as  these lines remain  intact. The files or
part of them may  not be sold  or  included  in any  other commercial
application  without agreement from the author. If you have questions
or comments, contact the author at http://www.jojoxx.net

© Copyright - www.jojoxx.net - 2004                                */

var basket = new Object();
var art = new Object();
var group = new Object();
var discount = new Object();
var order = new Object();
var session = new Object();
var userdata = new Object();
var version="2.51 RC3";
var homeurl;
var basketPage=["basket.html","basket-2.html"];
var webshopURL=(location.host&&location.pathname)?"http://"+location.host+location.pathname.substring(0,location.pathname.lastIndexOf("/")+1):"./";
var includes=["system/md5.js","plugin-login/checklogin.pl"];

var isActivePayson=(parent.PaysonKey && parent.PaysonKey.length>0 && parent.PaysonAgentId!="0" && parent.PaysonEpost && parent.PaysonEpost!="email@domain.com");
var isActivePayPal=(PayPalEpost && PayPalEpost!="email@domain.com");
var isActiveDIBS=(DIBSmerchant);
var isDemoMode=(location.host=="www.jojoxx.net");

order.ordernummertyp=ordernummertyp;

function addtocartvariant(ArtNr,Qty,Variant){
	if(!art[ArtNr]){ return null; }
	Qty = parseInt(Qty);
	if(!art[ArtNr].Price){ return null; }
	if(!art[ArtNr+"-"+Variant]){
		art[ArtNr+"-"+Variant] = new article(art[ArtNr].Name+ " ("+Variant+")",art[ArtNr].Price);
		art[ArtNr+"-"+Variant].notInDb = true;
	}
	if(!isNaN(Qty)){
		basket[ArtNr+"-"+Variant]=(isNaN(basket[ArtNr+"-"+Variant]))?Qty:parseInt(basket[ArtNr+"-"+Variant])+Qty;
		updatebasket();
	}
}
function addtocart(ArtNr,Qty,ArtName,ArtPrice){
	if(!art[ArtNr]){
		if(!ArtName || !ArtPrice){ return null; }
		art[ArtNr] = new article(ArtName,ArtPrice); art[ArtNr].notInDb = true;
	}
	Qty = parseInt(Qty);
	if(!art[ArtNr].Price){ return null; }
	if(!isNaN(Qty)){
		basket[ArtNr]=(isNaN(basket[ArtNr]))?Qty:parseInt(basket[ArtNr])+Qty;
		updatebasket();
	}
}
function removefromcart(ArtNr){ basket[ArtNr]=0; updatebasket(); }
function removefromcartNoUpdate(ArtNr){ basket[ArtNr]=0; }
function emptycart(){ for(var n in basket){ basket[n]=0; } updatebasket(); }
function article(Name,Price){
	// artikelobjekt
	this.Name = Name; this.Price = Price;
}
function prodgroup(Name){ this.Name = Name; }
function updatebasket(){
	wbasket.location.replace(webshopURL+basketPage[basketMode-1]); // "basket.html";
}
function priceToText(price){
	if(!price){ return "&nbsp;"; }
	price = price.toString();
	price += (price.indexOf(".")>=0) ?"00":".00";
	price = price.substring(0,price.indexOf(".")+3);
	price=price.replace(/\./,":");
	price=price.replace(/\:00/,":-");
	return price;
}
function addDiscount(ArtNr,limit,price){
	if(!discount[ArtNr]){ discount[ArtNr]=[]; }
	discount[ArtNr][discount[ArtNr].length]=[limit,price];
}
function getDiscountPrice(ArtNr,n){
	var limit=-1; var grp=ArtNr.substring(0,ArtNr.indexOf("-")); var grpn=0;
	for(var i in basket){
		if(!isNaN(basket[i])&&basket[i]>0&&grp){ grpn+=(i.substring(0,i.indexOf("-"))==grp)?basket[i]:0; }
	}
	if(discount[grp]){
		// Artikelgruppsrabatt
		for(var i=0;i<discount[grp].length;i++){ limit=(grpn>=discount[grp][i][0])?i:limit; }
		if(limit!=-1){
			// Procentrabatt eller nytt styckepris.
//			return (discount[grp][limit][1].match(/\%$/))?art[ArtNr].Price-(parseFloat(discount[grp][limit][1])/100)*art[ArtNr].Price:discount[grp][limit][1];
			if(discount[grp][limit][1].match(/\%$/)){
				return(art[ArtNr].Price-art[ArtNr].Price*(parseFloat(discount[grp][limit][1])/100) );
			}
			return discount[grp][limit][1];
		} else { return art[ArtNr].Price; }
	}else if(discount[ArtNr]){
		// Artikelrabatt
		for(var i=0;i<discount[ArtNr].length;i++){ limit=(n>=discount[ArtNr][i][0])?i:limit; }
		if(limit!=-1){
			// Procentrabatt eller nytt styckepris.
//			return (discount[ArtNr][limit][1].match(/\%$/))?art[ArtNr].Price-(parseFloat(discount[ArtNr][limit][1])/100)*art[ArtNr].Price:discount[ArtNr][limit][1];
			if(discount[ArtNr][limit][1].match(/\%$/)){
				return(art[ArtNr].Price-art[ArtNr].Price*(parseFloat(discount[ArtNr][limit][1])/100) );
			}
			return discount[ArtNr][limit][1];
		} else { return art[ArtNr].Price; }
	}else{
		return art[ArtNr].Price;
	}
}
function calculateShipping(){
	var n=0;
	for(var i in basket){
		if(!isNaN(basket[i])&&basket[i]>0){ n+=parseInt(basket[i]); }
	}
	return frakt;
}
function reloadShop(){ top.location.reload(); }

top.window.defaultStatus="Webshop "+version; // document.title="Webshop "+version;
// Ladda sessionobjekt.
var mySessioncookie=new cookie("webshopSession"); b=mySessioncookie.value.split(";");
for(var n in b){ if(b[n].split(":")[0]){ session[b[n].split(":")[0]]=b[n].split(":")[1]; } }

// Ladda artikeldatabaser.
if(session['db']){
	document.write('<scr'+'ipt type="text\/javascript" src="'+session['db']+'"><\/scr'+'ipt>');
}else{
	document.write('<scr'+'ipt type="text\/javascript" src="articles.js"><\/scr'+'ipt>');
}
for(var n=2;n<=10;n++){
//	document.write('<scr'+'ipt type="text\/javascript" src="articles-'+n+'.js"><\/scr'+'ipt>');
}
for(var n=0;n<includes.length;n++){
	document.write('<scr'+'ipt type="text\/javascript" src="'+includes[n]+'"><\/scr'+'ipt>');
}

