﻿// © Copyright Strategy Builders Inc. 2000-2009. All rights reserved.
//   
// The entire contents of this file is protected by International and
// U.S.Copyright Laws. Unauthorized reproduction, distribution, 
// redistribution and reverse engineering of any or all portion of the code 
// contained in this file is strictly prohibited and may
// result in severe civil and criminal penalties and will be
// prosecuted to the maximum extent possible under the law.
//  
// RESTRICTIONS 
//  
// THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES  
// ARE CONFIDENTIAL AND PROPRIETARY TRADE  
// SECRETS OF Strategy Builders Inc. 
//  
// THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED
// FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE  
// COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE 
// AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  
// AND PERMISSION FROM Strategy Builders Inc. 
//  
// CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON 
// ADDITIONAL RESTRICTIONS. 
//  

function ContainerControl(id) {

    this._children = new Array();
    this._contains = new Array();
    Control.call(this, id);
}

ContainerControl.prototype = new Control();
ContainerControl.prototype._children;

ContainerControl.prototype.getChildCount = function() {
    var size = 0;
    for (var i in this._children) {
        size = size + 1;
    }
    return this._children.length;
}

ContainerControl.prototype.getChild = function(i) {
    return this._children[i];
}

ContainerControl.prototype.getChildByName = function(name) {

    for (var i in this._children) {
        if (this.getChild(i).getId() == name) {
            return this.getChild(i);
        }
    }

    throw new Error('Child with name ' + name + ' not found in control ' + this.getId());
}

ContainerControl.prototype.getHTML = function() {
    var htmltext = Control.prototype.getHTML.call(this);

    var outHtml = htmltext;
    for (var i in this._children) {
        outHtml = outHtml.replace('[%' + this._children[i].getId() + '%]', this._children[i].getHTML());
    }


    return outHtml;
}

ContainerControl.prototype.RegisterEvents = function() {
    Control.prototype.RegisterEvents.call(this);

    for (var i in this._children) {
        this._children[i].RegisterEvents();
    }

    
}

ContainerControl.prototype.addControl = function(child) {
    var childName = child.getId();
    if (this._contains[childName] != null && this._contains[childName]) {
        alert('child ' + childName + ' already present in the container control ' + this.getName());
        return false;
    }
    else {
        this._contains[childName] = true;

        child.setParent(this);
        var newid = this._children.length;
        this._children[newid] = child;
        this._children[newid].setParent(this);
        if (this._children[newid].getParent() == null) { alert(this._children[newid].getId() + '\'s os null' ) };
        return newid;
    }
}

ContainerControl.prototype.removeControl = function(i) {
        var childRemoved = this._children[i];
        childRemoved.setParent(null);
        var size = this.getChildCount();
        if (i > size) { alert('out of range i=' + i); return false; }
        this._children.splice(i, 1);
        this._contains[this._children[i]] = false;
        return childRemoved;
}


