// encoding UTF-8

function hideInputBg(field)
{
	ipField = document.getElementById(field);
	ipField.style.background = "#fff";
}

/* >> ofracar company search */
	CompSearch = {
		target: null,
		connectMap: function(map, target){
			var mapChilds = $(map).childNodes;
			for (var i = 0; i < mapChilds.length; ++i){
				var title = getNodeAttribute(mapChilds[i], 'title');
				connect(mapChilds[i], 'onclick', bind('changeValueAndSubmit', this, title, target));
			}
		},
		changeValueAndSubmit: function(val, target, e){
			e.stop();
			$(target).value='';
			$(target).value=val;
			var form = this.findForm(e.src());
			if (form) form.submit();
		},
		findForm: function(node){
			while (node){
				if (node.nodeName.toLowerCase() == 'form'){
					return node;
				}
				if (node.nodeName.toLowerCase() == 'body'){
					return false;
				}
				node = node.parentNode;
			}
		}
	}
/* << */


/* >> selectbox splitter, require mochikit 1.4+ */
	function SelectboxSplitter(args){
		this.container = null;
		this.sourceElement = null;
		this.masterNameAttr = null;
		this.separator = null;
		this.promptSeparator = null;
		this.prompts = null;
		this.Data = null;
		this.level = 0;
		this.selectboxes = [];
		this.selected = null;
		this.selectPath = [];
		this.createdElements = [];
		this.seq = -1;
		this.lang = 'en';
		this.langRes = {
			'chooseAll': {'de': '--Alles ausgewählt--', 'en': '--Select all--'}
		}
		this.debug = false;
		this.init(args);
	}

	SelectboxSplitter.prototype.init = function(args){
		this.debug = args.debug;
		if (args.sourceElement){
			this.sourceElement = $(args.sourceElement);
			this.masterNameAttr = getNodeAttribute(this.sourceElement, 'name');
			setNodeAttribute(this.sourceElement, 'name', args.sourceElement + '_split_0');
		}else{
			return false;
		}
		if (args.separator){
			this.separator = args.separator;
		}else{
			return false;
		}
		if (args.promptSeparator){
			this.promptSeparator = args.promptSeparator;
		}else{
			return false;
		}
		if (args.container){
			this.container = this._getElementNode(args.container);
		}else{
			this.container = this._createBoxesShell(this.sourceElement);
		}
		
		if (args.lang){
			this.lang = this.formatLang(args.lang);
		}else{
			this.lang = this.getLang();
		}
		return true;
	}

	SelectboxSplitter.prototype._getElementNode = function(container){
		var c = container;
		if (typeof c == 'string'){
			var steps = c.split('/');
			if (this.debug) log('_getElementNode steps length:', steps.length);
			var node = $(this.sourceElement);
			var count = 0;
			while (count < steps.length -1){
				if (node.nodeType == 1){
					count++;
					node = node.parentNode;
				}
			}
			if (this.debug) log('_getElementNode with path located node:', node.nodeName);
			return node;
		}else if (typeof c == 'object'){
			return container;
		}
	}

	SelectboxSplitter.prototype.getLang = function(){
		var htmlEl = document.getElementsByTagName('html')[0];
		if (getNodeAttribute(htmlEl, 'lang')){
			return this.formatLang(getNodeAttribute(htmlEl, 'lang'));
		}else if(getNodeAttribute(htmlEl, 'xml:lang')){
			return this.formatLang(getNodeAttribute(htmlEl, 'xml:lang'));
		}
		return 'en';
	}
	
	SelectboxSplitter.prototype.formatLang = function (lang){
		if (lang.search(/-/) > -1) return lang.substring(0, lang.search(/-/));
		return lang;
	}

	SelectboxSplitter.prototype._strTrim = function(str){
		var ccSpace = 32;
		var ps = 0;
		var pe = 0;
		var psLock = false;
		var peLock = false;
		for (var i = 0; i < str.length; ++i){
			if (str.charCodeAt(i) == ccSpace){
				if (! psLock) ps++;
				if (str.charCodeAt(str.length - 1 - i) == ccSpace){
					if (! peLock) pe++;
				}else{
					peLock = true;
				}
			}else{
				psLock = true;
				if (str.charCodeAt(str.length - 1 - i) == ccSpace){
					if (! peLock) pe++;
				}else{
					peLock = true;
				}
			}
		}
		var string = str.substring(ps, str.length);
		return string.substring(0, str.length - pe - ps);
	}

	SelectboxSplitter.prototype._createBoxesShell = function(srcEl){
		var node = srcEl.parentNode;
		while (node.nodeType != 1){
			node = node.previousSibling;
		}
		var id = getNodeAttribute(this.sourceElement, 'id') + '_container';
		var container = DIV({'id': id});
		insertSiblingNodesAfter(node, container);
		return container;
	}

	SelectboxSplitter.prototype._splitOptionItems = function(option){
		var list = option.text.split(this.separator);
		for (var i = 0; i < list.length; ++i) list[i] = this._strTrim(list[i]);
		return list;
	}

	SelectboxSplitter.prototype._createDataModel = function(){
		this.Data = new SelectboxSplitterNode(null, null, 'root', null);
		var srcOpts = $(this.sourceElement).options;
		for (var i = 1; i < srcOpts.length; ++i){
			this.seq++;
			if (getNodeAttribute(srcOpts[i], 'selected')) this.selected = srcOpts[i].value;
			var itemList = this._splitOptionItems(srcOpts[i]);
			var len = itemList.length;
			var node = this.Data;
			for (var j = 0; j < len; ++j){
				this.seq++;
				
				node = node.makeChild(new SelectboxSplitterNode(this.seq, node.id, itemList[j], srcOpts[i].value, this.prompts[j]));
			}
		}
	}
	
	SelectboxSplitter.prototype._splitPrompt = function(pmtEl){
		pmt = pmtEl.childNodes[0].innerHTML.split(this.promptSeparator);
		for (var i = 0; i < pmt.length; ++i) pmt[i] = this._strTrim(pmt[i]);
		this.prompts = pmt;
		return pmt;
	}

	SelectboxSplitter.prototype._getBoxPrompt = function(){
		var boxParent = this._getElementNode('../');
		var node = boxParent.previousSibling;
		while (node.nodeType != 1){
			node = node.previousSibling;
		}
		if (this.debug) log(node.nodeName, node.className);
		return node;
	}
                                                                                                                       
	SelectboxSplitter.prototype._prepareMasterSelect = function(pmtEl){
		m = $(this.sourceElement);
		removeElement(m);
		removeElement(pmtEl);
		this.container.innerHTML = '';
		this.makeSelectboxFromValue(this.selected);
	}
	
	SelectboxSplitter.prototype.makeSelectboxFromValue = function(value){
		var newPath = this.Data.findPath(value);
		for (var i = 0; i < newPath.length; ++i){
			var newNode = newPath[i];
			var oldNode = this.selectPath.length > i ? this.selectPath[i] : null;
			if (oldNode != newNode){
				while (this.container.childNodes.length > i){
					var last = this.container.childNodes[this.container.childNodes.length - 1];
					removeElement(last);
				}
				if (newNode.children.length){
					var pmt = this._createPrompt(this.prompts[i]);
					var outer = DIV({'class': 'selectboxOuter', 'style': {'display': 'none'}}, pmt);
					var s = SELECT({'name': this.masterNameAttr});
					s.options[0] = new Option(this.langRes.chooseAll[this.lang], newNode.value);
					for (var j = 0; j < newNode.children.length; ++j){
						var n = newNode.children[j];
						if (newPath.length > i+1 && n == newPath[i+1]){
							s.options[j+1] = new Option(n.name, n.value, true, true);
						}else{
							s.options[j+1] = new Option(n.name, n.value);
						}
					}
					connect(s, 'onchange', bind('makeSelectbox', this));
					outer.appendChild(s);
					this.container.appendChild(outer);
					appear(outer);
				}
			}
		}
		while (this.container.childNodes.length > newPath.length){
			var last = this.container.childNodes[this.container.childNodes.length - 1];
			removeElement(last);
		}
		var c = this.container.childNodes;
		for (var i = 0; i < c.length; ++i){
			if (i != c.length -1){
				c[i].childNodes[1].name = this.masterNameAttr + '_' + i;
			}else{
				c[i].childNodes[1].name = this.masterNameAttr;
			}
		}
		this.selectPath = newPath;
	}
	
	SelectboxSplitter.prototype.makeSelectbox = function(e){
		var value = e.src().value;
		if (value == 'null') value = null;
		this.selected = value;
		this.makeSelectboxFromValue(value);
	}
	
	SelectboxSplitter.prototype._createPrompt = function(pmt){
		return DIV({'class': 'prompt'}, pmt);
	}

	SelectboxSplitter.prototype.split = function(){
		var pmtEl = this._getBoxPrompt();
		this._splitPrompt(pmtEl);
		this._createDataModel();
		this._prepareMasterSelect(pmtEl);
	}


	function SelectboxSplitterNode(id, pid, name, value, prompt){
		this.id = id;
		this.pid = pid;
		this.name = name;
		this.value = value;
		this.prompt = prompt;
		this.children = [];
	}
	
	SelectboxSplitterNode.prototype.findPath = function(value){
		if (this.value == value) return [this];
		for (var i = 0; i < this.children.length; ++i){
			var found = this.children[i].findPath(value);
			if (found) return new Array(this).concat(found);
		}
		return null;
	}
	
	SelectboxSplitterNode.prototype.makeChild = function(node){
		var c = this.children;
		for (var i = 0; i < c.length; ++i){
			if (c[i].name && c[i].name.match(node.name)) return c[i];
		}
		this.children.push(node);
		return node;
	}
/* << */

