var activeLookup = null;

/*--------------------------------------------------------------------------
 * LookupControl 1.0
 * Version 17/7/2007
 * 
 * Requires
 * - prototype 1.5.0
 * - okcanceldialog 1.0
 *--------------------------------------------------------------------------*/
 
LookupControl = Class.create();
LookupControl.prototype = {
	initialize: function(id, id2) {
		this.descriptionDiv = $(id+'_description');
		this.setButton($(id+'_button'));
		this.setSelectBox($(id+'_ID'));
		this.contentDiv = $(id+'_outerdiv');
		this.displaySelection();
		this.dialog = null;
		if (typeof id2 != 'undefined')
			this.setSelectBox2Id(id2);
	},
	setButton: function(element) {
		this.button = element;
		if(this.button != null)
			this.button.onclick = this.onButtonClick.bindAsEventListener(this);
	},
	setSelectBox: function(element) {
		this.selectBox = element;
		this.selectBox.size = 20;
		this.displaySelection();
	},
	setSelectBox2Id: function(element) {
		this.selectBox2Id = element+'_ID';
		if (this.selectBox2()) {
			this.selectBox2().size = 20;
			this.displaySelection();
		} else
			alert('set without box 2');
	},
	selectBox2: function() {
		return $(this.selectBox2Id);
	},
	onButtonClick: function(event) {
		if (this.dialog == null) {
			if (activeLookup) {
				activeLookup.dialog.close();
				activeLookup.close();
			}
			activeLookup = this;
			this.saveSelection();
			this.dialog = new OkCancelDialog(this.contentDiv);
			this.dialog.setCallbackHandler(this.onWindowOk.bindAsEventListener(this), this.onWindowCancel.bindAsEventListener(this));
			this.dialog.open();
		} else {
			this.dialog.onButtonOk();
		}
	},
	close: function() {
		this.clearSelection();
		this.displaySelection();
		this.dialog = null;
		activeLookup = null;
	},
	onWindowOk: function() {
		this.close();
		this.selectBox.form.submit();
		return true;
	},
	onWindowCancel: function() {
		this.restoreSelection();
		this.dialog = null;
		activeLookup = null;
		return true;
	},
	displaySelection: function() {
		if(this.descriptionDiv != null) {
			this.descriptionDiv.innerHTML = this.getDescription();
		}
	},
	getDescription: function() {
		var description = this.buildDescription(this.selectBox);
		var description2 = this.buildDescription(this.selectBox2());
		if(description2 != null) 
			return description2 + this.buildClearLink(this.selectBox2());
		if(description == null)
			return "(All)";
		else
			return description + this.buildClearLink(this.selectBox);
	},
	buildClearLink: function(selectBox) {
		return ' <a href="#" onclick="return clearSelectBox(\'' + selectBox.id + '\');">[x]</a>';
	}, 
	buildDescription: function(selectBox) {
		var description = null;
		if (selectBox != null) {
			var options = selectBox.options;
			for(var i=0; i < options.length; i++) {
				if (options[i].selected) {
					if(description == null) {
						description = options[i].text;	
					} else {
						description = description + ", " + options[i].text;
					}
				}
			}
		}
		return description;
	},
	remove: function(optionId) {
		var options = this.selectBox.options;
		for(var i=0; i<options.length; i++) {
			if(options[i].value == optionId) {
				this.selectBox.options[i].selected=false;
				break;
			}
		}
		this.displaySelection();
	},
	saveSelection: function() {
		this.backupOptions = this.getSelectedOptions(this.selectBox);
		this.backupOptionIds = this.getSelectedOptionIds(this.selectBox);
		if(this.selectBox2()) {
			this.backupOptions2 = this.getSelectedOptions(this.selectBox2());
			this.backupOptionIds2 = this.getSelectedOptionIds(this.selectBox2());
		}
	},
	restoreSelection: function() {
		this.setSelectedOptions(this.selectBox, this.backupOptions);
		this.setSelectedOptionIds(this.selectBox, this.backupOptionIds);
		if(this.selectBox2()) {
			this.setSelectedOptions(this.selectBox2(), this.backupOptions2);
			this.setSelectedOptionIds(this.selectBox2(), this.backupOptionIds2);
		}
	},
	clearSelection: function() {
		this.backupOptions = null;
		this.backupOptionIds = null;
		this.backupOptions2 = null;
		this.backupOptionIds2 = null;
	},
	getSelectedOptions: function(sel) {
		var result = new Array();
		for(var i=0; i < sel.options.length; i++)
			result[i] = sel.options[i].cloneNode(true);	
		return result;
	},
	setSelectedOptions: function(sel, options) {
		while (sel.firstChild)
			sel.removeChild(sel.firstChild);
		for(var i=0; i < options.length; i++)
			sel.appendChild(options[i]);
	},
	getSelectedOptionIds: function(sel) {
		var result = new Array();
		for(var i=0; i < sel.options.length; i++)
			result[i] = sel.options[i].selected;	
		return result;
	},		
	setSelectedOptionIds: function(sel, ids) {
		for(var i=0; i < sel.options.length; i++)
			sel.options[i].selected = ids[i];
	}
}

function clearSelectBox(id) {
	var sel = $(id);
	for (var i=0; i < sel.options.length; i++)
		sel.options[i].selected = false;
	sel.form.submit();
}