var intervalid;

function startit(name,dir) {
	eval('go' + dir + '("' + name + '")');
	intervalid = setInterval('go' + dir + '("' + name + '")', 500);
	document.getElementsByName(name).item(0).style.backgroundColor = '';
	return (false);
}

function stopit() {
	clearInterval(intervalid);
	return (false);
}

function goup(name) {
	var obj = document.getElementsByName(name).item(0);

	obj.value = (parseFloat(obj.value) + 0.25).toFixed(2);
}

function godown(name) {
	var obj = document.getElementsByName(name).item(0);

	obj.value = (Math.max(0, parseFloat(obj.value) - 0.25)).toFixed(2);
}

function nsorie() {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		return ('ie');
	} else if (navigator.appName == 'Netscape' && parseFloat(navigator.appVersion)>=5.0) {
		return ('ns');
	} else {
		return (null);
	}
}

function makerequest(what, data) {
	var xmlhttp;

	switch (nsorie()) {
	case 'ie':
		xmlhttp = new ActiveXObject("microsoft.XMLHTTP");
		break;
	case 'ns':
		xmlhttp = new XMLHttpRequest();
		break;
	default:
		alert('Unsupported browser');
		return (null);
	}
	xmlhttp.open('POST', 'lookup.php',false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('what='+what+'&data='+data);
	return (dataparse(xmlhttp.responseText));
	//return(xmlhttp.responseText);
}

function dataparse(data) {
	var ret = new Array();
	var a, b, n;

	a = data.split('\n');
	for (n in a) {
		b = a[n].split(':');
		if (b[0] != '') {
			ret[b[0]] = b[1];
		}
	}
	return (ret);
}

function addtomenu(menu, value, text) {
	if (nsorie() == 'ie') {
		menu.add(new Option(text, value));
	} else {
		menu.add(new Option(text, value), null);
	}
}

function lookupnames(selecteditem, destination) {
	var data;
	var i;

	if (selecteditem.value != '') {
		clearmenu(destination);
		data = makerequest('recipients', selecteditem.value);
		for (i in data) {
			addtomenu(destination, i, data[i]);
		}
		destination.selectedIndex = -1;
	} else {
		addtomenu(destination, 1, "nothing selected");
	}
}



function clearmenu(menu) {
	menu.options.length = 0;
}

function selectallmenu(menu) {
	var i;

	for (i = 0; i < menu.options.length; i++) {
		menu.options[i].selected = true;
	}
}

function showit(id) {
	document.getElementById(id).style.display = '';
}

function unshowit(id) {
	document.getElementById(id).style.display = 'none';
}

function keyupfunc(objname) {
	obj = eval(objname);
	if (obj.timer) {
		clearTimeout(obj.timer);
	}
	obj.timer = setTimeout(objname+'.timerfunc()', 500);
	return (true);
}

function fillin() {
	if (this.inputelement.value != '') {
		clearmenu(this.chooser);
		var data = makerequest(this.reqtype, this.inputelement.value);
		for (var i in data) {
			addtomenu(this.chooser, i, data[i]);
		}
		if (this.showid) {
			if (data.length) {
				this.showid.style.display = '';
			} else {
				this.showid.style.display = 'none';
			}
		}
		this.chooser.selectedIndex = -1;
	}
}

function nfillin() {
	if (this.inputelement.value != '') {
		var data = makerequest(this.reqtype, this.inputelement.value);
		if (data.length) {
			if (!this.chooser) {
				this.puthere.appendChild(document.createElement('br'));
				this.chooser = this.puthere.appendChild(this.newelement);
			}
			clearmenu(this.chooser);
			for (var i in data) {
				addtomenu(this.chooser, i, data[i]);
			}
			this.chooser.selectedIndex = -1;
			this.chooser.style.display = '';
			return;
		}
	}
	if (this.chooser) {
		clearmenu(this.chooser);
		this.chooser.style.display = 'none';
	}
}

function choose(e, d) {
	addtomenu(d, e.options[e.selectedIndex].value, e.options[e.selectedIndex].text);
}
