﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WebControls");

WebControls.DataSource = function(element) {
    WebControls.DataSource.initializeBase(this, [element]);

    // this._foreignID = null;
}

WebControls.DataSource.prototype = {
    initialize: function() {
        WebControls.DataSource.callBaseMethod(this, 'initialize');

        var that = this;

//        if (this._loadOnInit) {
//            this.loadList();
//        }                
        
//        this._targetControl.add_itemSave(
//            function(sender, e) {
//                that.saveItem(e.item);            
//            }
//        );

//        this._targetControl.add_itemDelete(
//            function(sender, e) {
//                that.deleteItem(e.item);
//            }
//        );

    },
    dispose: function() {
        //Add custom dispose actions here
        WebControls.DataSource.callBaseMethod(this, 'dispose');
    },
    
//    get_foreignID: function() {
//        return this._foreignID;
//    },
//    set_foreignID: function(value) {
//        this._foreignID = value;
//        
////        if (this._targetControl) {
////            this._targetControl.set_foreignID(value);
////        }
//    },
    
//    processGetPageCallback: function(result) {    
//        var rows = result.Rows;
//        if (this._paging) {
//            if (rows.length > 0) {
//                this._startRow = result.StartRow;
//                this._itemFromLabel.set_value(this._startRow);                                    
//            } else {
//                this._startRow = 1;
//                this._itemFromLabel.set_value(0);
//            }
//            this._itemToLabel.set_value(this._startRow + rows.length - 1);
//            this._itemCountLabel.set_value(result.TotalRowCount);
//        }
//        // this._tableView.set_itemCount(result.TotalRowCount);
//        this._targetControl.set_itemList(rows);        
//    },

    getListByForeignID: function(foreignID, filter, callbackFn) {
        var that = this;

        var serviceProxy = eval(this._serviceProxy);
        if (this._foreignFieldName) {        

            // Don't load if no foreignID
            if (foreignID > 0) {

                serviceProxy.GetListByForeignID(
                    this._daoName,
                    foreignID,
                    filter,
                    function(result) {                  
                        callbackFn(result);
                    },
                    webServiceError
                );
            } else {
                programError("foreignID must be > 0 in " + this.get_id());
                debugger;
            }
        } else {
            programError("No foreignFieldName");        
        }
    },
    
    saveItem: function(item, callbackFn) {
        var that = this;

        var serviceProxy = eval(this._serviceProxy);
    
        if (item[this._pkFieldName] > 0) {
            serviceProxy.Update(
                this._daoName,
                item,
                function(result) {
                    // that.loadList();
                    callbackFn(result);
                },
                webServiceError
            );
        } else {
            serviceProxy.Insert(
                this._daoName,
                item,
                function(result) {
                    // item[this._pkFieldName] = result;
                    // that.loadList();
                    callbackFn(result);
                },
                webServiceError
            );   
        }    
    },
    
    deleteItem: function(item, callbackFn) {
        var that = this;    
        var serviceProxy = eval(this._serviceProxy);
    
        serviceProxy.Delete(
            this._daoName,
            item,
            function(result) {
                // that.loadList();
                callbackFn(result);
            },
            webServiceError
        );
    }

//    loadList: function(id) {
//        var that = this;

//        var serviceProxy = eval(this._serviceProxy);

//        // Different web service calls if we're passing foreignID
//        if (this._foreignFieldName) {        

//            // Don't load if no foreignID
//            if (this._foreignID > 0) {
//        
//                if (!this._paging) {
//                    serviceProxy.GetListByForeignID(
//                        this._daoName,
//                        this._foreignID,
//                        this._filter || null,  // null,  // this._filterPanel.get_filter(),
//                        function(result) {                  
//                            // that._tableView.set_itemCount(result.length);      
//                            that._targetControl.set_itemList(result);
//                            if (id > 0) {
//                                that.set_selectedID(id);
//                            }
//                        },
//                        webServiceError
//                    );
//                } else if (id > 0) {
//                    serviceProxy.GetPageContainingItemByForeignID(
//                        this._daoName,
//                        this._foreignID,
//                        id,
//                        this._pageSize,
//                        null,
//                        null,
//                        this._filter || null,  // null, // this._filterPanel.get_filter(),
//                        function(result) {                        
//                            that.processGetPageCallback(result);                            
//                            that.set_selectedID(id);
//                        },
//                        webServiceError
//                    );
//                } else {
//                    serviceProxy.GetPageByForeignID(
//                        this._daoName,
//                        this._foreignID,
//                        this._startRow,
//                        this._pageSize,
//                        null,
//                        null,
//                        this._filter || null,  // null,  // this._filterPanel.get_filter(),
//                        function(result) {
//                            that.processGetPageCallback(result);
//                        },
//                        webServiceError
//                    );
//                }                
//            } else {
//                programError("No foreignID for grid " + this.get_id());
//            }
//        } else {
//            if (id > 0) {
//                serviceProxy.GetPageContainingItem(
//                    this._daoName,
//                    id,
//                    this._pageSize,
//                    null,
//                    null,
//                    this._filter || null,  // null, // this._filterPanel.get_filter(),
//                    function(result) {
//                        that.processGetPageCallback(result);
//                    },
//                    webServiceError
//                );
//            } else {
//                serviceProxy.GetPage(
//                    this._daoName,
//                    this._startRow,
//                    this._pageSize,
//                    null,
//                    null,
//                    this._filter || null,  // null,  // this._filterPanel.get_filter(),
//                    function(result) {
//                        that.processGetPageCallback(result);
//                    },
//                    webServiceError
//                );
//            }
//        }

//    }

//    saveItem: function(item) {    
//        var that = this;
//    
//        var serviceProxy = eval(this._serviceProxy);
//    
//        if (item[this._pkFieldName] > 0) {
//            serviceProxy.Update(
//                this._daoName,
//                item,
//                function(result) {
//                    // var item = result;                            
//                    // that.set_item(item);
//                    that.loadList();
//                },
//                webServiceError
//            );
//        } else {
//            serviceProxy.Insert(
//                this._daoName,
//                item,
//                function(result) {
//                    // that.set_itemID(result);
//                    // item.ID = result;
//                    item[this._pkFieldName] = result;
//                    that.loadList();
//                },
//                webServiceError
//            );   
//        }    
//    },
    
//    deleteItem: function(item) {
//        var that = this;    
//        var serviceProxy = eval(this._serviceProxy);
//    
//        serviceProxy.Delete(
//            this._daoName,
//            item,
//            function(result) {
//                that.loadList();
//            },
//            webServiceError
//        );        
//    }     
//        
}

// WebControls.DataSource.createProperty("targetControl");
WebControls.DataSource.createProperty("serviceProxy");
WebControls.DataSource.createProperty("daoName");
WebControls.DataSource.createProperty("pkFieldName");
WebControls.DataSource.createProperty("foreignFieldName");
WebControls.DataSource.createProperty("foreignID");
// WebControls.DataSource.createProperty("loadOnInit");
WebControls.DataSource.createProperty("paging");
WebControls.DataSource.createProperty("orderable");

// WebControls.DataSource.createEvent("itemSave");

WebControls.DataSource.registerClass('WebControls.DataSource', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();