// <!-- FEB 2003 -->
// reviewfun.js
// 14 November 2002
// Hooks for additional columns and total lines - code to be (included) in custom.htm

//  ===============================================================
//  Copyright © 2000-2002 Shop@ssistant eCommerce Solutions Ltd.
//  Shop@ssistant by Rodney Myers                     Version MC4_
//  ===============================================================


var ShowTotal;
var INSASS=false;
var d=window.document;
var reviewOnChangeOrRemove=true; // do not alter

function setvars(){ // For Review
if(top.INSASS)
{
	if(top.X.customBasketPreAssess){top.X.customBasketPreAssess();} // Nov 2002
    top.InitialiseTotals();
	ShowTotal=true;//false;
	//status
    top.counter();  // Refresh basket cookie AND counting frame if required
}
}//end of function setvars



function RV_Totline(Num,String,cSpan,captionclass,figureclass) {
var useCaptionClass = (captionclass==null) ? "totalcaption" : captionclass;
var useFigureClass  = (figureclass==null)  ? "totalfigure"  : figureclass;


d.write('<tr><td class="',useCaptionClass,'" colspan="',cSpan,'" align="right">',String,'</td>',
'<td  class="',useFigureClass,'" align="right">',top.MONEY.Format(Num),'</td><td class="',useCaptionClass,'"><br></td></tr>');
}

function RWriteBasket(){
RTableStart();
RBasketHead();
RBasketProducts(false);
RBasketTotals();
RTableEnd();
}

function RTableStart(){
d.write('<table border="',top.sp.reviewBorder,'" width="',top.sp.reviewWidth,'" cellpadding="4" cellspacing="0">');
}

function RTableEnd(){
d.write('</table>');
}

function RBasketHead(){
var exc="<br>"+top.SL.lExcl+top.SL.TaxNameShort;
var inc="<br>"+top.SL.lIncl+top.SL.TaxNameShort;
var PriceHd="<NOBR>"+top.SL.lUnitPrice+"</NOBR>";PriceHd+=(top.T.TaxRegistered)?((top.T.TaxExcluded)?exc:inc ):"";
var NetHd=top.SL.lTotal;NetHd+=(top.T.TaxRegistered)?exc:"";
var IncHd=top.SL.lTotal+"<br>"+top.SL.lIncl+top.SL.TaxNameShort;
var TaxHd=top.SL.TaxNameShort+"<br>"+top.SL.lTotal;
var GrossHd=top.SL.lTotal;GrossHd+=(top.T.TaxRegistered&&!top.T.TaxPerItem)?((top.T.TaxExcluded)?exc:inc ):"";

//Headings
d.write('<tr>');
if(top.X.reviewColHeadPos1){d.write(top.X.reviewColHeadPos1());} // Nov 2002
d.write('<th align="left"  >',top.SL.lProductHd,'</th>');
if(top.X.reviewColHeadPos2){d.write(top.X.reviewColHeadPos2());} // Nov 2002
d.write('<th align="center">',PriceHd,'</th>');
if(top.X.reviewColHeadPos3){d.write(top.X.reviewColHeadPos3());} // Nov 2002
d.write('<th align="center">',top.SL.lQuantityHd,'</th>');
if(top.X.reviewColHeadPos4){d.write(top.X.reviewColHeadPos4());} // Nov 2002
// if(reviewColHeadPos4){d.write(reviewColHeadPos4());} // Nov 2002


if(top.T.TaxPerItem)
{
d.write('<th align="right" >',TaxHd,'</th>');
d.write('<th align="right" >',(top.T.TaxExclusiveTotal)?NetHd:IncHd,'</th>');
}
else//1 col
{
d.write('<th align="right" >',GrossHd,'</th>');
}

d.write('<th align="center">',top.SL.lRemoveHd,'</th></tr>');
}

function RBasketProducts(showLastOnly){
showLastOnly=(showLastOnly==null)?false:showLastOnly;
var BsktLn;
for(BsktLn=1;BsktLn<=top.Basketline;BsktLn++){top.X.setcurrent(BsktLn);RWriteBline(top.B[BsktLn],BsktLn,showLastOnly);}
}


function RBasketTotals(){
var exc=(top.sp.suppressShipping)?"":top.SL.lExc1;
if(ShowTotal)
 {
  var span=(top.T.TaxPerItem)?4:3;
  if(typeof(top.X.addToSpan)=="number"){span+=top.X.addToSpan;}
  var tot_caption1=(top.T.TaxRegistered)?top.SL.lTotal+" "+top.SL.lExcl+top.SL.TaxNameShort+" "+exc:top.SL.lTotal+" "+exc;

  	if(top.X.reviewTotalPosition1){top.X.reviewTotalPosition1(window,span);} // Nov 2002

  RV_Totline(top.BASKET.GoodsNet,tot_caption1,span);

  if(top.T.TaxRegistered){
  exc=(top.sp.suppressShipping)?"":top.SL.lExc2;

	if(top.X.reviewTotalPosition2){top.X.reviewTotalPosition2(window,span);} // Nov 2002

  RV_Totline(top.BASKET.GoodsNet+top.BASKET.GoodsTax,top.SL.lTotal+" "+top.SL.lIncl+top.SL.TaxNameShort+" "+exc,span);

	if(top.X.reviewTotalPosition3){top.X.reviewTotalPosition3(window,span);} // Nov 2002


  }
 }
}



function RWriteBline(B,Current,showLastOnly){
var cellclass='class="datarow'+ Current%2 +'" ';
var td_default='<td '+cellclass+'>';
var td_center ='<td '+cellclass+'align="center">';
var td_right  ='<td '+cellclass+'align="right">';
B.xPrice=top.adjust(top.MONEY.pricefix(B.Price,true)); // added parameter 01 February 2003
B.LineCalc(false); //destinationKnown=false
B.DebugBasketObject(Current);

var showLine=B.lFlag && (!showLastOnly || showLastOnly && Current==top.Basketline);
if(showLine)
{

//Description and reference
d.writeln("<tr>");

if(top.X.reviewColPos1){d.writeln(top.X.reviewColPos1(B,cellclass));} // Nov 2002

d.writeln(td_default);
B.wDescRef(d,true); // true to show link
B.wVar12(d);
if(top.X.addToReview){top.X.addToReview(B,d);}
d.writeln(top.X.extra_elements(B,Current));
d.writeln("</td>")

if(top.X.reviewColPos2){d.writeln(top.X.reviewColPos2(B,cellclass));} // Nov 2002

//Price
d.write(td_center);
B.wPrice(d);
d.writeln('</td>');

if(top.X.reviewColPos3){d.writeln(top.X.reviewColPos3(B,cellclass));} // Nov 2002

//Quantity
d.write(td_center);
if(B.Qlock){B.wQty(d);} else {B.wQtyInput(d,Current,reviewOnChangeOrRemove);}

d.writeln('</td>');

if(top.X.reviewColPos4){d.writeln(top.X.reviewColPos4(B,cellclass));} // Nov 2002

if(top.T.TaxPerItem)
{
showFigure(td_right,B.TaxLine);
//Net OR Gross? depends on var top.T.TaxExclusiveTotal
if(top.T.TaxExclusiveTotal){showFigure(td_right,B.NetLine);}else{showFigure(td_right,B.GrossLine);}
}
else//no break TAX for item (+non-reg) //Quantity x Price
{ showFigure(td_right,B.Amount); }

//REMOVE CHECKBOX
d.write(td_center)
d.write(B.NoRemo?'<br>':'<input type="checkbox" class="remove" name=' + '"cbox'+Current+'"'+' onClick="top.delete_line('+Current+','+reviewOnChangeOrRemove+')" >');
d.writeln('</td>')
d.writeln('</tr>')
if(B.hasFixed) { RWriteBline(top.FC[B.Refno],Current,true); }
}//if lflag
}//RWriteBline

function showFigure(openCell,Fig){ d.write(openCell,top.MONEY.Format(Fig),'</td>'); }



function savenewqty(V,Qinc,Qmin,Qmax,I){
if(top.Debug){
	var msg='V = '+V+'\n';
	msg+='Qinc = '+Qinc+'\n';
	msg+='Qmin = '+Qmin+'\n';
	msg+='Qmax = '+Qmax+'\n';
	msg+='I = '+I+'\n';
	alert(msg);
}

var B=top.B[I];
B.Quant = ""+V+";"+Qinc+";"+Qmin+";"+Qmax;
B.QTY=V;
fix_price_break(top.B[I].Price,V,I);
}//


function fix_price_break(FullPstring,Q,I){
//alert("Price string : "+FullPstring + "\nQuantity : " + Q +"\nLine : " + I) ;
//FullPstring breaks to price|break_mins|prices  break_mins & prices are xxx;yyy;zzz
if(FullPstring.indexOf(top.colPriceMajor)>-1){
var tmp=FullPstring.split(top.colPriceMajor);
var min=tmp[1].split(top.colPriceMinor);
var prc=tmp[2].split(top.colPriceMinor);
var n;for(n=min.length;n>0;n--){if(Q>=1*min[n]){break;}}
top.B[I].Price=""+prc[n]+top.colPriceMajor+tmp[1]+top.colPriceMajor+tmp[2];
}
}

function ButtonBar(hvMode){
if(top.INSASS)
{
hvMode=(hvMode==null)?"H":hvMode;
eval("top.makeButtonBar"+hvMode+"(window,top.sp.ReviewRequiredLinks)");

}
else { document.write("Button Bar goes here"); }
}

function TextLinks(hvMode){
if(top.INSASS)
{
hvMode=(hvMode==null)?"H":hvMode;
eval("top.makeTextLinks"+hvMode+"(window,top.sp.ReviewRequiredLinks)");
}
else { document.write("Text Links go here"); }
}




function instructions() {
if(top.INSASS){
// Bulleted list of instructions for Review
var tbullethead = top.T.TaxExcluded ? top.SL.lTbE : top.SL.lTbI;
top.SL.lRbullets["tax"]=multibullet(tbullethead,top.SL.lTbullets);


var show;
d.writeln('<ul>');
for(var tag in top.SL.lRbullets)
{
show=true;
if(tag=="shipping" && top.sp.suppressShipping){show=false;}
if(tag=="tax" && !top.T.TaxRegistered){show=false;}
if(show){ d.writeln('<li class="rbullets">', top.SL.lRbullets[tag], '</li>');}
}
d.writeln('</ul>');


}
else{document.write("<ul><li>Bulleted list of instructions here</li></ul>")}
}

function multibullet(Header,Arr){
var ret=Header;
ret+='<ul>';
for(var i=0 ; i<Arr.length; i++)
{ if(Arr[i]) {ret+= '<li class="rbullets">' + Arr[i] + '</li>' ;} }
ret+='</ul>';
return(ret);
}




