﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WebControls");

WebControls.FormView = function(element) {
    WebControls.FormView.initializeBase(this, [element]);

    this._itemID;    
}

WebControls.FormView.prototype = {
    initialize: function() {
        WebControls.FormView.callBaseMethod(this, 'initialize');

        var that = this;

        var fields = this.get_fields();
        this._viewControls = {};
        this._editControls = {};
        this._deleteConfirmControls = {};
        for (var i in fields) {
            var field = fields[i];
            if (field.ViewControlID) {
                this._viewControls[field.FieldName] = $find(field.ViewControlID);
            }
            if (field.EditControlID) {
                this._editControls[field.FieldName] = $find(field.EditControlID);
            }
            if (field.DeleteConfirmControlID) {
                this._deleteConfirmControls[field.FieldName] = $find(field.DeleteConfirmControlID);
            }
        }

        if (this._editButton) {
            this._editButton.add_buttonClicked(
                function(sender, e) {
                    that._raiseEvent("editClicked", {});
                    that.showEdit();
                }
            );
        }
        if (this._deleteButton) {
            this._deleteButton.add_buttonClicked(
                function(sender, e) {
                    that.showDeleteConfirm();
                }
            );
        }
        if (this._saveButton) {
            this._saveButton.add_buttonClicked(
                function(sender, e) {                
                    var item = that.get_item();                            
                    if (that.validateEditControls()) {        
                        that._raiseEvent("itemSave", {item: item});                                
                    } else {
                        that.showMessage("One or more fields are invalid");
                    }        
                }                        
            );
        }
        if (this._cancelButton) {
            this._cancelButton.add_buttonClicked(
                function(sender, e) {
                    var item = that._item; // that.get_item();
                    if (item && item.ID > 0) {
                        that.showDefaultView();
                    } else {
                        that.showStartView();
                    }                    
                    that._raiseEvent("cancelClicked", { item: item });
                }
            );
        }
        if (this._deleteYesButton) {
            this._deleteYesButton.add_buttonClicked(
                function(sender, e) {
                    // that.showDefaultView();
                    // that.deleteItem();
                    var item = that.get_item();                            
                    that._raiseEvent("itemDelete", {item: item});
                }
            );
        }
        if (this._deleteNoButton) {
            this._deleteNoButton.add_buttonClicked(
                function(sender, e) {
                    that.showDefaultView();
                }
            );
        }
        
        // this.showDefaultView();
        this.showStartView();
    },
    dispose: function() {
        //Add custom dispose actions here
        WebControls.FormView.callBaseMethod(this, 'dispose');
    },
    
    showStartView: function() {
        // Shows the start depending on
        // which templates have been set up
        if (this._notSelectedPanelID) {
            this.showNotSelected();
        } else {
            this.showDefaultView();
        }    
    },    
    showDefaultView: function() {
        // Shows the appropriate view depending on
        // which templates have been set up
        if (this._viewPanelID) {
            this.showView();
        } else {
            this.showEdit();
        }    
    },    
    showNotSelected: function() {
        this._panelSet.showPanel(this._notSelectedPanelID);
    },
    showLoading: function() {
        // this._panelSet.showPanel("NotSelectedPanel");
        this._panelSet.showPanel(this._loadingPanelID);
    },
    showView: function() {
        this._panelSet.showPanel(this._viewPanelID);
    },
    showEdit: function() {
        // Reset the controls to the stored item
        // (in case they were changed in a previous edit that was cancelled)
        if (this._item) {
            // this.set_item(this._item);
            this.setControlsFromItem(this._item);
        }
        this._panelSet.showPanel(this._editPanelID);
    },
    showDeleteConfirm: function() {
        this._panelSet.showPanel(this._deleteConfirmPanelID);
    },
    
    showAddIndicator: function() {
        if (this._addIndicatorDiv) {
            this._addIndicatorDiv.style.display = '';
        }
    },
    hideAddIndicator: function() {
        if (this._addIndicatorDiv) {
            this._addIndicatorDiv.style.display = 'none';
        }
    },
    showEditOnly: function() {
        if (this._editOnlyDiv) {
            this._editOnlyDiv.style.display = '';
        }
    },
    hideEditOnly: function() {
        if (this._editOnlyDiv) {
            this._editOnlyDiv.style.display = 'none';
        }
    },
    
    showMessage: function(message) {
        alert(message);  // FOR NOW - Can optionally show in message label later
    },
    
    get_item: function() {    
        var item = this._item;
        if (item) {
            for (var i in this._editControls) {
                item[i] = this._editControls[i].get_value();
            }
        } else {
            alert("Trying to get a null item");
        }
        return item;   
    },
    
    setControlsFromItem: function(item) {
        for (var i in this._viewControls) {
            this._viewControls[i].set_value(item[i]);
        }
        for (var i in this._editControls) {
            // Clear the validation highlighting
            this._editControls[i].clearInvalidHighlighting();
            this._editControls[i].set_value(item[i]);
        }
        for (var i in this._deleteConfirmControls) {
            this._deleteConfirmControls[i].set_value(item[i]);
        }    
    },
    
    set_item: function(item) {    
        
        this.setControlsFromItem(item);        
        
        // Also save a copy of the item
        this._item = item;    

        // Hide the add indicator
        // (it will be shown if it's a new item)
        this.hideAddIndicator();
        this.showEditOnly();
        
        // If there is a view template, we should
        // make sure we are in view mode
        // (Can we leave it to the caller to do this?)
//        if (this._viewPanelID) {        
//            var that = this;
//            // setTimeout(function() {
//                that.showView();
//            // }, 600);                                    
//        }
    },
    
    get_itemID : function() {
        return this._itemID;
    },
    
//    // This triggers the loading of the record
//    // (should show a loading panel while it's loading)
//    set_itemID: function(id) {
//        // alert("id: " + id);    
//        this._itemID = id;
//        this.loadItem();
//    },
    
    setupNewItem : function() {
        if (this._itemClass) {
            var newItem;
            newItem = eval("new " + this._itemClass + "()");
            
            if (this._foreignFieldName && ! (this._foreignID > 0)) {
                programError("ForeignFieldName supplied but foreignID is not > 0");
            }
            
            if (this._foreignID) {
                newItem[this._foreignFieldName] = this._foreignID;
            }

            this._raiseEvent("constructingNewItem", { item: newItem });    
            this.set_item(newItem);    
            this.showAddIndicator();
            this.hideEditOnly();
            this.showEdit();    
        
        } else {
            programError("No item class specified");
        }    
    
    },
    
//    loadItem: function() {
//        var that = this;
//    
//        if (this._serviceProxy) {
//                   
//            var serviceProxy = eval(this._serviceProxy);
//        
//            if (this._itemID > 0) {
//                serviceProxy.Get(
//                    this._daoName,
//                    this._itemID,
//                    function(result) {
//                        var item = result;
//                        
//                        that.set_item(item);
//                    },
//                    webServiceError
//                );
//            } else {
//                programError("ID is not > 0");
//            }
//        } else {
//            programError("ServiceProxy not specified");
//        }
//    },
//        
    validateEditControls: function() {
        var valid = true;
        for (var i in this._editControls) {
            var control = this._editControls[i];
            if (!control.validate()) {
                valid = false;
            }
        }

//        // Allow for custom validation
//        var eventArgs = { "valid": true };
//        this._raiseEvent('customValidation', eventArgs);
//        if (eventArgs.valid == false) {
//            valid = false;
//        }

        return valid;
    }
    
//    saveItem: function() {
//        var that = this;    
//        var item = this.get_item();
//                
//        if (this.validateEditControls()) {        
//            this._raiseEvent("itemSave", {item: item});

//            if (this._serviceProxy) {
//                       
//                var serviceProxy = eval(this._serviceProxy);
//            
//                if (item.ID > 0) {
//                    serviceProxy.Update(
//                        this._daoName,
//                        item,
//                        function(result) {
//                            // var item = result;                            
//                            // that.set_item(item);
//                            that.loadItem();
//                            that._raiseEvent("itemSaved", {id: that._itemID});   
//                            if (that._messageLabel) {
//                                that._messageLabel.flashMessage("Item Saved");
//                            }
//                        },
//                        webServiceError
//                    );
//                } else {
//                    // programError("ID is not > 0");
//                    serviceProxy.Insert(
//                        this._daoName,
//                        item,
//                        function(result) {
//                            // var item = result;                            
//                            // that.set_item(item);
//                            that.set_itemID(result);
//                            that.loadItem();
//                            that._raiseEvent("itemSaved", {id: that._itemID});   
//                            if (that._messageLabel) {
//                                that._messageLabel.flashMessage("Item Saved");
//                            }
//                        },
//                        webServiceError
//                    );                    
//                }
//            }
//            
//            // Not sure if we should go back to the view mode
//            // here automatically or rely on the caller to do that
//            // Could be optional based on a parameter?
//            // (Also we are not necessarily going back to 
//            // view mode anyway, eg if it's being used a a filter panel etc)
//            
//        } else {
//            this.showMessage("One or more fields are invalid");
//        }        
//    },
    
//    deleteItem: function() {

//        var that = this;
//    
//        var item = this.get_item();

//        if (this.validateEditControls()) {        
//            this._raiseEvent("itemDelete", {item: item});

//            if (this._serviceProxy) {
//                       
//                var serviceProxy = eval(this._serviceProxy);
//            
//                serviceProxy.Delete(
//                    this._daoName,
//                    item,
//                    function(result) {
//                        // var item = result;                            
//                        // that.set_item(item);
//                        that._itemID = null;
//                        that.set_item({});
//                        // that.loadItem();
//                        that.showDefaultView();
//                        that._raiseEvent("itemDeleted", {});   
//                        if (that._messageLabel) {
//                            that._messageLabel.flashMessage("Item Deleted");
//                        }
//                    },
//                    webServiceError
//                );
//            }
//            
//            // Not sure if we should go back to the view mode
//            // here automatically or rely on the caller to do that
//            // Could be optional based on a parameter?
//            // (Also we are not necessarily going back to 
//            // view mode anyway, eg if it's being used a a filter panel etc)
//            
//        } else {
//            this.showMessage("One or more fields are invalid");
//        }        
//    }

}
WebControls.FormView.createProperty("fields");
WebControls.FormView.createProperty("panelSet");

// These may or may not exist...
WebControls.FormView.createProperty("editButton");
WebControls.FormView.createProperty("deleteButton");
WebControls.FormView.createProperty("saveButton");
WebControls.FormView.createProperty("cancelButton");
WebControls.FormView.createProperty("deleteYesButton");
WebControls.FormView.createProperty("deleteNoButton");
WebControls.FormView.createProperty("messageLabel");
WebControls.FormView.createProperty("saveMessage");

WebControls.FormView.createProperty("notSelectedPanelID");
WebControls.FormView.createProperty("loadingPanelID");
WebControls.FormView.createProperty("viewPanelID");
WebControls.FormView.createProperty("editPanelID");
WebControls.FormView.createProperty("deleteConfirmPanelID");
WebControls.FormView.createProperty("addIndicatorDiv");
WebControls.FormView.createProperty("editOnlyDiv");

//WebControls.FormView.createProperty("serviceProxy");
//WebControls.FormView.createProperty("daoName");
WebControls.FormView.createProperty("itemClass");
// WebControls.FormView.createProperty("loadOnInit");
WebControls.FormView.createProperty("itemID"); /* We don't want to be using a special setter */
WebControls.FormView.createProperty("foreignID");
WebControls.FormView.createProperty("foreignFieldName");

// WebControls.FormView.createEvent("saveClicked");
WebControls.FormView.createEvent("constructingNewItem");
WebControls.FormView.createEvent("editClicked");
WebControls.FormView.createEvent("cancelClicked");
WebControls.FormView.createEvent("itemSave");
WebControls.FormView.createEvent("itemSaved");
WebControls.FormView.createEvent("itemDelete");
WebControls.FormView.createEvent("itemDeleted");

WebControls.FormView.registerClass('WebControls.FormView', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();