﻿// © Copyright Strategy Builders Inc. 2000-2009. All rights reserved.
//   
// The entire contents of this file is protected by International and
// U.S.Copyright Laws. Unauthorized reproduction, distribution, 
// redistribution and reverse engineering of any or all portion of the code 
// contained in this file is strictly prohibited and may
// result in severe civil and criminal penalties and will be
// prosecuted to the maximum extent possible under the law.
//  
// RESTRICTIONS 
//  
// THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES  
// ARE CONFIDENTIAL AND PROPRIETARY TRADE  
// SECRETS OF Strategy Builders Inc. 
//  
// THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED
// FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE  
// COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE 
// AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  
// AND PERMISSION FROM Strategy Builders Inc. 
//  
// CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON 
// ADDITIONAL RESTRICTIONS.
//  

var GridControl_me;
function GridControl(id, data) {

    GridControl_me = this;
    
    ContainerControl.call(this, id);

    this._rowsPerPage = 20; // default value
    this._currentPageNumber = 0; // default select first page

    this._header = new Control('GridHeader');
    this._rows = new ListViewRepeaterControl('Rows', 'row', data);
    this._footer = new Control('GridFooter');

    this._rows.setStart(this._currentPageNumber);
    this._rows.setEnd(this._rowsPerPage);

    this.addControl(this._header);
    this.addControl(this._rows);
    this.addControl(this._footer);

    if (data != null) {
        this.setAttribute('RowCount', data.length);
        this._header.setAttribute('RowCount', data.length);
        this._footer.setAttribute('RowCount', data.length);

        this._header.setAttribute('rows', data);
        this._footer.setAttribute('rows', data);
    }


    this.setAttribute('CurrentPage', 0);
    this._header.setAttribute('CurrentPage', 0);
    this._footer.setAttribute('CurrentPage', 0);
}

GridControl.prototype = new ContainerControl();

GridControl.prototype.setDataSource = function(data) {
    this._rows.setCollection(data);
    this.setAttribute('RowCount', data.length);
    this._header.setAttribute('RowCount', data.length);
    this._footer.setAttribute('RowCount', data.length);
}

GridControl.prototype.appendDataSource = function(data) {
    this._rows.appendCollection(data);
    this.setAttribute('RowCount', this._rows.getCount());
    this._header.setAttribute('RowCount', this._rows.getCount());
    this._footer.setAttribute('RowCount', this._rows.getCount());
}

GridControl.prototype.setRowsPerPage = function(rowsPerPage) {
    this._rowsPerPage = rowsPerPage;
}

GridControl.prototype.setCurrentPage = function(currentPageNumber) {
    this._currentPageNumber = currentPageNumber;
    this.setAttribute('CurrentPage', this.getCurrentPage());
    this._header.setAttribute('CurrentPage', this.getCurrentPage());
    this._footer.setAttribute('CurrentPage', this.getCurrentPage());
    //    this.setGridCurrentPageNumberFromCookie();
}

GridControl.prototype.FirstPage_EventHandler = function(e) {
    var targ;
    if (!e) e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    me.firstPage();
    me.setGridCurrentPageNumberToCookie();
    me.getEventHandlers()['FirstPage'](me);
}

GridControl.prototype.LastPage_EventHandler = function(e) {

    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    me.lastPage();
    me.setGridCurrentPageNumberToCookie();
    me.getEventHandlers()['LastPage'](me);
}

GridControl.prototype.NextPage_EventHandler = function(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;

    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    me.nextPage();
    me.setGridCurrentPageNumberToCookie();
    me.getEventHandlers()['NextPage'](me);
}

GridControl.prototype.PreviousPage_EventHandler = function(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    me.previousPage();
    me.setGridCurrentPageNumberToCookie();
    me.getEventHandlers()['PreviousPage'](me);
}


GridControl.prototype.EditRow_EventHandler = function(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    var controls = targ.id.split('_');

    var me;
    me = eval(controls[0]);
    me.getEventHandlers()['EditRow'](me);
}

GridControl.prototype.RegisterEvents = function() {
    ContainerControl.prototype.RegisterEvents.call(this);

    for (var ControlEventName in this.getRegisterEventExpressions()) {
        //alert(this.getRegisterEventExpressions()[ControlEventName]);
        eval(this.getRegisterEventExpressions()[ControlEventName]);
    }
    for (var i = 0; i < this.getChildCount(); i++) {
        for (ControlEventName in this.getChild(i).getRegisterEventExpressions()) {
            //alert(ControlEventName);

            //alert(this.getChild(i).getRegisterEventExpressions()[ControlEventName]);
            eval(this.getChild(i).getRegisterEventExpressions()[ControlEventName]);
        }
    }
}

GridControl.prototype.firstPage = function() {
    this.setCurrentPage(0);
    //return this.getHTML();
}

GridControl.prototype.lastPage = function() {
    this.setCurrentPage((this.getRowCount() / this._rowsPerPage)-1);
    //return this.getHTML();
}

GridControl.prototype.previousPage = function() {
    var pageNumber = this.getCurrentPage();
    if (pageNumber <= 0) pageNumber = 0;
    else pageNumber = pageNumber - 1;
    this.setCurrentPage(pageNumber);
    //return this.getHTML();
}

GridControl.prototype.nextPage = function() {
    var pageNumber = this.getCurrentPage();
    if (pageNumber >= ((this.getRowCount()/this._rowsPerPage)-1) ) pageNumber = (this.getRowCount()/this._rowsPerPage) - 1;
    else pageNumber = pageNumber + 1;
    this.setCurrentPage(pageNumber);
    //return this.getHTML();
}

GridControl.prototype.getCurrentPage = function() {
    return this._currentPageNumber;
}

GridControl.prototype.getRowCount = function() {
    return this._rows.getCount();
}

GridControl.prototype.setHeaderTemplate = function(templateText) {
    this._header.setTemplate(templateText);
}

GridControl.prototype.setRowTemplate = function (templateText) {
    this._rows.setTemplate(templateText);
}

GridControl.prototype.setRowsLoadingTemplate = function (templateText) {
    this._rows.setLoadingTemplate(templateText);
}

GridControl.prototype.RowsLoadingComplete = function () {
    this._rows.LoadingComplete();
}

GridControl.prototype.StartLoadingRows = function () {
    this._rows.StartLoading();
}

GridControl.prototype.isRowsLoading = function () {
    return this._rows.isLoading();
}

GridControl.prototype.setFooterTemplate = function (templateText) {
    this._footer.setTemplate(templateText);
}

GridControl.prototype.setHeaderAttribute = function(name, value) {
    this._header.setAttribute(name, value);
}

GridControl.prototype.setRowAttribute = function(name, value) {
    this._rows.setAttribute(name, value);
}

GridControl.prototype.setFooterAttribute = function(name, value) {
    this._footer.setAttribute(name, value);
}

GridControl.prototype.setDataCallbackFunction = function(cb) {
    this._rows.setCallbackFunction(cb);
}

// Following 2 functions are just to make sure
// back button brings back the correct last page
// in the grid
GridControl.prototype.getGridCurrentPageNumberFromCookie = function() {
    var GridCurrentPage = getCookie(this.getId() + '_GridCurrentPage');
    if (GridCurrentPage != null && GridCurrentPage != "") {
        return GridCurrentPage;
    }
    else {
        return 0;
    }
}

GridControl.prototype.setGridCurrentPageNumberToCookie = function(GridCurrentPageNumber) {
    setCookie(this.getId() + '_GridCurrentPage', this.getCurrentPage(), 1);
}

GridControl.prototype.getHTML = function() {

    var t = null;
    //t.call();
    this._rows.setStart(this._currentPageNumber * this._rowsPerPage);
    this._rows.setEnd((this._currentPageNumber * this._rowsPerPage) + this._rowsPerPage);
    return ContainerControl.prototype.getHTML.call(this);
}


