/* QwwJs_TableMgr
 * 
 * Part of the QlikWeb WorkBench Javascript Library (http://www.qlikwebworkbench.com)
 *
 * Copyright (c) 2007 Industrial CodeBox Ltd. All Rights Reserved.
 * http://www.industrialcodebox.com
 * Email support@industrialcodebox.com for licensing and support information.
 */
function QwwJs_ButtonMgr(cfgObj)
{
    var _isInitialized = false;

    this.lastButtonXml = "";
    
    function a(s)
    {
        QwwJs.Alert("QwwJs_TableMgr:" + s);    
    };
        
    if(!cfgObj){
        a("No cfgObject specified");
    }
    else{
        this.Cfg = cfgObj;
    }
        
    if(cfgObj.ObjectID){
        var objId = cfgObj.ObjectID;
    }
    else{
        a("No ObjectID specified");
        return;
    };
        
    this.colsOfResults = new Array();
    this.rowsOfResults = new Array();
    
    function getID(id)
    {   
        return AvqView + "." + cfgObj.ObjectID; 
    };

    function initialize() 
    {                                       
        var ID = getID();

        var btn = AvqSelect(ID);
                             
        if(btn == null)
        {        
            avqSet(ID, "add", "mode;action", true);
            return false;
        }
        
        _isInitialized = true;

        return true; // handshake - there will not be a new update
    };

    this.SendClick = function(isFinal)
    {
        if(isFinal == null)
            isFinal = true;
        
        QwwJs_ButtonMgr.SendClick(cfgObj.ObjectID, isFinal);
    };

        
    this.OnAvqUpdateComplete = function()
    {
        if (!_isInitialized) {            
            if (! initialize()) {
                return;
            }
        }
        
        var ID = getID();
                                
        var CH = AvqSelect(ID);
        
        if(CH == null){
            // This might be called as the result of an OnAvqUpdateComplete
            // which might be for an unrelated control, in which case
            // the XML wont contain that for XH
            return;
        }
        else{        
            //alert(CH.xml);
            var text = CH.getAttribute("text");
            var mode = CH.getAttribute("mode");
                                    
            if(this.Cfg.OnUpdate && this.lastButtonXml != CH.xml)
                this.Cfg.OnUpdate(text, mode);
        }
    };  
    
    if(qwwHub)  
        qwwHub.Register(this);
};

QwwJs_ButtonMgr.SendClick = function(buttonId, isFinal)
{    
    if(isFinal == null) 
        isFinal =  true;
    
    avqSet(AvqView + "." + buttonId, "action", "", isFinal);
};