﻿var generic = {};
generic.list = function () {
    this.items = [];
};
generic.list.prototype.count = function () {
    return this.items.length;
};
generic.list.prototype.getItems = function () {
    return this.items;
};
generic.list.prototype.getItem = function (index) {
    return this.items[index];
};
generic.list.prototype.setItem = function (item, index) {
    this.items[index] = item;
};
generic.list.prototype.sort = function (type, column) {
    if (type == 'numeric') {
        sortCol = column;
        items.sort(numsort);
    }
};
generic.list.prototype.extendProperty = function (index, obj) {
    var key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) {
            this.setProperty(index, key, obj[key]);
        }
    }
};
generic.list.prototype.getProperty = function (index, property) {
    return this.items[index][property];
};
generic.list.prototype.setProperty = function (index, property, value) {
    this.items[index][property] = value;
};
generic.list.prototype.add = function (item) {
    if (this.items.length !== 0) {
        var oldlen = this.items.length;
        var tmp = new Array(oldlen + 1);
        var i = 0;
        for (i = 0; i < this.items.length; i++) {
            tmp[i] = this.items[i];
        }
        tmp[(tmp.length - 1)] = item;
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++) {
            this.items[i] = tmp[i];
        }
        tmp = null;
    } else {
        this.items = new Array(1);
        this.items[0] = item;
    }
};
generic.list.prototype.insert = function (item, index) {
    if (this.items.length !== 0) {
        var oldlen = this.items.length;
        var tmp = new Array(oldlen + 1);
        var i = 0;
        var j = 0;
        for (i = 0; i < tmp.length; i++) {
            if (i == index) {
                tmp[i] = item;
            } else {
                tmp[i] = this.items[j];
                j++;
            }
        }
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++) {
            this.items[i] = tmp[i];
        }
        tmp = null;
    } else {
        this.items = new Array(1);
        this.items[0] = item;
    }
};
generic.list.prototype.addRange = function (objectArray) {
    if (this.items.length !== 0) {
        var oldlen = this.items.length;
        var tmp = new Array(oldlen + objectArray.length);
        var i = 0;
        for (i = 0; i < this.items.length; i++) {
            tmp[i] = this.items[i];
        }
        for (i = 0; i < objectArray.length; i++) {
            tmp[(i + oldlen)] = objectArray[i];
        }
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++) {
            this.items[i] = tmp[i];
        }
        tmp = null;
    } else {
        var iloop = 0;
        this.items = new Array(objectArray.length);
        for (iloop = 0; iloop < objectArray.length; iloop++) {
            this.items[iloop] = objectArray[iloop];
        }
    }
};
generic.list.prototype.find = function (item) {
    var index = -1;
    var i = 0;
    for (i = 0; i < this.items.length; i++) {
        if (this.compare(this.items[i], item)) {
            index = i;
            break;
        }
    }
    return index;
};
generic.list.prototype.compare = function (a, b) {
    return (a == b);
};
generic.list.prototype.remove = function (item) {
    var index = this.find(item);
    if (index != -1) {
        var tmp = new Array(this.items.length - 1);
        var i = 0;
        var j = 0;
        for (i = 0; i < this.items.length; i++) {
            if (i != index) {
                tmp[j] = this.items[i];
                j++;
            }
        }
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++) {
            this.items[i] = tmp[i];
        }
        tmp = null;
        this.count--;
    }
};
generic.list.prototype.removeAt = function (index) {
    if (this.items.length == 1) {
        this.items = null;
        this.items = [];
    } else {
        var tmp = new Array((this.items.length - 1));
        var i = 0;
        var j = 0;
        for (i = 0; i < this.items.length; i++) {
            if (i != index) {
                tmp[j] = this.items[i];
                j++;
            }
        }
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++) {
            this.items[i] = tmp[i];
        }
        tmp = null;
    }
};
generic.list.prototype.join = function (seprator, property) {
    var i = 0;
    var result = "";
    for (i = 0; i < this.items.length; i++) {
        if (i == (this.items.length - 1)) {
            result += (property) ? this.items[i][property] : this.items[i];
        } else {
            result += (property) ? this.items[i][property] : this.items[i];
            result += seprator;
        }
    }
    return result;
};
generic.list.prototype.clear = function () {
    this.items = [];
};
var dataset = function (ID, headerSort) {
    this.id = ID;
    this.load = false;
    this.columns = new generic.list();
    this.columnBreaks = new generic.list();
    this.columnAdditionals = new generic.list();
    this.data = false;
    this.clientProcessing = true;
    this.sortCol = false;
    this.sortDesc = false;
    this.pageSize = 25;
    this.pageIndex = 0;
    this.externalVariable = '';
    this.checkboxSelectionField = false;
    this.selectedValues = new generic.list();
    this.additionalValues = new generic.list();
    this.asocArray = {};
    this.topAdditionals = false;
    this.bottomAdditionals = false;
    this.headers = false;
    this.endScript = '';
    this.dataUrl = '';
    this.customBuild = null;
    this.loadingDiv = false;
    this.pageSizes = '25,50,100';
    this.tableTemplate = true;
    this.hideHeader = false;
    this.hideSummary = false;
    this.parameters = {};
    this.estimatedWidth = 500;
    this.estimatedHeight = 300;
    this.error = false;
    this.afterScript = false;
    this.noResultsTemplate = 'No items found';
    this.onNoResults = null;
    var param = this.getHashParameter('psize');
    if (param) {
        this.pageSize = parseInt(param, 10);
    }
    param = this.getHashParameter('page');
    if (param) {
        this.pageIndex = parseInt(param, 10);
    }
    param = this.getHashParameter('sby');
    if (param) {
        this.sortCol = param;
    }
    param = this.getHashParameter('so');
    if (param) {
        this.sortDesc = (param == 'desc' ? true : false);
    }
};
dataset.prototype.getHashParameter = function (key) {
    var h = window.location.hash;
    if (h.length > 0) {
        h = h.substring(1, h.length);
    }
    var splits = h.split(',');
    var i = 0;
    for (i = 0; i < splits.length; i++) {
        if (splits[i].length > 0) {
            var ix = splits[i].indexOf('-');
            var xk = splits[i].substring(0, ix);
            var xv = splits[i].substring(ix + 1, splits[i].length);
            if ((this.externalVariable + key) == xk) {
                return xv;
            }
        }
    }
};
dataset.prototype.setHashParameter = function (key, value) {
    var h = window.location.hash;
    if (h.length > 0) {
        h = h.substring(1, h.length);
    }
    var splits = h.split(',');
    var i = 0;
    var res = '';
    var found = false;
    if (window.location.hash.length === 0) {
        res = '#';
    }
    for (i = 0; i < splits.length; i++) {
        if (splits[i].length > 0) {
            var ix = splits[i].indexOf('-');
            var xk = splits[i].substring(0, ix);
            var xv = splits[i].substring(ix + 1, splits[i].length);
            if ((this.externalVariable + key) == xk) {
                res += xk + '-' + value + ',';
                found = true;
            } else {
                res += xk + '-' + xv + ',';
            }
        }
    }
    if (!found) {
        res += this.externalVariable + key + '-' + value + ',';
    }
    window.location.hash = res.substring(0, res.length - 1);
};

dataset.prototype.getSelectedItems = function (separator) {
    var result = "";
    if (this.checkboxSelectionField) {
        var i = 0;
        var j = 0;
        for (i = 0; i < this.selectedValues.items.length; i++) {
            {
                result += this.selectedValues.items[i];
                for (j = 0; j < this.additionalValues.items.length; j++) {
                    result += "," + this.asocArray[this.selectedValues.items[i]][this.additionalValues.items[j]];
                }
                if (i != (this.selectedValues.items.length - 1))
                    result += separator;
            }
        }
    }
    return result;
}

dataset.prototype.selectAll = function (cb) {
    if (cb.checked) {
        var li = this.lowerIndex() - 1;
        var ui = this.upperIndex();
        var iloop = 0;

        for (iloop = li; iloop < ui; iloop++) {
            var val = this.data.items[iloop - li][this.checkboxSelectionField];
            if (this.selectedValues.find(val) == -1) {
                this.selectedValues.add(val);
            }
        }
    } else {
        this.selectedValues.clear();
    }

    var tr = cb.parentNode.parentNode;
    while ((typeof tr != 'undefined') && (tr !== null)) {
        tr = tr.nextSibling;
        if (tr !== null) {
            var ch = tr.childNodes[0].childNodes[0];
            if ((cb !== null) && (cb.nodeName == 'INPUT')) {
                if (ch.parentNode.parentNode.parentNode.style.display != 'none') {
                    ch.checked = cb.checked;
                }
            }
        }
    }
};

dataset.prototype.setSelection = function (cb, value) {
    if (cb.checked) {
        if (this.selectedValues.find(value) == -1) {
            this.selectedValues.add(value);
        }
    } else {
        this.selectedValues.remove(value);
    }
};

dataset.prototype.addMergeColumn = function (column) {
    this.columnMerges.Add(column);
};


dataset.prototype.addColumn = function (column) {
    this.columns.add(column);
};

dataset.prototype.setData = function (jsonData, rebuild) {
    this.data.items.addRange(jsonData);
    if (rebuild) {
        this.build();
    }
};
dataset.prototype.ajaxLoad = function (target) {
    var url = evalParams(this.url);
    var i = 0;
    try {
        if (!this.clientProcessing) {
            this.parameters = {};
            if (this.sortCol) {
                this.parameters.SortBy = this.sortCol;
            }
            this.parameters.SortDesc = this.sortDesc;
            this.parameters.LowerIndex = this.lowerIndex() - 1;
            this.parameters.UpperIndex = this.upperIndex();
            this.parameters.PageSize = this.pageSize;
            for (i = 0; i < this.columns.items.length; i++) {
                if (this.columns.items[i].FilterKey) {
                    var tb = document.getElementById(this.id + this.columns.items[i].FilterKey + 'tb');
                    if (tb) {
                        var filterValue = tb.value;
                        if (filterValue.length > 0) {
                            this.parameters['filterkey' + i] = this.columns.items[i].FilterKey;
                            this.parameters['filtervalue' + i] = filterValue;
                        }
                    }
                }
            }
        }        
    }
    catch (e) {        
        handleError(e, url, null);
    }
    if (this.urlParameters.length > 0) {
        var urlParametersSplit = this.urlParameters.split(',');
        for (i = 0; i < urlParametersSplit.length; i++) {
            var vals = urlParametersSplit[i].split('=');
            this.parameters[vals[0]] = evalParams(vals[1]);
        }
    }
    this.showLoading();
    var parameters = this.parameters;
    setTimeout(function () {
        jsonPost(url, parameters, target.setAjaxData, target);
    }, 200);
};
dataset.prototype.setDataUrl = function (target, urlBase, extension) {
    this.urlBase = urlBase;
    this.extension = extension;
    this.url = urlBase + '.' + extension;
};
dataset.prototype.setAjaxData = function (ajax, arg, successful) {
    if (successful === false) {
        arg.error = true;
        arg.build();
        return;
    }
    try {
        var json = jQuery.parseJSON(ajax);
        arg.data = json;
        if (json.SelectedValues) {
            arg.selectedValues.clear();
            arg.selectedValues.addRange(json.SelectedValues);
        }
        if ((arg.clientProcessing === false) || (arg.sortCol === false)) {
            if (typeof arg.data.SortBy != 'undefined') {
                arg.sortCol = arg.data.SortBy;
            }
            if (typeof arg.data.SortDesc != 'undefined') {
                arg.sortDesc = arg.data.SortDesc;
            }
        }
    }
    catch (e) {        
        handleError(e, this.url, null);
    }

    try {
        if (arg.clientProcessing) {
            try {
                arg.applySort();
            }
            catch (err) {
                alert('sort: ' + err);
            }
        }
        arg.build();
    }
    catch (e) { }
};
dataset.prototype.createButton = function (details, pseudo) {
    if ((details.pageIndex == this.pageIndex) || pseudo) {
        return details.text;
    } else {
        return '<a href="javascript:' + this.externalVariable + '.changePage(' + details.pageIndex + ');"> ' + details.text + ' </a>';
    }
    return res;
};
dataset.prototype.buildPagination = function (maxCount) {
    try {
        var res = '<span class="paginationspan">';
        var findex = Math.max(0, Math.floor(Number(this.pageIndex - 1) - (Number(maxCount - 1) / 2)));
        lindex = findex + Number(maxCount);
        if (lindex > this.pageCount()) {
            lindex = this.pageCount();
            findex = Math.max(0, this.pageCount() - Number(maxCount));
        }
        if (maxCount < this.pageCount()) {
            if (maxCount) {
                res += this.createButton({
                    title: 'First Page',
                    text: " \u00ab ",
                    pageIndex: 0
                }, this.pageIndex === 0);
            }
            if (maxCount > 2) {
                res += this.createButton({
                    title: 'Previous',
                    text: " \u2039 ",
                    pageIndex: this.pageIndex - 1
                }, this.pageIndex === 0);
            }
        }
        for (var i = findex; i < lindex; i++) {
            res += this.createButton({
                title: 'Page ' + i + 1,
                pageIndex: i,
                text: (i + 1)
            });
        }

        if (maxCount < this.pageCount()) {

            if (maxCount > 2) {
                res += this.createButton({
                    title: 'Next Page',
                    pageIndex: this.pageIndex + 1,
                    text: " \u203a "
                }, this.pageIndex == this.pageCount() - 1);
            }
            if (maxCount) {
                res += this.createButton({
                    title: 'Last Page',
                    pageIndex: this.pageCount() - 1,
                    text: " \u00bb "
                }, this.pageIndex == this.pageCount() - 1);
            }
        }
        return res + '</span>';
    }
    catch (ex) {        
        handleError(ex, window.location, null);
    }
};

var tempSortCol = false;
var tempSortDesc = false;

dataset.prototype.datesort = function (a, b) {
    var x = Date.parse(a[tempSortCol]);
    var y = Date.parse(b[tempSortCol]);
    return (tempSortDesc ? -1 : 1) * (x - y);
};
dataset.prototype.boolsort = function (a, b) {
    var x = Boolean(a[tempSortCol]);
    var y = Boolean(b[tempSortCol]);
    if (x == y) {
        return 0;
    } else {
        return (tempSortDesc ? -1 : 1) * (x ? 1 : -1);
    }
};
dataset.prototype.numsort = function (a, b) {
    var x = parseFloat(a[tempSortCol]);
    var y = parseFloat(b[tempSortCol]);
    return (tempSortDesc ? -1 : 1) * (x - y);
};
dataset.prototype.stringsort = function (a, b) {
    var x = a[tempSortCol].toLowerCase();
    var y = b[tempSortCol].toLowerCase();
    return (tempSortDesc ? 1 : -1) * ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
dataset.prototype.doFilter = function (colIdx) {
    var key = this.id + this.columns.items[colIdx].FilterKey;
    var value = document.getElementById(key + 'tb').value;
    this.setHashParameter('f' + this.columns.items[colIdx].FilterKey, value);
    this.parameters['f' + key] = value;
    hideFilter(key + 'lbl', key + 'div', key + 'btn');
    this.ajaxLoad(this);
};
dataset.prototype.showLoading = function () {
    if (!this.loadingDiv) {
        this.loadingDiv = document.createElement('DIV');
        if (this.loadingDivAdditionalClasses) {
            this.loadingDiv.className = 'dataloading ' + this.loadingDivAdditionalClasses;
        } else {
            this.loadingDiv.className = 'dataloading';
        }
        this.loadingDiv.style.display = 'none';
        document.body.appendChild(this.loadingDiv);
    }
    var table = document.getElementById(this.id + 'filler');
    if (!table) {
        this.build();
        table = document.getElementById(this.id + 'filler');
        if (!table) {
            table = document.getElementById(this.tableId);
        }
    }
    if (table) {
        var width = table.clientWidth;
        var height = table.clientHeight;
        this.loadingDiv.style.width = width + 'px';
        this.loadingDiv.style.height = height + 'px';
        this.loadingDiv.style.top = ajaxTooltip_getTopPos(table) + 'px';
        this.loadingDiv.style.left = ajaxTooltip_getLeftPos(table) + 'px';
        this.loadingDiv.style.display = 'block';
        this.loadingDiv.style.position = 'absolute';
        table.style.display = '';
    }

};
dataset.prototype.applySort = function () {
    tempSortCol = this.sortCol;
    tempSortDesc = this.sortDesc;
    var sortType = 'numeric';
    var i;
    for (i = 0; i < this.columns.count(); i++) {
        if ((this.columns.items[i].SortKey == tempSortCol) && (this.columns.items[i].SortType)) {
            sortType = this.columns.items[i].SortType;
        }
    }
    this.sort(sortType, tempSortCol);
};
dataset.prototype.changePageSize = function (dropdown) {
    try {
        var myindex = dropdown.selectedIndex;
        var selValue = dropdown.options[myindex].value;
        var oldLowerIndex = this.lowerIndex();
        this.pageSize = selValue;
        this.pageIndex = Math.floor(oldLowerIndex / selValue);
        this.setHashParameter('psize', this.pageSize);
        this.changePage(this.pageIndex);
    }
    catch (e) {
        
    }
};
dataset.prototype.changePage = function (page) {
    if (this.pageIndex != page) {
        this.pageIndex = page;
        this.setHashParameter('page', page);
    }
    if (this.clientProcessing) {
        this.build();
    } else {
        this.ajaxLoad(this);
    }
};
dataset.prototype.changeSort = function (key) {
    if (this.sortCol == key) {
        this.sortDesc = !this.sortDesc;
    } else {
        this.sortCol = key;
        this.sortDesc = false;
    }
    this.setHashParameter('sby', key);
    this.setHashParameter('so', this.sortDesc ? 'desc' : 'asc');
    if (this.clientProcessing) {
        this.applySort();
        this.build();
    } else {
        this.ajaxLoad(this);
    }

};
dataset.prototype.sort = function (type, column) {
    if (type == 'numeric') {
        this.data.items.sort(this.numsort);
    } else if (type == 'date') {
        this.data.items.sort(this.datesort);
    } else if (type == 'bool') {
        this.data.items.sort(this.boolsort);
    } else {
        this.data.items.sort(this.stringsort);
    }
};
dataset.prototype.replaceWithItemValues = function (input, item, index) {
    var result = input;
    var key;
    for (key in item) {
        if (item.hasOwnProperty(key)) {
            myregexp = new RegExp();
            myregexp.compile("eval/" + key + "/", "g");
            result = result.replace(myregexp, item[key]);
            myregexp = new RegExp();
            myregexp.compile("evalurl/" + key + "/", "g");
            result = result.replace(myregexp, urlencode(item[key]));
            if (item[key]) {
                myregexp = new RegExp();
                myregexp.compile("evaljq/" + key + "/", "g");
                result = result.replace(myregexp, item[key].toString().replace(/\'/g, "\\\'"));
                myregexp = new RegExp();
                myregexp.compile("evalhdq/" + key + "/", "g");
                result = result.replace(myregexp, item[key].toString().replace(/\"/g, "&quot;"));
            }
        }
    }
    myregexp = new RegExp();
    myregexp.compile("dataset/index/", "g");
    result = result.replace(myregexp, index);
    var re = /eval\/(.+?)\/eval/gim;
    var out = '';
    var li = 0;
    var mat = re.exec(result);
    var evaled = false;
    while (mat !== null) {
        out += result.substr(li, mat.index - li);
        out += altEval(mat[1]);
        li = mat.index + mat[0].length;
        mat = re.exec(result);
        evaled = true;
    }
    out += result.substr(li);
    if (evaled) {
        return this.replaceWithItemValues(out, item, index);
    } else {
        return out;
    }
};
dataset.prototype.getTemplateForItem = function (colIndex, item, index) {
    var result = this.columns.items[colIndex].Template;
    var m = result.indexOf('/!if');
    while (m != -1) {
        var expEnd = result.indexOf('!/');
        var exp = result.substring(m + 5, expEnd);
        var endifidx = result.indexOf('/!endif!/');
        var replc = '';

        if (altEval(this.replaceWithItemValues(exp, item, index)) === true) {
            replc = result.substring(expEnd + 2, endifidx);
        }
        result = result.replace(result.substring(m, endifidx + 9), replc);
        m = result.indexOf('/!if');
    }
    return result;

};
dataset.prototype.isChecked = function (item) {
    var v = item[this.checkboxSelectionField];
    return this.selectedValues.find(v) > -1;
};
dataset.prototype.getTemplate = function (index, item) {
    var result = '';
    if (this.tableTemplate) {
        result = '<tr';
        if ((index % 2 == 1) || (item.Signup)) {
            result += ' class="';
            if (index % 2 == 1)
            { result += 'odd'; }
            if (item.Signup)
            { result += ' reg'; }
            result += '"';
        }
        result += '>';
    }
    if (this.checkboxSelectionField) {
        if (this.tableTemplate) {
            result += "<td>";
        }
        result += "<input type=\"checkbox\" value=\"evalhdq/" + this.checkboxSelectionField + "/\" onclick=\"" + this.externalVariable + ".setSelection(this, 'evalhdq/" + this.checkboxSelectionField + "/');\" " + (this.isChecked(item) ? "checked" : "") + "/>";
        if (this.tableTemplate) {
            result += "</td>";
        }
    }
    var colLoop = 0;
    for (colLoop = 0; colLoop < this.columns.count(); colLoop++) {
        var template = this.getTemplateForItem(colLoop, item, index);
        if (this.tableTemplate) {
            result += this.buildClassNode('td', item.Signup ? this.columns.items[colLoop].RegClass : this.columns.items[colLoop].CellClass);
        }
        result += template;
        if (this.tableTemplate) {
            result += '</td>';
        }
    }
    if (this.tableTemplate) {
        result += '</tr>';
    }
    return this.replaceWithItemValues(result, item, index);
};
dataset.prototype.renderItem = function (index, item) {
    if (this.checkboxSelectionField) {
        this.asocArray[item[this.checkboxSelectionField]] = item;
    }
    return this.getTemplate(index, item);
};
dataset.prototype.buildClassNode = function (nodeType, className) {
    return this.buildNodeOptionalAttributes(nodeType, 'class', className);
};
dataset.prototype.buildNodeOptionalAttributes = function (nodeType, p1, pv1, p2, pv2, p3, pv3, p4, pv4) {
    var res = '<' + nodeType;
    if (pv1 && p1) {
        res += ' ' + p1 + '="' + pv1 + '"';
    }
    if (pv2 && p2) {
        res += ' ' + p2 + '="' + pv2 + '"';
    }
    if (pv3 && p3) {
        res += ' ' + p3 + '="' + pv3 + '"';
    }
    if (pv4 && p4) {
        res += ' ' + p4 + '="' + pv4 + '"';
    }
    res += '>';
    return res;
};
dataset.prototype.getHeaderTemplate = function () {
    if (this.hideHeader) {
        return '';
    }
    var i = 0;
    var tmp = this.buildNodeOptionalAttributes('table', 'class', this.tableClass, 'id', this.tableId, 'cellspacing', '0', 'cellpadding', '0');
    if (this.data && this.data.TotalRowCount && (this.data.Export || (this.pageCount() > 1) || !this.hideSummary)) {
        tmp += '<tr><td class="tableheader" colspan="' + (this.columns.count() + (this.checkboxSelectionField ? 1 : 0)) + '">';
        tmp += '<div class="resultSummary">';
        if (this.data && this.data.TotalRowCount) {
            tmp += 'Results ' + this.lowerIndex() + ' to ' + this.upperIndex() + ' of ' + this.data.TotalRowCount + '<br/>';
        }
        if (this.pageCount() > 1) {
            tmp += this.buildPagination(5);
        }
        tmp += '</div>';
        tmp += this.buildExports(0);
        if (this.topAdditionals) {
            tmp += this.topAdditionals;
        }
        tmp += "</td></tr>";
        tmp += '<tr>';
    }
    try {
        if (this.checkboxSelectionField) {
            var headerRowCount = 1;
            if (this.headers) {
                for (i = 0; i < this.headers.length; i++) {
                    if (this.headers[i].IsBreak) {
                        headerRowCount++;
                    }
                }
            }
            tmp += "<th rowspan=\"" + headerRowCount + "\">";
            tmp += "<input type=\"checkbox\" onclick=\"" + this.externalVariable + ".selectAll(this);\"/>";
            tmp += "</th>";
        }
        if (this.headers) {
            var hloop = 0;
            var row = 0;
            var offset = (this.checkboxSelectionField ? 1 : 0);
            for (hloop = 0; hloop < this.headers.length; hloop++) {
                var h = this.headers[hloop];
                if (h.IsBreak) {
                    tmp += '</tr></tr>';
                } else {
                    var hid;

                    if (typeof h.Id != 'undefined') {
                        hid = 'h-' + (h.Id + offset) + '-' + row;
                    }
                    if (typeof h.ColumnIdx != 'undefined') {
                        tmp += this.buildNodeOptionalAttributes('th', 'class', this.columns.items[h.ColumnIdx].HeaderClass, 'ColSpan', this.columns.items[h.ColumnIdx].ColSpan, 'rowspan', this.columns.items[h.ColumnIdx].RowSpan, 'id', hid);
                        var divClass = '';
                        var hClass = this.columns.items[h.ColumnIdx].HeaderClass;
                        if (hClass !== null && typeof hClass != 'undefined') {
                            var splits = hClass.split(" ");
                            for (i = 0; i < splits.length; i++) {
                                if (splits[i].substring(0, 2) != 'al') {
                                    divClass += splits[i];
                                }
                            }
                        }

                        if (this.columns.items[h.ColumnIdx].Tip) {
                            tmp += this.buildNodeOptionalAttributes('div', 'class', divClass + ' addPad', 'onmouseover', 'Tip(\'' + this.columns.items[h.ColumnIdx].Tip + '\', FADEIN, 200, FADEOUT, 200)');
                            if (this.columns.items[h.ColumnIdx].TipParam) {
                                tmp += ',' + this.columns.items[h.ColumnIdx].TipParam;
                            }
                        }
                        else
                            tmp += this.buildNodeOptionalAttributes('div', 'class', divClass);

                        if (this.columns.items[h.ColumnIdx].SortKey) {
                            tmp += '<a href="javascript:' + this.externalVariable + '.changeSort(\'' + this.columns.items[h.ColumnIdx].SortKey + '\');';
                            if (this.columns.items[h.ColumnIdx].OnSort) tmp += this.columns.items[h.ColumnIdx].OnSort;
                            tmp += '">';
                            if (this.sortCol == this.columns.items[h.ColumnIdx].SortKey) {
                                if (this.sortDesc) {
                                    tmp += '<img class="sortdesc" align="top" alt="desc" src="/img/1px.gif"/>';
                                } else {
                                    tmp += '<img class="sortasc" align="top" alt="desc" src="/img/1px.gif"/>';
                                }
                            }
                            tmp += this.columns.items[h.ColumnIdx].Title + '</a>';
                        } else {
                            tmp += this.columns.items[h.ColumnIdx].Title;
                        }

                        if (this.columns.items[h.ColumnIdx].FilterKey) {
                            var fkey = this.id + this.columns.items[h.ColumnIdx].FilterKey;
                            tmp += '&nbsp; <img align=\"top\" style="border-width: 0px; cursor: pointer;" alt="Filter" src="/img/magnifier.gif" onclick="showFilter(\'' + fkey + 'lbl\', \'' + fkey + 'div\',\'' + fkey + 'tb\');\" id=\"' + fkey + 'btn\"/>';
                            tmp += '<span id="' + fkey + 'lbl">';
                            if ((this.data) && (typeof this.data['f' + this.columns.items[h.ColumnIdx].FilterKey] != 'undefined')) {
                                tmp += '- ' + this.data['f' + this.columns.items[h.ColumnIdx].FilterKey];
                            }
                            tmp += '</span>';
                            tmp += '<div style="display: none;" class="FilterTB" id="' + fkey + 'div"><input type="text" style="font-size: 8pt; height: 12px;" onkeypress="javascript:if ((event.which &amp;&amp; event.which == 13) || (event.keyCode &amp;&amp; event.keyCode == 13)) {' + this.externalVariable + '.doFilter(' + h.ColumnIdx + ');}if ((event.which &amp;&amp; event.which == 27) || (event.keyCode &amp;&amp; event.keyCode == 27)) {hideFilter(\'' + fkey + 'lbl\',\'' + fkey + 'div\',\'' + fkey + 'btn\');return false;}" class="textbox" id="' + fkey + 'tb" name="' + fkey + 'tb"';
                            if ((this.data) && (typeof this.data['f' + this.columns.items[h.ColumnIdx].FilterKey] != 'undefined')) {
                                tmp += ' value="' + this.data['f' + this.columns.items[h.ColumnIdx].FilterKey] + '"';
                            }
                            tmp += '/><img align="middle" style="border-width: 0px; vertical-align: middle;" alt="Ok" src="/img/tick.gif" onclick="' + this.externalVariable + '.doFilter(' + h.ColumnIdx + ')" class="filterbutton"/><img align="middle" style="border-width: 0px; vertical-align: middle;" alt="Cancel" src="/img/cross.gif" onclick="hideFilter(\'' + fkey + 'lbl\',\'' + fkey + 'div\',\'' + fkey + 'btn\'); return false;" class="filterbutton"/></div>';
                        }
                        if (this.columns.items[h.ColumnIdx].Tip) {
                            tmp += "<img align=\"top\" src=\"/img/1px.gif\" class=\"help_w\"/>";
                        }
                        tmp += '</div></th>';
                    } else {
                        tmp += this.buildNodeOptionalAttributes('th', 'class', h.HeaderClass, 'ColSpan', h.ColSpan, 'RowSpan', h.RowSpan, 'id', hid);
                        tmp += this.buildNodeOptionalAttributes('div', 'class', h.HeaderClass);
                        tmp += h.Title + '</div></th>';
                    }
                }
            }
        }
        tmp += '</tr>';
        return tmp;
    }
    catch (e1) {
        handleError(e1, this.url, null);
    }
};
dataset.prototype.doexport = function (el, e, idx) {
    try {
        var url = evalParams(this.urlBase + '.' + e);
        var div = el.parentNode;
        var ids = false;
        if (this.checkboxSelectionField != '') {
            var cb = div.getElementsByTagName('input')[0];
            if (cb.checked === true) {
                ids = this.selectedValues.items.join(',');
                if (ids.length === 0) {
                    alert('no items selected');
                    return;
                }
                this.parameters[this.checkboxSelectionField] = ids;
            } else {
                this.parameters[this.checkboxSelectionField] = undefined;
            }
        }
        var newform = document.createElement('form');
        newform.setAttribute('action', url);
        newform.setAttribute('method', 'post');
        var key;
        for (key in this.parameters) {
            if (this.parameters[key]) {
                var opElement = document.createElement('input');
                opElement.setAttribute('type', 'hidden');
                opElement.setAttribute('id', key);
                opElement.setAttribute('name', key);
                opElement.setAttribute('value', this.parameters[key]);
                newform.appendChild(opElement);
            }
        }

        document.body.appendChild(newform);
        newform.submit();
    }
    catch (ee) {
        alert(ee);
    }
    return false;
};
dataset.prototype.buildExports = function (idx) {
    var res = '';
    if (this.data.Export) {
        res += '<a href=\"javascript:void(0);\" class=\"export\" onclick="showExportDiv(this, \'' + this.externalVariable + '\', ' + idx + ', \'' + this.checkboxSelectionField + '\')">Export</a>';
    } else {
        res = ' ';
    }
    return res;
};
dataset.prototype.renderHeader = function () {
    return this.getHeaderTemplate();
};
dataset.prototype.renderErrorMessage = function () {
    var res = '<div style="width:' + this.estimatedWidth + 'px; height: ' + this.estimatedHeight + 'px;" id="' + this.id + 'error" class="dataerror"><p style="line-height:' + (this.estimatedHeight - 60) + 'px">The data is unavailable for the moment. Please try again later.</p></div>';
    return res;
};
dataset.prototype.renderLoadingFiller = function () {    
    var res = '<div style="width:' + this.estimatedWidth + 'px; height: ' + this.estimatedHeight + 'px;" id="' + this.id + 'filler" class="dataloading';
    if (this.loadingDivAdditionalClasses) {
        res = res + ' ' + this.loadingDivAdditionalClasses;
    }
    res = res + '"><p style="line-height:' + (this.estimatedHeight - 30) + 'px">Loading...</p></div>';
    return res;
};
dataset.prototype.renderFooter = function () {
    if (this.hideHeader) {
        return '';
    }
    var tmp = '';
    if ((this.pageCount() > 1) || (this.bottomAdditionals)) {
        tmp = '<tr class="no"><td colspan="' + (this.columns.count() + (this.checkboxSelectionField ? 1 : 0)) + '">';
        if (this.pageCount() > 1) {
            tmp += '<div class="pagination">';
            tmp += this.buildPagination(5);
            if ((this.pageSizes) && (this.pageSizes.split(',').length > 1)) {
                tmp += '<br/><span class="pagesizeselector">Page Size: <select class="pagdrop" classname="pagdrop" onchange="' + this.externalVariable + '.changePageSize(this)">';
                var i = 0;
                var sizes = this.pageSizes.split(',');
                for (i = 0; i < sizes.length; i++) {
                    tmp += '<option value="' + sizes[i] + '"' + (sizes[i] == this.pageSize ? 'selected' : '') + '>' + sizes[i] + '</option>';
                }
                tmp += '</select></span>';
            }
            tmp += '</div>';

        }
        if (this.bottomAdditionals) {
            tmp += this.bottomAdditionals + '<br/>';
        }
        tmp += this.buildExports(1);
        tmp += '</td></tr>';
    }
    return tmp;
};
dataset.prototype.build = function () {
    if (this.loadingDiv) {
        this.loadingDiv.style.display = 'none';
    }
    var htmlContent = "";
    var iloop = 0;
    try {
        if (this.error) {
            htmlContent = this.renderErrorMessage();
        } else if (!this.data) {
            htmlContent = this.renderLoadingFiller();
        } else {
            if ((this.data.items) && (this.data.items.length > 0)) {
                var li, ui;
                if (this.clientProcessing) {
                    li = this.lowerIndex() - 1;
                    ui = this.upperIndex();
                } else {
                    li = 0;
                    ui = this.data.items.length;
                }

                for (iloop = li; iloop < ui; iloop++) {
                    if (this.data.items[iloop]) {
                        htmlContent += this.renderItem(iloop, this.data.items[iloop]);
                    }
                }
                htmlContent = this.renderHeader() + htmlContent + this.renderFooter();
            } else {

                var wrap = "<tr><td colspan=\"" + this.headers.length + "\">"
                var endwrap = "</td></tr>";
                htmlContent = this.renderHeader() + wrap + this.noResultsTemplate + endwrap + this.renderFooter();
                if (this.onNoResults !== null) {
                    try {
                        this.onNoResults();
                    }
                    catch (ex) {                    
                    }
                }
            }
        }
    }
    catch (e) {        
        handleError(e, window.location, null);
    }
    if (!document.getElementById(this.id)) return false;
    document.getElementById(this.id).innerHTML = htmlContent;
    if (this.customBuild) {
        var f = this.customBuild;
        var d = this.data;
        setTimeout(function () { f(d) }, 1000);
    }
    if (this.afterScript) {
        setTimeout(this.afterScript, 1000);
    }
    fixMinWidthForIE(document.getElementById(this.id));
};
dataset.prototype.startLoading = function () {
    this.loadTemplate();
    document.getElementById(this.id).innerHTML = this.template.loading;
};
dataset.prototype.count = function () {
    return this.data.items.length;
};
dataset.prototype.endLoading = function () {
    this.build();
};
dataset.prototype.pageCount = function () {
    return Math.ceil(this.data.TotalRowCount / this.pageSize);
};
dataset.prototype.lowerIndex = function () {
    return (this.pageIndex) * (this.pageSize) + 1;
};
dataset.prototype.upperIndex = function () {
    var t = (this.pageIndex + 1) * (this.pageSize);
    if (this.data) {
        if (t > this.data.TotalRowCount) {
            return this.data.TotalRowCount;
        }
    }
    return t;
};

