
function WebComboItem(caption, value, icon, selectable) {
	this.selectable = true;
	this.caption = null;
	this.icon = null;
	this.value = null;
}var __WebComboObjects = new Array();

function WebCombo(name, className) {
	this.value = null;
	this.name = name;
	this.items = new Array();
	this.element = null;
	this.highlitIndex = -1;
	this.selectedIndex = -1;
	this.className = className;
	
	this.selectItem = WebComboSelectItem;
	this.highlightItem = WebComboHighlightItem;
	this.selectedIndex = WebComboSelectedIndex;
	
	this.addItem = WebComboAddItem;
	this.getItem = WebComboGetItem;
	this.removeItem = WebComboRemoveItem;
	
	this.keyUp = WebComboElementKeyUp;
	this.keyPress = WebComboElementKeyPress;
	this.keyDown = WebComboElementKeyDown;
	this.mouseDown = WebComboItemMouseDown;
	this.mouseOver = WebComboItemMouseOver;
	
	this.setValue = WebComboSetValue;
	
	this.getTable = WebComboGetTable;
	this.loadOptions = WebComboLoadOptions;
	
	this.hideList = WebComboHideList;
	this.showList = WebComboShowList;
	
	__WebComboObjects[name] = this;
	
	this.init = WebComboInitialize;
	this.init();
}

function WebComboAddItem(caption, value, icon, selectable) {
	var item = new WebComboItem(caption, value, icon, selectable);
	this.items.push(item);
	return item;
}

function WebComboGetItem(index) {
	return this.items[index];
}

function WebComboRemoveItem(index) {
	this.items.splice(index, 1);
}

function WebComboInitialize() {
	//place root object
	document.write("<INPUT id=" + this.name + "_value type=hidden name=" + this.name + ">");
	document.write("<INPUT type=text class=" + this.className + " id=" + this.name + "_face onclick=\"this.value=''\">");
	document.write("<DIV class=list id=" + this.name + "_list style='display: none'></DIV>");
	this.valueElement = $(this.name + '_value');
	this.listElement = $(this.name + '_list');
	this.element = $(this.name + '_face');
	if (needsIFrames()) {
	
	selhid = document.createElement('iframe');
	
	selhid.id = 'form_help_iframe';
	
	selhid.src = '/null.html';
	
	selhid.style.position = 'absolute';
	
	selhid.style.left = 0;
	
	selhid.style.top = 0;
	
	selhid.style.filter = 'mask()';
	
	selhid.style.border = 'none';
	
	selhid.style.zIndex = -1;
	
	this.listElement.appendChild(selhid);
	}
	this.element.onkeyup = this.keyUp.bind(this);
	this.element.onkeypress = this.keyPress.bind(this);
	//if it is IE arrow keys do not work in keypress (AAAAAAAAAAAAAARGH!)
	//therefore this easy fix handles the keydown event, which DOES get triggered by arrow keys...
	//not that this eliminates the possibility of scrolling through the list by holding down an arrow key
	//...only in IE... grummm...
	if (isIE()) {
	
	this.element.onkeydown = this.keyDown.bind(this);
	}}

function WebComboSelectItem(index) {
	if (this.items[index].selectable) {
	
	this.selectedIndex = index;
	
	this.valueElement.value = this.items[index].value;
	
	if (this.valueElement.onchange != null) {
	
	
	this.valueElement.onchange();
	
	}
	
	
	
	this.element.value = this.items[index].caption;
	
	
	
	this.hideList();
	}}

function WebComboSetValue(value, caption) {
	this.valueElement.value = value;
	if (this.valueElement.onchange != null) {
	
	this.valueElement.onchange();
	}
	
	
	this.element.value = caption;
	this.hideList();
}

function WebComboHighlightItem(index) {
	var table = this.getTable();
	if (this.highlitIndex > -1) {
	
	if (this.items[this.highlitIndex].selectable) {
	
	
	table.rows.item(this.highlitIndex).childNodes[1].className = 'item';
	
	} else {
	
	
	table.rows.item(this.highlitIndex).childNodes[1].className = 'header';
	
	
	
	
	}
	}
	
	if (this.items[index].selectable) {
	
	table.rows.item(index).childNodes[1].className = 'item_highlight'
	
	var seltop = table.rows.item(index).offsetTop;
	
	var height = table.rows.item(index).offsetHeight;
	
	
	
	while (seltop < this.listElement.scrollTop) {
	
	
	this.listElement.scrollTop -= height;
	
	}
	
	while (seltop + height > this.listElement.scrollTop + this.listElement.offsetHeight) {
	
	
	this.listElement.scrollTop += height;
	
	}
	}
	this.highlitIndex = index;
}

function WebComboGetTable() {
	for (var i = 0; i < this.listElement.childNodes.length; i++) {
	
	if (this.listElement.childNodes[i].tagName == 'TABLE') {
	
	
	return this.listElement.childNodes[i];
	
	}
	}
	
	return null;
}

function WebComboShowList() {
	if (__currentPopup != this.listElement) {
	
	hidePopup();
	}
	var div = this.listElement;
	//add contents
	var html = '<table cellspacing=0 cellpadding=0 width=100%>';
	for (var i = 0; i < this.items.length; i++) {
	
	var item = this.items[i];
	
	
	
	if (item.selectable) {
	
	
	html += '<tr class=item onmouseover="__WebComboObjects[\'' + this.name + '\'].mouseOver(' + i + ');"';
	
	
	html += ' onmousedown="__WebComboObjects[\'' + this.name + '\'].mouseDown(' + i + ');"><td>';
	
	} else {
	
	
	html += '<tr class=header onmouseover="__WebComboObjects[\'' + this.name + '\'].mouseOver(' + i + ');"';
	
	
	html += ' onmousedown="__WebComboObjects[\'' + this.name + '\'].mouseDown(' + i + ');"><td>';
	
	}
	
	if (item.icon != null) {
	
	
	html += '<img src="' + itme.icon + '">';
	
	} else {
	
	
	html += '&nbsp;';
	
	}
	
	html += '</td><td>' + item.caption + '</td></tr>\n';
	}
	if (this.items.length == 0) {
	
	html += '<tr><td class=header>Searching ...</td></tr>';
	}
	html += '</table>';
	//alert(html);
	div.innerHTML = html;
	
	if (needsIFrames()) {
	
	var selhid = document.createElement('iframe');
	
	//selhid.src = '/null.html';
	
	selhid.style.position = 'absolute';
	
	selhid.style.left = 0;
	
	selhid.style.top = 0;
	
	selhid.style.filter = 'mask()';
	
	selhid.style.border = 'none';
	
	selhid.style.zIndex = -1;
	
	this.listElement.appendChild(selhid);
	
	//selhid.style.width = div.offsetWidth;
	
	selhid.style.height  = '200px';
	}
	div.style.display = 'block';
	//setTopMost(div);
	handleAsPopup(div, false, null);
}

function WebComboHideList() {
	hideCurrentPopup();
}

function WebComboLoadOptions(search) {
	var getParameters = new Array();
	getParameters[this.urlParam] = search;
	
	var combo = this;
	var url = this.url + '?' + this.urlParam + '=' + search;
	
	if (this.request) {
		try {
			this.request.transport.abort();
		} catch(e) {}
	}
	this.request = new Ajax.Request(url, {
	
	method: 'get',
	
	onSuccess: 

function(http) {
	
	
	combo.items = eval('(' + http.responseText + ')');
	combo.showList();
	
	},
	
	onFailure: 

function(request) {}
	});
}

function WebComboSelectedIndex() {
	return this.selectedIndex;
}

function WebComboElementKeyUp(event) {
	e = event || window.event; 
	c = e.keyCode || e.charCode || e.which; 
	
	
	var allowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-, '";
	
	if (c == 13) {
	
	if (this.items.length > 0) {
	
	
	if (this.highlitIndex == -1) {
	
	
	
	var selIndex = 0;
	
	
	
	while ((selIndex < this.items.length) && (!this.items[selIndex].selectable)) {
	
	
	
	
	selIndex++;
	
	
	
	}
	
	
	
	if (selIndex < this.items.length) {
	
	
	
	
	this.selectItem(selIndex);
	
	
	
	}
	
	
	} else {
	
	
	
	this.selectItem(this.highlitIndex);
	
	
	}
	
	
	this.hideList();
	
	}
	
	return false;
	} else if (c == 27) {
	
	this.hideList();
	} else {
	
	if ((allowedCharacters.indexOf(String.fromCharCode(c)) != -1) ||
	
	
	(c == 8) || (c == 46)) {
	
	
	this.highlitIndex = -1;
	
	
	if (this.element.value.length > 0) {
	
	
	
	this.loadOptions(this.element.value);
	
	
	}
	
	}
	}
	//e.cancelBubble = true;
}

function WebComboElementKeyPress(event) {
	e = event || window.event; 
	c = e.keyCode || e.charCode || e.which; 
	
	var allowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-, '";
	
	if (c == 38) {
	
	if (this.highlitIndex > 0) {
	
	
	var selIndex = this.highlitIndex - 1;
	
	
	while ((selIndex > -1) && (!this.items[selIndex].selectable)) {
	
	
	
	selIndex--;
	
	
	}
	
	
	if (selIndex == -1) {
	
	
	
	this.listElement.scrollTop = 0;
	
	
	} else {
	
	
	
	this.highlightItem(selIndex);
	
	
	}
	
	}
	
	return false;
	} else if (c == 40) {
	
	if (this.highlitIndex < this.items.length - 1) {
	
	
	var selIndex = this.highlitIndex + 1;
	
	
	while ((!this.items[selIndex].selectable) && (selIndex < this.items.length)) {
	
	
	
	selIndex++;
	
	
	}
	
	
	this.highlightItem(selIndex);
	
	}
	
	
	
	
	return false;
	}
	if (allowedCharacters.indexOf(String.fromCharCode(c)) == -1) {
	
	return (c == 8) || (c == 46);
	}}

function WebComboItemMouseOver(index) {
	this.highlightItem(index);
}

function WebComboItemMouseDown(index) {
	this.selectItem(index);
}

function WebComboElementKeyDown(event) {
	e = event || window.event; 
	c = e.keyCode || e.charCode || e.which; 
	
	
	if (c == 38) {
	
	if (this.highlitIndex > 0) {
	
	
	var selIndex = this.highlitIndex - 1;
	
	
	while ((selIndex > -1) && (!this.items[selIndex].selectable)) {
	
	
	
	selIndex--;
	
	
	}
	
	
	if (selIndex == -1) {
	
	
	
	this.listElement.scrollTop = 0;
	
	
	} else {
	
	
	
	this.highlightItem(selIndex);
	
	
	}
	
	}
	
	return false;
	} else if (c == 40) {
	
	if (this.highlitIndex < this.items.length - 1) {
	
	
	var selIndex = this.highlitIndex + 1;
	
	
	while ((!this.items[selIndex].selectable) && (selIndex < this.items.length)) {
	
	
	
	selIndex++;
	
	
	}
	
	
	this.highlightItem(selIndex);
	
	}
	
	
	
	
	return false;
	}}