//    Copyright (C) 1998   Zhong Yang  oops@beida.com
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                


function view_cart() {
   mycookies=new Array(20);
   var i=0;
   var startpoint=0;
   whole_len=document.cookie.length;
   while(whole_len!=startpoint && i< 20)
   {
     if ((temlen=document.cookie.indexOf(";",startpoint))!=-1)
     {
	mycookies[i]=document.cookie.substring(startpoint,temlen);
	i++;
	startpoint=temlen+1
     }
     else if (startpoint != whole_len)
     {
        mycookies[i]=document.cookie.substring(startpoint,whole_len);
        i++;
        startpoint=whole_len;
     }
   }
    printtitle();
    for (j=0;j<i;j++)
     {
//filter out cookies that are not used by shopping cart
	if((mycookies[j].substring(0,4)=="oops")||(mycookies[j].substring(0,5)==" oops"))
	   getCookie(mycookies[j]);
     }
    if (j == 0) 		// cart is empty??
      document.write("<TR> <TD COLSPAN=4> Your cart is empty. </TD> </TR>");
    document.write("</TABLE>");
}


function getCookie (acookie) {
    clen = acookie.length;
    nlen= acookie.indexOf("=",0);
    //get rid of the "oops" at the beginning, thus start at 4
    if(acookie.substring(0,1)==" ")
       cname=acookie.substring(5,nlen);
    else
       cname= acookie.substring(4,nlen);
    bname= acookie.substring(nlen+1,clen);
    sname_len=cname.indexOf("+",0);
    store=cname.substring(0,sname_len);
    dlen=cname.indexOf("+",sname_len+1);
    descrip=cname.substring(sname_len+1,dlen)
    product=cname.substring(dlen+1,nlen);

    qlen=bname.indexOf("+",0);
    rlen=bname.indexOf("+",qlen+1);
    quant=bname.substring(0,qlen);
    if(quant==0)
	document.cookie="oops"+cname+"=bb; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+"";

    refer=bname.substring(qlen+1,rlen);
    price=bname.substring(rlen+1,clen-nlen);
//    price=price_format(price);
    printcontent();
}

function putback(str1){
  document.cookie="oops"+str1+"=bb; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+""; 
}

// There's no need to touch anything above this line
// You can use "store","descrip","product","quant","price"
// and "refer" in the code below

function printtitle(){
tabletitle="<TABLE BORDER=2 CELLSPACING=0 CELLPADDING=3 WIDTH=100%> <TR> \
<TH>Item</TH> <TH>Format</TH> <TH>Quantity</TH> <TH>Edit</TH> </TR>";
document.writeln(tabletitle);
}

function printcontent2(){
  if(quant!=0){
    document.write("<TR> <TD>"+product+"</TD>\n");
    document.write("<TD>"+descrip+"</TD>\n");
    document.write("<TD>"+quant+"</TD>\n");
    document.write("<TD ALIGN=CENTER> <FORM><INPUT TYPE=SUBMIT VALUE=Return "); 
    document.write("onClick=\"putback(\'"+store+"+"+descrip+"+"+product);
    document.write("\')\"> </FORM> </TD> </TR>\n");
   }
}

//add a field so that shopper can change amount of product in cart
function printcontent(){
  if(quant!=0){
    document.write("<TR> <TD>"+product+"</TD>\n");
    document.write("<TD>"+descrip+"</TD>\n");
    document.write("<TD> <FORM METHOD=get ");
    document.write("action=view.html> <INPUT TYPE=text SIZE=3");
    document.write(" NAME=quantity VALUE="+quant+">");
    document.write("<INPUT TYPE=hidden NAME="+price);
    document.write(" VALUE=\""+descrip+"\">");
    document.write("<INPUT TYPE=hidden NAME=\""+product);
    document.write("\" VALUE=\""+store+"\"></TD>\n");
    document.write("<TD ALIGN=CENTER> <INPUT TYPE=submit VALUE=Update ");
    document.write("onClick=addtocart(this.form,'view.html')>");
    document.write("</FORM> </TD> </TR>\n");
   }
}

function price_format(tmpprice){
// get rid off the extra digits behind "."
   dot = tmpprice.toString().indexOf(".");
   if(dot!=-1)
      tmpprice=tmpprice.toString().substr(0,dot+4);
   tmpprice=Math.round(tmpprice*100)/100;
// add "0" if necessary
   dot = tmpprice.toString().indexOf(".");
   if(dot ==-1)
      tmpprice=tmpprice.toString()+".00";
   else if((tmpprice.toString().length-dot)==2)
      tmpprice=tmpprice.toString()+"0";
   return tmpprice;
}

