var total=0; 
var quant=0;

function view_cart(){
	mycookies=new Array(20);
	var i=0;
	var startpoint=0;
	var 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;
     	} // end if
     	else if(startpoint!=whole_len) {
	        mycookies[i]=document.cookie.substring(startpoint,whole_len);
	        i++;
	        startpoint=whole_len;
		} // end else
	} // end while
    printtitle();
    for(j=0;j<i;j++) { // filter out cookies that are not used by shopping cart
		if((mycookies[j].substring(0,6)=="reeski")||(mycookies[j].substring(0,7)==" reeski")) getCookie(mycookies[j]);
     } // end for
	 printtotal();
} // end view_cart()

function getCookie(acookie){
	clen=acookie.length;
	nlen=acookie.indexOf("=",0);
    if(acookie.substring(0,1)==" ") cname=acookie.substring(7,nlen);
//    if (acookie.substring(0,1)==" ") cname=acookie.substring(5,nlen);
    else cname= acookie.substring(6,nlen); // cookie name should start with 'reeski'
//    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);
    product=cname.substring(sname_len+1,dlen)
//    descrip=cname.substring(sname_len+1,dlen)
    descrip=cname.substring(dlen+1,nlen);
//    product=cname.substring(dlen+1,nlen);
    qlen=bname.indexOf("+",0);
    quant=bname.substring(0,qlen);
    if(quant==0) document.cookie="reeski"+cname+"=bb; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+"";
//    if(quant==0) document.cookie="oops"+cname+"=bb; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+"";
//    rlen=bname.indexOf("+",qlen+1);
//    refer=bname.substring(qlen+1,rlen);
    price=bname.substring(qlen+1,clen-nlen);
//    price=bname.substring(rlen+1,clen-nlen);
    price=price_format(price);
    if(quant!=0) {
		subtotal=price*quant;
		total=total+subtotal;
		subtotal=price_format(subtotal);
	} // end if
    printcontent();
}

function printtitle(){
tabletitle="<table width=100% border=0 cellspacing=0 cellpadding=20> \
	<tr> \
		<td> \
			<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#CCCCCC> \
				<tr> \
					<td> \
						<table width=100% align=center border=0 cellspacing=1 cellpadding=2> \
							<tr bgcolor=#FFCC00 align=center> \
								<td><img src=images/cart_product.gif width=41 height=20 border=0></td> \
								<td><img src=images/cart_description.gif width=59 height=20 border=0></td> \
								<td><img src=images/cart_each.gif width=24 height=20 border=0></td> \
								<td><img src=images/cart_quantity.gif width=47 height=20 border=0></td> \
								<td><img src=images/cart_subtotal.gif width=45 height=20 border=0></td> \
							</tr>";
document.write(tabletitle);
}

function printcontent(){
  if(quant!=0){
    document.write("<tr align=center bgcolor=#FFFFFF><td class=copy>"+product+"</td><td class=copy>");
    document.write(descrip+"&nbsp;</td><td class=copy>$"+price+"</td><form method=get action=view.htm><td class=copy>");
    document.write("<input type=text size=3 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=\""+price+"\">");
	document.write("<input type=button name=add2cart value=Update ");
	document.write("onClick=addtocart(this.form,'view.htm')></td></form>");
	document.write("<td class=copy>$"+subtotal);
    document.write("</td></form></tr>");
   } // end if
} // end printcontent()

function printtotal(){
	if(quant!=0){
		bottomline1="<tr bgcolor=#EEEEEE> \
						<td colspan=4 align=right><img src=images/cart_total.gif width=34 height=20 \
						border=0></td> \
						<td align=center class=copy><b>$";
		document.write(bottomline1);
		total=price_format(total);
		document.write(total);
		bottomline2="</b></td> \
					  </tr> \
					  </table></td></tr></table></td></tr></table>";
		document.write(bottomline2);
	} // end if
	else document.write("</table></td></tr></table></td></tr></table>");
} // end printtotal()

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 ".00" 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;
} // end price_format()
