﻿function FwUtilsClass() {

    this.AddLoadEvent = function(func) {
        if (typeof (Sys) == "object") {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            if (prm && (prm.get_isInAsyncPostBack())) {
                prm.add_endRequest(function(sender, args) {
                    func();
                });
                return;
            }
        }

        var oldonload = window.onload;
        if (typeof window.onload == 'function') {
            window.onload = function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        } else {
            window.onload = func;
        }
    }

    this.AttachEvent = function(oTarget, sEventName, fNewFunction) {
        var oOldEvent = oTarget[sEventName];
        if (typeof oOldEvent == "function") {
            oTarget[sEventName] = function(e) {
                oOldEvent(e);
                fNewFunction(e);
            }
        } else {
            oTarget[sEventName] = fNewFunction;
        }
    }


    this.GetJsIdControls = function(parentNode) {
        var controls = new Object();

        function LoopChildren(parentNode) {
            if (parentNode && (parentNode.getAttribute)) {
                var jsid = parentNode.getAttribute("jsid");
                if (jsid) {
                    var current = controls[jsid];
                    if (current) {
                        if (current instanceof Array) {
                            current[current.length] = parentNode;
                        } else {
                            controls[jsid] = new Array(controls[jsid], parentNode);
                        }
                    } else {
                        controls[jsid] = parentNode;
                    }
                }
            }
            if (parentNode.childNodes) {
                for (var index = 0; index < parentNode.childNodes.length; index++) {
                    LoopChildren(parentNode.childNodes[index]);
                }
            }
        }

        LoopChildren(parentNode);
        return controls;
    }


    function Observer() {
        var self = this;
        var oSubscriptions;

        function Subscription(sEventName, fCallBack) {
            this.EventName = sEventName;
            this.CallBack = fCallBack;
        }

        this.FireEvent = function(sEventName, oParams) {
            if (self.oSubscriptions != null) {
                for (var i = 0; i < self.oSubscriptions.length; i++) {
                    if (self.oSubscriptions[i] != null) {
                        if (self.oSubscriptions[i].EventName == sEventName) {
                            if (typeof (self.oSubscriptions[i].CallBack) == "function") {
                                self.oSubscriptions[i].CallBack(oParams);
                            }
                        }
                    }
                }
            }
        }

        this.Subscribe = function(sEventName, fCallback) {
            if (self.oSubscriptions == null) {
                self.oSubscriptions = new Array();
            }
            self.oSubscriptions[self.oSubscriptions.length] = new Subscription(sEventName, fCallback);
        }
    }

    //Use this to request an Ajax postback. oFunction would typically click a button in an UpdatePanel.
    //This routine queues a second postback if a first is already active.
    this.RequestAsyncPostback = function(oFunction) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        if (prm) {
            window.setTimeout(function() {
                var oFuncToCall = function(sender, args) {
                    prm.remove_endRequest(oFuncToCall);
                    FwUtils.RequestAsyncPostback(oFunction);
                }

                if (prm.get_isInAsyncPostBack()) {
                    prm.add_endRequest(oFuncToCall);
                } else {
                    oFunction();
                }
            }, 0);
        } else {
            oFunction();
        }
    }


    this.observer = new Observer();
}

var FwUtils = new FwUtilsClass();

