/* 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_TableMgr(cfgObj)
{
    var _isInitialized = false;

    function a(s)
    {
        alert("QwwJs_TableMgr:" + s);    
    };
        
    if(!cfgObj)
        a("No cfgObject specified");
    
    var pageSize = 50;
    
    if(cfgObj.PageSize)
        pageSize = cfgObj.PageSize;
    
    if(cfgObj.ObjectID){
        var objId = cfgObj.ObjectID;
    }
    else{
        a("No objectId specified");
        return;
    };
    
    if(cfgObj.ColumnIndexes)
    {
        var arrOfColumns = cfgObj.ColumnIndexes;
    }
    else{
        a("No arrayOfColumns specified");
        return;
    };
    
    this.colsOfResults = new Array();
    this.rowsOfResults = new Array();
    
    function getID(id)
    {   
        return AvqView + "." + objId; 
    };

    this.Initialise = function()
    {
        initialize();
    };
    
    function initialize() 
    {                                       
        var ID = getID();

        var box = AvqSelect(ID);
                             
        if(box == null)
        {        
            avqSet(ID, "add", "mode;pageoffset;pagesize;totalsize", false);
            
            for(var i = 0; i < arrOfColumns.length; i++)
            {
                avqSet(ID + ".C" + arrOfColumns[i], "add", "mode;text;value", false);
            }
            
            avqSet(ID, "pagesize", pageSize + 1, false);
            avqSet(ID, "pageoffset", 0, true);
    
            return false;
        }
        
        _isInitialized = true;

        return true; // handshake - there will not be a new update
    };

    this.OnUpdate = null;
    this.OnUpdateArgs = null;

    this.RegisterUpdatedCallBack = function(method, args)
    {
        this.OnUpdate = method;
        this.OnUpdateArgs = args;
    };      

    this.SelectSingleRecord = function(recordNumber, selectColumn)
    {
        var ID = getID();
        
        if(!selectColumn) selectColumn = 0;
        
        avqSet(ID, "rect", selectColumn + ":" + recordNumber + ":1:1", true);
    };

    this.TotalSize = -1;
    this.PageOffset = -1;
    this.PageSize = -1;
    this.NoRows = -1;
    this.NoPages = -1;
    this.HeaderRowPresent = false;
    this.CurrentPage = 1;
    this.InitialTotalSize = -1;
    this.InitialNoPages = -1;
    
    this.SetPage = function(pageNumber)
    {
        var ID = getID();
        
        var newOffset;
        
        this.CurrentPage = pageNumber;
        
        if(pageNumber == 1){
            avqSet(ID, "pagesize", pageSize + 1, false);
            newOffset = 0;
        }
        else{
            avqSet(ID, "pagesize", pageSize, false);
            newOffset = ((pageNumber*1 - 1) * this.PageSize) + 1;
        }
                
        avqSet(ID, "pageoffset", newOffset, true);
    };
    
    this.PageUp = function()
    {
        if(this.CurrentPage < this.NoPages)
        {
            this.SetPage(this.CurrentPage + 1);
        }
    };

    this.PageDown = function()
    {
        if(this.CurrentPage > 1)
        {
            this.SetPage(this.CurrentPage - 1);
        }
    };

    this.GetRow = function(rowNumber, parseAdditionalAllAttributed)
    {
        var req = rowNumber;
        
        if(this.rowsOfResults[rowNumber] == null)
        {
        
            var row = {};
                            
            row.RecordNumber = (1*this.PageOffset) + (1*rowNumber);
            
            var rowToUse = rowNumber;
            
            if(this.HeaderRowPresent == true)
            {
                row.RecordNumber++;
                rowToUse++;
            }
        
            //alert("rowNumber " + req + " requested, actual = " + rowToUse);
                        
            for(var i = 0; i < this.colsOfResults.length; i++)
            {
                var colOfResults = this.colsOfResults[i];
                var rowItem = {};
                
                if(rowToUse >= colOfResults.length)
                {
                    //alert("colsOfResults.length = " + this.colsOfResults.length + ", rowToUse = " + rowToUse);

                    //alert("rowToUse >= colOfResults.length, setting dummy data");
                    rowItem.Text = "No data for record " + rowToUse;
                }
                else
                {                
                    var elem = colOfResults[rowToUse];
                    
                    if(!row.IsHeader)
                    {
                        var boolIsHeader = elem.getAttribute("isheader") == "true";
                        row.IsHeader = boolIsHeader;
                    }
                    
                    rowItem.Text = elem.getAttribute("text");
                    
                    //alert("rowItem.Text=" + rowItem.Text + ",row.IsHeader=" + row.IsHeader);
                    
                    if(parseAdditionalAllAttributed)
                    {
                        rowItem.Style = elem.getAttribute("style");
                        rowItem.IsNumber = elem.getAttribute("isnum");
                        rowItem.SelectType = elem.getAttribute("selecttype");
                    }
                }
                
                row[i] = rowItem;
            }
            
            this.rowsOfResults[rowNumber] = row;
        }
        
        return this.rowsOfResults[rowNumber];
    };
        
        
    function x(){}
    
    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 table or lisbox, in which case
            // the XML wont contain that for XH
            return;
        }
        else{        
            //alert(CH.xml);
                        
            this.colsOfResults = new Array();
            this.rowsOfResults = new Array();
                                           
            for(var i = 0; i < arrOfColumns.length; i++)
            {
                var col = arrOfColumns[i];
                
                //var pattern = "value[attribute('name')='C" + col + "']/element";
                var pattern = "value[@name='C" + col + "']/element";
                
                var vals = CH.selectNodes(pattern);
                
                this.colsOfResults[i] = vals;
            }
            
            this.NoRows = this.colsOfResults[0].length;
            
            this.PageSize = CH.getAttribute("pagesize");
            
            if(this.NoRows > 0)
            {   
                if(this.colsOfResults[0][0].getAttribute("isheader") == "true"){
                    this.NoRows--;
                    this.PageSize--;
                    this.HeaderRowPresent = true;
                }
                else{
                    this.HeaderRowPresent = false;
                }
            }
            else
            {
                this.HeaderRowPresent = false;
            }
            
            this.TotalSize = CH.getAttribute("totalsize") - 1 // this will include the header;
            this.PageOffset = CH.getAttribute("pageoffset");
    
            if(this.InitialTotalSize == -1)
                this.InitialTotalSize = this.TotalSize;

            this.NoPages = this.TotalSize / this.PageSize;
            this.NoPages = Math.round(this.NoPages + 0.5);

            if(this.InitialNoPages == -1)
                this.InitialNoPages = this.NoPages;
            
            //alert("this.HeaderRowPresent=" + this.HeaderRowPresent + ",this.TotalSize=" + this.TotalSize + ",this.PageOffset=" + this.PageOffset + ",this.InitialTotalSize=" + this.InitialTotalSize + ",this.NoPages=" + this.NoPages + ",this.InitialNoPages=" + this.InitialNoPages);
            
            if(this.OnUpdate)
                this.OnUpdate(this.OnUpdateArgs);
        }
    };  
          
    if(qwwHub)  
        qwwHub.Register(this);
};