
function valid(f) {
f.value = f.value.replace(/[^A-z-0-9-\s]/g,'');// allows only letters, numbers and single white spaces
} 

function runText(){
new TypingText(document.getElementById("sniff1"), 100, function(i){ var ar = new Array("\\", "|", "/", "-"); return " " + ar[i.length % ar.length]; });

//Type out examples:
TypingText.runAll();
}


function hideSniffCount() {
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('lblRecCountSearch').style.visibility = 'hidden'; 
}
}
function showSniffCount() {
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('lblRecCountSearch').style.visibility = 'visible'; 
}
}


// JavaScript Document

// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function AutoCompleteDB()
{
	// set the initial values.
	this.bEnd = false;
	this.nCount = 0;
	this.aStr = new Object;
}

AutoCompleteDB.prototype.add = function(str)
{
	// increment the count value.
	this.nCount++;

	// if at the end of the string, flag this node as an end point.
	if ( str == "" )
		this.bEnd = true;
	else
	{
		// otherwise, pull the first letter off the string
		var letter = str.substring(0,1);
		var rest = str.substring(1,str.length);
		
		// and either create a child node for it or reuse an old one.
		if ( !this.aStr[letter] ) this.aStr[letter] = new AutoCompleteDB();
		this.aStr[letter].add(rest);
	}
}

AutoCompleteDB.prototype.getCount = function(str, bExact)
{
	// if end of search string, return number
	if ( str == "" )
		if ( this.bEnd && bExact && (this.nCount == 1) ) return 0;
		else return this.nCount;
	
	// otherwise, pull the first letter off the string
	var letter = str.substring(0,1);
	var rest = str.substring(1,str.length);
	
	// and look for case-insensitive matches
	var nCount = 0;
	var lLetter = letter.toLowerCase();
	if ( this.aStr[lLetter] )
		nCount += this.aStr[lLetter].getCount(rest, bExact && (letter == lLetter));
	
	var uLetter = letter.toUpperCase();
	if ( this.aStr[uLetter] )
		nCount += this.aStr[uLetter].getCount(rest, bExact && (letter == uLetter));
	
	return nCount;	
}

AutoCompleteDB.prototype.getStrings = function(str1, str2, outStr)
{
	if ( str1 == "" )
	{
		// add matching strings to the array
		if ( this.bEnd ) 
			outStr.push(str2);

		// get strings for each child node
		for ( var i in this.aStr )
			this.aStr[i].getStrings(str1, str2 + i, outStr);
	}
	else
	{
		// pull the first letter off the string
		var letter = str1.substring(0,1);
		var rest = str1.substring(1,str1.length);
		
		// and get the case-insensitive matches.
		var lLetter = letter.toLowerCase();
		if ( this.aStr[lLetter] )
			this.aStr[lLetter].getStrings(rest, str2 + lLetter, outStr);

		var uLetter = letter.toUpperCase();
		if ( this.aStr[uLetter] )
			this.aStr[uLetter].getStrings(rest, str2 + uLetter, outStr);
	}
}


function AutoComplete(aStr, oText, oDiv, nMaxSize)
{
	// initialize member variables
	this.oText = oText;
	this.oDiv = oDiv;
	this.nMaxSize = nMaxSize;
	
	// preprocess the texts for fast access
	this.db = new AutoCompleteDB();
	var i, n = aStr.length;
	for ( i = 0; i < n; i++ )
	{
		this.db.add(aStr[i]);
	}
			
	// attach handlers to the text-box
	oText.AutoComplete = this;
	oText.onkeyup = AutoComplete.prototype.onTextChange;
	oText.onblur = AutoComplete.prototype.onTextBlur;
}

AutoComplete.prototype.onTextBlur = function()
{
	this.AutoComplete.onblur();
}

AutoComplete.prototype.onblur = function()
{
	this.oDiv.style.visibility = "hidden";
}

AutoComplete.prototype.onTextChange = function()
{
	
	this.AutoComplete.onchange();
}

AutoComplete.prototype.onDivMouseDown = function()
{
	this.AutoComplete.oText.value = this.innerHTML;
}

AutoComplete.prototype.onDivMouseOver = function()
{
	this.className = "AutoCompleteHighlight";
}

AutoComplete.prototype.onDivMouseOut = function()
{
	this.className = "AutoCompleteBackground";
}

AutoComplete.prototype.onchange = function()
{
	var txt = this.oText.value;
	
	// count the number of strings that match the text-box value
	var nCount = this.db.getCount(txt, true);
	
	// if a suitable number then show the popup-div
	if ( (this.nMaxSize == -1 ) || ((nCount < this.nMaxSize) && (nCount > 0)) )
	{
		// clear the popup-div.
		while ( this.oDiv.hasChildNodes() )
			this.oDiv.removeChild(this.oDiv.firstChild);
			
		// get all the matching strings from the AutoCompleteDB
		var aStr = new Array();
		this.db.getStrings(txt, "", aStr);
		
		// add each string to the popup-div
		var i, n = aStr.length;
		for ( i = 0; i < n; i++ )
		{
			var oDiv = document.createElement('div');
			this.oDiv.appendChild(oDiv);
			oDiv.innerHTML = aStr[i];
			oDiv.onmousedown = AutoComplete.prototype.onDivMouseDown;
			oDiv.onmouseover = AutoComplete.prototype.onDivMouseOver;
			oDiv.onmouseout = AutoComplete.prototype.onDivMouseOut;
			oDiv.AutoComplete = this;			
		}
		this.oDiv.style.visibility = "visible";
	}
	else // hide the popup-div
	{
		this.oDiv.innerHTML = "";
		this.oDiv.style.visibility = "hidden";
	}
}

function createAutoComplete()
{
var aNames;
var myString1;

	//var aNames =
	//[
		//"Arron", "Callum", "Les","Lorna"
	//];
	
	myString= [];               
	myString=RegExp.$1;
	//alert(myString);
	aNames=myString.split(",");
	//alert("Found " + aNames.length + " Matching Records");
	showSniffCount();
	document.getElementById("sniff1").innerHTML ="Filtered through a possible -  "
	runText();
	document.getElementById("lblrecords").innerHTML = " records."
	
	document.getElementById("lblRecCountSearch").innerHTML = aNames.length;
	new AutoComplete(
		aNames, 
		document.getElementById('TXT_Main_Search'), 
		document.getElementById('theDiv'), 
		55
	);
	hidediv();
}



var MY_XMLHTTP; //This is the object we'll be using.

function createXHRObject() {
  if (window.ActiveXObject) {
    //Microsoft uses an ActiveX Object
    MY_XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
    return true;
  }
  else if (window.XMLHttpRequest) {
    //Firefox and Safari have it built into the Document Model
    MY_XMLHTTP = new XMLHttpRequest();
	
    return true;
  }
  else {
    return false;
  }
}

//The  letter  is   matched to the names in the database already
function getNames() {
//alert('into get username function')
  //Only try to use the object if it's actually created  
  //document.getElementById("status").innerHTML = "images/tiny_red.gif";
  var mytext= document.form1.TXT_Main_Search.value;
  //var strmydb=document.form1.hfDB.value;
  var strmydb= document.getElementById('hfDB').value;
;
myLen=mytext.length;
//alert("Text Length: "+myLen);


//if (unicode != 8 ) {

if (myLen==2) {  
showdiv();
      if (createXHRObject()) {
         //The open method tells the object where it's going
		// var sURL = "/includes/inc_ajaxSearchTest2.asp?letter="+mytext+;
		var sURL = "/includes/inc_ajaxSearchTest2.asp?mydb="+strmydb+"&letter="+mytext;
	     MY_XMLHTTP.open("GET", sURL);
         //The onreadystatechange property tells the object what function to call when it returns.
         MY_XMLHTTP.onreadystatechange = respond;
         //The send method actually starts the call.
         //If sending post data, or and XML Document, you'd pass it in here.
         MY_XMLHTTP.send(null);
        }
 
   else {
   
    //alert("Ajax not supported");
  }
 }
//}
}
function respond()
{

var GetResult;
// if xmlhttp shows "loaded"
if (MY_XMLHTTP.readyState==4)
  {
  // if "OK"
  if (MY_XMLHTTP.status==200)
    {
    // ...see if the username...
	//window.alert("The String is : " + MY_XMLHTTP.responseText);
	GetResult = MY_XMLHTTP.responseText;
	//window.alert(GetResult);
	if(GetResult!=0) 
			     {
         		   	//alert("Geiing String Names From Other Page");	
					//alert("About To Load");
	                		   
				   GetResult.match(/Names:(.*)/);
				   	createAutoComplete();  
					
					            			
				 }
    }
  else
    {
	//alert("Problem Retrieving The Data \n Or No Records Held That Match What Your Typing")
    } 
 }
}

