/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004-2005 SumTotal Systems, Inc. All rights reserved.
//
// The copyright to the computer software herein is proprietary and remains
// the property of SumTotal Systems, Inc. It may be used and/or copied only with
// the written consent of SumTotal Systems, Inc. or in accordance with the terms and
// conditions stipulated in the agreement/contract under which this software
// has been supplied.
//
// $Id: SYS_Cart_Runtime.js,v 1.15 2005/07/01 14:52:00 sampatht Exp $
// $Date: 2005/07/01 14:52:00 $
// $RCSfile: SYS_Cart_Runtime.js,v $
// $Revision: 1.15 $
//
// Abstract:
//
//    Cart Operations : Runtime for Cart operations
//
// Revision History:
//
//    2003-10-07   Kaushik Raj    Created file
//
/////////////////////////////////////////////////////////////////////////////

/**
* JavaScript that supports the functionality for cart at HTML Page.
* This page should be included with the SYS_UTL_AspenCart file.
*/

var objCart                 = null;
var bWithinCode             = false;
var g_strType               = null;
var g_bCalledFromCheckAll   = false;
var g_bAlertedOverFlowOnce  = false;


//////////////////////////////////////////////////////////
// function that should be called with the XML that needs
// to set the cart items and also the checkbox name which
// needs to be set.
//////////////////////////////////////////////////////////

function Cart_OnPageLoad( strCartXML , strType )
{   
    // Remove any selections checked before onload 
    Cart_UnCheckAll ( strType );
    
    Cart_loadCartXml ( strCartXML );

    //update the graphic item in cart.
    Cart_SetSelectedItems_UI( strType );
    Cart_UpdateUICount();
    
    // set the maximum number of items that can be
    // added in the cart.
    
    objCart.SetCartMaxCount( I_CART_MAX_COUNT );   
    
    //store the value of strType for later use.
    g_strType = strType; 
}

//////////////////////////////////////////////////////////
//
// Function : Cart_loadCartXml
//
// Summary  : function that intializes the cart with the given XML.
//
//////////////////////////////////////////////////////////

function Cart_loadCartXml( strCartXML )
{
    //initialize the cart.
    objCart = new SYS_UTL_AspenCart();
                                
    if ((strCartXML !=null ) && ( strCartXML != "" ))
    {
        objCart.Set( strCartXML );
    }               
}

//////////////////////////////////////////////////////////
//
// Function : Cart_CheckClick
//
//////////////////////////////////////////////////////////

function Cart_CheckClick( objCheckbox , index )
{

    if ( objCart == null ) return ;
    if ( bWithinCode ) return ;
            
    var itemProperties = arrItemList[index];
            
    if ( objCheckbox.checked == true )
    {
        // check if the maximum for the cart has reached.
        // there will not be a radio with cart.

        if ( HasCartReachedLimit() )
        {
            // alert max reached once when called from check all
            // or alert everytime called from single click
            if ( !g_bCalledFromCheckAll ) 
            {
                alert( L_Cart_Max_Reached );
            }
            else
            {
                if ( !g_bAlertedOverFlowOnce ) 
                    alert( L_Cart_Max_Reached );
                
                //set the overflow as true
                g_bAlertedOverFlowOnce = true;
            }
            
            objCheckbox.checked = false;
                
            //fire the event.
            objCheckbox.onclick();
            return;
        }

        
        if ( itemProperties != null )
        {
            var ItemId = itemProperties[0];
                    
            var objCartItem = new SYS_UTL_CartItem( ItemId ); 
            var rgFields = objCart.GetCartItemProperties();
            
            for ( var i=0; i < rgFields.length; i++ )
            {
                objCartItem.AddProperty( rgFields[i] ,itemProperties[i+1]);
            }
                        
            objCart.AddItem( objCartItem );
        }
    }
    else
    {
        // get the id and then remove

        if ( itemProperties != null )
        {
            var ItemId = itemProperties[0];        
            objCart.Remove( ItemId );
        }
    }

                
    //update the graphic item in the cart
    if ( !g_bCalledFromCheckAll )
    {
        Cart_UpdateUICount();
    }    
    
}


///////////////////////////////////////////////////////
//
// Function : Cart_CheckClickAll
//
// Summary  : function that should be called when all 
//            the checkbox item is selected.
//
///////////////////////////////////////////////////////

function Cart_CheckClickAll( objCheckbox, strType )
{
    if ( objCart == null ) return ;
    if ( bWithinCode ) return;
    
    // set the global variable
    g_bCalledFromCheckAll = true;
    
    // get all the checkboxes with the given name.
    
    var arrCheckboxs = document.getElementsByName( strType );
    var i = 0;

    var arrNeedsToClicked = new Array();
    
    for ( i=0; i < arrCheckboxs.length; i++ )
    {                  

        if ( (!arrCheckboxs[i].disabled) && (!arrCheckboxs[i].checked) )
        {
            //fire the event
            arrCheckboxs[i].checked = true;
            arrNeedsToClicked[ arrNeedsToClicked.length ] = i;
        }
        else
        {
            // element is added. check if was already in cart.
            var itemProperties = arrItemList[i];
            var ItemId         = itemProperties[0];
            
            if ( (!arrCheckboxs[i].disabled) && ( !objCart.Exists(ItemId)) )
            {          
                arrCheckboxs[i].checked = false; 
                arrNeedsToClicked[ arrNeedsToClicked.length ] = i;
            }   
        }

    }//:~for

    for( i=0; i < arrNeedsToClicked.length; i++ )
    {
        arrCheckboxs[ arrNeedsToClicked[i] ].click();
    }
    
    // reset the flags
    g_bCalledFromCheckAll  = false;
    g_bAlertedOverFlowOnce = false;
    
    
    //update the graphic item in the cart
    Cart_UpdateUICount();
}


//////////////////////////////////////////////////////////
//
// Function : Cart_SetSelectedItems_UI
//
// Summary  : Will automatically highlight the rows which are
//            already selected in the cart.
//
//////////////////////////////////////////////////////////

function Cart_SetSelectedItems_UI( strType )
{
    var arrIds = objCart.GetIds();
    
    // get all the checkboxes in the page.
    var arrCheckboxs = document.getElementsByName( strType );
    
    bWithinCode = true;
                
    for ( var i =0; i <arrIds.length; i++ )
    {
                    
        for ( var j=0; j < arrCheckboxs.length ; j++ )
        {
             //  if ( arrCheckboxs[j].value == arrIds[i] )
        
            // In list view scenarios, the selected checkbox is getting unselected. This condition 
            // helps in preventing the unselection.
            if ( arrCheckboxs[j].value == arrIds[i] && ! arrCheckboxs[j].checked )
            {
               arrCheckboxs[j].click();
            }
        }
        
    }
    
    bWithinCode = false;
    
}
    
//////////////////////////////////////////////////////////
//
// function that will update the count on the cart in HTML
// display.
//
//////////////////////////////////////////////////////////
           
function Cart_UpdateUICount()
{
    // get all the elements.
    
    for ( var i=0 ; i < iCartOnPage; i++ )
        document.getElementById("idCartCountLabel_" + i ).innerHTML = objCart.Count() ;
    
}

//////////////////////////////////////////////////////////
//
// Function : Cart_ModalAttributes
//
// Summary  : function that return attributes for the modal window
//
//////////////////////////////////////////////////////////

function Cart_ModalAttributes()
{
    return "dialogHeight: " + Cart_ModalAttributes_Height() + "px; dialogWidth: " + Cart_ModalAttributes_Width() + "px; center:yes; " + "resizable:no; scroll:auto; help:no; status:no;";
}

//////////////////////////////////////////////////////////
//
// Function : Cart_ModalAttributes_Width
//
// Summary  : function that returns width attribute for the modal window
//
//////////////////////////////////////////////////////////

function Cart_ModalAttributes_Width()
{
    return 450;
}

//////////////////////////////////////////////////////////
//
// Function : Cart_ModalAttributes_Height
//
// Summary  : function that returns height attribute for the modal window
//
//////////////////////////////////////////////////////////

function Cart_ModalAttributes_Height()
{
    return 580;
}

//////////////////////////////////////////////////////////
//
// Function : Cart_ReviewPage
//
// Summary  : function that will review the cart items.
//
//              The last argument helps in picking up the 
//              USERMODE corresponding CSS file in the
//              cart view page
//
//            return a string to indicate whether ModalDialogWindow is needed
//////////////////////////////////////////////////////////

function Cart_ReviewPage( bWithinCode , bItemDeleted , szOrder , iCol, szReqUserMode )
{
    var strModal = "";

    if ( objCart == null ) return strModal;

    // intialize the parameters.
    if ( bWithinCode == null ) bWithinCode = false;
    if ( bItemDeleted == null ) bItemDeleted = false;
    if ( szOrder == null ) szOrder = "A";
    if ( iCol == null) iCol = 1;
    
    if ( !bWithinCode && (objCart.Count() == 0) )
    {
        alert( L_Cart_NoItemInCart );
    }
    else
    {

        var strWindowName = L_Cart_Window_ReviewName;
        var strTitle      = L_Cart_Title;

        
        var szDeleted = "false";
        if ( bItemDeleted ) szDeleted = "true";
        
        // This following statement is modified to show USERMODE corresponding CSS file in the cart view page
        // Further modified by Nidhi on 05/19/2004 
        // Added the title in the querystring to avoid the hardcoding of title in the modal dialogue window.
        strModal = "?deleted=" + szDeleted 
                         +"&order=" + szOrder + "&col=" + iCol 
                         + "&UserMode=" + szReqUserMode 
                         + "&Title=" + encodeURIComponent(strTitle);
        
    }

    return strModal; 

}

///////////////////////////////////////////////////////////////////////
// Function     : HasCartReachedLimit
//
// Summary      : Tells whether the cart has reached its maximum limit
//
// Parameters   : N/A
//
///////////////////////////////////////////////////////////////////////

function HasCartReachedLimit()
{
    if ( objCart == null ) return true ;
    if ( ( objCart.Count() == objCart.GetCartMaxCount() ) && !bWithinCode ) return true;
    return false;
}

///////////////////////////////////////////////////////////////////////
//
// Function     : Cart_GetCount()
//
// Summary      : Gets the cart Count
//                  This function is called from cart review page.
//
// Parameters   : N/A
//
///////////////////////////////////////////////////////////////////////

function Cart_GetCount()
{
    if ( objCart == null ) return 0;
    return objCart.Count();
}

///////////////////////////////////////////////////////////////////////
//
// Function     : Cart_RemoveItems ( strList )
//
// Summary      : Removes the item from the cart and refreshes the view.
//                This function is called from Cart re-view page.
//
// Parameters   : strList - The list of items that needs to be removed.
// 
//////////////////////////////////////////////////////////////////////

function Cart_RemoveItems( strList )
{
    if ( objCart == null ) return ;
    
    var arrIds = strList.split(",");
     
    for ( var i =0; i< arrIds.length; i++ )
    {
       objCart.Remove( arrIds[i] );
    }
    
    // get all the checkboxes in the page.
    var arrCheckboxs = document.getElementsByName( g_strType );
    
    bWithinCode = true;
                
    for ( var i =0; i < arrIds.length; i++ )
    {
                    
        for ( var j=0; j < arrCheckboxs.length ; j++ )
        {
            if ( arrCheckboxs[j].value == arrIds[i] )
            {
                arrCheckboxs[j].click();
            }
        }
        
    }
    
    bWithinCode = false;
    
    //update the count
    Cart_UpdateUICount();
}

////////////////////////////////////////////////////////////////////
//
// Summary : Function that sets the values in the form to submit
//           for successful creation of cart at posted page.
//
///////////////////////////////////////////////////////////////////

function Cart_SetCartFormValue( objForm )
{   
    
    if ( objCart == null ) return ;
    // set the value
    objForm.xmlDocument.value = objCart.XML();
}


////////////////////////////////////////////////////////////////////
//
// Summary : Removes all items from cart.
//
///////////////////////////////////////////////////////////////////
function Cart_RemoveAll()
{
    if ( objCart == null ) return ;
    if ( objCart.Count() > 0 )
    {
        objCart.RemoveAll();
        Cart_UpdateUICount();
    }
}


///////////////////////////////////////////////////////////////////////
//
// Function     : Cart_UnCheckAll ( strType )
//
// Summary      : Removes the selection for all the checked items in the page.
//                This function is called from Cart_OnPageLoad.
//
// 
//////////////////////////////////////////////////////////////////////

function Cart_UnCheckAll( strType )
{
    // get all the checkboxes in the page.
    var arrCheckboxs = document.getElementsByName( strType );
                
    for ( var i =0; i < arrCheckboxs.length; i++ )
    {
        if ( arrCheckboxs[i].checked )
        {
            arrCheckboxs[i].click(); // Calls the onclick event to uncheck.
        }
    }
}


