﻿<!--
function DataTableWithPaging(id, processCall)
{
    return DataTableWithPaging(id, processCall, null, null, false);
}

function DataTableWithPaging(id, processCall, tabs)
{
    return DataTableWithPaging(id, processCall, tabs, null, false);
}

function DataTableWithPaging(id, processCall, tabs, conditions)
{
    return DataTableWithPaging(id, processCall, tabs, conditions, false);
}

function DataTableWithPaging(id, processCall, tabs, conditions, debug)
{
    try
    {
        if(!tabs || tabs.length < 1)
        {
            tabs = [["", ""]];
        }
    } catch(e) {
        alert("Tabs are requeried to function correctly.");
        return null;
    }
    
    try
    {
        if(!conditions || conditions.length < 1)
        {
            conditions = [["", ""]];
        }
    } catch(e) {
        alert("Conditions are required to function correctly.");
        return null;
    }
    
    var obj =
    {
        ID:"",
        ProcessCall:"",
        SortColumn:"",
        SortColumnOld:"",
        SortDirection:"asc",
        SortDirectionOld:"asc",
        SortRenderType:"tabs", /* or "radio" */
        PageNo:1,
        PageSize:20,
        PageCount:-1,
        RowCount:-1,
        PageLinksCount:9,
        
        Tabs:"",
        TabsIconDesc:"<img border='0' src='/Files/Billeder/site/flSortUp.gif'>",
        TabsIconAsc:"<img border='0' src='/Files/Billeder/site/flSortDown.gif'>",
        TabsTemplate:"<ul id='flSortMenu' class='<!--@ID-->_tab_container'><!--@tabs--></ul>",
        TabsTemplateActive:"<li class='activeItem <!--@ID-->_tab_active'><a href=\"javascript: void(0);\" onclick=\"<!--@ID-->.ChangeColumn('<!--@tabColumnName-->', '<!--@tabId-->'); <!--@ProcessCall-->\"><b><!--@tabTitle--> <!--@iconSortDirection--></b></a></li>",
        TabsTemplateInActive:"<li class='<!--@ID-->_tab_inactive'><a href=\"javascript: void(0);\" onclick=\"<!--@ID-->.ChangeColumn('<!--@tabColumnName-->', '<!--@tabId-->'); <!--@ProcessCall-->\"><b><!--@tabTitle--> <!--@iconSortDirection--></b></a></li>",
        TabsTemplateSpacer:"",
        TabsRender:function() {
            var rtn = this.TabsTemplate;
			rtn = this.ReplaceToEnd(rtn);
            var tmp = "";
            if(this.Tabs.length > 0 && this.Tabs[0][0] != "")
            {
                for(i=0; i<this.Tabs.length; i++)
                {
                    var tmpItem = "";
                    if(this.Tabs[i][1] == this.SortColumn)
                    {
                        tmpItem = this.TabsTemplateActive;
                    }else{
                        tmpItem = this.TabsTemplateInActive;
                    }
                    
                    tmpItem = this.ReplaceToEnd(tmpItem);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@tabTitle-->", this.Tabs[i][0]);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@tabColumnName-->", this.Tabs[i][1]);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@tabId-->", i);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@iconSortDirection-->", this.Tabs[i][1] == this.SortColumn ? this.SortDirection == "asc" ? this.TabsIconAsc : this.TabsIconDesc : "");
                    
                    if(i < this.Tabs.length)
                    {
                        tmpItem += this.TabsTemplateSpacer;
                    }
                    
                    tmp += tmpItem;
                }
            }
            
            return rtn.replace("<!--@tabs-->", tmp);
        },

        SortIconDesc:"<img border='0' src='/Files/Billeder/Icons/arrow_up.gif' align='absmiddle' />",
        SortIconAsc:"<img border='0' src='/Files/Billeder/Icons/arrow_down.gif' align='absmiddle' />",
        SortTemplate:"<div id='flSortMenu' class='<!--@ID-->_tab_container'><!--@tabs--></div>",
        SortTemplateActive:"<label class='<!--@ID-->_condition'><input type='radio' name='<!--@ID-->_tabs' id='<!--@ID-->_tabs_<!--@i-->' onclick=\"<!--@ID-->.ChangeColumn('<!--@tabColumnName-->', '<!--@tabId-->', 'act'); <!--@ProcessCall-->\" value='' <!--@checked--> /><!--@tabTitle--></label><!--@iconSortDirection-->",
        SortTemplateInActive:"<label class='<!--@ID-->_condition'><input type='radio' name='<!--@ID-->_tabs' id='<!--@ID-->_tabs_<!--@i-->' onclick=\"<!--@ID-->.ChangeColumn('<!--@tabColumnName-->', '<!--@tabId-->', 'inact'); <!--@ProcessCall-->\" value='' <!--@checked--> /><!--@tabTitle--></label><!--@iconSortDirection-->",
        SortTemplateSpacer:"&nbsp;",
        SortRender:function() {
            var rtn = this.SortTemplate;
			rtn = this.ReplaceToEnd(rtn);
            var tmp = "";
            if(this.Tabs.length > 0 && this.Tabs[0][0] != "")
            {
                for(i=0; i<this.Tabs.length; i++)
                {
                    var tmpItem = "";
                    if(this.Tabs[i][1] == this.SortColumn)
                    {
                        tmpItem = this.SortTemplateActive;
                    }else{
                        tmpItem = this.SortTemplateInActive;
                    }
                    
                    tmpItem = this.ReplaceToEnd(tmpItem);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@i-->", i);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@tabTitle-->", this.Tabs[i][0]);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@tabColumnName-->", this.Tabs[i][1]);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@tabId-->", i);
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@iconSortDirection-->", this.Tabs[i][1] == this.SortColumn ? this.SortDirection == "asc" ? this.SortIconAsc : this.SortIconDesc : "");
                    tmpItem = this.ReplaceToEnd(tmpItem, "<!--@checked-->", this.Tabs[i][1] == this.SortColumn ? " CHECKED=CHECKED " : "");
                    
                    if(i < this.Tabs.length)
                    {
                        tmpItem += this.SortTemplateSpacer;
                    }
                    
                    tmp += tmpItem;
                }
            }
            
            return rtn.replace("<!--@tabs-->", tmp);
        },
        SortConvert: function() { if(this.Tabs.length > 0 && this.Tabs[0][0] != "") { for(i=0; i<this.Tabs.length; i++) { CustomRadioButtons.convert($(this.ID + "_tabs_" + i)); } }},
        
        ChangeColumn:function(columnName, tabIndex, tmp) 
        { 
            this.ChangePageNo(1); 
            this.SortColumnOld = this.SortColumn; 
            this.SortDirectionOld = this.SortDirection; 
            if(this.SortColumnOld == columnName) 
            { 
                if(this.SortDirection == "asc") 
                { 
                    this.SortDirection = "desc"; 
                } 
                else 
                { 
                    this.SortDirection = "asc" 
                }
            } 
            else 
            { 
                if (this.Tabs[tabIndex].length >= 4) 
                {
                    //--use default column's sortDirection if it was defined
                    this.SortDirection = this.Tabs[tabIndex][3];
                }
                else
                {
                    this.SortDirection = "asc";
                }
            } 
            this.SortColumn = columnName; 
        },
        
        CurrentCondition:"",
        Conditions:"",
        ConditionsRender:function() { var rtn = ""; if(this.Conditions.length > 0 && this.Conditions[0][0] != "") { for(i=0; i<this.Conditions.length; i++) { rtn += "<label class='" + this.ID + "_condition'><input type='radio' name='" + this.ID + "_conditions' id='" + this.ID + "_conditions_" + i + "' onclick='" + this.ID + ".PageNo = 1; " + this.ID + ".CurrentCondition = this.value; " + this.ProcessCall + "' value='" + this.Conditions[i][1] + "'" + (this.Conditions[i][2] == true ? " CHECKED=CHECKED" : "") + " />" + this.Conditions[i][0] + "</label>"; if(i<this.Conditions.length) { rtn+= "&nbsp;"; }  } } return rtn; },
        ConditionsConvert: function() { if(this.Conditions.length > 0 && this.Conditions[0][0] != "") { for(i=0; i<this.Conditions.length; i++) { CustomRadioButtons.convert($(this.ID + "_conditions_" + i)); } }},
        
        PageNumberingTemplate:"<span class='<!--@ID-->_page_container'><!--@pages--></span>",
        PageNumberingTemplateActive:"<span class='flivPagingItem activeItem <!--@ID-->_page_active'><!--@CurrentPage--></span>",
		PageNumberingTemplateInActive:"<span><a class='flivPagingItem <!--@ID-->_page_inactive' href='javascript: void(0);' onclick='<!--@ID-->.ChangePageNo(<!--@CurrentPage-->); <!--@ProcessCall-->'><!--@CurrentPage--></a></span>",
        PageNumberingTemplateSpacer:"&nbsp;",
        PageNumberingTemplatePreviousBtn: "<span id='FlivAjaxPaging_PrevBtn'><a class='flivPagingItem <!--@ID-->_page_inactive' href='javascript: void(0);' onclick='<!--@ID-->.ChangePageNo(<!--@PrevPageNo-->); <!--@ProcessCall-->'>Forrige</a></span>",
        PageNumberingTemplateNextBtn: "<span id='FlivAjaxPaging_NextBtn'><a class='flivPagingItem <!--@ID-->_page_inactive' href='javascript: void(0);' onclick='<!--@ID-->.ChangePageNo(<!--@NextPageNo-->); <!--@ProcessCall-->'>Næste</a></span>",
        PageNumberingRender:function() {
            if(this.PageCount == -1 && this.Debug == true)
            {
                alert("You are required to process " + this.ProcessCall + " before rendering page numbering html");
                return;
            }
            
            if(this.PageCount == 1) { return ""; }
            
            var rtn = this.PageNumberingTemplate;
            rtn = this.ReplaceToEnd(rtn);
            
            var tmpPrev = '';
            var tmpNext = '';
            
            var tmp = "";
                        
            var PagingStartPage; //--number of the first page for pager control
            var PagingEndPage; //--number of the last page for pager control
            
            var PageCountRight; //--count of linkbuttons left from current
            var PageCountLeft; //--count of linkbuttons right from current
            
            if (this.PageLinksCount % 2 == 0) //even count of pagelinks
            {
                PageCountRight = this.PageLinksCount / 2; 
                PageCountLeft = PageCountRight - 1;    
            }
            else //odd
            {
                PageCountRight = PageCountLeft = (this.PageLinksCount - 1) / 2;
            }
            
            var tmpLeftPos = this.PageNo - PageCountLeft;
            var tmpRightPos = this.PageNo + PageCountRight;
            
            if (tmpLeftPos <= 0)
            {
                tmpRightPos = tmpRightPos + (tmpLeftPos * -1) + 1;
                tmpLeftPos = 1;
            }
            
            if (tmpRightPos >= this.PageCount)
            {
                tmpRightPos = this.PageCount;
                
                if ((tmpRightPos - tmpLeftPos) < (this.PageLinksCount - 1))
                {
                    var newLeftPos = tmpLeftPos - (((this.PageLinksCount - 1) - (tmpRightPos - tmpLeftPos)));
                    tmpLeftPos = (newLeftPos > 1) ? newLeftPos : 1;
                }
            }
                        
            PagingStartPage = tmpLeftPos;
            PagingEndPage = tmpRightPos;
            
            if (this.PageNo > 1)
            {
                tmpPrev = this.ReplaceToEnd(this.PageNumberingTemplatePreviousBtn);
                tmpPrev = this.ReplaceToEnd(tmpPrev, "<!--@PrevPageNo-->", this.PageNo - 1);
                tmpPrev += this.PageNumberingTemplateSpacer;
                tmp = tmpPrev;
            }
            
            /*console.log('start: ' +PagingStartPage + '; end: '+ PagingEndPage);
            console.log('pageCount: ' + this.PageCount);
            console.log('rowCount: ' + this.RowCount);
            console.log('currentPage: ' + this.PageNo);
            console.log('rows per page: ' + this.PageSize);*/
            
            for(i=PagingStartPage; i<=PagingEndPage; i++)
            {
                var tmpItem = "";
                if(this.PageNo == i)
                {
                    tmpItem = this.PageNumberingTemplateActive;
                } else {
                    tmpItem = this.PageNumberingTemplateInActive;
                }
                
                tmpItem = this.ReplaceToEnd(tmpItem);
                tmpItem = this.ReplaceToEnd(tmpItem, "<!--@CurrentPage-->", i);
                
                if(i<this.PageCount)
                {
                    tmpItem += this.PageNumberingTemplateSpacer;
                }
                
                tmp += tmpItem;
            }
            
            if (this.PageNo < this.PageCount)
            {
                tmpNext = this.ReplaceToEnd(this.PageNumberingTemplateNextBtn);
                tmpNext = this.ReplaceToEnd(tmpNext, "<!--@NextPageNo-->", this.PageNo + 1);
                tmp += this.PageNumberingTemplateSpacer;
                tmp += tmpNext;
            }
            
            return rtn.replace("<!--@pages-->", tmp);
        },
        ChangePageNo:function(pageNo) { this.PageNo = pageNo; },
        
        Status:function() { return "\nFirstTimeInit: " + this.FirstTimeInit + "\n\nSortColumn: " + this.SortColumn + " (" + this.SortColumnOld + ")\nSortDirection: " + this.SortDirection + " (" + this.SortDirectionOld + ")\nPageNo: " + this.PageNo + "\nPageSize: " + this.PageSize + "\nPageCount: " + this.PageCount + "\nRowCount: " + this.RowCount + "\nCurrentCondition: " + this.CurrentCondition + "\nCondition: " + this.Conditions + "\nTabs: " + this.Tabs; },
        ReplaceToEnd:function(text, tag, tagValue) {
            var replaceTxt = text;
            var replaceCounter = 0;
            
            if(tag == null && tagValue == null)
            {
                replaceTxt = this.ReplaceToEnd(replaceTxt, "<!--@ID-->", this.ID);
                replaceTxt = this.ReplaceToEnd(replaceTxt, "<!--@ProcessCall-->", this.ProcessCall);
            }
            else
            {
                while(replaceTxt.indexOf(tag)>0)
                {
                    replaceTxt = replaceTxt.replace(tag, tagValue);
                    replaceCounter++;
                }
            }
            return replaceTxt;
        },
        Debug:false,
        FirstTimeInit:true,
        
        Render:function() { document.getElementById(this.ID + "_render_tabs").innerHTML = (this.SortRenderType == "radio" ? this.SortRender() : this.TabsRender()); if (this.SortRenderType == "radio") this.SortConvert(); document.getElementById(this.ID + "_render_pageNumbering").innerHTML = this.PageNumberingRender(); if(this.FirstTimeInit) { document.getElementById(this.ID + "_render_conditions").innerHTML = this.ConditionsRender(); this.ConditionsConvert(); } this.FirstTimeInit = false; }
        
        , RenderHelp: function() {
            alert("Not implemented yet"); 
            }
    };
    
    obj.ID = id;
    obj.ProcessCall = processCall;
    obj.Tabs = tabs;
    obj.Conditions = conditions;
    obj.Debug = debug;
    
    /* get default sort column */
    var tabSet = false;
    for(var i=0; i<obj.Tabs.length; i++)
    {
        if(obj.Tabs[i][2] == true)
        {
            obj.SortColumn = obj.Tabs[i][1];
            tabSet = true;
            break;
        }
    }
    if(!tabSet)
    {
        obj.SortColumn = obj.Tabs[0][1];
        obj.Tabs[0][2] = true;
    }
    
    /* get default condition */
    var conditionSet = false;
    for(var i=0; i<obj.Conditions.length; i++)
    {
        if(obj.Conditions[i][2] == true)
        {
            obj.CurrentCondition = obj.Conditions[i][1];
            conditionSet = true;
            break;
        }
    }
    if(!conditionSet)
    {
        obj.CurrentCondition = obj.Conditions[0][1];
        obj.Conditions[0][2] = true;
    }
    
    return obj;
}
//-->

