function SimpleTree( id, parentNode ) {
    this.init( id, parentNode );
};
SimpleTree.prototype.init = function( id, parentNode  ) {
	this.id = id;
	this.parent = parentNode;
	this.childList = new Array();
	this.childMap = new Object();
};
SimpleTree.prototype.getId = function() {
    return this.id;
};
SimpleTree.prototype.getParent = function() {
    return this.parent;
};
SimpleTree.prototype.getChildById = function( childId ) {
    return this.childMap[ childId ];
};
SimpleTree.prototype.addChildById = function( childId ) {
    var childNode = new SimpleTree( childId, this );
    this.childMap[ childId ] = childNode;
    this.childList.push( childNode );
    return childNode;
};
SimpleTree.prototype.getChilds = function() {
    return this.childList;
};
SimpleTree.prototype.findChildById = function( childId ) {
    var childNode = getChildById( childId );
    if( childNode != null ) {
        for( var i=0; i < this.childList.length; i++ ) {
            childNode = this.childList[i].findChildById( childId );
            if( childNode != null ) {
                break;
            }
        }
    }
    return childNode;
};

SimpleTree.prototype.attachFormElement = function( formInputElement ) {
    if( formInputElement != null ) {
        this.formNode = new FormNode( formInputElement );
        formInputElement.treeNode = this;
        formInputElement.onclick = function() {
            this.treeNode.traverse();
        }
    }
}
SimpleTree.prototype.getAllChildsState = function (){
    var checkedState = true;
    for( var i=0; i < this.childList.length; i++ ) {
        if( !this.childList[i].isEnabled() ) {
            checkedState = false;
            break;
        }
    }
    if( checkedState ) {
        for( var i=0; i < this.childList[i].length; i++ ) {
            if( !this.childList.getAllChildsState() ) {
                checkedState = false;
                break;
            }
        }
    }
    return checkedState;
};
SimpleTree.prototype.isEnabled = function() {
    return this.formNode.getMetaData().isChecked();
}
SimpleTree.prototype.setState = function ( checkedState ) {
    if( checkedState ) {
        this.formNode.setSelectedValue( this.formNode.getValue() );
    } else {
        this.formNode.removeSelectedValue( this.formNode.getValue() );
    }
};
SimpleTree.prototype.traverse = function () {
    this.traverseChilds();
    this.traverseParent();
};
SimpleTree.prototype.traverseChilds = function () {
    var checkedState = this.isEnabled();
    for( var i=0; i < this.childList.length; i++ ) {
        this.childList[i].setState( checkedState );
        this.childList[i].traverse();
    }
};
SimpleTree.prototype.traverseParent = function () {
    if( this.parent != null ) {
        if( this.isEnabled() ) {
            if( this.parent.getAllChildsState() ) {
                this.parent.setState( true );
                this.parent.traverseParent();
            }
        } else {
            this.parent.setState( false );
            this.parent.traverseParent();
        }
    }
};

