function processReqChange()
{
	if (req.readyState == 4)
	{
		// only if "OK"
		if (req.status == 200)
		{
			response = req.responseXML.documentElement;

			method = response.getElementsByTagName('method')[0].firstChild.data;
			eval(method + '(response)');
		} 
		else
		{
			//change this--should not be alert()
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function loadXMLDoc(url) 
{
	if (window.XMLHttpRequest)			// ff, opera, safari, konqueror
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	}
	else if (window.ActiveXObject)		// ie
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req)
		{
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function mycomplete(input, ev)
{
	if (input == "")
	{
		HideDiv("container");
	}
	else
	{
		if (window.event)
		{
			// for IE, e.keyCode or window.event.keyCode can be used
			if (ev.keyCode == 40)
				move(-1);
			else if (ev.keyCode == 38)
				move(1);
			else
			{
				// encodeURI... only required for IE--Mozilla did it automatically
				url = 'greekserv.php?w=' + encodeURIComponent(input) + '&n=' + (dropLimit + 1);
				loadXMLDoc(url);
			}
		}
		else if (ev.which)
		{
			// netscape
			if (ev.which == 40)
				move(-1);
			else if (ev.which == 38)
				move(1);
			else
			{
				// encodeURI... only required for IE--Mozilla did it automatically
				url = 'greekserv.php?w=' + encodeURIComponent(input) + '&n=' + (dropLimit + 1);
				loadXMLDoc(url);
			}
		}
	}
}

function onsearchsubmit()
{
	if (ddPos && ddPos.id)
	{
		HideDiv("container");
		window.location="index.php?id=" + ddPos.id;
	}
}

function setDropDown(response)
{
	mywords = response.getElementsByTagName('w');
	ids = response.getElementsByTagName('i');
	var con = document.getElementById("container");

	for (i = 0; i < ddentries.length; i++)
	{
		con.removeChild(ddentries[i]);
	}

	ddPos = null;

	if (mywords.length > dropLimit)
	{
		over = 1;
		len = dropLimit;
	}
	else
	{
		over = 0;
		len = mywords.length;
	}

	ddentries = new Array(len);

	for (i = 0; i < len; i++)
	{
		ddentries[i] = document.createElement('div');
		ddentries[i].setAttribute('class','treerow');
		ddentries[i].id = ids[i].firstChild.data;

		ddentries[i].onmouseover=function(){ this.style.backgroundColor='#8899EE'; this.style.color='white'; if (ddPos) { ddPos.style.backgroundColor='white'; ddPos.style.color='black'; } ddPos = this; };

		text = document.createTextNode(mywords[i].firstChild.data);
		ddentries[i].onclick=onsearchsubmit;

		ddentries[i].appendChild(text);
		con.appendChild(ddentries[i]);
	}

	if (over == 1)
	{
		ddentries[i] = document.createElement('div');
		ddentries[i].setAttribute('class','treerow');

		ddentries[i].onmouseover=function(){ this.style.backgroundColor='#8899EE'; this.style.color='white'; if (ddPos) { ddPos.style.backgroundColor='white'; ddPos.style.color='black'; } ddPos = this; };
	
		text = document.createTextNode("...");

		ddentries[i].appendChild(text);
		con.appendChild(ddentries[i]);
	}

	if (mywords.length > 0)
		ShowDiv("container");
	else
		HideDiv("container");
}

function move(upDown)
{
	var con = document.getElementById("container");

	if (!ddPos)
	{
		n = con.firstChild;
	}
	else if (upDown > 0)
	{
		if (ddPos == con.firstChild)
			n = con.lastChild;
		else
			n = ddPos.previousSibling;
	}
	else
	{
		if (ddPos == con.lastChild)
			n = con.firstChild;
		else
			n = ddPos.nextSibling;
	}

	if (!n)
		return;

	if (ddPos)
	{
		ddPos.style.backgroundColor='white';
		ddPos.style.color='black';
	}

	n.style.backgroundColor='#8899EE';
	n.style.color='white';

	ddPos = n;
}

function enableblursearch()
{
	s = document.getElementById("searchbox");

	s.onblur = blursearch;
}

function disableblursearch()
{
	s = document.getElementById("searchbox");

	s.onblur = null;
}

function blursearch()
{
	HideDiv("container");
}

function get_analyses(input)
{
	if (input == "")
	{
		alert("blank");
	}
	else
	{
		m = document.getElementById("morph");
		m.innerHTML = "Asking Perseus for morphological analysis of " + input + " ...";

		url = 'morphserv.php?word=' + encodeURIComponent(input);
		loadXMLDoc(url);
		m2 = document.getElementById("morphwrap");
		m2.style.display = 'block';
	}
}

function show_morph (response)
{
	analysis = response.getElementsByTagName('analysis');
	var str = "";
	ediv = document.getElementById("morph");
	mhead = document.getElementById("morphhead");

	if (analysis.length > 0)
	{
	        mhtext = analysis[0].getElementsByTagName('unicodeform').item(0).firstChild.data;
		mhead.innerHTML = "Morphology of " + mhtext;
	}
	else
	{
		mhead.innerHTML = "Morphology";
		wa = response.getElementsByTagName('lookup');
		str = "Could not analyze " + wa[0].firstChild.nodeValue;
	}

	var gender, casex, person, voice, number, beta, unicodelemma, form, unicodeform, pos, mood, tense;

	for (i = 0; i < analysis.length; i++)
	{
		c = analysis[i].firstChild;

		z = 0;
		while (c = c.nextSibling)
		{
			switch (c.tagName)
			{
				case 'gender':
					gender = c.firstChild.nodeValue;
					break;
				case 'form':
					form = c.firstChild.nodeValue;
					break;
				case 'beta':
					beta = c.firstChild.nodeValue;
					break;
				case 'number':
					number = c.firstChild.nodeValue;
					break;
				case 'case':
					casex = c.firstChild.nodeValue;
					break;
				case 'person':
					person = c.firstChild.nodeValue;
					break;
				case 'voice':
					voice = c.firstChild.nodeValue;
					break;
				case 'mood':
					mood = c.firstChild.nodeValue;
					break;
				case 'tense':
					tense = c.firstChild.nodeValue;
					break;
				case 'unicodelemma':
					unicodelemma = c.firstChild.nodeValue;
					break;
				case 'unicodeform':
					unicodeform = c.firstChild.nodeValue;
					break;
				case 'pos':
					pos = c.firstChild.nodeValue;
					break;
			}
		}
		str += "<a href='index.php?beta=" + encodeURIComponent(beta) + "'>" + unicodelemma + "</a><br />";
		if (pos)
			str += pos + "<br />";
		if (tense)
			str += tense + "<br />";
		if (casex)
			str += casex + "<br />";
		if (person)
			str += person + "<br />";
		if (gender)
			str += gender + "<br />";
		if (number)
			str += number + "<br />";
		if (voice)
			str += voice + "<br />";
		if (mood && pos != 'part')
			str += mood + "<br />";
		str += "<br />";

		pos = "";
		number = "";
		casex = "";
		person = "";
		tense = "";
		voice = "";
		gender = "";
		beta = "";
		mood = "";
		unicodelemma = "";
	}

	ediv.innerHTML = str;
}

function ShowDiv(divId)
{
	if (document.layers) 
		document.layers[divId].visibility="show";
	else 
		document.getElementById(divId).style.visibility="visible";
}

function HideDiv(divId)
{
	if (document.layers) 
		document.layers[divId].visibility="hide";
	else 
		document.getElementById(divId).style.visibility="hidden";
}

function BodyLoad()
{
	HideDiv("container");
	document.forms[0].wordEntry.focus();
}

function onRight(tid)
{
	if (tid.id != stab)
		tid.style.backgroundColor = 'BBAAFF';
}

function outRight(tid)
{
	if (tid.id != stab)
		tid.style.backgroundColor = '8899EE';
	else
		tid.style.backgroundColor = 'white';
}

function showCol(colNum)
{
	var div = new Array(4);
	var tab = new Array(4);

	div[0] = document.getElementById("etyms");
	div[1] = document.getElementById("syns");
	div[2] = document.getElementById("ants");
	div[3] = document.getElementById("rels");

	tab[0] = document.getElementById("etymHead");
	tab[1] = document.getElementById("synHead");
	tab[2] = document.getElementById("antHead");
	tab[3] = document.getElementById("relHead");

	for (var i = 0; i < 4; i++)
	{
		if (i == colNum)
		{
			div[i].style.display = 'block';
			tab[i].style.backgroundColor = 'white';
			tab[i].style.color = 'black';
			stab = tab[i].id;
		}
		else
		{
			div[i].style.display = 'none';
			tab[i].style.backgroundColor = '#8899EE';
			tab[i].style.color = 'white';
		}
	}
}
