function save_alterations ()
{
   // Save all the alterations data to "alt.x" cookies.
   //
   // This cookie is defined when the user clicks "Proceed" in alterations.js
   //
   // Format is:
   //
   //   field[0] = panel color, name, and size string (unique key)
   //   field[1] = alteration length (=0 for none)
   //   field[2] = panel color, name, and size string (unique key)
   //   field[3] = alteration length (=0 for none)
   //
   //   ...and so on for every panel in the basket.
   //
   //   The last field in the cookie contains the total number of alterations.
   //
   // If a panel is removed from the basket or requested alterations becomes "No",
   // then delete this cookie.

   var total_alterations=0;
   var total_orders=read_cookie('basket_total_cookie');
   var cookie_str="";

   for (i=1; i <= total_orders; i++)
   {
      current_order="order."+i;
      database="";
      database=unescape(read_cookie (current_order));

      Token0=database.indexOf("|", 0);
      Token1=database.indexOf("|", Token0+1);
      Token2=database.indexOf("|", Token1+1);
      Token3=database.indexOf("|", Token2+1);
      Token4=database.indexOf("|", Token3+1);

      field=new Array;
      field[0]=database.substring (0, Token0);                   // name;
      field[1]=database.substring (Token0+1, Token1);            // color;
      field[2]=database.substring (Token1+1, Token2);            // size;
      field[3]=database.substring (Token2+1, Token3);            // quantity;
      field[4]=database.substring (Token3+1, database.length);   // price per;

      for (item_num in lace)
      {
         if (lace[item_num][4] == field[0])
         {
            break;
         }
         item_num+=1;
      }

      var t=lace[item_num][0];
      var rootname=field[0];
      var fullname=lace[item_num][3];
      var color=unescape(field[1]);
      var quantity=parseInt(field[3]);
      var name_str=field[1]+" "+field[0]+" "+field[3];

      var size_str="";
      if (field[2] == 0) { size_str="("+lace[item_num][7]+ "&quot;W x "+lace[item_num][8] +"&quot;L)"; }
      if (field[2] == 1) { size_str="("+lace[item_num][11]+"&quot;W x "+lace[item_num][12]+"&quot;L)"; }
      if (field[2] == 2) { size_str="("+lace[item_num][15]+"&quot;W x "+lace[item_num][16]+"&quot;L)"; }
      if (field[2] == 3) { size_str="("+lace[item_num][19]+"&quot;W x "+lace[item_num][20]+"&quot;L)"; }
      if (field[2] == 4) { size_str="("+lace[item_num][23]+"&quot;W x "+lace[item_num][24]+"&quot;L)"; }
      if (field[2] == 5) { size_str="("+lace[item_num][27]+"&quot;W x "+lace[item_num][28]+"&quot;L)"; }
      if (field[2] == 6) { size_str="("+lace[item_num][31]+"&quot;W x "+lace[item_num][32]+"&quot;L)"; }
      if (field[2] == 7) { size_str="("+lace[item_num][35]+"&quot;W x "+lace[item_num][36]+"&quot;L)"; }
      if (field[2] == 8) { size_str="("+lace[item_num][39]+"&quot;W x "+lace[item_num][40]+"&quot;L)"; }
      if (field[2] == 9) { size_str="("+lace[item_num][43]+"&quot;W x "+lace[item_num][44]+"&quot;L)"; }

      // t=1 means lace panels;
      if (t == 1)
      {
         for (j=1; j <= quantity; j++)
         {
            alt_val=document.getElementById("alt_"+rootname+"_"+j).value;
            if (alt_val == null) { alt_val=0; }

            if (alt_val > 0)
            {
               cookie_str+="Shorten one "+color+" "+fullname+" panel "+size_str+" to "+alt_val+"&quot;.|";
               total_alterations+=1;
            }
         }
      }
   }
   cookie_str+=total_alterations;

   // Delete the old cookie
   document.cookie="alterations=; expires=Thu, 01-Jan-70 00:00:01 GMT";

   if (total_alterations > 0)
   {
      set_cookie ("alterations", cookie_str);
   }
}

function load_alterations ()
{
   // Retrieves the alterations data from a cookie.
   database=unescape(read_cookie ("alterations"));

   pos=database.lastIndexOf("|");
   len=database.length;

   var total_alterations=database.substring (pos+1, len);

   return parseInt(total_alterations);
}

function cost_summary (disp, final)
{
   // Calculates and displays the subtotal, the tax total, the shipping fee, and
   // the grand total.  Returns the grand total.  Set disp to 1 to display the
   // cost_str in basket_subtotal.
   // Set final to 1 if displaying the final order confirmation message.
   // Set final to 2 if displaying the final order confirmation reciept (black on white).

   var cost_str="";
   var taxtotal=0;
   var subtotal=0;
   var grandtotal=0;
   var in_texas=0;
   var total_orders=0;
   var alter_cost=0;

   var alter=load_alterations ();
   if (isNaN(alter)) { alter=0; }

   in_texas=load_address_data ();   // Returns 1 if the shipping state is Texas;
   total_orders=read_cookie('basket_total_cookie');

   // Get the subtotal from all the current orders.
   for (i=1; i <= total_orders; i++)
   {
      current_order="order."+i;
      database="";
      database=unescape(read_cookie (current_order));

      Token0=database.indexOf("|", 0);
      Token1=database.indexOf("|", Token0+1);
      Token2=database.indexOf("|", Token1+1);
      Token3=database.indexOf("|", Token2+1);
      Token4=database.indexOf("|", Token3+1);

      field=new Array;
      field[0]=database.substring (0, Token0);                   // name;
      field[1]=database.substring (Token0+1, Token1);            // color;
      field[2]=database.substring (Token1+1, Token2);            // size;
      field[3]=database.substring (Token2+1, Token3);            // quantity;
      field[4]=database.substring (Token3+1, database.length);   // price per;

      subtotal+=(parseInt(field[3]))*(parseInt(field[4]));
   }

   if (in_texas == 1) { taxtotal=subtotal*tx_tax; }
   alter_cost=alter*alter_fee;

   if ((final != 1) && (final != 2))
   {
      cost_str+="Your subtotal is "+addcommas(subtotal, 1)+".&nbsp;&nbsp;"

      if (in_texas == 1)
      {
         subtotal+=taxtotal;
         cost_str+="Texas tax ("+(tx_tax*100)+"%) on this purchase is "+addcommas(taxtotal, 1)+" bringing your subtotal to "+addcommas(subtotal, 1)+".<br />";
      }

      if (alter > 0)
      {
         subtotal+=alter_cost;
         if (alter == 1)
         {
            cost_str+="You've requested we shorten 1 panel at a cost of "+addcommas(alter_fee, 1)+".&nbsp; This brings your subtotal to "+addcommas(subtotal, 1)+".<br />";
         }
         else
         {
            cost_str+="You've requested we shorten "+alter+" panels at a cost of "+addcommas(alter_fee, 1)+" per.&nbsp; This brings your subtotal to "+addcommas(subtotal, 1)+".<br />";
         }
      }

      if (shipping_fee > 0)
      {
         cost_str+="We also charge a flat shipping fee of "+addcommas(shipping_fee, 1)+" per order&nbsp;"
      }

      grandtotal=subtotal+shipping_fee;

      cost_str+="bringing your total purchase price to "+addcommas(grandtotal, 1)+"."

      if (disp == 1)
      {
         document.getElementById('basket_subtotal').innerHTML=cost_str;
      }

      return grandtotal;
   }
   else if (final >= 1)
   {
      grandtotal=subtotal+taxtotal+alter_cost+shipping_fee;

      cost_str="Your subtotal is "+addcommas(subtotal, 1)+".&nbsp;&nbsp;"

      // With three variables, there are 8 possible combinations of wording:
      //
      //  Texas  Alter  Ship
      //
      //    1      1      1   =  With sales tax (TX), x requested alterations, and shipping, your...
      //    1      1      0   =  With sales tax (TX) and x requested alterations, your...
      //    1      0      1   =  With sales tax (TX) and shipping, your...
      //    1      0      0   =  With sales tax (TX), your...
      //    0      1      1   =  With x requested alterations and shipping, your...
      //    0      1      0   =  With x requested alterations, your...
      //    0      0      1   =  With shipping, your...
      //    0      0      0   =  Your...

      if ((in_texas == 1) && (alter >= 1) && (shipping_fee >= 1))
         { cost_str+="With sales tax, "+alter+" requested alteration(s), and shipping, your"; }
      if ((in_texas == 1) && (alter >= 1) && (shipping_fee == 0))
         { cost_str+="With sales tax and "+alter+" requested alteration(s), your"; }
      if ((in_texas == 1) && (alter == 0) && (shipping_fee >= 1))
         { cost_str+="With sales tax and shipping, your"; }
      if ((in_texas == 1) && (alter == 0) && (shipping_fee == 0))
         { cost_str+="With sales tax, your"; }
      if ((in_texas == 0) && (alter >= 1) && (shipping_fee >= 1))
         { cost_str+="With "+alter+" requested alteration(s) and shipping, your"; }
      if ((in_texas == 0) && (alter >= 1) && (shipping_fee == 0))
         { cost_str+="With "+alter+" requested alteration(s) your"; }
      if ((in_texas == 0) && (alter == 0) && (shipping_fee >= 1))
         { cost_str+="With shipping your"; }
      if ((in_texas == 0) && (alter == 0) && (shipping_fee == 0))
         { cost_str+="Your"; }

      cost_str+="&nbsp;total purchase price is "+addcommas(grandtotal, 1)+".";

      return cost_str;
   }
}

function save_address_data (same)
{
   // Saves the shipping and billing data to cookies.

   var s_cookie_name="shipping_info";
   var b_cookie_name="billing_info";
   var ship_val="";
   var bill_val="";

   ship_val=ship_val+same+"|";
   ship_val=ship_val+document.getElementById("ship_first").value+"|";
   ship_val=ship_val+document.getElementById("ship_middle").value+"|";
   ship_val=ship_val+document.getElementById("ship_last").value+"|";
   ship_val=ship_val+document.getElementById("ship_addr1").value+"|";
   ship_val=ship_val+document.getElementById("ship_addr2").value+"|";
   ship_val=ship_val+document.getElementById("ship_city").value+"|";
   ship_val=ship_val+document.getElementById("ship_state").value+"|";
   ship_val=ship_val+document.getElementById("ship_zip").value;

   bill_val=bill_val+same+"|";
   bill_val=bill_val+document.getElementById("bill_first").value+"|";
   bill_val=bill_val+document.getElementById("bill_middle").value+"|";
   bill_val=bill_val+document.getElementById("bill_last").value+"|";
   bill_val=bill_val+document.getElementById("bill_addr1").value+"|";
   bill_val=bill_val+document.getElementById("bill_addr2").value+"|";
   bill_val=bill_val+document.getElementById("bill_city").value+"|";
   bill_val=bill_val+document.getElementById("bill_state").value+"|";
   bill_val=bill_val+document.getElementById("bill_zip").value;

   set_cookie (s_cookie_name, ship_val);
   set_cookie (b_cookie_name, bill_val);
}

function load_address_data ()
{
   // Retrieves the shipping and billing data from cookies.
   // Returns 1 if the shipping state is Texas.

   var s_cookie_name="shipping_info";
   var b_cookie_name="billing_info";

   database="";
   database=unescape(read_cookie (s_cookie_name));

   Token0=database.indexOf("|", 0);
   Token1=database.indexOf("|", Token0+1);
   Token2=database.indexOf("|", Token1+1);
   Token3=database.indexOf("|", Token2+1);
   Token4=database.indexOf("|", Token3+1);
   Token5=database.indexOf("|", Token4+1);
   Token6=database.indexOf("|", Token5+1);
   Token7=database.indexOf("|", Token6+1);

   ship=new Array;
   ship[0]=database.substring (0, Token0);                   // Same info flag;
   ship[1]=database.substring (Token0+1, Token1);            // First name;
   ship[2]=database.substring (Token1+1, Token2);            // Middle name;
   ship[3]=database.substring (Token2+1, Token3);            // Last name;
   ship[4]=database.substring (Token3+1, Token4);            // Address line 1;
   ship[5]=database.substring (Token4+1, Token5);            // Address line 2;
   ship[6]=database.substring (Token5+1, Token6);            // City;
   ship[7]=database.substring (Token6+1, Token7);            // State;
   ship[8]=database.substring (Token7+1, database.length);   // Zip;

   database="";
   database=unescape(read_cookie (b_cookie_name));

   Token0=database.indexOf("|", 0);
   Token1=database.indexOf("|", Token0+1);
   Token2=database.indexOf("|", Token1+1);
   Token3=database.indexOf("|", Token2+1);
   Token4=database.indexOf("|", Token3+1);
   Token5=database.indexOf("|", Token4+1);
   Token6=database.indexOf("|", Token5+1);
   Token7=database.indexOf("|", Token6+1);

   bill=new Array;
   bill[0]=database.substring (0, Token0);                   // Same info flag;
   bill[1]=database.substring (Token0+1, Token1);            // First name;
   bill[2]=database.substring (Token1+1, Token2);            // Middle name;
   bill[3]=database.substring (Token2+1, Token3);            // Last name;
   bill[4]=database.substring (Token3+1, Token4);            // Address line 1;
   bill[5]=database.substring (Token4+1, Token5);            // Address line 2;
   bill[6]=database.substring (Token5+1, Token6);            // City;
   bill[7]=database.substring (Token6+1, Token7);            // State;
   bill[8]=database.substring (Token7+1, database.length);   // Zip;

   if (ship[7] == "Texas")
   {
      return (1);
   }
   else
   {
      return (0);
   }
}

function delete_cookie (name, value, expires, path, domain, secure)
{
   // Removes a specific order cookie and decrements by 1 the total number of
   // orders in the basket_total_cookie.  If this reduces the total orders to
   // zero, then also delete the basket_total_cookie.

   // First, delete the order cookie;
   document.cookie=name+"="+
                   ((path) ? "; path=" + path : "")+
                   ((domain) ? "; domain=" + domain : "")+
                   "; expires=Thu, 01-Jan-70 00:00:01 GMT";

   // Second, re-sort the order cookies so they are sequentially numbered;

   // Find which cookie was deleted;
   var order_that_was_deleted=0;
   var total_orders=read_cookie('basket_total_cookie');

   for (i=1; i <= total_orders; i++)
   {
      current_order="order."+i;

      database="";
      database=read_cookie (current_order);

      // If it equals null then this is the one which was deleted;
      if (database == null)
      {
         order_that_was_deleted=i;
      }
   }

   // Now, renumber all the order cookies after that one;
   for (i=order_that_was_deleted+1; i <= total_orders; i++)
   {
      current_order="order."+i;
      new_order="order."+(i-1);
      database="";
      database=unescape(read_cookie (current_order));
      set_cookie (new_order, database);
   }

   // Delete the last order cookie;
   document.cookie=current_order+"="+
                   ((path) ? "; path=" + path : "")+
                   ((domain) ? "; domain=" + domain : "")+
                   "; expires=Thu, 01-Jan-70 00:00:01 GMT";

   // Next, update the total orders cookie;
   var old_total_orders=read_cookie('basket_total_cookie');
   total_orders=parseInt(old_total_orders)-1;
   set_cookie ("basket_total_cookie", total_orders);

   // Last, remove the basket cookie if the total orders now equal zero;
   if (total_orders <= 0)
   {
      document.cookie="basket_total_cookie="+
                      ((path) ? "; path=" + path : "")+
                      ((domain) ? "; domain=" + domain : "")+
                      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }

   // Finally, fill in the basket rows with the new order cookies;
   for (i=1; i <= total_orders; i++)
   {
      current_order="order."+i;
      database="";
      database=unescape(read_cookie (current_order));

      Token0=database.indexOf("|", 0);
      Token1=database.indexOf("|", Token0+1);
      Token2=database.indexOf("|", Token1+1);
      Token3=database.indexOf("|", Token2+1);
      Token4=database.indexOf("|", Token3+1);

      field=new Array;
      field[0]=database.substring (0, Token0);                   // name;
      field[1]=database.substring (Token0+1, Token1);            // color;
      field[2]=database.substring (Token1+1, Token2);            // size;
      field[3]=database.substring (Token2+1, Token3);            // quantity;
      field[4]=database.substring (Token3+1, database.length);   // price per;

      f=display_basket_item (field[0], field[1], field[2], field[3], i);
      subtotal=subtotal+f;
   }

   if (total_orders >= 1)
   {
      document.getElementById('basket_subtotal').innerHTML="Your current subtotal is "+addcommas(subtotal, 1)+".";
   }
}

function set_cookie (name, value, expires, path, domain, secure)
{
   document.cookie=name+"="+escape(value)+
                   ((expires) ? "; expires="+expires.toGMTString() : "") +
                   ((path) ? "; path="+path : "") +
                   ((domain) ? "; domain="+domain : "") +
                   ((secure) ? "; secure" : "");
}

function read_cookie (name)
{
   var nameEQ=name+"=";
   var ca=document.cookie.split (';');

   for (var i=0; i < ca.length; i++)
   {
      var c=ca[i];
      while (c.charAt(0) == ' ')
      {
         c=c.substring (1,c.length);
      }
      if (c.indexOf(nameEQ) == 0)
      {
         return c.substring (nameEQ.length, c.length);
      }
   }
   return null;
}

function update_cookies (name, color, size, quantity, price)
{
   var already_in_basket=0;
   var already_in_basket_num=0;
   var total_orders=0;
   var shop_str=name+"|"+color+"|"+size+"|"+quantity+"|"+price;
   var shop_str_qty="";
   var q=0;

   // Update the total number of orders currently in the basket;
   var old_total_orders=read_cookie('basket_total_cookie');

   // Cycle through all the order cookies and check if this specific product has
   // already been added to the basket (must equal all name, color, and size).
   if (old_total_orders != null)
   {
      for (i=1; i <= old_total_orders; i++)
      {
         Order="order."+i;
         database="";
         database=unescape(read_cookie (Order));

         Token0=database.indexOf("|", 0);
         Token1=database.indexOf("|", Token0+1);
         Token2=database.indexOf("|", Token1+1);
         Token3=database.indexOf("|", Token2+1);
         Token4=database.indexOf("|", Token3+1);

         field=new Array;
         field[0]=database.substring (0, Token0);                   // name;
         field[1]=database.substring (Token0+1, Token1);            // color;
         field[2]=database.substring (Token1+1, Token2);            // size;
         field[3]=database.substring (Token2+1, Token3);            // quantity;
         field[4]=database.substring (Token3+1, database.length);   // price per;

         // If the old three unique keys are the same as the new ones then only
         // update the quantity.
         if ((field[0] == name) && (field[1] == color) && (field[2] == size))
         {
            already_in_basket_num=i;
            already_in_basket=1;
            q=parseInt(field[3])+parseInt(quantity);
            shop_str_qty=name+"|"+color+"|"+size+"|"+q+"|"+price;
         }
      }
   }
   else
   {
      total_orders=1;
   }

   // If not already in the basket then add this as a new order.  Otherwise, just
   // update the quantity in the existing order cookie.
   if (already_in_basket == 0)
   {
      if (old_total_orders != null)
      {
         total_orders=parseInt(old_total_orders)+1;
      }
      set_cookie ("basket_total_cookie", total_orders);
      set_cookie ("order."+total_orders, shop_str);
   }
   else
   {
      set_cookie ("order."+already_in_basket_num, shop_str_qty);
   }
}

function remove_all_order_cookies ()
{
   // After submitting an order, delete all the cookies related to that order.
   // Note: This does not delete the receipt cookie, or the shipping and billing
   // info cookies.

   var total_orders=read_cookie('basket_total_cookie');

   for (i=1; i <= total_orders; i++)
   {
      current_order="order."+i;
      document.cookie=current_order+"=; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
   document.cookie="basket_total_cookie=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

