//
// The following are the indexes into the Arrays containing information.
//
var  iBasicKyma         = 0;
var  iRackMountSub      = 1;
var  iRackMount         = 2;
var  iAudioExpansion    = 3;
var  iExpansionCard     = 4;
var  iMotorMix          = 5;
var  iFlightCase        = 6;
var  iCarryingCase      = 7;
var  iKymaXRevealed     = 8;
var  iKymaX             = 9;
var  iFireWireInterface = 10;
var  iAESCable          = 11;
var  iIntuos4x5         = 12;
var  iTauShirt			= 13;
var  iXShirt			= 14;

// New Products as of January 2009

var  iPacarana			= 15;
var  iFireWire400Sub	= 16;
var  iPaca				= 17;
var  iRackMountPaca		= 18;
var  iTBBrainCell		= 19;
var  iTBBrainBag		= 20;

// New Products as of July 2009

var  iCup				= 21;

var  iMax		= iCup + 1;

//
// Initialize the pricing information.
//
var  Pricing = new Array;
	Pricing [ iBasicKyma ]         = 3470;
	Pricing [ iRackMountSub ]      = 0;
	Pricing [ iRackMount ]         = 40;
	Pricing [ iAudioExpansion ]    = 995;
	Pricing [ iExpansionCard ]     = 595;
	Pricing [ iMotorMix ]          = 599;
	Pricing [ iFlightCase ]        = 300;
	Pricing [ iCarryingCase ]      = 75;
	Pricing [ iKymaXRevealed ]     = 35;
	Pricing [ iKymaX ]             = 175;
	Pricing [ iFireWireInterface ] = 495;
	Pricing [ iAESCable ]          = 16;
	Pricing [ iIntuos4x5 ]	       = 198;
	Pricing [ iTauShirt ]          = 8;
	Pricing [ iXShirt ]            = 8;

// Pricing on new products January 2009

	Pricing [ iPacarana ]			= 4402;
	Pricing [ iFireWire400Sub ]		= 0;
	Pricing [ iPaca ]				= 2970;
	Pricing [ iRackMountPaca ]		= 32;
	Pricing [ iTBBrainCell ]		= 60;
	Pricing [ iTBBrainBag ]			= 140;
	
// Pricing on new products July 2009

	Pricing [ iCup ]				= 7;


//
// Initialize the weight information.
//
var  Weights = new Array;
	Weights [ iBasicKyma ]         = 16;
	Weights [ iRackMountSub ]      = 0;
	Weights [ iRackMount ]         = 5;
	Weights [ iAudioExpansion ]    = 2;
	Weights [ iExpansionCard ]     = 1;
	Weights [ iMotorMix ]          = 10;
	Weights [ iFlightCase ]        = 31;
	Weights [ iCarryingCase ]      = 5;
	Weights [ iKymaXRevealed ]     = 4;
	Weights [ iKymaX ]             = 1;
	Weights [ iFireWireInterface ] = 4;
	Weights [ iAESCable ]          = 1;
	Weights [ iIntuos4x5 ]	       = 4;
	Weights [ iTauShirt ]          = 1;
	Weights [ iXShirt ]            = 1;

// Weights on new products January 2009
	Weights [ iPacarana ]			= 10;
	Weights [ iFireWire400Sub ]		= 0;
	Weights [ iPaca ]            	= 9;
	Weights [ iRackMountPaca ]     	= 1;
	Weights [ iTBBrainCell ]		= 2;
	Weights [ iTBBrainBag ]       	= 2;
	
// Weights on new products July 2009
	Weights [ iCup ]				= 1;


//
// Initialize the dimension information: include in system box (=1), length, width, height
//
var  Dims = new Array;
        Dims [ iBasicKyma ]         = new Array ( 0, 22, 22, 12 );
        Dims [ iRackMountSub ]      = new Array ( 1,  0,  0,  0 );
        Dims [ iRackMount ]         = new Array ( 1, 19,  9,  6 );
        Dims [ iAudioExpansion ]    = new Array ( 1,  9,  8,  6 );
        Dims [ iExpansionCard ]     = new Array ( 1, 13, 11,  3 );
        Dims [ iMotorMix ]          = new Array ( 0, 15, 13,  9 );
        Dims [ iFlightCase ]        = new Array ( 0, 24, 24, 12 );
        Dims [ iCarryingCase ]      = new Array ( 0, 22, 22,  9 );
        Dims [ iKymaXRevealed ]     = new Array ( 0, 13, 11,  3 );
        Dims [ iKymaX ]             = new Array ( 0, 10,  6,  1 );
        Dims [ iFireWireInterface ] = new Array ( 0, 13, 10,  3 );
        Dims [ iAESCable ]          = new Array ( 1, 13, 11,  3 );
		Dims [ iIntuos4x5 ]			= new Array ( 0, 14, 11,  3 );
        Dims [ iTauShirt ]          = new Array ( 0, 13, 11,  3 );
        Dims [ iXShirt ]            = new Array ( 0, 13, 11,  3 );

// Dimensions of new products January 2009
		Dims [ iPacarana ]			= new Array ( 0, 18, 12, 6 );
		Dims [ iFireWire400Sub ]    = new Array ( 1,  0,  0, 0 );
		Dims [ iPaca ]				= new Array ( 0, 18, 12, 6 );
		Dims [ iRackMountPaca ]	    = new Array ( 1, 12,  4, 4 );
		Dims [ iTBBrainCell ]	    = new Array ( 0, 18, 12, 6 );
		Dims [ iTBBrainBag ]	    = new Array ( 0, 18, 12, 6 );
		
// Dimensions of new products July 2009
		Dims [ iCup ]				= new Array ( 0,  6,  4, 4 );

//
// Initialize the description information
//
var  Description = new Array;
        Description [ iBasicKyma ]         = "Basic Kyma System";
        Description [ iRackMountSub ]      = "Substitute Rack Mount for Desktop";
        Description [ iRackMount ]         = "Capybara Rack Mounting Kit";
        Description [ iAudioExpansion ]    = "Four Channel Audio I/O Expansion";
        Description [ iExpansionCard ]     = "Dual-processor Expansion Card";
        Description [ iMotorMix ]          = "MotorMix Controller";
        Description [ iFlightCase ]        = "Capybara Flight Case";
        Description [ iCarryingCase ]      = "Capybara Padded Carrying Case";
        Description [ iKymaXRevealed ]     = "Kyma X Revealed! Book";
        Description [ iKymaX ]             = "Kyma X Software CD-ROM";
        Description [ iFireWireInterface ] = "Flame FireWire interface";
        Description [ iAESCable ]          = "AES to SPDIF Adapter";
		Description [ iIntuos4x5 ]		   = "Wacom Intuos3 4x5 Tablet";
        Description [ iTauShirt ]          = "Kyma Tau T-Shirt";
        Description [ iXShirt ]            = "Kyma X T-Shirt";

// Descriptions of new products January 2009
		Description [ iPacarana ]			= "Pacarana sound computer (FireWire 800 cable)";
		Description [ iFireWire400Sub ]		= "Substitute FireWire 400 cable";
		Description [ iPaca ]				= "Paca sound computer (FireWire 800 cable)";
		Description [ iRackMountPaca ]		= "Paca(rana) Rack Ears";
		Description [ iTBBrainCell ]		= "Tom Bihn BrainCell";
		Description [ iTBBrainBag ]			= "Tom Bihn BrainBag";
		
// Descriptions of new products July 2009
		Description [ iCup ]				= "Dancing Pacaranas Espresso Mug";


//
// A table associating form select fields with item indexes.
//
var  FormQuantityFields = new Array;
function AssociateQuantityFieldWithItem ( field, item ) { FormQuantityFields[item] = field; }
var  FormSubtotalFields = new Array;
function AssociateSubtotalFieldWithItem ( field, item ) { FormSubtotalFields[item] = field; }
var  FormAddressFields = new Array;
function AssociateAddressField ( field ) { FormAddressFields[FormAddressFields.length] = field; }

//
// The actual order information: an Array of pairs of item index and quantity.
// There should be no zero quantity items in this list.
//
var  Order = new Array;
var  ShippingAddress = "";
var  BillingAddress = "";
var  ShippingMethod = "";
var  PaymentMethod = "";
var  PaymentInfo = "";
var  SecurePaymentInfo = "";
var  International = "";
var  NeedsSalesTax = "";

//
// Some routines for the Order information.
//

//=================================================================================

function ComputeOrderTotal ( ) {
//
// Return the total of the items ordered.
//
   var shippingCost = 0;

   if ( ShippingMethod.length > 0 ) {
      shippingCost = 1 * ShippingMethod.substr ( ShippingMethod.indexOf ( "$" ) + 1, ShippingMethod.length );
      }

   return ( ComputeOrderSubtotal ( ) + ComputeOrderSalesTax ( ) + shippingCost );
   }

//=================================================================================

function ComputeOrderSalesTax ( ) {
//
// Return the sales tax on the items ordered.
//
   return ( NeedsSalesTax ? 0.01 * Math.round ( 7.75 * ComputeOrderSubtotal ( ) ) : 0 );
   }

//=================================================================================

function ComputeOrderSubtotal ( ) {
//
// Return the subtotal of the items ordered.
//
   var total = 0;

   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][0] == iExpansionCard && Order[i][1] > 3 ) {
         total = total + 0.9 * Pricing[Order[i][0]] * Order[i][1];
         }
      else {
         total = total + Pricing[Order[i][0]] * Order[i][1];
         }
      }
   return ( total );
   }

//=================================================================================

function ComputeOrderWeight ( ) {
//
// Return the weight of the items ordered.
//
   var total = 0;

   for ( i = 0; i < Order.length; ++i )  total = total + Weights[Order[i][0]] * Order[i][1];
   return ( total );
   }

//=================================================================================

function ComputeOrderDims ( ) {
//
// Return the dimensions of the items ordered.
//
   var total = new Array ( 0, 0, 0 );
   var includesSystem = 0;
   var volume = 0;

   // Set includesSystem to 1 if there is a system in the order.
   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][0] == iBasicKyma )
         includesSystem = 1;
      if ( Order[i][0] == iPacarana )
         includesSystem = 1;
      if ( Order[i][0] == iPaca )
         includesSystem = 1;
      }

   // Get the maximum length and width, but accumulate the height. Do not count any items
   // that can be included with a system.
   for ( i = 0; i < Order.length; ++i ) {
      dims = Dims[Order[i][0]];

      if ( dims[0] != 1 || includesSystem != 1 ) {
         total[0] = Math.max ( total[0], dims[1] );
         total[1] = Math.max ( total[1], dims[2] );
         total[2] = total[2] + dims[3] * Order[i][1];
         volume = volume + dims[1] * dims[2] * dims[3] * Order[i][1];
         }
      }

   if ( volume != 0 ) {
      total[2] = volume / ( total[0] * total[1] );
      }

   return ( total );
   }

//=================================================================================

function ComputeOrderDescriptionString ( ) {
//
// Return the description of the items ordered.
//
   var descr = "<br>";

   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][1] != 0 ) {
         if ( Order[i][0] == iExpansionCard && Order[i][1] > 3 ) {
            descr = descr + Description[Order[i][0]] + "<br>(includes 10% multiple card discount)" + "<br>";
            }
         else {
            descr = descr + Description[Order[i][0]] + "<br>" + "<br>";
            }
         }
      }

   return ( descr );
   }

//=================================================================================

function ComputeOrderQuantityString ( ) {
//
// Return the description of the items ordered.
//
   var descr = "<br>";

   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][1] != 0 )
         descr = descr + Order[i][1] + "<br>" + "<br>";
      }

   return ( descr );
   }

//=================================================================================

function ComputeOrderUnitPriceString ( ) {
//
// Return the description of the items ordered.
//
   var descr = "<br>";

   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][1] != 0 ) {
         if ( Order[i][0] == iExpansionCard && Order[i][1] > 3 ) {
            descr = descr + FormatAmount ( 0.9 * Pricing[Order[i][0]] ) + " <br>" + "<br>";
            }
         else {
            descr = descr + FormatAmount ( Pricing[Order[i][0]] ) + " <br>" + "<br>";
            }
         }
      }

   return ( descr );
   }

//=================================================================================

function ComputeOrderSubtotalString ( ) {
//
// Return the description of the items ordered.
//
   var descr = "<br>";

   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][1] != 0 ) {
         if ( Order[i][0] == iExpansionCard && Order[i][1] > 3 ) {
            descr = descr + FormatAmount ( 0.9 * Pricing[Order[i][0]] * Order[i][1] ) + " <br>" + "<br>";
            }
         else {
            descr = descr + FormatAmount ( Pricing[Order[i][0]] * Order[i][1] ) + " <br>" + "<br>";
            }
         }
      }

   return ( descr );
   }

//=================================================================================

function GetOrderFromFields ( ) {
//
// Read the item quantities from the list fields to create the order.
//

   Order = new Array;

   for ( i = 0; i < FormQuantityFields.length; ++i ) {
      var itemField = FormQuantityFields[i];

      if ( itemField ) {
         var itemCount = 1 * GetListValue ( itemField );
         if ( itemCount > 0 ) {
            Order [ Order.length ] = new Array ( i, itemCount );
            }

         if ( i == iExpansionCard && itemCount > 3 ) {
            FormSubtotalFields[i].value = FormatAmount ( 0.9 * itemCount * Pricing[i] );
            }
         else {
            FormSubtotalFields[i].value = FormatAmount ( itemCount * Pricing[i] );
            }
         }
      }
   }

//=================================================================================

function AddRackSubstitution ( ) {
//
// Add the rack substitution for this order.
//
  var newOrder = new Array;

   for ( i = 0; i < Order.length; ++i ) {
      newOrder[newOrder.length] = Order[i];
      if ( Order[i][0] == iBasicKyma ) {
         newOrder[newOrder.length] = new Array ( iRackMountSub, Order[i][1] );
         }
      }
   Order = newOrder;
   }

//=================================================================================

function Add400Substitution ( p1, p2 ) {
//
// Add the FireWire 400 substitution for this order.
//
  var newOrder = new Array;

   for ( i = 0; i < Order.length; ++i ) {
      newOrder[newOrder.length] = Order[i];
      if ( p1 == "1" && Order[i][0] == iPacarana ) {
         newOrder[newOrder.length] = new Array ( iFireWire400Sub, Order[i][1] );
         }
      if ( p2 == "1" && Order[i][0] == iPaca ) {
         newOrder[newOrder.length] = new Array ( iFireWire400Sub, Order[i][1] );
         }
      }
   Order = newOrder;
   }

//=================================================================================

function OrderHasSystem ( ) {
//
// Return true if the order has a system in it.
//
   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][0] == iBasicKyma )  return ( true );
      if ( Order[i][0] == iPacarana )   return ( true );
      if ( Order[i][0] == iPaca )       return ( true );
      }

   return ( false );
   }

//=================================================================================

function OrderIsManualOnly ( ) {
//
// Return true if the order has a system in it.
//
   return ( Order.length == 1 && Order[0][0] == iKymaXRevealed && Order[0][1] == 1 );
   }

//=================================================================================

function GetShippingAddressFromFields ( ) {
//
// Read the shipping address from the text fields.
//

   ShippingAddress = GetAddressFromFields ( );
   }

//=================================================================================

function GetBillingAddressFromFields ( ) {
//
// Read the shipping address from the text fields.
//

   BillingAddress = GetAddressFromFields ( );
   }

//=================================================================================

function GetAddressFromFields ( ) {
//
// Read the address from the text fields.
//

   addr = "";

   for ( i = 0; i < FormAddressFields.length; ++i ) {
      var itemField = FormAddressFields[i];

      if ( itemField && itemField.value.length > 0 ) {
         if ( itemField.value.charAt ( itemField.value.length - 1 ) == ":" ) {
            if ( FormAddressFields[i + 1].value.length > 0 )
               addr = addr + itemField.value + " ";
            }
         else if ( itemField.name == "s" ) {
            if ( FormAddressFields[i - 1].value.length > 0 && FormAddressFields[i + 1].value.length > 0 )
               addr = addr.substr ( 0, addr.length - 1 ) + ", ";
            }
         else if ( itemField.name == "z" ) {
            if ( FormAddressFields[i - 1].value.length > 0 && FormAddressFields[i + 1].value.length > 0 )
               addr = addr.substr ( 0, addr.length - 1 ) + " ";
            }
         else
            addr = addr + itemField.value + "\n";
         }
      }

   return ( addr.substr ( 0, addr.length - 1 ) );
   }

//=================================================================================

function GetPaymentInfoFromArgs ( cardType, cardNumber, expMonth, expYear ) {
//
// Read the payment information from the arguments.
//
   var len = cardNumber.length;
   var secureCC = cardNumber;

   for ( i = 0; i <= 9; ++i )  secureCC = Replace1 ( secureCC, "" + i, "X" );

   secureCC =
      len > 4
         ? secureCC.substr ( 0, len - 4) + cardNumber.substr ( len - 4, len )
         : secureCC;

   PaymentInfo = cardType + ": " + cardNumber + "  exp: " + expMonth + "/" + expYear;
   SecurePaymentInfo = cardType + ": " + secureCC + "  exp: " + "XX/" + expYear;

   if ( PaymentMethod != "Credit Card" ) {
      PaymentInfo = SecurePaymentInfo = PaymentMethod;
      }
   }

//=================================================================================

function GetLocationInfoFromArgs ( withinUS, withinIllinois ) {
//
// Read the location information from the arguments.
//
   International = withinUS ? 0 : 1;
   NeedsSalesTax = withinUS * withinIllinois ? 1 : 0;
   }

//=================================================================================

function UPSShippingMethod ( ) {
//
// Return the selected shipping method.
//

   if ( ShippingMethod.length == 0 )  return ( "To be arranged" );
   return ( ShippingMethod.substr ( 0, ShippingMethod.indexOf ( "$" ) - 1 ) );
   }

//=================================================================================

function UPSShippingCost ( ) {
//
// Return the selected shipping cost.
//

   if ( ShippingMethod.length == 0 )  return ( "To be arranged" );
   return ( ShippingMethod.substr ( ShippingMethod.indexOf ( "$" ) + 1, ShippingMethod.length ) );
   }

//=================================================================================

function OrderToText ( ) {
//
// Convert the current order into a string and return it.
//
   var s = "";

   for ( i = 0; i < Order.length; ++i ) {
      s = s + Order[i][0] + ":" + Order[i][1] + ";";
      }

   return ( s.substr ( 0, s.length - 1 ) + "|||" + ShippingAddress + "|||" + ShippingMethod + "|||" + BillingAddress + "|||" + PaymentMethod + "|||" + PaymentInfo + "|||" + SecurePaymentInfo + "|||" + International + "|||" + NeedsSalesTax );
   }

//=================================================================================

function TextToOrder ( s ) {
//
// Initialize the current order from the given string.
//
   if ( s.length == 0 )   return;

   var chunks = s.split ( "|||" );
   var lineItems = chunks[0].split ( ";" );

   ShippingAddress   = chunks[1];
   ShippingMethod    = chunks[2];
   BillingAddress    = chunks[3];
   PaymentMethod     = chunks[4];
   PaymentInfo       = chunks[5];
   SecurePaymentInfo = chunks[6];
   International     = chunks[7] * 1;
   NeedsSalesTax     = chunks[8] * 1;

   Order = new Array;
   for ( i = 0; i < lineItems.length; ++i ) {
      Order[i] = lineItems[i].split ( ":" );
      }
   }

//=================================================================================

function OrderToEmail ( ) {
//
// Convert the current order into a string appropriate for emailing.
//
   var s = "\n";

   s = s + "---------------------------------\n\n";
   s = s + "Order Date: " + DateStringToday ( ) + "\n";
   s = s + "Ship via:   " + UPSShippingMethod ( ) + "\n\n";
   s = s + "---------------------------------\n\n";
   s = s + "Ship to:\n\n" + ShippingAddress + "\n\n";
   s = s + "---------------------------------\n\n";
   s = s + "Bill to:\n\n" + BillingAddress + "\n\n";
   s = s + "---------------------------------\n\n";
   s = s + "Order:\n\n";

   for ( i = 0; i < Order.length; ++i ) {
      if ( Order[i][1] != 0 ) {

         // Quantity, Price Each, Subtotal, Description

         if ( Order[i][1] < 10 )  s = s + " ";
         s = s + Order[i][1] + "  ";

         if ( Order[i][0] == iExpansionCard && Order[i][1] > 3 ) {
            s = s + FormatAmount ( 0.9 * Pricing[Order[i][0]] ) + "  ";
            s = s + FormatAmount ( 0.9 * Pricing[Order[i][0]] * Order[i][1] ) + "  ";
            }
         else {
            s = s + FormatAmount ( Pricing[Order[i][0]] ) + "  ";
            s = s + FormatAmount ( Pricing[Order[i][0]] * Order[i][1] ) + "  ";
            }

         s = s + Description[Order[i][0]] + "\n";
         }
      }

   s = s + "======================================================================\n";
   s = s + "Subtotal      " + FormatAmount ( ComputeOrderSubtotal ( ) ) + "\n";
   s = s + "IL Sales Tax  " + FormatAmount ( ComputeOrderSalesTax ( ) ) + "\n";
   s = s + "Shipping      " + FormatAmount ( UPSShippingCost ( ) ) + "\n";
   s = s + "======================================================================\n";
   s = s + "Total         " + FormatAmount ( ComputeOrderTotal ( ) ) + "\n\n";

   s = s + "---------------------------------\n\n";

   s = s + "Payment by: " + PaymentInfo + "\n\n";

   s = s + "---------------------------------\n\n";


   return ( s );
   }

//=================================================================================
