//global var to hold all variants (one per product)
var shoppingcartbubble_variants_list = new Array();

//global var for variant ItemIndex
var shoppingcartbubble_varItemIndex = 0;

/**
 * @class ProductBubble_Class
 * This class handles operation with shopping cart like add to baga, retrive shopping cart, display etc..
 * for more details, look at its method
 * P.S: This class has not been quite finished as it needs some more methods in order to hanadle most of our needs
 * @constructor 
 */
 Core.ShoppingCartBubble_Class=function () {
    //Properties
     /**
     *  The main dhtml layer for the shopping cart
     */
    this.ShoppingCartBubble_DivID='ShoppingCartBubble_Div';
    //IDS
    /**
     *  A dhtml layer where subtotal will be displayed
     */
    this.ShoppingCartBubble_SubTotalID='ShoppingCartBubble_SubTotal';
     /**
     *  A dhtml layer where subtotal will be displayed on the header outside of the bubble
     */
   this.ShoppingCartBubble_SubTotalID_Header='ShoppingCartBubble_SubTotal_header';
    /**
     *  A dhtml layer where total will be displayed
     */
    this.ShoppingCartBubble_TotalID='ShoppingCartBubble_Total';
    /**
     *  A dhtml layer where tax will be displayed
     */
    this.ShoppingCartBubble_TaxTotalID='ShoppingCartBubble_TaxTotal';
    /**
     *  A dhtml layer where shipping subtotal will be displayed
     */
    this.ShoppingCartBubble_ShippingSubTotalID='ShoppingCartBubble_ShippingSubTotal';
    //
    /**
     * A dhtml layer where total items will be displayed
     */
    this.ShoppingCartBubble_TotalItemsID='ShoppingCartBubble_TotalItems'
    /**
     * A dhtml layer where total items will be displayed outside of the bubble
     */
    this.ShoppingCartBubble_TotalItemsID_Header_Outside='ShoppingCartBubble_TotalItems_header'
    /**
     *  success layer, not used yet
     */
    this.ShoppingCartBubble_SuccessID='ShoppingCartBubble_Success';
    /**
     *  failure layer not used yet
     */
    this.ShoppingCartBubble_FailureID='ShoppingCartBubble_Failure';

 
    /**
     * It holds the DHTML Object ID for product Name of last item added
     */
    this.ShoppingCartBubble_ProductNameID="ShoppingCartBubble_ProductName";  
     /**
     * It holds the DHTML Object ID for product Image of last item added
     */
    this.ShoppingCartBubble_ProductImageID="ShoppingCartBubble_ProductImage";  
    /**
     * It holds the DHTML Object ID for Variant Name of last item added
     */
    this.ShoppingCartBubble_VariantNameID="ShoppingCartBubble_VariantName";  
    /**
     * It holds the DHTML Object ID for item unit price of last item added
     */
    this.ShoppingCartBubble_ItemPriceID="ShoppingCartBubble_ItemPrice";      
    /**
     * It holds the DHTML Object ID for item quantity of last item added
     */
    this.ShoppingCartBubble_ItemQuantityID="ShoppingCartBubble_ItemQuantity"; 
    /**
     * It holds the DHTML Object ID for last item total example: UnitPrice * Quantity
     */
    this.ShoppingCartBubble_LastItemTotalAmountID="ShoppingCartBubble_LastItemTotalAmount";
     /**
     * It holds the DHTML Object ID for last item layer
     */
    this.ShoppingCartBubble_Last_Item_Added="ShoppingCartBubble_Last_Item_Added";
     /**
     * It holds the DHTML Object ID for last item layer as a stand alone object
     */
    this.ShoppingCartBubble_Last_Item_Added_Caption="ShoppingCartBubble_Last_Item_Added_Caption";
    /**
     *
     * Events
     */
     /**
     * the name of the function to be called before the popup opens
     */
    this.beforeOpen=null; 
    
    /**
     * the name of the function to be called before the popup closes
     */
    this.beforeClose=null;
    /**
     * Handles the callback from the web service AddSingleProductToCart
     * @param {object} result the data that comes into a JASON format from a web service     
     */
    this.AddSingleProductToCartResultHandler=function(result)  {
                
        DoDefault();
        if (result != null) {
            if (result.ActivityStatus==true) { 
                //Display The Shopping Cart
                //var _WebServiceAPI=new  WebServiceAPI_Class();
                _WebServiceAPI.RetrieveShoppingCart(GetCurrentCustomerID(),_ShoppingCartBubble_Class.RetrieveShoppingCartResultHandler);
            }
            else  { alert(PCSStringReplace(result.ErrorString)); }  
         }
    }

    /**
     * Handles the callback from the web service RetrieveShoppingCartLastItems
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.RetrieveShoppingCartLastItemsResultHandler=function (result) {
        DoDefault();
        if (result != null) {
            _ShoppingCartBubble_Class.DisplayShoppingCartAndLastItems(result);
        }
    }

    /**
     * Displays the shopping and last items
     */
    this.DisplayShoppingCartAndLastItems=function() {
        alert('this method is not finished yet, it is in progress');
        this.Show();
    }

    /**
     * Handles the callback from the web service RetrieveShoppingCart
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.RetrieveShoppingCartResultHandler=function (result) {
        DoDefault();
        if (result != null) {
            _ShoppingCartBubble_Class.DisplayShoppingCart(result);
        }
    }

    /**
     * Displays the shopping cart information
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.DisplayShoppingCart=function(result) {
        DoDefault();
        //display subtotal
         var obj=$get(this.ShoppingCartBubble_SubTotalID);
        if (obj!=null) {
            obj.innerHTML=result.SubTotal.toFixed(2);
            obj.innerHTML="$" + obj.innerHTML;
        }
        //populate header subtotal
        var obj_h=$get(this.ShoppingCartBubble_SubTotalID_Header);
        
        if (obj_h!=null) {
            obj_h.innerHTML=result.SubTotal.toFixed(2);
            obj_h.innerHTML="$" + obj_h.innerHTML;
        }
         
        //display total amount
        if ($get(this.ShoppingCartBubble_TotalID)!=null) {
            $get(this.ShoppingCartBubble_TotalID).innerHTML=result.Total.toFixed(2);
        }
        //display tax 
        if ($get(this.ShoppingCartBubble_TaxTotalID)!=null) {
            $get(this.ShoppingCartBubble_TaxTotalID).innerHTML=result.TaxTotal.toFixed(2);
        }
        //display shipping cost
        if ($get(this.ShoppingCartBubble_ShippingSubTotalID)!=null) {
         $get(this.ShoppingCartBubble_ShippingSubTotalID).innerHTML=result.ShippingSubTotal.toFixed(2);
        
        }
        //total items within bubble
        if ($get(this.ShoppingCartBubble_TotalItemsID)!=null) {
            $get(this.ShoppingCartBubble_TotalItemsID).innerHTML=result.Quantity;
        }
        //updating total items outside of bubble like header
         if ($get(this.ShoppingCartBubble_TotalItemsID_Header_Outside)!=null) {
            $get(this.ShoppingCartBubble_TotalItemsID_Header_Outside).innerHTML=result.Quantity;
        }
         this.DisplayShoppingCartLastItem(); //new
         this.Show();
    }

   /**
     * Displays the shopping cart information
     */
    this.DisplayShoppingCartLastItem=function() { //new method
        //product name
       if ($get(this.ShoppingCartBubble_ProductNameID)!=null) {
            $get(this.ShoppingCartBubble_ProductNameID).innerHTML=  _LastShoppingCartItem.ItemParentName;
        }
        
        var objimage=$get(this.ShoppingCartBubble_ProductImageID);
        if (objimage!=null) {
         objimage.src=IMAGES_RELATIVE_OR_ABSOLUTE_PATH + PRODUCT_SMALL_IMAGE_PREFIX  + _LastShoppingCartItem.ItemParentCode +  PRODUCT_SMALL_IMAGE_SUFFIX;
        }
        //variant name
        if ($get(this.ShoppingCartBubble_VariantNameID)!=null) {
            $get(this.ShoppingCartBubble_VariantNameID).innerHTML=  _LastShoppingCartItem.ItemName;
        }
        //variant price
        if ($get(this.ShoppingCartBubble_ItemPriceID)!=null) {
               $get(this.ShoppingCartBubble_ItemPriceID).innerHTML=  _LastShoppingCartItem.ItemPrice;//.toFixed(2);
        }
        //variant quantity
        if ($get( this.ShoppingCartBubble_ItemQuantityID)!=null) {
               $get( this.ShoppingCartBubble_ItemQuantityID).innerHTML=  _LastShoppingCartItem.ItemQuantity;
        }
     
        //total item for last item quantity * price
        if ($get( this.ShoppingCartBubble_LastItemTotalAmountID)!=null) {
                var _ItemTotalAmount=0
                _ItemTotalAmount=parseInt(_LastShoppingCartItem.ItemQuantity) * _LastShoppingCartItem.ItemPrice;
               $get( this.ShoppingCartBubble_LastItemTotalAmountID).innerHTML=  _ItemTotalAmount.toFixed(2);
        }
        
         if ($get( this.ShoppingCartBubble_Last_Item_Added)!=null) {
             $get( this.ShoppingCartBubble_Last_Item_Added).style.display="";
         }
         if ($get( this.ShoppingCartBubble_Last_Item_Added_Caption)!=null) {
            $get( this.ShoppingCartBubble_Last_Item_Added_Caption).style.display="";
         }
    
    
    }
    /**
     * Displays the shopping cart bubble
     */
    this.Show=function() {
        var obj=$get(this.ShoppingCartBubble_DivID);
        obj.style.visibility='visible';
        //obj.style.display='';
        // slideContent(this.ShoppingCartBubble_DivID.toString(),10);
          if (this.beforeOpen!=null) {
             eval(this.beforeOpen + "();");
        
        }
    }

    /**
     * Hides the shopping cart bubble
     */
    this.Hide=function() {
        var obj=$get(this.ShoppingCartBubble_DivID);
        obj.style.visibility='hidden';
        // obj.style.display='none';
        _QuickShop_Class.Hide();
        if (this.beforeClose!=null) {
             eval(this.beforeClose + "();");
        
        }
    }
    
}

 /**
 * It registers the ShoppingCartBubble_Class using registerClass method of MS AJAX Library
 */
 if (IsTypeDefined) { Core.ShoppingCartBubble_Class.registerClass('Core.ShoppingCartBubble_Class'); }


/**
 * @class ShoppingCartItem_Class
 * This class stores the information of the last item added to bag
 * @constructor 
 */
 Core.ShoppingCartItem_Class=function () {
    /**
     * Variant code of the item added to shopping cart
     */    
    this.ItemCode="";
    /**
     * Variant name
     */ 
    this.ItemName="";
    /**
     * Product code(parent product code of the variant)
     */ 
    this.ItemParentCode="";
    /**
     * Product Name(parent product name of the variant)
     */ 
    this.ItemParentName="";
    /**
     * quantity of the last add to bag
     */ 
    this.ItemQuantity=1;
    /**
     * item unit price
     */ 
    this.ItemPrice=0;
    
    /**
     * Populates the major data of the object
     @param {_ItemCode,_ItemName,_ItemParentCode,_ItemParentName,_ItemPrice} 
     */ 
    this.PopulateData=function(_ItemCode,_ItemName,_ItemParentCode,_ItemParentName,_ItemPrice){
                this.ItemCode=_ItemCode;
                this.ItemName=_ItemName;
                this.ItemParentCode=_ItemParentCode;
                this.ItemParentName=_ItemParentName;
                this.ItemPrice=_ItemPrice;
        
        }
}

/**
 * It registers the Core.ShoppingCartItem_Class using registerClass method of MS AJAX Library
 */
 if (IsTypeDefined) { Core.ShoppingCartItem_Class.registerClass('Core.ShoppingCartItem_Class'); }
 
  
/************************** Added for Shu Art of Hair ***********************/
/* The following functions have been added to support sIFR which doesn't execute on dynamic display
   of textual content. So DIV layers have been built for every product and we show and hide them
   accordingly.
   These functions are used when user clicks Add to Bag on Quick Shop Product Bubble and is used
   to display the items added to cart in a seperate shopping cart bubble.
*/    

//Hide or Show Added ShoppingCartItem in the shopping cart bubble
function HideShowShoppingBubbleCartAddedItem(variantCode, display)
{
    var productDivID = variantCode+"_SingleShoppingCartBubbleProduct_AddedDisplay_DIV";
    var productDivObj = $get(productDivID);
     if (productDivObj != null) 
     {  
         if(display==true)
         {
            productDivObj.style.display="";
            productDivObj.style.visibility="visible";
         }
         else
         {
            productDivObj.style.display="none";
            productDivObj.style.visibility="hidden";
         }
     }     
     
}

//Hide or show the Single Shopping Cart Error Messages 
function HideShowSingleShoppingCartBubbleErrorMsg(errorMsg,display)
{
    var SingleShoppingCartBubble_ErrorMsg_Div   = $get("SingleShoppingCartBubble_ErrorMsg_Div"); 
    var SingleShoppingCartBubble_ErrorMsg_Text  = $get("SingleShoppingCartBubble_ErrorMsg_Text"); 
  
    if(SingleShoppingCartBubble_ErrorMsg_Div != null)
    { 
                  
        if(display == true)
        {
           SingleShoppingCartBubble_ErrorMsg_Div.style.display="";
           SingleShoppingCartBubble_ErrorMsg_Div.style.visibility="visible";               
        }
        else
        {
           SingleShoppingCartBubble_ErrorMsg_Div.style.display="none";
           SingleShoppingCartBubble_ErrorMsg_Div.style.visibility="hidden";           
        }
        if(SingleShoppingCartBubble_ErrorMsg_Text!=null)
        {             
            SingleShoppingCartBubble_ErrorMsg_Text.innerHTML=errorMsg;
        }
    }
}

//Hide or show the "Added To Bag" section
function HideShowSingleShoppingCartAddedToBagSection(display)
{
    var  SingleShoppingCartBubble_AddedToBagSection_Div   = $get("ShoppingCartBubble_AddedToBagSection_Div"); 
      
    if(SingleShoppingCartBubble_AddedToBagSection_Div != null)
    {                   
        if(display == true)
        {
           SingleShoppingCartBubble_AddedToBagSection_Div.style.display="";
           SingleShoppingCartBubble_AddedToBagSection_Div.style.visibility="visible";               
        }
        else
        {
           SingleShoppingCartBubble_AddedToBagSection_Div.style.display="none";
           SingleShoppingCartBubble_AddedToBagSection_Div.style.visibility="hidden";
        }        
    }
}

//Hide or Show the Shopping Cart Bubble
function HideShowSingleShoppingCartBubble(display)
{
    var SingleShoppingCartBubble   = $get("ShoppingCartBubble_Div"); 
    
    if(SingleShoppingCartBubble != null)
    {           
         if(display==true)
         {
            SingleShoppingCartBubble.style.display="";
            SingleShoppingCartBubble.style.visibility="visible";
         }
         else
         {
            SingleShoppingCartBubble.style.display="none";
            SingleShoppingCartBubble.style.visibility="hidden";         
         }
    }
}

//Hide or Show the Shopping Cart Error Message [ DK ]
function HideShowSingleShoppingCartErrorMessage(strMessage)
{
    // alert('Error' + strMessage);
    var objError   = $get("SingleShoppingCartBubble_ErrorMsg_Div"); 
    
    if(objError != null)
    {           
         if(strMessage != '')
         {
            objError.style.display="block";
            objError.style.visibility="visible";
            objError.innerHTML = strMessage;
         }
         else
         {
            objError.style.display="none";
            objError.style.visibility="hidden";         
            objError.innerHTML = strMessage;
         }
    }
}

//update shopping cart bubble's product price and variant name
function UpdateShoppingCartProductInfo(variantCode)
{ 
   var ShoppingCartBubble_VariantNameStr =  variantCode + "_ShoppingCartBubble_VariantName";
   var ShoppingCartBubble_ItemQuantityStr = variantCode + "_ShoppingCartBubble_ItemQuantity";
   
   var ShoppingCartBubble_VariantNameObj  = $get(ShoppingCartBubble_VariantNameStr);
   var ShoppingCartBubble_ItemQuantityObj = $get(ShoppingCartBubble_ItemQuantityStr);   
  
   //We do not have to set the Variant Name as SHU has only 1 variant per Product
   /*if(ShoppingCartBubble_VariantNameObj != null)
   { 
        ShoppingCartBubble_VariantNameObj.innerHTML = selectedShadeName; 
   }*/
     
   var prdQty = _LastShoppingCartItem.ItemQuantity;   
      
   if(ShoppingCartBubble_ItemQuantityObj != null)
   { 
        ShoppingCartBubble_ItemQuantityObj.innerHTML = prdQty; 
   }                                    

}

//Display Shopping bag bubble div. 
//allProductErrorMessagesArray is an array of ProductErrorMessage Objs
function DisplayShoppingCartBubble()
{   
   var varCode = _LastShoppingCartItem.ItemCode;
      
   //Display the Shopping bag bubble based on Last Item added to bag SKU       
   HideShowShoppingBubbleCartAddedItem(varCode, false);
          
   //alert('ADDED VAR :' + varCode);     
   UpdateShoppingCartProductInfo(varCode);
   HideShowShoppingBubbleCartAddedItem(varCode, true);
   
   //Show the Added to Bag section
   HideShowSingleShoppingCartAddedToBagSection(true);
                
   //now show the shopping cart bubble
   HideShowSingleShoppingCartBubble(true);
        
}

//AddSingleProductOnComplete Handler - Modified for Art of Hair
function AddSingleProductToCartResultHandlerModified(result)  {
   
   DoDefault();
   try 
   {   
        if (result != null) 
        {
          if (result.ActivityStatus==true)          {
                
                DisplayShoppingCartBubble();
                HideShowSingleShoppingCartErrorMessage('');
                
                //display the shopping cart totals
                _WebServiceAPI.RetrieveShoppingCart(GetCurrentCustomerID(),_ShoppingCartBubble_Class.RetrieveShoppingCartResultHandler);
          }
          else  
          {
                var errorString = "";
                //alert(PCSStringReplace(result.ErrorString));
                
                // if (result.ErrorString.indexOf('The maximum amount of the shopping cart is reach')!=-1 &&  result.ErrorString.indexOf(':')!=-1)
                // {
                //     var error_array=result.ErrorString.split(':');
                //     alert(PCSStringReplace(result.ErrorString));
                //     //error_array[0]='The total amount of your order may not exceed $';
                //     // //alert (error_array[0] + formatAsMoney(error_array[1].replace(" ","")));
                //     //errorString = error_array[0] + formatAsMoney(error_array[1].replace(" ",""));
                // }
                // else 
                // {
                //     //alert(result.ErrorString);
                //     errorString = result.ErrorString;
                // }
                
                //Hide the Added to Bag section
                HideShowSingleShoppingCartAddedToBagSection(false);
                //Show the shopping cart bubble
                HideShowSingleShoppingCartBubble(true);
                //show the Error
                HideShowSingleShoppingCartErrorMessage(PCSStringReplace(result.ErrorString.toString()));
                
                
          }  
        }
        //reset value            
         
    }
    catch (err) 
    { 
        alert(err);
    }
                 
}

//AddSingleProductOnComplete Handler - Modified for Art of Hair for Basket Page ONLY
function AddSingleProductToCartResultHandlerModifiedForBasket(result)  {
   
   DoDefault();
   try 
   {   
        if (result != null) 
        {
          if (result.ActivityStatus==true)          {
                
                 //Redirect to Basket page
                 document.location.href = "index_basket_browse.aspx";
          }
          else  
          {
            alert(PCSStringReplace(result.ErrorString));
                //if (result.ErrorString.indexOf('The maximum amount of the shopping cart is reach')!=-1 &&  result.ErrorString.indexOf(':')!=-1)
                //{
                //    alert(PCSStringReplace(result.ErrorString));
                //    //var error_array=result.ErrorString.split(':');
                //    //error_array[0]='The total amount of your order may not exceed $';
                //    //alert (error_array[0] + formatAsMoney(error_array[1].replace(" ","")));
                //}
                //else 
                //{
                //    alert(result.ErrorString);
                //}
          }  
        }
        //reset value            
         
    }
    catch (err) 
    { 
        alert(err);
    }
                 
}

//Intializes the page
function ResetShoppingCartBubble()
{    
    var tempProducts=new Array();    
    tempProducts=shoppingcartbubble_variants_list;
        
    for(var i=0;i<tempProducts.length;i++)
    {   
        //alert('HIDING :' + tempProducts[i]); 
        //Hide all the ADDED TO BAG section shopping cart bubble items
        HideShowShoppingBubbleCartAddedItem(tempProducts[i], false);         
    } 
    
    //Hide the Added to Bag section
    HideShowSingleShoppingCartAddedToBagSection(false);
                
    //hide shopping cart bubble
    HideShowSingleShoppingCartBubble(false);
    
      
}
 

