/**
 * @(#)library/lib_genplace.js
 *
 * Copyright 2000-2008(c) MijnWinkel B.V.
 *
 * Version 8.2.0 16-mar-2008
 */
 
 function getOrderFields(it){
	var s="";
	if(self.OrderFields&&it.set){
		var i=0,set=it.set(),count;
		if(set!=null){
			count=set.itemCount();
			for(;i<count;i++){
				var item=set.getItem(i),name=item.getName();
				if(name.indexOf(":")==-1&&(name.indexOf("text")==0||name.indexOf("upload")==0)){
					var fv=it.get(name);
					if(fv!="")s+='<br/>'+it.get(name+":label")+': '+fv;
				}
			}
		}
	}
	return s;
}

function ShippingBasket(){
	function ShippingCosts(ship){
		var me=this,sc=ship,price=0,mailbox=0,weight=0;
		var index=0,qs=new QueryString("");
		
		me.getQueryString=function(){return qs};
		me.count=function(){return index};
		
		me.add=function(it,q){
			if(ship.remote==1){
				qs.set("orderline_productid"+index,trim(it.get("id")));
				qs.set("orderline_quantity"+(index++),q);
			}
			
			var oid=""+it.get("oid");
			if(oid.indexOf("**#")==0){
				if(sc.hasPriceExtra>0)price+=(it.getNumber("shipping_cost")*q);
				if(sc.weightAddition>0)weight+=(it.getNumber("weight")*q);
				if(sc.weightAddition>1&&mailbox<=1){
					var v=Math.abs(it.getNumber("number_mailbox"));
					mailbox+=((v==0)?2:((1/v)*q));
				}
			}
			else{
			//dalert("wadd:"+sc.weightAddition+" fld:field_"+ship.weightFld+" val:"+(it.getNumber("field_"+ship.weightFld))+" q:"+q);
				
				if(sc.hasPriceExtra>0)price+=(it.getNumber("field_"+ship.priceFld)*q);
				if(sc.weightAddition>0)weight+=(it.getNumber("field_"+ship.weightFld)*q);
				if(sc.weightAddition>1&&mailbox<=1){
					var v=Math.abs(it.getNumber("field_"+ship.maxMailBoxFld));
					//if(getVID()==22539)alert("max mailbox"+v+" name"+ship.maxMailBoxFld);
					mailbox+=((v==0)?2:((1/v)*q));
				}
			}
			
		};
		me.select=function(l,subtotal,vat){
			sc.select(l);
			var n=0,e=0;
			if(sc.weightAddition>0){
				//dalert("mailbox:"+mailbox+" w:"+weight+" list:"+sc.mail+" result:"+range(weight,sc.mail));
				n=((mailbox>1))?range(weight,sc.box):range(weight,sc.mail);
			}
			n+=price;
			if(sc.extra>0){
				e=subtotal;
				if(sc.extraFrom==0)e+=vat;
				n+=(e*(sc.extra/100));
			}
			ship.setAdditional(l,n);
			// After setting the additional cost for the shipping line, reset the 
			// selection to force the price to be recalculated.
			sc.select(l);
		};
		function range(v,l){
			var r=0,i=0;
			for(;i<l.length;i++){
				if(v<l[i][0])break;
				r=l[i][1];
			}
			return r;
		}
	}

	var me=this;
	me.getShippingCosts=function(ship){return new ShippingCosts(ship)};
	me.productTotal=0;
	me.vatTotal=0;
	me.currency=new Valuta();
	
	var it=getBag().iterator("s.order","article.id"),lt;
	while(it.next()){
		me.currency.filter(it.get("price"));
		lt=it.getNumber("linetotal");
		me.productTotal+=lt;
		me.vatTotal+=getCalcVat(it.getNumber("vat"))*0.01*lt;
	}
	me.reduction=new Reduction();

	// legacy method
	me.getTotal=function(){
		var t=me.productTotal+me.vatTotal;
		if(me.reduction.enabled)t-=me.reduction.calculate();
		return t;
	};

	me.format=function(n){return me.currency.format(n)};
	me.number=function(n){return me.currency.number(n)};

	me.calculateExtra=function(ship,id){
		// pre-select the shipping method if specified.
		if(id!=null)ship.select(id);
		var shipc=new ShippingCosts(ship),it=getBag().iterator("s.order","article.id");
		while(it.next())shipc.add(it,it.get("quantity"));
		if(ship.remote==1){
			if(__b.get("s.system.rshipping.set")!="1"){
				var qs=shipc.getQueryString();
				qs.set("orderline_count",shipc.count());
				qs.set("shipping_id",ship.id);
				
				
				if(self.ActionList){
					var al=new ActionList(null,true);
					al.select("coupon_n");
					qs.set("action_code",al.getState("code"));
				}
				
				
				new Remote(false,false,false).request(me.response,"shipping_info","productinfo",escape(qs.getQueryString(true)));
				__b.set("s.system.rshipping.set","1");
			}
		}
		if(__b.is("s.actions.active"))shipc.select(ship.id,me.productTotal+__b.getNum("s.actions.total"),me.vatTotal+getCalcVat(__b.getNum("s.actions.vattotal")));
		else shipc.select(ship.id,me.productTotal,me.vatTotal);
		return ship.price;
	};
	function getAttr(c,n){
		var v="";
		try{v=c.getAttribute(n)}
		catch(e){}
		return v
	}
	
	function getBagVal(id,name){return __b.get("s.system.rshipping.id"+id+"."+name)}
	function setBagVal(id,name,value){__b.set("s.system.rshipping.id"+id+"."+name,value)}
	
	me.response=function(doc){
		if(doc){
			var rows=doc.getElementsByTagName('row'),i=0,row,col;
			if(rows){
			
				for(;i<rows.length;i++){
					row=rows[i];
				 	if(getAttr(row,"type")=="shipping"){
				 		var id=getAttr(row,"id");
				 		if(id!=""){
							var cols=row.getElementsByTagName('col'),l,n=0,col,name;
							if(cols){
								l=cols.length;
								for(;n<l;n++){
									col=cols[n];
									name=getAttr(col,"name");
									if(name=="cost")setBagVal(id,name,col.firstChild.nodeValue+"0");
									else if("title|description".indexOf(name)!=-1)setBagVal(id,name,col.firstChild.nodeValue);
								}
							}
						}
					}
				}
			}
		}
	};
}

function ZIPFilter(ship){
	var me=this;
	function startWith(n){
		var b=false;
		n+="";
		n=n.toLowerCase();
		// remove all whitespace characters
		n=n.replace(/\s+|&nbsp;|&#160;/gi,'');
		for(var i=0;i<ship.zip.length;i++){
			if(n.indexOf(ship.zip[i].toLowerCase())==0){
				b=true;
				break;
			}
		}
		return b;
	}
	me.test=function(n){return true};
	if(ship.zipType==1)me.test=startWith;
	else if(ship.zipType==2)me.test=function(n){return !startWith(n)};
}


function getActiveZIPCode(){
	var b=getBag(),r='';	
	if(b.get("c.adr.deliveryaddress")=='1')r=b.get("c.adr.deliver.zip");
	if(r=="")r=b.get("c.adr.billing.zip");
	return r;
}

function getCredit(){return(this._credit!=null)?_credit:""}
function getStock(){return(this._stock!=null)?_stock:""}



function equalsPrevious(i){
	var n=getIndex(i),r=(__products!=null)?__products.getRowNumber():-1;
	return(r>0&&__rowData[n]==_d[r-1][n])
}
var __k2i=[];
function getIndex(n){
	if(isNaN(n)){
	if(__k2i.length==0)for(var i=1;i<_m.length;i++)__k2i[_m[i][4]]=i;n=__k2i[n]}
	return n
}
function equalsNext(i){
	var n=getIndex(i),r=(__products!=null)?__products.getRowNumber():-2;
	return(r!=-2&&r<(_d.length-1)&&__rowData[n]==_d[r+1][n])
}
function isEmpty(i,type,offset){
	var v=(type!=null&&offset!=null)?fieldIndexed(i,type,offset,true):__rowData[getIndex(i)];
	return(v!=null&&v!='')?(v.replace(/&#160;|&nbsp;|^\\s+|\\s+$/gi,'')==''):true
}
function doCustomSubmit(p){
	var b=false,f=getData();
	if(f!=null){
		b=true;
		f.action=unescape(p);
		f.submit();
	}
	return b
}
function getPI2FI(n){
	if(isNaN(n)){
	if(__k2i.length==0)for(var i=1;i<_m.length;i++)__k2i[_m[i][4]]=i;
	n=__k2i[n]
	}
	else if(n<=__picRefList.length)n=__picRefList[n-1];
	return n
}

function Products(related){
	var me=this,data=null;
	if(related!=null&&related){
		if(self.getRelatedData)data=getRelatedData();
		if(!data&&self.RelatedData!=null)data=new RelatedData();
	}
	var products=(data!=null)?data.count():_d.length,productindex=-1,active=(products>0);
	me.count=products;
	me.getRowNumber=function(){return productindex};
	me.next=function(){
		if(data!=null){
			data.select(++productindex);
			__settingsBuild();
			__initOrderBuild();
		}
		else __rowData=_d[++productindex];
		__activeRow=productindex
	};
	me.hasNext=function(index){
		var n=(index!=null&&index!='')?_i(index):0;
		active=((productindex+n)<(products-1));
		return active
	};
	me.has=function(index){
		var n=(index!=null&&index!='')?_i(index):0;
		return((productindex+n)>=0&&(productindex+n)<products)
	};
	me.hasActive=function(){return active&&(productindex<products)&&(productindex>=0)};
	me.getColumn=function(){
		return productindex%getColsPerPage()
	};
	if(data!=null)data.select(0);
	else __rowData=_d[0];
	
}

var _qmap;
function getInfoQuantity(n){return(_qmap==null||getQFOption()==2)?getQFDefault():_qmap.get(n)}
function setInfoQuantity(c){
	if(_qmap==null)_qmap=new HashMap();
	_qmap.set(c.name,c.value)
}
function placeOrderRelated(c,oid,n,cid,count){
	if(self.isLockedAjax&&isLockedAjax())return;
	if(getQFType()==1){
		var val=1;
		if(cid==null)cid=getCID();
		oid=((count!=null)?count+'__':'')+oid
		if(c==null&&n!=null){
			var f=getData();
			if(f)c=f[n];
			if(c)val=c.value;
		}
		if(productValidate(oid,c,1,false)!=null)showPopup(oid,cid,null,null,null,null,val);
	}
	else relatedOrder(oid,cid,count);
}
function placeOrder(c,oid,n,cid,count){
	if(self.isLockedAjax&&isLockedAjax())return;
	if(c==null)c=getData()[n];
	if(c==null||getQFType()!=1)order(oid);
	else{
		if(cid==null)cid=getCID();
		oid=((count!=null)?count+'__':'')+oid
		if(productValidate(oid,c,1,false)!=null)showPopup(oid,cid,null,null,null,1,c.value);
	}
}
function keyPress(e,c,oid,cid,active){
	if(!e)e=window.event;
	var k=e.keyCode;
	if(k==13)placeOrder(c,oid,null,cid,active);
	return (k!=13)
}
function keyUp(c){
	setInfoQuantity(c);
	return true
}
