﻿// JScript File

function SAGrid()
{
    this.tableid = "";
    this.parentid = "";
    this.operation = "";
    this.row = "";
    this.column = null;
    this.flexioption = null;
    this.pagesize = 10;
    this.pagenumber = 1;
    this.total = 0;
    this.usedefaultbuttons = true;
    this.usedefaultsearch = true;
    this.resourcesfolder = "Resources/";
    this.mode = "all";
    this.searchterm = "";
    
    //tableids: Table ID in DOM
    //operations: Data to be sent with AJAX request (object literal)
    //rows: the row name
    //columns: the field names
    //flexioptions: flexioptions in object literal format
    //pagesizes: page size
    //pagenumbers: page number
    this.load = function (settings)
    {
        //store the data;
        this.tableid = settings.tableid;
        this.operation = settings.operation;
        this.row = settings.row;
        this.column = settings.columns;
        this.flexioption = settings.flexioptions;
        this.pagesize = settings.pagesize;
        this.pagenumber = settings.pagenumber;
        //add pagesize and pagenumber to the data to be sent to server
        this.operation["pagesize"] = this.pagesize;
        this.operation["pagenumber"] = this.pagenumber;
        //show loader
        this.toggleprogress(true);
        //get the data
        SAjax.fromData(this.operation, this);                     
    };
    this.reload = function (settings)
    {
        if(settings!=null)
        {
            this.pagesize = settings.pagesize;
            this.pagenumber = settings.pagenumber;
        }else
        {
            this.pagesize = 10;
            this.pagenumber = 1;
        }
        this.operation["pagesize"] = this.pagesize;
        this.operation["pagenumber"] = this.pagenumber;
        //reload datas
        this.toggleprogress(true);
        SAjax.fromData(this.operation, this);
    };
    
    /* INITIAL CALLBACKS */
    this.handle = function(data, textStatus)
    {        
        var status = $(data).find("response").attr("status");
        var totals = $(data).find("response").attr("total");
        var feedback = $(data).find("response").text();
        if(status=="1")
        {
            //validate page
            if(this.pagesize==0)
            {
                this.pagesize=5;
            }
            if(this.pagenumber==0)
            {
                this.pagenumber=1;
            }
            this.total = totals;
            this.lastpage = Math.ceil(this.total/this.pagesize);
            this.populate(data);            
        }else
        {        
            this.clear();   
            $("#"+this.tableid).after("<div class='"+this.tableid+"_feedback'>"+feedback+"</div>");
        }
        //$("#"+this.tableid).after("Network problem encountered.");
    };    
    
    /* TABLE BUILDING ACTIONS */
    this.populate = function (doto)
    {             
        this.clear();
        var current = $("#"+this.tableid);
        var parent = $("#"+this.tableid+"_parent");
        var flexi = parent.find(".flexigrid");        
        var writer = "";
        var self = this;                   
        if($(doto).find(this.row).length>0)
        {        
            //if navigation isn't there, prepend it
            if(this.usedefaultbuttons && parent.find("#"+this.tableid+"_nav").length<1)
            {
                parent.append("<div id='"+this.tableid+"_nav'><a id='"+this.tableid+"_first'><img src='"+this.resourcesfolder+"resultset_first.png' /></a> <a id='"+this.tableid+"_prev'><img src='"+this.resourcesfolder+"resultset_previous.png' /></a> <span id='"+this.tableid+"_paging' class='tbl_msg_lbl'></span> <a id='"+this.tableid+"_next'><img src='"+this.resourcesfolder+"resultset_next.png' /></a> <a id='"+this.tableid+"_last'><img src='"+this.resourcesfolder+"resultset_last.png' /></a><span id='"+this.tableid+"_progress'><img src='"+this.resourcesfolder+"resultset_loader.gif' width='185' height'16' /></span><span class='tbl_msg_lbl' id='"+this.tableid+"_message'></span></div>");
                //format the nav bar
                $("#"+this.tableid+"_nav").css({background:'#dedede', padding:'3px', fontWeight:'bold'}).find("img").css({verticalAlign:'middle', border:'0'}).end().find("a").css({padding:'3px', border:'0'}).siblings("span").css({padding:'3px', width:'100px'}).filter(".tbl_msg_lbl").css({background:'#fff'});
                //set actions and initiate paging
                this.setclicks();
                this.setpaging();
            }        
            parent.append("<table id='"+this.tableid+"' class='tbb'></table>");   
                      
            $(doto).find(this.row).each(
                function()
                {
                    writer += "<tr>"; 
                    var id = $(this).attr("id");
                    var curRow = $(this);
                    jQuery.each(
                        self.column,
                        function()
                        {
                            var tp = "";
                            if(this.columnName=="id")
                            {   
                               tp = id;
                            }else if(this.columnName.length>0)
                            {
                               tp = curRow.find(this.columnName).text();  
                            }
                            var jp = this.columnFormat;           
                            if(this.columnType=="image")
                            {                            
                                jp = jp.replace(/iival/gi, tp);
                                tp = "<img src='"+jp+"' />";
                            }else if(this.columnType=="link")
                            {
                                jp = jp.replace(/iival/gi, tp);
                                tp = "<a href='"+jp+"' alt='"+jp+"'>"+tp+"</a>";
                            }else if(this.columnType=="custom")
                            {
                                jp = jp.replace(/iival/gi, tp);
                                tp = jp;
                            }
                            tp = tp.replace(/iiid/gi, id);
                            writer += "<td>"+tp+"</td>";
                        }
                    );             
                    writer += "</tr>";                
                }
            );         
            //if empty draw dash
            $("#"+this.tableid).append(writer);   
            $("#"+this.tableid).flexigrid(this.flexioption);      
        }else
        {
            $("#"+this.tableid).after("<div id='"+this.tableid+"_feedback'>No entry.</div>");
        }
        
        this.toggleprogress(false);
    };
    this.clear = function()
    {
        var current = $("#"+this.tableid);
        var parent = $("#"+this.tableid+"_parent");
        if(parent.length<1)
        {
            current.wrapAll("<div id='"+this.tableid+"_parent'></div>");
        }
        parent = $("#"+this.tableid+"_parent");              
        parent.find(".flexigrid").remove(); 
        $("."+this.tableid+"_feedback").remove();    
        $("#"+this.tableid+"_nav").remove();
    };
    /* BUTTON NAVIGATION ACTIONS */
    this.gotofirstpage = function()
    {
        if(this.pagenumber!=1)
        {
            this.pagenumber = 1;
            this.operation["pagesize"] = this.pagesize;
            this.operation["pagenumber"] = this.pagenumber;
            //reload data
            this.toggleprogress(true);
            SAjax.fromData(this.operation, this);
        }
    };
    this.gotolastpage = function()
    {
        if(this.pagenumber!=this.lastpage)
        {
            this.pagenumber = this.lastpage;
            this.operation["pagesize"] = this.pagesize;
            this.operation["pagenumber"] = this.pagenumber;
            //reload data
            this.toggleprogress(true);
            SAjax.fromData(this.operation, this);
        }
    };
    this.gotonextpage = function ()
    {
        //check if exceed the total
        //this.pagenumber = pagenumbers;
        if(this.pagenumber<this.lastpage)
        {
            this.pagenumber++;
            this.operation["pagesize"] = this.pagesize;
            this.operation["pagenumber"] = this.pagenumber;
            //reload data
            this.toggleprogress(true);
            SAjax.fromData(this.operation, this);
        }        
    };
    this.gotoprevpage = function ()
    {
        //check if below 1
        if(this.pagenumber>1)
        {
            this.pagenumber--;
            this.operation["pagesize"] = this.pagesize;
            this.operation["pagenumber"] = this.pagenumber;
            //reload data
            this.toggleprogress(true);
            SAjax.fromData(this.operation, this);
        }
    };
    
    /* PROGRESS AND MESSAGE ACTIONS */
    this.toggleprogress = function(yes)
    {
        if(yes)
        {
            $("#"+this.tableid+"_progress").show();
        }else
        {
            $("#"+this.tableid+"_progress").hide();
        }
    };
    this.setmessage = function(what)
    {
        $("#"+this.tableid+"_message").text(what);
        if(what.length && what.length>0)
        {
            $("#"+this.tableid+"_message").show();
        }else
        {
            $("#"+this.tableid+"_message").hide();
        }
    };
    this.setclicks = function()
    {
        var self = this;
        $("#"+this.tableid+"_first").click(
            function()
            {
                self.gotofirstpage();
                self.setpaging();
            }
        );
        $("#"+this.tableid+"_prev").click(
            function()
            {
                self.gotoprevpage();
                self.setpaging();
            }
        );
        $("#"+this.tableid+"_next").click(
            function()
            {
                self.gotonextpage();
                self.setpaging();
            }
        );
        $("#"+this.tableid+"_last").click(
            function()
            {
                self.gotolastpage();
                self.setpaging();
            }
        );
    };
    this.setpaging = function()
    {
        $("#"+this.tableid+"_paging").text(this.pagenumber+"/"+this.lastpage);
    };   
}
