function getLabelForElement(id){
	var labels = document.getElementsByTagName("label");
	if(labels.length != 0){
		for(var i in labels){
			if(labels[i].htmlFor == id){
				return(labels[i]);
			}
		}
	}else{
		return(null);
	}
	return("onbekend");
}

function FormValidator(formName, callWhenValidated){
	this.formName = formName;
	this.items = new Array();
	this.callWhenValidated = callWhenValidated;
	
	
	this.addItem = function(id, obligatory, regExp, maxLength){
		var label = getLabelForElement(id);
		if(label != null){
			var name = label.innerHTML;
		}
		this.items.push({"element" : document.getElementById(id), "id" : id, "label" : label, "name" : name, "obligatory" : obligatory, "regExp" : regExp, "maxLength" : maxLength});
	}
	
	this.validate = function(){
		var item;
		var showAlert = false;
		var alertString = "";
		for(var i in this.items){
			var item = this.items[i];
			item.label.className = "captionNormal";
			if(item.obligatory && item.element.value.length == 0){
				alertString += "No value entered for " + item.name + "\n";
				item.label.className = "captionHighlight";
				showAlert = true;
			}else if(item.element.value.length > item.maxLength){
				alertString += "Text too long for " + item.name + "\n";
				item.label.className = "captionHighlight";
				item.element.value = item.element.value.substring(0, item.maxLength);
				showAlert = true;
			}else if(!(item.element.value.match(item.regExp) || item.element.value.length == 0)){
				alertString += "Invalid value entered for " + item.name + "\n";
				item.label.className = "captionHighlight";
				showAlert = true;
			}
		}
		if(showAlert){
			alert(alertString);
			return(false);
		}
		if(this.callWhenValidated != null){
			return(this.callWhenValidated(this.formName));
		}
		return(true);
	}
	
	if(document.getElementById(this.formName).onsubmit == null || document.getElementById(this.formName).onsubmit == ""){
		document.getElementById(this.formName).setAttribute("onsubmit", "return(" + this.formName + "Validator.validate());");
	}
}

function MultipleSelector(id){
	this.id = id;
	this.poolId = id + "Pool";
	this.pool = getDOMObject(this.poolId);
	this.selectionId = id + "Selection";
	this.selection = getDOMObject(this.selectionId);
	this.buttonId = id + "MoveButton";
	this.button = getDOMObject(this.buttonId);
	this.direction = "right";
	this.leftArrow = new Image();
	this.leftArrow.src = "/images/l-arr.gif";
	this.rightArrow = new Image();
	this.rightArrow.src = "/images/r-arr.gif";

	
	this.click = function(e){
		var sourceChildren, i, j, option;
		var moveOptions = new Array();
		var selectedOptions = new Array();
		if(this.direction == "right"){
			sourceChildren = this.pool.childNodes;
			destination = this.selection;
		}else{
			sourceChildren = this.selection.childNodes;
			destination = this.pool;
		}
		for(i in sourceChildren){
			option = sourceChildren[i];
			if(option.nodeType == 1 && option.selected){
				moveOptions.push(option);
//				destination.appendChild(option);
				option.selected = false;
			}
		
		}
		for(i in moveOptions){
			destination.appendChild(moveOptions[i]);
		}
		this.sort(destination);
		for(i in this.selection.childNodes){
			child = this.selection.childNodes[i];
			if(child.nodeType == 1 && child.nodeName.toLowerCase() == "option" && child.parentNode.id == this.selectionId){
				option = "'" + escape(child.value) + "'";
				if(!inArray(option, selectedOptions)){
					selectedOptions.push(option);
				}
			}
		}
		document.getElementById(this.id).value = selectedOptions.join(",");
		e.stopPropagation();
	}
	
	this.sort = function(node){
		var sortArray = new Array();
		var row = new Array();
		var child;
		var sortData = new Array();
		while(child = node.firstChild){
			if(child && child.nodeType == 1 && child.nodeName.toLowerCase() == "option"){
				row["value"] = child.innerHTML;
				row["node"] = child;
				sortArray.push(this.optionArray(child));
			}
			node.removeChild(child);
		}
		sortArray.sort(this.sortByValue);
		for(var i in sortArray){
			node.appendChild(sortArray[i]["node"]);
		}
	}
	
	this.optionArray = function(node){
		var row = new Array();
		row["node"] = node;
		if(node.firstChild && node.firstChild.nodeType == 3){
			row["value"] = node.firstChild.nodeValue;
		}else{
			return(null);
		}
		return(row);
	}
	
	this.sortByValue = function(a, b){
		a = a["value"].toLowerCase();
		b = b["value"].toLowerCase();
		return((a < b) ? -1 : (a > b) ? 1 : 0);
	}
	
	this.leftFocus = function(e){
		this.direction = "right";
		this.button.src = this.rightArrow.src;
	}
	
	this.rightFocus = function(e){
		this.direction = "left";
		this.button.src = this.leftArrow.src;
	}
	
	connect(this.button, "onclick", this, this.click);
	connect(this.pool, "onfocus", this, this.leftFocus);
	connect(this.selection, "onfocus", this, this.rightFocus);
}