﻿// Register the namespace for the control.
Type.registerNamespace('CommonControls');

//
// Define the behavior properties.
//
CommonControls.PageError = function(element) {
    CommonControls.PageError.initializeBase(this, [element]);
    this._onEndRequestHandler = null;
    this._onInitializeRequestHandler = null;
    this._errorPopupID = null;
    this._errorMessageID = null;
}

//
// Create the prototype for the behavior.
//
CommonControls.PageError.prototype = {

    initialize: function() {
        CommonControls.PageError.callBaseMethod(this, 'initialize');
        this._onEndRequestHandler = Function.createDelegate(this, this._onEndRequest);
        this._onInitializeRequestHandler = Function.createDelegate(this, this._onInitializeRequest);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._onEndRequestHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(this._onInitializeRequestHandler);
    },

    dispose: function() {
        Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(this._onEndRequestHandler);
        Sys.WebForms.PageRequestManager.getInstance().remove_initializeRequest(this._onInitializeRequestHandler);
        CommonControls.PageError.callBaseMethod(this, 'dispose');
    },

    //
    // Event delegates
    //
    _onEndRequest: function(sender, args) {
        if (args.get_response().get_responseAvailable() && args.get_error() != undefined) {
            if (args.get_response().get_statusCode() == '200') {
                var message = args.get_error().message;
                var pos = message.indexOf('#CC.PageError');
                if (pos >= 0) {
                    var messagePos = message.indexOf('#', pos + 1);
                    if (confirm(message.substr(messagePos + 1))) {
                        this.get_element().value = message.substr(pos + 13, messagePos - pos - 13);
                        this._postBackElement.click();
                        this.get_element().value = "";
                    }
                }
                else {
                    $get(this.get_errorMessageID()).contentWindow.document.write(message.substr(message.indexOf('#') + 1));
                    $get(this.get_errorMessageID()).contentWindow.document.close();
                    $find(this.get_errorPopupID()).show();
                }
            }
            else if (args.get_response().get_responseData()) {
                $get(this.get_errorMessageID()).contentWindow.document.write(args.get_response().get_responseData());
                $get(this.get_errorMessageID()).contentWindow.document.close();
                $find(this.get_errorPopupID()).show();
            }
        }
        args.set_errorHandled(true);
    },

    _onInitializeRequest: function(sender, args) {
        this._postBackElement = args.get_postBackElement();
    },

    //
    // Control properties
    //
    get_errorPopupID: function() {
        return this._errorPopupID;
    },

    set_errorPopupID: function(value) {
        if (this._errorPopupID !== value) {
            this._errorPopupID = value;
            this.raisePropertyChanged('errorPopupID');
        }
    },

    get_errorMessageID: function() {
        return this._errorMessageID;
    },

    set_errorMessageID: function(value) {
        if (this._errorMessageID !== value) {
            this._errorMessageID = value;
            this.raisePropertyChanged('errorMessageID');
        }
    }
}

CommonControls.PageError.descriptor = {
    properties:
    [
        { name: 'errorPopupID', type: String },
        { name: 'errorMessageID', type: String }
    ]
}

// Register the class as a type that inherits from Sys.UI.Control.
CommonControls.PageError.registerClass('CommonControls.PageError', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();