//    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 alterError(value) {
   if (value<=0.99) {
      newPounds = '0';
   } else {
      newPounds = parseInt(value);
   }
   newPence = parseInt((value+.0008 - newPounds)* 100);
   if (eval(newPence) <= 9) newPence='0'+newPence;
   newString = newPounds + '.' + newPence;
   return (newString);
}

function view_cart() {
   mycookies=new MakeArray(20);
   var i=0;
   var startpoint=0;
   var totprice=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();
    totprice = 0
    for (j=0;j<i;j++)
     {
	getCookie(mycookies[j],0);
      totprice = (eval(totprice + getCookie(mycookies[j],1)));
     }
    printextras(totprice);
    document.write("</table>");
}

function MakeArray(n) {
   this.length = n;
   for (var i = 1; i <= n; i++) { 
     this[i] = 0 }
     return this
}

function getCookie (acookie,subr) {
    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);
    if (subr == 0) {
       printcontent();
    } else if (subr == 1){ 
       toval = (eval(price*quant));
       if (toval>0) 
       	return toval; else return 0;
    }
}

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=1 CELLSPACING=0 CELLPADDING=0 width=578><tr align=center> \
   <td bgcolor='#ffff00' width=100><b><FONT FACE='Courier New,Courier'> \
   <TT>Product</TT></FONT></b></td><td bgcolor='#ffff00'><b> \
   <FONT FACE='Courier New,Courier'><TT>Description</TT></FONT> \
   </b></td><td bgcolor='#ffff00'><b><FONT FACE='Courier New,Courier'> \
   <TT>Qty</TT></FONT></b></td><td bgcolor='#ffff00' width=80><b> \
   <FONT FACE='Courier New,Courier'><TT>Price Each</TT></FONT></b></td> \
   <td width=80 bgcolor='#ffff00'><b><FONT FACE='Courier New,Courier'> \
   <TT>Line Price</TT></FONT></b></td><td width=100 bgcolor='#ffff00'><b> \
   <FONT FACE='Courier New,Courier'><TT>View Item</TT></FONT></b></td> \
   <td width=100 bgcolor='#ffff00'><b><FONT FACE='Courier New,Courier'> \
   <TT>Return Product</TT></FONT></b></td></tr>";
   document.write(tabletitle);
}

function printcontent(){
  if(quant!=0){
    lineprice = 0;
    lineprice = eval(price)*quant;
	 if (lineprice==0){lineprice="N/A"} else {lineprice=alterError(lineprice)}
    document.write("<tr align=center><td>"+product+"</td><td>");
    document.write(descrip+"</td><td>"+quant+"</td><td>$");
    document.write(price+"</td><td>$"+lineprice+"</td><td>");
    document.write("<a href=sample.asp?id="+product+">"+store+"</a>");
    document.write("</td><td><form><input type='submit' value='Return'"); 
    document.write("onClick=\"putback(\'"+store+"+"+descrip+"+"+product);
    document.write("\')\"></form></td></tr>");
   }
}

function printextras(totprice) {
   shipprice = 0;
   grandtotal = 0;
   pricetot = alterError(totprice);
   document.write("<tr><td colspan=4 bgcolor='#0066FF' align=right><b>Total</b></td><td align=center bgcolor='#0066FF'>$");
   document.write(pricetot + "</td><td colspan=3 bgcolor='#ffff00' align=center><b>Thank You!</b></td></tr>");
}