﻿// Helpers

// Automate the declaration of properties
Type.prototype._createGetter = function(fieldName) {
    return function() {
        return this[fieldName];
    }
}
Type.prototype._createSetter = function(fieldName) {
    return function(value) {
        this[fieldName] = value;
    }
}
Type.prototype.createProperty = function(propName) {
    var fieldName = '_' + propName;

    var getter = this._createGetter(fieldName);
    var setter = this._createSetter(fieldName);

    this.prototype['get_' + propName] = getter;
    this.prototype['set_' + propName] = setter;
}

// Automate the creation of events
Type.prototype._createAddHandler = function(eventName) {
    return function(handler) {
        this.get_events().addHandler(eventName, handler);
    }
}
Type.prototype._createRemoveHandler = function(eventName) {
    return function(handler) {
        this.get_events().removeHandler(eventName, handler);
    }
}
Type.prototype.createEvent = function(eventName) {
    var addHandler = this._createAddHandler(eventName);
    var removeHandler = this._createRemoveHandler(eventName);

    this.prototype['add_' + eventName] = addHandler;
    this.prototype['remove_' + eventName] = removeHandler;

    if (!this.__events) {  // This is so this method is only added once
        if (!this.inheritsFrom(Sys.Component)) {
            this.prototype.get_events = function() {
                if (!this._events) {
                    this._events = new Sys.EventHandlerList();
                }

                return this._events;
            }
        }

        this.prototype._raiseEvent = function(eventName, eventArgs) {
            var handler = this.get_events().getHandler(eventName);

            if (handler) {
                if (!eventArgs) {
                    eventArgs = Sys.EventArgs.Empty;
                }
                handler(this, eventArgs);
            }
        }

        this.__events = true;

    }
}

function programError(message) {
    alert("ERROR: " + message); // FOR NOW
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
){
    var strText = this;
    var intIndexOfMatch = strText.indexOf( strTarget );
     
    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1){
        // Relace out the current instance.
        strText = strText.replace( strTarget, strSubString )
          
        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf( strTarget );
    }
     
    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
}

// Clones an object.    
function cloneObject (theObject) {
    var objectClone = new theObject.constructor();
    for (var property in theObject)
        objectClone[property] = theObject[property];
    return objectClone;
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();