function getpos_left( obj ) {
	var left = 0;
	while ( obj != null ) {
		if (obj.id != 'main')
			left += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return left + 1;
}

function getpos_top( obj ) {
	var top = 0;
	while ( obj != null ) {
		if (obj.id != 'main')
			top += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return top + 1;
}

var options = new Array();

function tgmdropdown(selectname, varname) {
	this.dropdownbox = document.all[selectname];
	this.dropdownbox.selectedIndex = -1;
	this.getvalue = getvalue;
	this.update = update;
	this.changed = changed;
	this.blurred = blurred;
	this.setvalue = setvalue;
	this.keyup = keyup;
	this.prevtext = "";

	var name = selectname + "_text";
	var s = '<input type="text" id="' + name + '" name="' + name + '" class="form" onblur="' + varname + '.blurred();" onkeydown="jumpdown(event,\'' + name + '\');" onkeyup="' + varname + '.keyup()" style="display: none;" value="" autocomplete="off">';
	this.dropdownbox.insertAdjacentHTML("afterEnd", s);
	this.textbox = document.all[name];

	var name = selectname + "_value";
	var s = "<input type='hidden' "+ " id=" + name + " name=" + name + " >";
	this.dropdownbox.insertAdjacentHTML("afterEnd", s);
	this.valuebox = document.all[name];

	this.updating = false;
	this.update();
	options[options.length] = this;
}

function blurred() {
	var dropdownbox = this.dropdownbox;
	var textbox = this.textbox;
	var valuebox = this.valuebox;
	var i;
	valuebox.value = textbox.value;
	dropdownbox.selectedIndex = -1;
	if (textbox.value == "") {
		return;
	}
	var n = dropdownbox.options.length;
	for (i=0; i<n; i++) {
		var t = dropdownbox.options(i).text;
		var val = textbox.value;
		t = t.toUpperCase();
		val = val.toUpperCase();
		if (t == val) {
			dropdownbox.selectedIndex = i;
			valuebox.value = dropdownbox.options(i).value;
			textbox.value = valuebox.value;
			return;
		}
	}
}


function update() {
	if (!this.updating) {
		this.updating = true;
		this.textbox.style.display = "none";
		this.dropdownbox.style.position="static";
		this.textbox.style.posLeft = getpos_left(this.dropdownbox);
		this.textbox.style.posTop = getpos_top(this.dropdownbox);
		this.textbox.style.posWidth = this.dropdownbox.offsetWidth - 16;
		this.textbox.style.posHeight = this.dropdownbox.offsetHeight;
		this.dropdownbox.style.position = "absolute";
		this.dropdownbox.style.posLeft = this.textbox.style.posLeft;
		this.dropdownbox.style.posTop = this.textbox.style.posTop;
		this.width = this.dropdownbox.offsetWidth;
		var clip = "rect(0 " + (this.dropdownbox.offsetWidth) + " " + this.dropdownbox.offsetHeight + " " + (this.textbox.style.posWidth - 2 ) + ")";
		this.dropdownbox.style.clip = clip;
		this.textbox.style.display="";
		this.updating = false;
	}
}

function changed() {
	var cursel = this.dropdownbox.selectedIndex;
	var curval = this.dropdownbox.options[cursel];
	this.textbox.value = curval.text;
	this.textbox.select();
	this.dropdownbox.selectedIndex=-1;
}

function getvalue() {
	return this.textbox.value;
}

function setvalue(newval) {
	var n = this.dropdownbox.options.length;
	var i;
	for (i=0; i<n; i++) {
		if (this.dropdownbox.options(i).text == newval) {
			this.dropdownbox.selectedIndex = i;
			this.textbox.value = this.dropdownbox.options(i).text;
			this.valuebox.value = this.dropdownbox.options(i).value;
			return;
		}
	}
	this.dropdownbox.selectedIndex = -1;
	this.textbox.value = newval;
}

function keyup() {
	if (event.keyCode < 32) {
		return;
	}
	var curtext = this.textbox.value;
	var prev = this.prevtext;
	var i;
	if ((curtext == "") || (curtext == prev) ) {
		this.prevtext = curtext;
		return;
	}
	var n = this.dropdownbox.options.length;
	var s;
	for (i=0; i<n; i++) {
		s = this.dropdownbox.options(i).text;
		s = s.toUpperCase();
		curtext = curtext.toUpperCase();
		if (s.indexOf(curtext) == 0) {
			var s = this.dropdownbox.options(i).text;
			this.textbox.value = this.textbox.value + s.substr(curtext.length);
			this.dropdownbox.selectedIndex = i;
			this.prevtext = this.textbox.value;
			var r = this.textbox.createTextRange();
			r.moveStart("character", curtext.length);
			r.select();
			return;
		}
	}
}

function resized() {
	var i;
	for (i=0; i < options.length; i++) {
		options[i].update();
	}
}

