//-----------------------------------------------------------------------
// Module name   : GtEcart
// Author        : Paul Battersby
// Creation Date : June 27/09
//  This provides Javasript client side support for the shopping cart
//  For example:
//
//  Dynamic shipping cost
//  =====================
//  This handles the updating of the shipping costs of the static shopping cart
//  when allowed to change dynamically by the shopping cart.
//
//  This uses GtEcar_ajax.php to handle the Ajax call
//
//  Usage:
//    var cart = new GtEcart();
//
//    <select id='shippingCostSelect' onchange='cart.setNewShippingCost(this)'>
//      <option value='CanPost - overnight,20.00'>Canada Post Overnight</option>
//      <option value='CanPost - 3 days,12.00'>Canada Post 3 days</option>
//    </select>
//
//             OR
//
//    cart.setNewShippingCost("Can Post - 3 days",12.00);
//
//  Popup balloon for CSC help
//  ==========================
//  This shows a tooltip balloon style dialog showing a user where to find the CVC
//  code on a credit cart.
//
//  Usage:
//    <script type="text/javascript" src="gtLib/GtTooltips/GtTooltips.js"></script>
//    <script type="text/javascript" src="gtLib/GtTooltips/GtTooltips_balloon.js"></script>
//    GtEcart.attachCscHelp("myElementOrId");
//
//    "myElementOrId" could be "qmark.gif" which is included in the GtEcart directory
//
//  $Log: gtEcart.js $
//  Revision 1.4  2010-04-14 09:02:34-04  Battersby
//  - added attachCscHelp
//
//  Revision 1.3  2010-04-07 14:14:15-04  Battersby
//  - now uses new naming convention
//
//  Revision 1.2  2010-04-05 20:04:00-04  Battersby
//  - corrected root path
//  - added GtEcart.confirmAddToGiftReg()
//
//  Revision 1.1  2009-10-21 10:21:14-04  Battersby
//  Initial revision
//
//------------------------------------------------------------------------

//---------------------------- INCLUDE FILES -----------------------------

var GtEcart = new Class({
  Implements: [Events, Options],

//----------------------------- CONSTANTS --------------------------------


//----------------------------- VARIABLES --------------------------------
  options : {

    // html path from the root to this file
    rootPath : "/gtLib/GtEcart",

    // function to be called when something is complete
    onComplete : Class.empty
  },

  ajaxCall : null, // used to make an ajax call for updating shipping costs

//----------------------------- FUNCTIONS --------------------------------

  //************************************************************************
  // Name   : setNewShippingCost
  //  This sets a new value for the shipping cost, which triggers an Ajax
  //  call to recalculate the taxes and the total cost in the shopping
  //  cart and in the form fields used by the payment gateway
  //
  //  (mixed) selectElementOrMethod - the select element which requires a comma serperated
  //                        value setting in each option tag like this:
  //
  //             <option value='CanPost - overnight,20.00'>CanPost - overnight = $20.00</option>
  //                                 OR
  //             "CanPost - overnight"
  //
  //   (float) shippingCost - the new shipping cost (only needed if not passing
  //                           the shipping cost through the select option)
  //
  // Returns : (nothing)
  //************************************************************************
  setNewShippingCost : function(selectElementOrMethod,shippingCost) {
    var value;

    // if this came from a select object, read the selected value
    if ($type(selectElementOrMethod) == "element") {
      // read the selected value
      value = selectElementOrMethod.options[selectElementOrMethod.options.selectedIndex].value;
      // break it into label and cost
      value = value.split(",");

      // set the shipping method and cost
      selectElementOrMethod = value[0];
      shippingCost = value[1];
    } // endif

    // update the shipping cost
    this.ajaxCall.post({
      newShippingMethod : selectElementOrMethod,
      newShippingCost : shippingCost
    });
  },

  //************************************************************************
  // Name   : (private) updateShipping
  //  This updates the specified elements and form fields with the given
  //  shipping and tax values
  //
  //  (array) updatable - an associative array of element id and form field
  //                      to be updated. See header in GtEcart_ajax.php
  //
  // Returns : (nothing)
  //************************************************************************
  _updateShipping : function(updatable) {
    // loop through the updatable fields and elements
    for (var index in updatable) {
      // if this is an element whose inner html is to be updated
      if (updatable[index]["type"] == "display") {
        $(index).innerHTML = updatable[index]["value"];

      // if this is a form field whose value property is to be updated
      } else {
        $(index).setProperty("value",updatable[index]["value"]);
      } // endif
    } // end for
  },

  //************************************************************************
  // Name   : initialize (constructor)
  //
  //  (object) options - (optional) configuration options. See options declaration
  //                     above for the available options
  //
  // Returns : (nothing)
  //*************************************************************************
  initialize : function(options) {

    this.setOptions(options);

    // create ajax call object
    this.ajaxCall = new Request.JSON({
      url : this.options.rootPath + "/GtEcart_ajax.php",
      onComplete : function(updatable) {
        this._updateShipping(updatable);
      }.bind(this)
    });
  }

});

//----------------------------- STATIC FUNCTIONS -----------------------------

//************************************************************************
// Name   : GtEcart.confirmAddToGiftReg
//  This asks the user to confirm the desire to add shopping cart contents
//  to the gift registry. If user clicks "ok" this will redirect the browser
//  to the given url
//
//  (string) processGiftRegUrl - location where the browser should go if
//                               the user wants to add the shopping cart
//                               to the gift registry
//
// Returns : (nothing)
//************************************************************************
GtEcart.confirmAddToGiftReg = function(processGiftRegUrl) {

  // if user wants to add shopping cart to gift registry
  if (confirm("Add shopping cart to gift registry?")) {
    window.location.href = processGiftRegUrl;
  } // endif
};


//************************************************************************
// Name   : attachCscHelp
// Author : Paul Battersby
//  This attaches a mouseover tooltip to the given element or id for the
//  purpose of showing a user how to find the CSC code on their credit card
//
//  (mixed) myElementOrId - the element or id to which the mouseover event
//                          is to apply
//
// Returns :
//  (nothing)
//*************************************************************************
GtEcart.attachCscHelp = function(myElementOrId)
{
  window.addEvent("domready",function() {
    // don't pass any parameters here so we don't interfere with
    // the balloon as used for form verification
    gtTooltipsInit();

    // create a div where the html content can be stored
    var helpHtml = new Element("div",{
      "id" : "gtEcart_cscHelp",
      "styles" : {
        "display" : "none"
      }
    });

    // add the element to the body and load the content
    $(document.body).adopt(helpHtml);
    helpHtml.load("gtLib/GtEcart/GtEcart_cscHelp.htm");

    // add events to target element
    $(myElementOrId).addEvents({
      "mouseover" : function() {
        TagToTip("gtEcart_cscHelp",BALLOON,true,ABOVE,true,BALLOONIMGPATH,"gtLib/GtTooltips/GtTooltips_balloonSimple");
      },

      "mouseout" : function() {
        UnTip();
      }
    });
  });
} // end of attachCscHelp


