document.onmouseup = mpMouseUpHandler;
//document.ontouchend = mpMouseUpHandler; // For iPhone/iPad, but doesn't work well for iPhone.


function mpMouseUpHandler(){
	// Get what is highlighted.
	var text;
	if (window.getSelection) {
		text = window.getSelection();
	}
	else if (document.selection) { // For IE
		MSselectedText = document.selection.createRange();
		text = MSselectedText.text;
	}

	var stringText = new String( text );
	var length = stringText.length;
	var lengthMin = 2;
	var lengthLimit = 20;

	// Our output container.
	var container = document.getElementById("terminfo");

	if( length <= lengthLimit && length >= lengthMin )
	{
		// Query script for a result.
		var xmlhttp;				
		xmlhttp=GetXmlHttpObject();
		var url="http://milepoint.com/inc/glossary_lookup_xml.php?text="+encodeURIComponent(text);
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);
		xmlDoc=xmlhttp.responseXML;
		var status = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
		var definition = xmlDoc.getElementsByTagName("definition")[0].childNodes[0].nodeValue;
					
		if( text != '' && length <= lengthLimit )
		{
			// Set container text.			
			container.innerHTML = '<h5>'+text+'</h5>'+definition;

			// Show the container.
			container.style.visibility = 'visible';
		}
	}
	else if( length > lengthLimit )
		{
			// Set container text.			
			container.innerHTML = '<h5>Selected text too long</h5>'+'The highlighted text is too long.  Highlight any word or short phrase to see what it means.';

			// Show the container.
			container.style.visibility = 'visible';		
		}
	else // If nothing is selected hide the definition box.
	{
		container.style.visibility = 'hidden';
	}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return null;
}

