var ie4=document.all;
var ns6=document.getElementById&&!document.all;

function xmlInit()
{
  xmlhttp=false;
  
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try 
 {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } 
 catch (e) 
 {
  try 
  {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } 
  catch (E) 
  {
    xmlhttp = false;
  }
 }
@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	  xmlhttp = new XMLHttpRequest();
}


function AJAXRequest(Filename, CallbackFunction)
{
  xmlInit();
  xmlhttp.open("GET", Filename, true);
  xmlhttp.onreadystatechange=CallbackFunction;
  xmlhttp.send(null)
}


function InitAJAXDropDown(Filename, TextBoxValue, TextBoxIdent, DropDownIdent, AllowMulti)
{
	Suggestion = GetLastSuggestion(TextBoxValue, AllowMulti);
	
	if (Suggestion=="")
	  fnFindBrowserStyle(document.getElementById(DropDownIdent)).display="none";
	else
	{
      DropMenu = DropDownIdent;
	  DropMenuMulti = AllowMulti;
	  DropTextBoxId = TextBoxIdent;
	  
	  document.getElementById(DropMenu).innerHTML = "Fetching suggestions...";	  
	  fnFindBrowserStyle(document.getElementById(DropDownIdent)).display="block";
	  AJAXRequest(Filename+"&sug="+Suggestion, cb_FeedDropMenu);
	}
}

function MessageSuggest(parent_id, Pseudonym)
{
	if (DropMenuMulti)
	{
		if (document.getElementById(DropTextBoxId).value.lastIndexOf(';') >= 0)
			document.getElementById(DropTextBoxId).value = document.getElementById(DropTextBoxId).value.substr(0, document.getElementById(DropTextBoxId).value.lastIndexOf(';')) + "; "+Pseudonym+"; ";
		else 
			document.getElementById(DropTextBoxId).value = Pseudonym+"; ";
	}
	else
  		document.getElementById(DropTextBoxId).value = Pseudonym;
		
	fnFindBrowserStyle(document.getElementById(DropMenu)).display="none";
	document.getElementById(DropTextBoxId).focus();
}


function cb_FeedDropMenu()
{
	if (xmlhttp.readyState==4) // Ensure it only attempts to populate details on success.
	{
	  Results = xmlhttp.responseText;
	  
	  if (Results=="")
	  {
		 fnFindBrowserStyle(document.getElementById(DropMenu)).display="none";
	  }
	  else
	  {
	  	document.getElementById(DropMenu).innerHTML = Results;
	  	fnFindBrowserStyle(document.getElementById(DropMenu)).display="block";
	  }
	}
}


function GetLastSuggestion(TextBoxValue, AllowMulti)
{
  if ((AllowMulti) && (TextBoxValue.indexOf(";") >= 0))
  {
	arrSuggestions = TextBoxValue.split(";");
	return (TrimString(arrSuggestions[arrSuggestions.length-1]));
  }
  else
    return (TrimString(TextBoxValue));
}

function TrimString(strVal)
{
	while (strVal.substring(0,1) == ' ')
		strVal = strVal.substring(1, strVal.length);
	while (strVal.substring(strVal.length-1, strVal.length) == ' ')
		strVal = strVal.substring(0,strVal.length-1);
		
	return strVal;
}

function fnFindBrowserStyle(objIdent)
{ return ((ie4||ns6) ? objIdent.style : objIdent); }