﻿// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property.
//
// Remember that both are case sensitive!
//

Type.registerNamespace('OnLineExt');

OnLineExt.ChangeCheckBehavior = function(element) {

    OnLineExt.ChangeCheckBehavior.initializeBase(this, [element]);
    this._TargetControlsID = null;
    this._onclickHandler = null;
    this._EnTargets = true;
    this._UserPostBack = null;
    this._UserPostBackArg = OnLineExt.PostBackArg.undef;
    this._ValuePostBack = null;
    this._FindInContainer = true;
}

OnLineExt.ChangeCheckBehavior.prototype = {

    initialize: function() {
        OnLineExt.ChangeCheckBehavior.callBaseMethod(this, 'initialize');

        this._onclickHandler = Function.createDelegate(this, this._onclick);
        var e = this.get_element();
        $addHandler(e, "click", this._onclickHandler);
        if (e.checked) {
            this._populateTargets();
        }
    },

    dispose: function() {
        if (this._onclickHandler) {
            $removeHandler(this.get_element(), "click", this._onclickHandler);
        }
        OnLineExt.ChangeCheckBehavior.callBaseMethod(this, 'dispose');
    },


    get_FindInContainer: function() {
        return this._FindInContainer;
    },

    set_FindInContainer: function(value) {
        this._FindInContainer = value;
    },


    get_TargetControlsID: function() {
        return this._TargetControlsID;
    },

    set_TargetControlsID: function(value) {
        this._TargetControlsID = value;
    },

    get_UserPostBack: function() {
        return this._UserPostBack;
    },

    set_UserPostBack: function(value) {
        this._UserPostBack = value;
    },

    get_ValuePostBack: function() {
        return this._ValuePostBack;
    },

    set_ValuePostBack: function(value) {
        this._ValuePostBack = value;
    },

    get_UserPostBackArg: function() {
        return this._UserPostBackArg;
    },

    set_UserPostBackArg: function(value) {
        this._UserPostBackArg = value;
    },


    get_EnTargets: function() {
        return this._EnTargets;
    },

    set_EnTargets: function(value) {
        this._EnTargets = value;
    },


    _populateTargets: function() {
        if (this._TargetControlsID != null) {
            var ss = this._TargetControlsID.split(",");
            if (this._EnTargets) {
                for (s in ss) {
                    var e2 = $get(ss[s]);
                    e2.setAttribute('disabled', true);
                }
                var e3 = $get(ss[0]);
                e3.removeAttribute('disabled');
            }
            else {
                for (s in ss) {
                    var e2 = $get(ss[s]);
                    e2.removeAttribute('disabled');
                }
                var e3 = $get(ss[0]);
                e3.setAttribute('disabled', true);
            }
        }
    },

    _onclick: function(e) {
        if (this._UserPostBack == null || this._UserPostBack == "" || this._UserPostBackArg == OnLineExt.PostBackArg.undef)
            this._populateTargets();
        else {
            var postbackCode = "";
            switch (this._UserPostBackArg) {
                case OnLineExt.PostBackArg.value:
                    postbackCode = this._UserPostBack.replace("value", e.target.value);
                    window.setTimeout(postbackCode, 0);
                    break;
                case OnLineExt.PostBackArg._checked:
                    postbackCode = this._UserPostBack.replace("_checked", e.target.checked);
                    window.setTimeout(postbackCode, 0);
                    break;
                case OnLineExt.PostBackArg.innerText:
                    postbackCode = this._UserPostBack.replace("innerText", e.target.innerHTML);
                    window.setTimeout(postbackCode, 0);
                    break;
                case OnLineExt.PostBackArg.myvalue:
                    if (this._FindInContainer) {
                        var c = Sys.Application.getComponents();
                        for (var i = 0; i < c.length; i++) {
                            var tpVar = Object.getType(c[i]);
                            if (tpVar.implementsInterface(OnLineExt.Common.IUserPostBack)) {
                                c[i].RunPostBack(this._UserPostBack, this._ValuePostBack);
                                return;
                            }
                        }
                    }
                    postbackCode = this._UserPostBack.replace("myvalue", this._ValuePostBack);
                    window.setTimeout(postbackCode, 0);
                    break;
            }
        }
    }
}

OnLineExt.ChangeCheckBehavior.registerClass('OnLineExt.ChangeCheckBehavior', AjaxControlToolkit.BehaviorBase);
