﻿// 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 );
}

// IndexOf method needs to be added manually for IE ///////
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
    
// Clones an object.    
function cloneObject (theObject) {
    var objectClone = new theObject.constructor();
    for (var property in theObject)
        objectClone[property] = theObject[property];
    return objectClone;
}

function clone(myObj)
{
	if(typeof(myObj) != 'object') return myObj;
	if(myObj == null) return myObj;

	var myNewObj = new Object();

	for(var i in myObj)
		myNewObj[i] = clone(myObj[i]);

	return myNewObj;
}

//Object.prototype.clone2 = function() {
//  var newObj = (this instanceof Array) ? [] : {};
//  for (i in this) {
//    if (i == 'clone2') continue;
//    if (this[i] && typeof this[i] == "object") {
//      newObj[i] = this[i].clone2();
//    } else newObj[i] = this[i]
//  } return newObj;
//};

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();