// specialoffers.js included by custom.htm
// both files in system/user
// Script for pricing features
// XforY   : Get X for the price of Y  eg 3 for 2
// NcaseP  : Resolve price for units and cases of N

function customPriceResolve(Pstring){

// alert("customPriceResolve : "+ Pstring); //!!

var CPA=Pstring.split("[");

if(CPA[1].indexOf("for")>0) // eg "3for2" "12for10"
	{
	top.B[top.Basketline].XforY=XforY;
	top.B[top.Basketline].XforY(1*CPA[0],CPA[1]);
	return(1*CPA[0]);
	}

if(CPA[1].indexOf("get")>0) // eg "1get1" "2get1"
	{
	top.B[top.Basketline].AgetB=AgetB;
	top.B[top.Basketline].AgetB(1*CPA[0],CPA[1]);
	return(1*CPA[0]);
	}


if(CPA[1].indexOf("case")>0) // eg "6case10" £10 for case of 6
	{
	top.B[top.Basketline].NcaseP=NcaseP;
	return(top.B[top.Basketline].NcaseP(1*CPA[0],CPA[1]));
	}
}


function XforY(Price,xfy){ // method of object
var tmp=xfy.split("for")
var X=parseInt(tmp[0]);
var Y=parseInt(tmp[1]);

var Q = this.QTY;
if(Q>=Y)
	{
	var pay = Math.floor(Q/X)*Y + (Q%X);
	pay -= (Q%X)>Y ? (Q%X)-Y : 0;
	//!! alert("pay = "+pay);
	var free = Math.floor(pay/Y)*(X-Y);
	this.QTY=pay;
	this.Qlock=true;
	this.Quant=this.QTY+";1;1";
	this.VarL2+=(this.VarL2.length>0)?top.CRLF:"";
	this.VarL2+=top.swapstring(top.SL.pfPlusFree,"$#$",""+free);
	this.Price = Price;
	}
}//

function AgetB(Price,agb){ // method of object
var tmp=agb.split("get")
var A=parseInt(tmp[0]);
var B=parseInt(tmp[1]);

var Q = this.QTY;
if(Q>=A)
	{
	var free = Math.floor(Q/A)*B
	this.Qlock=true;
	this.Quant=this.QTY+";1;1";
	this.VarL2+=(this.VarL2.length>0)?top.CRLF:"";
	this.VarL2+=top.swapstring(top.SL.pfPlusFree,"$#$",""+free);
	this.Price = Price;
	}
}//

function NcaseP(Price,ncp){ // method of object
//alert("NcaseP"); //!!

var tmp=ncp.split("case")
var caseQty  =parseInt(tmp[0]);
var casePrice=parseInt(tmp[1]);
var ret=Price;
// quantity exact multiple of caseQty --> new basket object for case quantity item

var Q = this.QTY;
if(Q%caseQty == 0)
	{
    // quantity exact multiple of caseQty --> change basket object to be case quantity
	this.QTY=Q/caseQty;
	this.Quant=this.QTY+";1;1";
	this.Descr += " " +top.swapstring(top.SL.pfCaseOf,"$#$",""+caseQty);
	this.Price = ""+casePrice;
	this.Weigh = caseQty * this.Weigh;
	ret=casePrice;
	}

else if( Q>caseQty    &&    Q%caseQty > 0 )
	{
    // quantity more than 1 case and not exact multiple of caseQty
    // change current basket object to remainder quantity
    var nCases = (Q - Q%caseQty)/caseQty;
	this.QTY=Q%caseQty;
	this.Quant=this.QTY+";1;1;"+(caseQty-1);
	this.Qmax=(caseQty-1);
	this.Price = ""+Price;
    // add new object for case(s)
	top.Basketline++;
	top.B[top.Basketline]=new top.Bobj(this.Descr + " " + "Case of "+caseQty,
										this.Refno,this.VarL1,this.VarL2,""+casePrice,
										nCases+";1;1",
										caseQty * this.Weigh,this.Taxco,1,this.Group,true,this.LinkP);
	top.counter();
	if(!top.msg.AddThenReview){top.reviewbasket();}
	}
else
	{
	// quantity less than 1 case
	this.Price = ""+Price;
	this.Quant=this.QTY+";1;1;"+(caseQty-1);
	this.Qmax=(caseQty-1);
	}
return(ret)
}


// specialoffers.js ENDS

