﻿Type.registerNamespace("Res.Commerce.Web.Composants");
Res.Commerce.Web.Composants.ScriptControlBase = function (element) {
    Res.Commerce.Web.Composants.ScriptControlBase.initializeBase(this, [element]);

    this._clientStateField = null;
    this._clientState = null;
    this._enableClientState = null;
    this._isFirstLoad = true;

    this._onSubmitHandler = null;
    this._onPartialUpdateEnd = null;
    this._dataSource = null;
    this._composantsEnfants = null;
    this._enfantsPotentiels = null;
}
Res.Commerce.Web.Composants.ScriptControlBase.prototype =
{
    initialize: function() {
        Res.Commerce.Web.Composants.ScriptControlBase.callBaseMethod(this, 'initialize');

        if (this._enableClientState) {
            this.loadClientState();

            this._onSubmitHandler = Function.createDelegate(this, this._onSubmit);
            this._onPartialUpdateEnd = Function.createDelegate(this, this._onUpdateEnd);

            if (typeof (Sys.WebForms) !== "undefined" && typeof (Sys.WebForms.PageRequestManager) !== "undefined") {
                Array.add(Sys.WebForms.PageRequestManager.getInstance()._onSubmitStatements, this._onSubmitHandler);
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._onPartialUpdateEnd);
            }
            else
                $addHandler(document.forms[0], "submit", this._onSubmitHandler);
        }
        if (typeof (Page) !== "undefined")
        {
            Page.set_ComponentReady();
            this._isFirstLoad = false;
        }
    },

    dispose: function() {
        if (typeof (Sys.WebForms) !== "undefined" && typeof (Sys.WebForms.PageRequestManager) !== "undefined") {
            if (this._onSubmitHandler) {
                Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(this._onPartialUpdateEnd);
                this._onSubmitHandler = null;
            }
            if (this._onSubmitHandler) {
                Array.remove(this._pageRequestManager._onSubmitStatements, this._onSubmitHandler);
                this._onSubmitHandler = null;
            }
        }
        else {
            if (this._onSubmitHandler) {
                $removeHandler(document.forms[0], "submit", this._onSubmitHandler);
                this._onSubmitHandler = null;
            }
        }

        if (this._clientStateField)
            this._clientStateField = null;
        if (this._clientState)
            this._clientState = null;

        Res.Commerce.Web.Composants.ScriptControlBase.callBaseMethod(this, 'dispose');
    },
     GetQueryString : function (key, default_) {
      if (default_==null) default_="";
      key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
      var qs = regex.exec(window.location.href);
      if(qs == null)
        return default_;
      else
        return qs[1];
    },
    loadClientState: function() {
        if (this._clientStateField.value == '' || this._clientStateField.value == 'undefined')
            return;
        this._clientState = Sys.Serialization.JavaScriptSerializer.deserialize(this._clientStateField.value);
    },

    saveClientState: function() {
        return null;
    },

    _onSubmit: function() {
        if (!this._clientStateField)
            return true;

        if (null != this.saveClientState()) {
            this._clientStateField.value = this.saveClientState();
        }

        return true;
    },

    _onUpdateEnd: function() {
        this.loadClientState();
        return true;
    },
    FormatId: function (selector) {
        return '#' + this._element.id + ' ' + selector;
    },
    get_clientStateField: function() {
        return this._clientStateField;
    },

    set_clientStateField: function(value) {
        if (this._clientStateField != value) {
            this._clientStateField = value;
        }
    },

    get_clientState: function() {
        return this._clientState;
    },

    set_clientState: function(value) {
        if (this._clientState != value) {
            this._clientState = value;
        }
    },
    get_EnableClientState: function() {
        return this._enableClientState;
    },

    set_EnableClientState: function(value) {
        this._enableClientState = value;
    },

    get_ComposantsEnfants: function () {
        return this._composantsEnfants;
    },

    set_ComposantsEnfants: function (value) {
        this._composantsEnfants = value;
    },
    get_EnfantsPotentiels: function () {
        return this._enfantsPotentiels;
    },
    set_EnfantsPotentiels: function (value) {
        this._enfantsPotentiels = value;
    },
    get_DataSource: function () {
        return this._dataSource;
    },
    set_DataSource: function (value) {
        this._dataSource = value;
    },
    get_IsFirstLoad: function () {
        return this._isFirstLoad;
    }
}

Res.Commerce.Web.Composants.ScriptControlBase.registerClass('Res.Commerce.Web.Composants.ScriptControlBase', Sys.UI.Control);
