// -------------------------------------------------------------------
// autoComplete (text_input, select_input, ["text"|"value"], [true|false])
//   Use this function when you have a SELECT box of values and a text
//   input box with a fill-in value. Often, onChange of the SELECT box
//   will fill in the selected value into the text input (working like
//   a Windows combo box). Using this function, typing into the text
//   box will auto-select the best match in the SELECT box and do
//   auto-complete in supported browsers.
//   Arguments:
//      field = text input field object
//      select = select list object containing valid values
//      property = either "text" or "value". This chooses which of the
//                 SELECT properties gets filled into the text box -
//                 the 'value' or 'text' of the selected option
//      forcematch = true or false. Set to 'true' to not allow any text
//                 in the text box that does not match an option. Only
//                 supported in IE (possible future Netscape).
// -------------------------------------------------------------------
var MATCHWINDOW;
var aMatches = new Array;
function getOffsetLeft (el) 
{
	var ol = el.offsetLeft - el.scrollLeft;

	while ((el = el.offsetParent) != null) 
	{ 
		ol += (el.offsetLeft - el.scrollLeft); 
	}
	
	return ol;
}

function getOffsetTop (el) 
{
	var ot = el.offsetTop - el.scrollTop;

	while((el = el.offsetParent) != null) 
	{ 
		ot += (el.offsetTop - el.scrollTop); 
	}
	
	return ot;
}

function hideSelection()
{
	if (MATCHWINDOW && !MATCHWINDOW.closed) 
		MATCHWINDOW.close();
}

function resetAutoComplete(fieldname)
{
	if(aMatches[fieldname])		aMatches[fieldname] = '';}
	
function autoComplete (fieldname, selectname, property, forcematch) 
{
	var field = document.getElementById(fieldname);
	var select = document.getElementById(selectname);	var bFound = false;
	var sItem = "";
	var sCompare = "";	
	if(!aMatches[fieldname])
		aMatches[fieldname] = '';
			sCompare = field.value.toLowerCase();

	for (var i = 0; i < select.options.length; i++) 	{		sItem = select.options[i][property].toLowerCase();		
		if (sItem.indexOf(sCompare) == 0) 
		{
			bFound=true; 
			break;
		}
	}
	
	if (bFound) 
		select.selectedIndex = i; 
	else 
		select.selectedIndex = -1; 
	
	if (field.createTextRange) 
	{
		if (forcematch && !bFound) 
		{
			field.value=field.value.substring(0,field.value.length-1); 			aMatches[fieldname] = field.value;			
			return;
		}
		
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		
		if (cursorKeys.indexOf(event.keyCode+";") == -1) 
		{
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = bFound ? select.options[i][property] : oldValue;
					aMatches[fieldname] = oldValue;			
			if (newValue != field.value) 
			{
				field.value = newValue;
			
				var rNew = field.createTextRange();
			
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}		else		{
			var r1 = field.createTextRange();
			
			aMatches[fieldname] = r1.text;		}
	}	
}

function setField(fieldname, selectname, newvalue)
{
	var field = document.getElementById(fieldname);
	var select = document.getElementById(selectname);
		var value = newvalue.toLowerCase();
	var item = "";
	var quote = new RegExp("\x27", "ig");
	var double = new RegExp("\x22", "ig");
				
	for (var i = 0; i < select.options.length; i++) 	{		item = select.options[i].text.toLowerCase();
		
		// filter out quotes & double quotes		item = item.replace(quote, "");
		item = item.replace(double, "");							
		if (item.indexOf(value) == 0) 
		{
			// reset new value to selected value (quotes & all)			newvalue = select.options[i].text;			
			select.selectedIndex = i;
			break;
		}
	}	
		field.value = newvalue;
	hideSelection();
}
function showSelection(fieldname, selectname, href, functionname)
{	var field = document.getElementById(fieldname);	var select = document.getElementById(selectname);	
	var sHTML = "";
	var bMatched = false;	var sItem = "";	var nCount = 1;	var sOnClick = "";	
	hideSelection();
	if(!aMatches[fieldname])
		aMatches[fieldname] = '';
	
	// Find position relative to anchor
	var x = getOffsetLeft(document.getElementById(href));
	var y = getOffsetTop(document.getElementById(href));
	
	sHTML = "";
	sHTML += "<html>\r\n";
	sHTML += "<head>\r\n";
	sHTML += "<link rel='stylesheet' type='text/css' href='stylesheets/global.css' />\r\n";
	sHTML += "<title>Other Matches                    </title>";
	sHTML += "</head>\r\n";
	sHTML += "<body class='matchpage'>\r\n";
	sHTML += "<table cellpadding='0' cellspacing='0' border='0' width='300' bgcolor='#ffffff'>\r\n";
	sHTML += "<tr>";
	sHTML += "<td class='columnheader' align='center' width='300'>&nbsp;Match&nbsp;</td>";
	sHTML += "</tr>\r\n";
	sHTML += "</table>\r\n";
	sHTML +="<div class='match'>\r\n";	sHTML += "<table cellpadding='0' cellspacing='0' border='0' width='284' bgcolor='#ffffff'>\r\n";
	
	for (var i = 0; i < select.options.length; i++) 	{
		sItem = select.options[i].text.toLowerCase();
				if (sItem.indexOf(aMatches[fieldname].toLowerCase()) == 0 || aMatches[fieldname] == "") 
		{
			// filter out stray quotes & double quotes			var quote = new RegExp("\x27", "ig");
			var double = new RegExp("\x22", "ig");
				
			sItem = sItem.replace(quote, "");
			sItem = sItem.replace(double, "");							// build on click statement
			sOnClick = "window.opener.setField(\"" + field.name + "\", \"" + select.name + "\", \"" + sItem + "\"); "			
			if(functionname.length > 0 )
			{				sOnClick += "window.opener." + functionname + "(\"" + sItem + "\");"
			}			
			sHTML += "<tr class='item'>";
			sHTML += "<td nowrap><a class='autocomplete' href='javascript:" + sOnClick + "'>&nbsp;" + nCount + ". " + select.options[i].text + "&nbsp;</a></td>";
			sHTML += "</tr>\r\n";
			
			nCount++;
			bMatched = true;
		}
	}
		if(!bMatched)
	{
		sHTML += "<tr>";
		sHTML += "<td colspan='2' class='item'>&nbsp;No Matches.</td>";
		sHTML += "</tr>\r\n";
	}
		
	sHTML += "</table>\r\n";
	sHTML += "</div>\r\n";
	sHTML += "</body>\r\n";
	sHTML += "</html>\r\n";
	
	var windowx = window.screenLeft;
	var windowy = window.screenTop;
		
	x = x + windowx;
	y = y + windowy;
		
	var windowprops = "toolbar=0,location=0,directories=0,status=0, " +
					  "menubar=0,scrollbars=0,resizable=0,titlebar=0,title=''," + 
					  "width=300,height=250,screenX="+x+",left="+x+",screenY="+y+",top="+y+""

	MATCHWINDOW = window.open("", "matchwindow", windowprops);
		
	MATCHWINDOW.document.open();
	MATCHWINDOW.document.write(sHTML);
	MATCHWINDOW.document.close();
}
