///////////////////////////////////////////////////////
     function amendItem(itemno, newquant) {
          newItemList=null;
          itemlist=0;
          for (var i=0; i <= fulllist.length; i++) {
               if (fulllist.substring(i,i+1) == '[') {
                    thisitem=1;
                    itemstart=i+1;
                    fullstart=i+1;
               } else if (fulllist.substring(i,i+1) == ']') {
                    itemend=i;
                    itemlist=itemlist+1;
                    if (itemlist != itemno) {
                         newItemList=newItemList+'['+fulllist.substring(fullstart, itemend)+']';
                    } else {
                         newItemList=newItemList + '['+theindex+'|'+newquant+']';
                    }
               } else if (fulllist.substring(i,i+1) == '|') {
                    if (thisitem==1) theindex=fulllist.substring(itemstart, i);
                    thisitem++;
                    itemstart=i+1;
               }
          }
          index=parent.document.cookie.indexOf("theBasket");
          parent.document.cookie="theBasket="+newItemList;
          self.location='cart.php';

     }

///////////////////////////////////////////////////////
     function removeItem(itemno) {
          newItemList=null;
          itemlist=0;
          for (var i=0; i <= fulllist.length; i++) {
               if (fulllist.substring(i,i+1) == '[') {
                    itemstart=i+1;
               } else if (fulllist.substring(i,i+1) == ']') {
                    itemend=i;
                    theitem=fulllist.substring(itemstart, itemend);
                    itemlist=itemlist+1;
                    if (itemlist != itemno) {
                         newItemList=newItemList+'['+fulllist.substring(itemstart, itemend)+']';
                    }
               }
          }
          index=parent.document.cookie.indexOf("theBasket");
          parent.document.cookie="theBasket="+newItemList;
          self.location='cart.php';
     }

/////////////////////////////////////////////////////
// clearBasket() - removes all items from the basket
     function clearBasket() {
          if (confirm('Are you sure you wish to remove all shopping cart contents ?')) {
               index=parent.document.cookie.indexOf("theBasket");
               parent.document.cookie="theBasket=.";
          self.location='cart.php';
          }
     }

/////////////////////////////////////////////////////
// showItems() - display shopping basket in a table
     function showItems() {
          index=parent.document.cookie.indexOf("theBasket");
          countbegin=(parent.document.cookie.indexOf("=", index) + 1);
          countend=parent.document.cookie.indexOf(";", index);
          if (countend == -1) {
                    countend=parent.document.cookie.length;
          }
          fulllist=parent.document.cookie.substring(countbegin, countend);
        
          document.writeln('<FORM NAME="basket">');
          document.writeln('<TABLE border=0 cellSpacing=0 cellPadding=0 width=540><TR>');
          document.writeln('<TD vAlign=middle align=center width=35 bgColor=#e7e7de><font class=desctext>Qua</font></TD>');
          document.writeln('<TD vAlign=top align=left width=11 background="images/dotted_divider.gif"><IMG src="images/pd_table_dotted_top.gif" width=11 height=32></TD>');
          document.writeln('<TD vAlign=middle align=center width=241 bgColor=#e7e7de><font class=desctext>Name/Description</font></TD>');
          document.writeln('<TD vAlign=top align=left width=11><IMG src="images/pd_table_dotted_top.gif" width=11 height=32></TD>');
          document.writeln('<TD vAlign=middle align=center width=115 bgColor=#e7e7de><font class=desctext>Price Each</font></TD>');
          document.writeln('<TD vAlign=top align=left width=11><IMG src="images/pd_table_dotted_top.gif" width=11 height=32></TD>');
          document.writeln('<TD vAlign=middle align=center width=115 bgColor=#e7e7de><font class=desctext>Total</font></TD>');
          document.writeln('<TD vAlign=top align=left width=11><IMG src="images/pd_table_dotted_top.gif" width=11 height=32></TD>');
          document.writeln('<TD vAlign=middle align=center width=90 bgColor=#e7e7de><font class=desctext>Action</font></TD>');
          document.writeln('</TR><TR vAlign=top align=left>');

          var subtotalUSD=0;
          var itemlist=0;
          var totalBags=0;

          for (var i=0; i <= fulllist.length; i++) {
               if (fulllist.substring(i,i+1) == '[') {
                    itemstart=i+1;
                    thisitem=1;
               } else if (fulllist.substring(i,i+1) == ']') {
                     itemend=i;
                     thequantity=fulllist.substring(itemstart, itemend);
                     thepriceUSD=catalog[theindex].priceOzUSD*catalog[theindex].weight;
                     totalBags=totalBags+parseInt(thequantity);
                     itemtotalUSD=0;
                     itemtotalUSD=convDec(thepriceUSD*thequantity);
                     subtotalUSD=subtotalUSD + parseFloat(convDec(itemtotalUSD));

//alert ('fulllist='+fulllist);
//alert ('checkSierra()='+checkSierra());
//alert ('theindex='+theindex);
//alert ('thequantity='+thequantity);


                     theprice=thepriceUSD;
                     itemtotal=itemtotalUSD;
                     subtotal=subtotalUSD;
                     sierraChk=checkSierra();

   
                    itemlist=itemlist+1;
                    document.write('<TR><TD ALIGN=CENTER VALIGN=MIDDLE width=35><FONT class=pricetext><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=2 maxLength=2 style="width: 28px"></TD>');
                    document.write('<TD background="images/dotted_divider.gif" width=11><IMG src="images/dotted_divider.gif" width=11 height=6></TD>');

			if (catalog[theindex].cat == 'drm') {
	           	document.write('<TD ALIGN=CENTER VALIGN=MIDDLE><FONT class=pricetext>'+catalog[theindex].name+'<BR>('+catalog[theindex].weight+' oz / '+parseInt(catalog[theindex].weight*28.3495)+' gr '+packaging[catalog[theindex].pkgCode].type+')</TD>');
				} else {
				document.write('<TD ALIGN=CENTER VALIGN=MIDDLE><FONT class=pricetext>'+catalog[theindex].name+'<BR>('+catalog[theindex].weight/16+' lb / '+convDec(catalog[theindex].weight*28.3495/1000)+' kg '+packaging[catalog[theindex].pkgCode].type+')</TD>');
			}

/*
document.write('<TD ALIGN=CENTER VALIGN=MIDDLE><FONT class=pricetext>'+catalog[theindex].name+'<BR>('+catalog[theindex].weight/16+' lb / '+parseInt(catalog[theindex].weight*28.3495/1000)+' kg '+packaging[catalog[theindex].pkgCode].type+')</TD>');
*/

                    document.write('<TD background="images/dotted_divider.gif" width=11><IMG src="images/dotted_divider.gif" width=11 height=6></TD>');
                    document.write('<TD ALIGN=center VALIGN=MIDDLE HEIGHT=30><FONT class=pricetext>USD &#36;'+convDec(theprice)+'</TD>');
                    document.write('<TD background="images/dotted_divider.gif" width=11><IMG src="images/dotted_divider.gif" width=11 height=6></TD>');
                    document.write('<TD ALIGN=center><FONT class=pricetext>USD &#36;'+convDec(itemtotal)+'</TD>');
                    document.write('<TD background="images/dotted_divider.gif" width=11><IMG src="images/dotted_divider.gif" width=11 height=6></TD>');
                    document.write('<TD ALIGN=CENTER VALIGN=MIDDLE><A class=footer HREF="javascript:amendItem('+itemlist+',document.basket.quant'+itemlist+'.value)">Update</A><BR><A class=footer HREF="javascript:removeItem('+itemlist+')">Remove</A></TD></TR>');

               } else if (fulllist.substring(i,i+1) == '|') {
                    if (thisitem==1) theindex=fulllist.substring(itemstart, i);
                    thisitem++;
                    itemstart=i+1;
               }
          }

          document.writeln('<TR><TD COLSPAN=10 HEIGHT=14 background="images/top_dottedline.gif"><FONT class=pricetext>&nbsp;</font></TD>');
			if (checkSierra() == 0) {
						document.writeln('<TR><TD ALIGN=RIGHT VALIGN=MIDDLE COLSPAN=3 HEIGHT=30><FONT class=pricetext>Total Weight:&nbsp;&nbsp;'+totalWeight()+' oz ('+parseInt(totalWeight()*28.34952313)+' gr)</font></TD>');
						} else {
						document.writeln('<TR><TD ALIGN=RIGHT VALIGN=MIDDLE COLSPAN=3 HEIGHT=30><FONT class=pricetext>Total Weight:&nbsp;&nbsp;'+totalWeight()/16+' lb ('+parseInt(totalWeight()*28.34952313)+' gr)</font></TD>');
						}

          document.writeln('<TD ALIGN=RIGHT VALIGN=MIDDLE COLSPAN=3 HEIGHT=30><FONT class=pricetext>Subtotal:</font></TD>');
          document.writeln('<TD ALIGN=center VALIGN=MIDDLE HEIGHT=30><FONT class=pricetext>USD &#36;'+convDec(subtotal)+'</font></TD>');
          document.writeln('<TD ALIGN=CENTER VALIGN=MIDDLE HEIGHT=30 colspan=2>&nbsp;</TD>');
          document.writeln('</TR></TABLE></FORM>');
     }

var theflags;
var newString;
var baseString="https://secure.internetsecure.com/process.cgi?submit=Purchase&MerchantNumber=9241&language=English&ReturnURL=http://www.pacrimmushrooms.com/thankyou.php&Products=Price::Qty::Code::Description::Flags|"


/////////////////////////////////////////////////////
// getRateCode
function getRateCode() {

var theTotalWeight=totalWeight();

	if (checkSierra() == 0) {
		if (theTotalWeight <= 16) {theRateCode="A";}
			else if (theTotalWeight >= 17 && theTotalWeight <= 32) {theRateCode="B";}
			else if (theTotalWeight >= 33 && theTotalWeight <= 48) {theRateCode="C";}
			else if (theTotalWeight >= 49 && theTotalWeight <= 54) {theRateCode="D";}
			else if (theTotalWeight >= 55 && theTotalWeight <= 60) {theRateCode="E";}
			else if (theTotalWeight >= 61 && theTotalWeight <= 76) {theRateCode="F";}
			else if (theTotalWeight >= 77) {theRateCode="G";}
		} else {
		if (theTotalWeight <= 95) {theRateCode="A";}
			else if (theTotalWeight >= 96 && theTotalWeight <= 239) {theRateCode="B";}
			else if (theTotalWeight >= 240 && theTotalWeight <= 399) {theRateCode="C";}
			else if (theTotalWeight >= 400) {theRateCode="D";}
	}
return theRateCode;
}


/*
Sierra shipping rates:
1-6  lb               :  $8 per pound   ->95
6-15 lb              :  $5 per pound    96-239
15-25 lb            :  $4 per pound     240-399
+25 lb              :  $3.5 per pound   400->
*/


/////////////////////////////////////////////////////
// doString() - generate i-secure transaction code
     function doString() {
          index=parent.document.cookie.indexOf("theBasket");
          countbegin=(parent.document.cookie.indexOf("=", index) + 1);
          countend=parent.document.cookie.indexOf(";", index);
          if (countend == -1) {
                    countend=parent.document.cookie.length;
          }
          theCookie=parent.document.cookie;

          fulllist=parent.document.cookie.substring(countbegin, countend);

          var itemlist=0;
          var addString="";
          var shipSierra=0; // 0=dried only, 1=fresh or combination (USA FedEx)

          for (var i=0; i <= fulllist.length; i++) {
               if (fulllist.substring(i,i+1) == '[') {
                    itemstart=i+1;
                    thisitem=1;
               } else if (fulllist.substring(i,i+1) == ']') {
                    itemend=i;
                    thequantity=fulllist.substring(itemstart, itemend);

					if (catalog[theindex].cat != "drm") {
						shipSierra=1;
						}

               		thesize=catalog[theindex].weight;
                    thepriceUSD=catalog[theindex].priceOzUSD*catalog[theindex].weight;
					/* below, used to separate USD/CAD */
                    //if (cur=="USD") {theprice=thepriceUSD;theflags='{USD}';} else {theprice=thepriceCAD;theflags='';};
                    theprice=thepriceUSD;
                    theflags='{USD}';
                    /* below, used to test only */
                    //if (cur=="USD") {theprice=thepriceUSD;theflags='{USD}{TEST}';} else {theprice=thepriceCAD;theflags='{TEST}';};
                    
                    //theCode=catalog[theindex].code;
                    theCode=catalog[theindex].code+thesize+'USD';
                    theDesc=catalog[theindex].name+' ('+catalog[theindex].weight+' oz / '+parseInt(catalog[theindex].weight*28.3495)+' gr '+packaging[catalog[theindex].pkgCode].type+')';
                    
                    if (itemlist==0) {
                         addString=addString+theprice+"::"+thequantity+"::"+theCode+"::"+theDesc+"::"+theflags;
                         } else {
                         addString=addString+"|"+theprice+"::"+thequantity+"::"+theCode+"::"+theDesc+"::"+theflags;
                         }
                    newString=baseString+addString;
                    itemlist=itemlist+1;
               } else if (fulllist.substring(i,i+1) == '|') {
                    if (thisitem==1) theindex=fulllist.substring(itemstart, i);
                    if (thisitem==2) theoptidx=fulllist.substring(itemstart, i);
                    thisitem++;
                    itemstart=i+1;
               }
          }

var totShipping=0;
var shipDesc;
var totalBags=totalItems(0);
var length = shipping.length;
var theTotalWeight=totalWeight();
var theRateCode=getRateCode();


for (var i=0; i < length; i++) {
		if (shipping[i].code == theShipCode && shipping[i].rateCode == theRateCode) {
			//check Sierra
			if (checkSierra() == 0) {
				totShipping=parseFloat(shipping[i].rateUSD);theIndex=i;
				shipDesc=shipping[i].name;
				} else {
				//totShipping=parseFloat(eval('shipping[i].rate'+'USD'));
				totShipping=parseFloat(shipping[i].rateUSD*(theTotalWeight/16));theIndex=i;
				shipDesc=shipping[i].name;
				}
			break;
		}
}

//alert ('totShipping='+totShipping);
//alert ('shipDesc='+shipDesc);


shipString='|'+totShipping+'::1::shp'+theShipCode+"USD::"+shipDesc+"::"+theflags;
finString=newString+shipString;

          return finString;
     }

/*
https://secure.internetsecure.com/process.cgi?submit=Purchase&MerchantNumber=9241&language=English&ReturnURL=http://www.pacrimmushrooms.com/thankyou.htm&Products=Price::Qty::Code::Description::Flags|112::2::mor16USD::Dried Morel (16 oz / 454 gr bag)::{USD}|65::2::mor8USD::Dried Morel (8 oz / 227 gr bag)::{USD}|35::2::mor4USD::Dried Morel (4 oz / 114 gr bag)::{USD}|37.8::1::shpCAN2USD::CanPost Xpresspost::{USD}
w/USA3:
https://secure.internetsecure.com/process.cgi?submit=Purchase&MerchantNumber=9241&language=English&ReturnURL=http://www.pacrimmushrooms.com/thankyou.php&Products=Price::Qty::Code::Description::Flags|49::2::mat16USD::Dried Matsutake (Pine) Mushroom (16 oz / 453 gr bag)::{USD}|28.9::1::shpUSA1USD::CanPost USA Small Packet (Air)::{USD}

https://secure.internetsecure.com/process.cgi?submit=Purchase&MerchantNumber=9241&language=English&ReturnURL=http://www.pacrimmushrooms.com/thankyou.php&Products=Price::Qty::Code::Description::Flags|49::2::mat16USD::Dried Matsutake (Pine) Mushroom (16 oz / 453 gr bag)::{USD}|14.85::1::shpUSA2USD::CanPost USA Small Packet (Surface)::{USD}
https://secure.internetsecure.com/process.cgi?submit=Purchase&MerchantNumber=9241&language=English&ReturnURL=http://www.pacrimmushrooms.com/thankyou.php&Products=Price::Qty::Code::Description::Flags|49::2::mat16USD::Dried Matsutake (Pine) Mushroom (16 oz / 453 gr bag)::{USD}|32.4::1::shpINT1USD::CanPost International Small Packet (Air)::{USD}
*/
