var arr_mapname = new Array();
var arr_checkbox = new Array();
var arr_mapindex = new Array();
var ueberArray = new Array();
var puffArray = new Array();

// Array Remove - By John Resig (MIT Licensed)
Array.remove = function(array, from, to) {
	//alert(from);
	var rest = array.slice((to || from) + 1 || array.length);
	array.length = from < 0 ? array.length + from : from;
	return array.push.apply(array, rest);
};


function CMapCheckClass(id){
	this.rootId = id;

	this.childs = new Array();
	this.checked = false;
	this.parent = null;  // CMapCheckClass
	this.activeList = null; // should only be in root node

	if(!id){
		this.activeList = new CList();
	}

	this.setParent = function(parent){
		this.parent = parent;
	}

	this.addChild = function(child){
		child.setParent(this);
		this.childs.push(child);
	}

	this.isChecked = function(){
		return this.checked;
	}

	this.clicked = function(id){
		for(idx in this.childs){
			this.childs[idx].clicked(id);
		}
		if(this.rootId == id){
			if(document.getElementById(id).checked){
				this.check();
			}else{
				this.uncheck();
			}
		}
	}

	this.check = function(){
		this.checked = true;
		document.getElementById(this.rootId).checked = true;

		for(idx in this.childs){
			this.childs[idx].check()
		}
		this.addToList(id, true);
		this.parent.checkStatusChanged();
	}

	this.uncheck = function(){
		this.checked = false;
		document.getElementById(this.rootId).checked = false;

		for(idx in this.childs){
			this.childs[idx].uncheck();
		}
		this.addToList(id, false);
		this.uncheckParent();
		this.parent.checkStatusChanged();
	}

	this.uncheckParent = function(){
		if(this.rootId){
			this.parent.uncheckParent();
			document.getElementById(this.rootId).checked = false;
		}
	}

	this.onlyCheck = function(){
		this.checked = true;  // dirty hack ;D
		document.getElementById(this.rootId).checked = true;
	}

	this.setCheckedStatus = function(id, val){
	    //alert(this.childs.length);
		for(idx in this.childs){
			this.childs[idx].setCheckedStatus(id, val)
		}
		if (this.rootId == id){
			this.check();
		}
	}

	this.checkStatusChanged = function(){
		var allChecked = true;

		for (idx in this.childs){
			if (!this.childs[idx].isChecked()){
				allChecked = false;
			}
		}
		if (!allChecked){
			this.checked = false;
		}else{
			this.checked = true;
			document.getElementById(this.rootId).checked = true;
		}
		if (this.parent){
			this.parent.checkStatusChanged();
		}
	}

	this.getChilds = function(){
		return this.childs;
	}

	this.getAllChilds = function(){
		if (this.childs.length == 0){
		    //
		}else{
		    //alert(this.childs.length);
			for (idx in this.childs){
				this.childs[idx].getAllChilds()
			}
		}
	}

	this.addToList = function(name,addrem){
		if(name){
			if(this.rootId){
				this.parent.addToList(name,addrem);
			}else{
				if (addrem){
					this.activeList.add(name);
				}else{
					this.activeList.rem(name);
				}
			}
		}
	}

	this.getActive = function(){
		if (this.rootId){
			return false;
		}else{
			return this.activeList.getList();
		}
	}
}

function CList(){
	this.list = new Array();

	this.add = function(val){
		var inthere = false;
		for (idx in this.list){
			if (this.list[idx] == val){
				inthere = true;
			}
		}
		if (!inthere){
			this.list.push(val)
		}
	}

	this.rem = function(val){
		for (idx in this.list){
			if (this.list[idx] == val){
				this.list.splice(idx,1);
			}
		}
	}

	this.getList = function(){
		var str = '';
		for (idx in this.list){
			str += this.list[idx] + ',' ;
		}
		return str;
	}
}

