/*------------------------------------------------------------------------------
|   Add Product to Cart
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
|   Initialize Global Variables
------------------------------------------------------------------------------*/

   var def_PriceZero               = "$0.00"      ;
   var def_CartDescWidth           = "25"         ;
   var def_ItemWeight              = "0.0"        ;
   var def_ItemClassification      = "NONE"       ;
   var def_ItemShippingModifier    = "NONE"       ;
   var def_ItemTaxable             = "YES"        ;
   var def_OptionName              = "itemOpt"    ;
   var def_DescName                = "itemXDesc"  ;

   var add_Debug                   = false        ;

   var add_ItemID                                 ;
   var add_ItemName                               ;
   var add_ItemDesc                               ;
   var add_ItemBasePrice                          ;
   var add_ItemWeight                             ;
   var add_ItemClassification                     ;
   var add_ItemShippingModifier                   ;
   var add_ItemTaxable                            ;
   var add_ItemQuantity                           ;
   var add_ItemPriceModifier                      ;

   var add_ItemCartDesc                           ;
   var add_ItemCartDescWidth                      ;

   var flt_ItemPriceModifier                      ;


/*------------------------------------------------------------------------------
|  Add Product to Cart
+-----------------------------------------------------------------------------*/
function googlecart_ComputePrice (thisForm, iSet)
{

/*
   if ( add_Debug ) googlecart_Debug(thisForm);
*/

   flt_ItemPriceModifier     = 0.00                                   ;
   add_ItemBasePrice         = thisForm.itemBasePrice.value           ;
   add_ItemTotalPrice        = def_PriceZero                          ;

   add_ProcessForm(thisForm, false) ;

   flt_ItemTotalPrice        = parseFloat(stripChar(add_ItemBasePrice,"$")) + flt_ItemPriceModifier;
   add_ItemTotalPrice        = formatDollar( flt_ItemTotalPrice, true ) ;

   if (iSet)
   {
      thisForm.itemCurrentPrice.value = add_ItemTotalPrice ;
   }

	
   return add_ItemTotalPrice ;

}

/*------------------------------------------------------------------------------
|  Initialization
+-----------------------------------------------------------------------------*/
function add_Initialization (thisForm)
{
   flt_ItemPriceModifier     = 0.00                                   ;

   add_ItemID                = thisForm.itemID.value                  ;
   add_ItemName              = thisForm.itemName.value                ;
   add_ItemDesc              = thisForm.itemDesc.value                ;
   add_ItemBasePrice         = thisForm.itemBasePrice.value           ;
   add_ItemQuantity          = thisForm.itemQuantity.value            ;

   add_ItemWeight            = getValue(thisForm.itemWeight          , def_ItemWeight           ) ;
   add_ItemClassification    = getValue(thisForm.itemClassification  , def_ItemClassification   ) ;
   add_ItemShippingModifier  = getValue(thisForm.itemShippingModifier, def_ItemShippingModifier ) ;
   add_ItemTaxable           = getValue(thisForm.itemTaxable         , def_ItemTaxable          ) ;

   add_ItemCartDescWidth     = getValue(thisForm.cartDescWidth       , def_CartDescWidth        ) ;


   add_ItemCartDesc          = buildDesc("" , add_ItemDesc , add_ItemCartDescWidth)   ;
}

/*------------------------------------------------------------------------------
|  Process Form Fields
+-----------------------------------------------------------------------------*/
function add_ProcessForm (thisForm, iBuildDesc)
{
   for ( i = 0 ; i < thisForm.length; i++ )
   {
      fld     = thisForm.elements[i];

      if ( fld.name.substr(0,7) == def_OptionName )
      {  process_FormFields (thisForm, iBuildDesc, fld); }


      if ( fld.name.substr(0,9) == def_DescName &&  fld.name.substr(fld.name.length-5,5) != "Label")
      {  process_Hidden   (thisForm, fld, iBuildDesc); }

   }          /* End For       */

}


/*------------------------------------------------------------------------------
|  Process Form Fields
+-----------------------------------------------------------------------------*/
function process_FormFields (thisForm, iBuildDesc, fld)
{
   /*------------------------------------------------------------------------
   |  Determine Form Field Type
   +-----------------------------------------------------------------------*/
   switch (fld.type)
   {
      /*---------------------------------------------------------------------
      |  The following field types are not processed
      +--------------------------------------------------------------------*/
      case "button"          :
         break;
      case "hidden"          :
         break;
      case "password"        :
         break;

      /*---------------------------------------------------------------------
      |  The following field types are processed
      +--------------------------------------------------------------------*/
      case "checkbox"        :
         process_CheckBox (thisForm, fld, iBuildDesc);
         break;

      case "radio"           :
         process_CheckBox (thisForm, fld, iBuildDesc);
         break;

      case "select-multiple" :
         process_Select   (thisForm, fld, iBuildDesc);
         break;

      case "select-one"      :
         process_Select   (thisForm, fld, iBuildDesc);
         break;

      case "text"                :
         process_Text     (thisForm, fld, iBuildDesc);
         break;

      case "textarea"            :
         process_Text     (thisForm, fld, iBuildDesc);
         break;

      default :
         break;

   }       /* End Switch    */

}


/*------------------------------------------------------------------------------
|  Process Form Fields
|  select-one
|  select-multiple
+-----------------------------------------------------------------------------*/
function process_Select ( thisForm , fld, iBuildDesc )
{
   tmp_PriceModifier = 0.00 ;
   if ( iBuildDesc)
   {
      tmp_Label = add_FindField(thisForm, fld, "Label", fld.name);
   }

   for ( j = 0 ; j < fld.length; j++)
   {
      if ( fld.options[j].selected )
      {
         tmp_Array             = getOption(fld.options[j].value) ;
         tmp_Value              = tmp_Array[0]          ;
         if (tmp_Array[1] != "")  add_ItemBasePrice = tmp_Array[1];
         flt_ItemPriceModifier += parseFloat(stripChar(tmp_Array[2],"$"))                ;

         if ( iBuildDesc)
         {
            add_DescEntry ( tmp_Label, tmp_Value ) ;
         }

      }
   }
}


/*------------------------------------------------------------------------------
|  Process Form Fields
|  hidden
+-----------------------------------------------------------------------------*/
function process_Hidden ( thisForm , fld, iBuildDesc )
{
   tmp_Value = spaceTrim(fld.value) ;
   if ( tmp_Value > " " )
   {
      tmp_Price              = add_FindField(thisForm, fld, "Price", def_PriceZero) ;
      flt_ItemPriceModifier += parseFloat(stripChar(tmp_Price,"$"))                 ;

      if ( iBuildDesc)
      {
         tmp_Label = add_FindField(thisForm, fld, "Label", fld.name);
         add_DescEntry ( tmp_Label, tmp_Value ) ;
      }
   }
}

/*------------------------------------------------------------------------------
|  Process Form Fields
|  text
+-----------------------------------------------------------------------------*/
function process_Text ( thisForm , fld, iBuildDesc )
{
   tmp_Value = spaceTrim(fld.value) ;
   if ( tmp_Value > " " )
   {
      tmp_Price              = add_FindField(thisForm, fld, "Price", def_PriceZero) ;
      flt_ItemPriceModifier += parseFloat(stripChar(tmp_Price,"$"))                 ;

      if ( iBuildDesc)
      {
         tmp_Label = add_FindField(thisForm, fld, "Label", fld.name);
         add_DescEntry ( tmp_Label, tmp_Value ) ;
      }
   }
}

/*------------------------------------------------------------------------------
|  Process Form Fields
|  checkbox
|  radio
+-----------------------------------------------------------------------------*/
function process_CheckBox ( thisForm , fld, iBuildDesc )
{
   if ( fld.checked )
   {
      tmp_Array              = getOption(fld.value) ;
      tmp_Value              = tmp_Array[0]          ;
      if (tmp_Array[1] != "")  add_ItemBasePrice = tmp_Array[1];
      flt_ItemPriceModifier += parseFloat(stripChar(tmp_Array[2],"$"))                ;

      if ( iBuildDesc)
      {
         tmp_Label = add_FindField(thisForm, fld, "Label", fld.name);
         add_DescEntry ( tmp_Label, tmp_Value ) ;
      }

   }
}

/*------------------------------------------------------------------------------
|  Add Description Entry
+-----------------------------------------------------------------------------*/
function add_DescEntry (iLabel , iValue )
{
   tmpDE_Label = spaceTrim(iLabel," ") + ": " ;
   tmpDE_Value = spaceTrim(iValue," ")        ;
   tmpDE_Desc  = tmpDE_Label + tmpDE_Value    ;

   add_ItemCartDesc = buildDesc(add_ItemCartDesc         ,
                                tmpDE_Desc               ,
                                add_ItemCartDescWidth)   ;

}

/*------------------------------------------------------------------------------
|  Find Form Field
+-----------------------------------------------------------------------------*/
function add_FindField (thisForm, iFld, iExtension, iDefault)
{
   tmpFF_Field = iFld.name + iExtension ;
   tmpFF_Value = iDefault  ;

   for ( x = 0 ; x < thisForm.length; x++ )
   {
      fldFF   = thisForm.elements[x];

      if ( fldFF.name == tmpFF_Field )
      {
         tmpFF_Value = fldFF.value ;
      }
   }

   return tmpFF_Value;
}

/*------------------------------------------------------------------------------
|  
+-----------------------------------------------------------------------------*/
function copy_TextArea ( thisForm , fldin, fldout )
{
	fldout.value = spaceTrim(fldin.value) ;
}





/*------------------------------------------------------------------------------
|  Google Cart - BeforeAdd
+-----------------------------------------------------------------------------*/
function googlecartBeforeAdd(item) {

    if ( add_Debug ) googlecart_Debug(thisForm);
    
    //Get all the attributes of the item added to cart        
    var attribs = item.getCustomAttributes();

    for (var i in attribs) {
        //Obtain attributes with values
        //and use attribute name: value for text value
        if (attribs[i] != "") {
            var attr_val = i.substr(2).toUpperCase() + ": " + attribs[i] + "          ";
            item.setCustomAttribute(i, attr_val);
        }
    }
    return true;
}

