// (C) 2000 John M Hanna under the terms of the GPL
// copy freely if you include source!

// script for shopping cart w/ encryption (soon to be)
// put onClick="itemAdd(description, quantity, price)" in your html
// and showItems() to show the cart

// You may need to customize buyItems() for your server

// change this if you're not using dollars
currency="$ "
// if you need descriptions longer than 60 chars change this:
dlen=100
// if your quantity or price need be longer than 6 chars, change this:
qlen=6
plen=6
// if this cookie name conflicts with another javascript cookie, change this
basket="myBasket"

// how do you calculate shipping costs
function shipping(total) {
 return total/10; // shipping for us is 10% of total
}

function buyItems() {
 window.location="mybuy.html"
}
// change the showItems() function if you don't like how the
// shopping cart looks -- it's at the end of the script

// You shouldn't need to change anything else here until the body...
// have fun!

tlen=(dlen+qlen+plen+dlen)+1
cookie=''
function getCookie() {
 if(cookie) return cookie
 cookie=document.cookie.substr(document.cookie.indexOf(basket+"=")+1+basket.length)
 return cookie
}
function setCookie(c) {
 cookie=c
 document.cookie=basket+"="+c
}
function doBuy(f) {
	 // validate fields
	 var error=''
	 if(itemCount(getCookie()) == 0){
		error+="Your cart is empty.\n"
		alert(error);
	  	return false;
	 }else if(f.billing.value.length < 20){
	  	error+="Please enter a billing address.\n"
	  	f.billing.focus();
	  	alert(error);
	  	return false;
	 }else if(f.phone.value.length < 10){
	  	error+="Please enter a phone number with area code.\n"
	  	f.phone.focus();	  
	 	alert(error);
	  	return false;
	}else if(f.email.value.indexOf('@')<0){
	  	f.email.focus();	  
	  	error+="Please enter a valid email address\n"
	  	alert(error);
	  	return false;
	}else{
/*	 var order=cartString() +"\n\
	 Phone: "+f.phone.value+"\n\
	 E-mail: "+f.email.value+"\n\
	 Fax: "+f.fax.value+"\n\
	 \n   Billing address:\n"+f.billing.value+"\n\n\
	 \n   Shipping Address:\n"+f.shipping.value+"\n\n\
	 \n   Comments:\n"+f.comments.value*/
	 return true;
	 }
}

function itemCount(c) { // return number of items in basket
 n=Math.floor(c.length/tlen);
 return Math.floor(c.length/tlen)
}
function chop(s,start,end) { return s.substr(0,start)+s.substr(end) }
function delItem(n) {
 setCookie(chop(getCookie(),n*tlen,(n+1)*tlen))
 refresh()
}
function fetch(s,n) { return s.substr(n*tlen,tlen) }
function itemDescription(n) { // n=item number
 return fetch(getCookie(),n).substr(0,dlen)
}
function itemQuantity(n) {
 return fetch(getCookie(),n).substr(dlen,qlen)
}
function itemPrice(n) {
 return fetch(getCookie(),n).substr(dlen+qlen,plen)
}
function itemSize(n) {
 return fetch(getCookie(),n).substr(dlen+qlen+plen,dlen)
}
function pad(s,n) {
 var i=n-s.length
 while(i-- > 0) { s+=" " }
 return s.substr(0,n)
}
function itemqSet(n,q) { // just change quantity
 q=pad(q,qlen)
 cookie=getCookie()
 setCookie(cookie.substr(0,n*tlen+dlen)+q+cookie.substr((n+1)*tlen-plen-1))
}
function itemSet(n,d,q,p,s) { // description, quantity, price
 var str=pad(d,dlen)+pad(q,qlen)+pad(p,plen)+pad(s,dlen)
 cookie=getCookie()
 setCookie(cookie.substr(0,n*tlen)+str+"_"+cookie.substr((n+1)*tlen))
}
function itemAdd(d,q,p,s) {
 itemSet(itemCount(getCookie()),d,q,p,s);
// refresh()
}
function emptyCart() {
 setCookie('')
 refresh()
}
function refresh() {
 window.location.reload()
}
function money(v) {
 v=Math.round(parseFloat(v)*100)
 v=String(v)
 var l=v.length-2
 v=v.substring(0,l)+"."+v.substring(l)
 return currency+" "+v
}
function showItems() {
 var n, m=itemCount(getCookie())
 t=0
 if(m==0) {
  document.writeln('Your cart is empty<p>')
 } else {
  document.writeln('\
<center><font size="4" face="Arial" color=#013A2F><strong>\
Your Cart:</strong></font><font size="2" face="Arial"><strong></strong></font>\
<table border="1" cellpadding="2" cellspacing="0">\
    <tr>\
        <td align="center"><font size="2" face="Arial"><u>Description</u></font></td>\
        <td align="center"><font size="2" face="Arial"><u>Quantity</u></font></td>\
        <td align="center"><font size="2" face="Arial"><u>Size</u></font></td>\
        <td align="center"><font size="2" face="Arial"><u>Price</u></font></td>\
        <td align="center"><font size="2" face="Arial"><u>Extended</u></font></td>\
        <td><p align="center"><font size="2" face="Arial"><u>Action</u></font></p></td>\
    </tr>')
 for(n=0; n<m; n++) {
  var p=itemPrice(n), q=itemQuantity(n), v=parseFloat(p)*parseFloat(q)
  var d=itemDescription(n)
  var s=itemSize(n)
  
  t+=v
  document.writeln('\
    <tr>\
        <td align="center"><font face="Arial">'+d+'</font></td>\
        <td align="center"><font face="Arial"><input type="text"\
        size="2" value="'+q+'"\
        onchange="itemqSet('+n+',this.value)">\
        </font></td>\
        <td align="center"><font face="Arial">'+s+'</font></td>\
        <td align="right"><font face="Arial">'+money(p)+'</font>\
        </td>\
        <td align="right"><font face="Arial"><em>'+money(v)+'\
        </em></font>\
        </td>\
        <td align="center"><font face="Arial"><input\
        type="button" value="Remove" onclick="delItem('+n+')"></font></td>\
    </tr>')
 }
 document.writeln('\
    <tr>\
        <td>&nbsp;</td>\
        <td>&nbsp;</td>\
        <td>&nbsp;</td>\
		<td align="center"><font color="#008080" size="2"\
        face="Arial"><strong><u>Total</u></strong></font></td>\
        <td align="right"><font face="Arial"><strong>\
        '+money(t+shipping(t))+'</strong></font>\
        </td>\
        <td align="center"><font face="Arial"><input\
        type="button" value="Retotal" onclick="refresh()"></font></td>\
    </tr>\
</table>\
<table>\
    <tr>\
        <td>&nbsp;</td>\
        <td>&nbsp;</td>\
        <td>&nbsp;</td>\
        <td align="center"><font face="Arial"><input\
        type="button" value="Empty Cart" onclick="emptyCart()"></font></td>\
        <td align="center"><font face="Arial"><input\
        type="button" value="Continue" onclick="buyItems()"></font></td>\
    </tr>\
</table>\
</center>')
 }
}
function cartString() {
 var n, m=itemCount(getCookie())
 var r=''
 t=0
 if(m==0) {
  return('Empty shopping cart.')
 } else {
  for(n=0; n<m; n++) {
   var p=itemPrice(n), q=itemQuantity(n), v=parseFloat(p)*parseFloat(q)
   var d=itemDescription(n)
   t+=v
   r+=q + " @ " +money(p)+" ("+money(v)+") "+d+"\n"
  }
  r+="shipping: "+money(shipping(t))+"\n"
  r+="total: "+money(t+shipping(t))+'\n'
 }
 // trim extra spaces
 m=0; n=0
 var rr='', c
 while(n<r.length) {
  c=r.charAt(n++)
  if(c != ' ' || t != ' ') rr+=c
  t=c
 }
 return rr
}
function ccIsValid(st) {
 // Encoding only works on cards with less than 19 digits
 if (st.length > 19)
  return (false);

 var sum = 0, mul = 1, i; var digit
 for (i=st.length-1; i>=0; i--) {
  digit=st.charCodeAt(i)-48
  if(digit <= 9 && digit >=0) {
   digit *=mul;
   if(digit > 9) digit=digit % 10 +1
   sum += digit
   mul ^=3
  }
 }

 return ((sum % 10) == 0)
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}