var subxmlHttp
var xmlHttp
var ListRef
var FieldRef
var StateRef
var PCodeRef
var SelID

function fnAC()  {
	document.getElementById("FromSub").setAttribute("autocomplete","off");
	document.getElementById("ToSub").setAttribute("autocomplete","off"); 
}

function fnDispList(str,ListID,FieldID,StateID,PCodeID,Key)	{ 

  try  {

	var KeyNum;
	var CurFocusID=SelID;
	CurFocusID = parseInt(CurFocusID);
	
	if(window.event)  { //IE
	  KeyNum = Key.keyCode;
	}
	else if(Key.which)  {  //FF,NS,OP
	  KeyNum= Key.which;
	}
	
	try  {
	  switch (KeyNum)  {
	    case 38:  //UPARW
		  if(CurFocusID==1)  {
			var i
			for(i=10;i>=0;i--)  {
			  if(document.getElementById(i))  {
			    fnHighlight(i);
				return;
			  }
			}
		  }
		  fnReset(CurFocusID);
		  CurFocusID = CurFocusID-1;
		  fnHighlight(CurFocusID);
	      return;
	    case 40:  //DWNARW
		  if(!document.getElementById(CurFocusID+1))  {
		    CurFocusID=1;
			fnHighlight(CurFocusID);
			return;
		  }
		  if(CurFocusID!=0)  {
		    fnReset(CurFocusID);
		  }
		  CurFocusID = CurFocusID+1;
		  fnHighlight(CurFocusID);
	      return;
	    case 13:  //ENT
		  var NewVal = document.getElementById("Sub"+CurFocusID).value;
		  var StateVal = document.getElementById("State"+CurFocusID).value;
		  var PCodeVal = document.getElementById("PCode"+CurFocusID).value; 
		  fnUpdateSub(NewVal,StateVal,PCodeVal);
		  document.getElementById(FieldID).select();
		  return false;
		case 33:  //PGUP
		  fnHighlight(1);
		  return;
		case 34:  //PGDWN
		  var i
		  for(i=10;i>=0;i--)  {
			if(document.getElementById(i))  {
			  fnHighlight(i);
			  return;
			}
		  }
		case 27:  //ESC
	      fnRemoveList(ListID);
	      return;
		case 9:  //TAB
		  fnRemoveList(ListID);
	      return;
		default:
		  break;
	  }
	}
	catch(err) {
	}
	SelID=1;
	ListRef=ListID;
	FieldRef=FieldID;
	StateRef=StateID;
	PCodeRef=PCodeID;
	
	if(str.length>1)  {
	  
	  try  {
	    if(subxmlHttp)  {
	      subxmlHttp.abort();
	    }
	  }
	  catch(err)  {
	    err = "asyncsubs.js -> line 94 -> "+err
	    fnAJAXReportErrs(err);
	  }
	  
	  subxmlHttp=null;
	  
	  try  {
	    subxmlHttp=new XMLHttpRequest();
	  }
	  catch (e)  {
	    try  {
		  subxmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e)  {
		  subxmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }
	  
	  if (subxmlHttp==null)	{
	    alert ("Your browser does not support this feature.");
	    return;
	  }  
	  var url="asyncsublist.asp";
	  url=url+"?ListID="+ListRef+"&FieldID="+FieldRef+"&q="+str;
	  url=url+"&sid="+Math.random();
	  subxmlHttp.onreadystatechange=function()  {
	    try  {
		  if (subxmlHttp.readyState==4 && subxmlHttp.status == 200)  {
		    document.getElementById(ListRef).innerHTML=subxmlHttp.responseText;
		    document.getElementById(ListRef).style.display="block";
		    fnHighlight(1);
		  }
	    }
	    catch(err)  {
	    }
	  }
	  subxmlHttp.open("GET",url,true);
	  subxmlHttp.send(null);
	}
	else  {
	  fnRemoveListFinally(ListID);
	  return;
	}

  }
  catch(err)  {
    err = "asyncsubs.js -> line 15 -> "+err
	fnAJAXReportErrs(err);
  }

}

function fnHighlight(RowID)	{
  try  {
	var PrevSelect = SelID;
	PrevSelect = parseInt(PrevSelect);
	try  {
	  if(PrevSelect!=0)  {
	    fnReset(PrevSelect);
	  }
	  document.getElementById(RowID).style.background='#cfcdd4';
	  document.getElementById("S"+RowID).style.background='#ffd9d9';
	  document.getElementById(RowID).style.color='#45424e';
	  document.getElementById(RowID).style.cursor="default";
	  SelID=RowID;
	}
	catch(err)  {
	}
  }
  catch(err)  {
    err = "asyncsubs.js -> line 155 -> "+err
	fnAJAXReportErrs(err);
  }
}

function fnReset(RowID)	{
  try {
	document.getElementById(RowID).style.background='#45424e';
	document.getElementById("S"+RowID).style.background='#ae0004';
	document.getElementById(RowID).style.color='#ffffff';
  }
  catch(err)  {
  }
}

function fnUpdateSub(NewVal,StateVal,PCodeVal)	{
  try  {
	if (NewVal!=null)	{
		document.getElementById(FieldRef).value=NewVal;
		document.getElementById(StateRef).value=StateVal;
		document.getElementById(PCodeRef).value=PCodeVal;
		document.getElementById(ListRef).innerHTML="";
		document.getElementById(ListRef).style.display="inline";
	}
  }
  catch(err)  {
    err = "asyncsubs.js -> line 188 -> "+err
	fnAJAXReportErrs(err);
  }
}

function fnRemoveList(ListRef)	{
  try  {
    if(xmlHttp)  {
	  xmlHttp.abort();
    }
    setTimeout('fnRemoveListFinally(ListRef)',550);
  }
  catch(err)  {
    err = "asyncsubs.js -> line 204 -> "+err
	fnAJAXReportErrs(err);
  }
}

function fnRemoveListFinally(ListRef)	{
  try  {
    if(xmlHttp)  {
	  xmlHttp.abort();
    }
	if(document.getElementById(ListRef))  {
	  document.getElementById(ListRef).innerHTML="";
	  document.getElementById(ListRef).style.display="inline";
	  SelID=1;
	}
	else  {
	  SelID=1;
	}
  }
  catch(err)  {
    err = "asyncsubs.js -> line 217 -> "+err
	fnAJAXReportErrs(err);
  }
}
