/*
 * AutoCompleteClass.js
 */

function AutoComplete_Request(url,str)
{
	var data = AutoComplete_Request_get(url + escape(str));
	if (data) 
	{
		data = data.split("\n");
		for (var i=0;i<data.length;i++) 
		{
			if (data[i].length > 0) 
			{
				this.data[this.data.length] = data[i];
			}
		}
	} 
	else 
	{
		this.data = new Array();
	}
}

function AutoComplete_Request_get(targetURL) 
{
	try 
	{
		var httpRequest = new XMLHttpRequest();			
	} 
	catch ( e ) 
	{
		try 
		{
			var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (e) 
		{
			alert("Error creating the connection!");
			return;
		}
	}

	try 
	{
		httpRequest.open("GET", targetURL, false, null, null);
		httpRequest.send("");
	} catch ( e ) 
	{
		//alert("An error has occurred calling the external site: " + e);
		return false;
	} 
	switch ( httpRequest.readyState ) 
	{
		case 1, 2, 3:
			alert("Bad Ready State: " + httpRequest.status);
			return false;
		break;
		
		case 4:
			if ( httpRequest.status != 200 ) 
			{
				//alert("The server respond with a bad status code: " + httpRequest.status);
				return false;
			} else 
			{
				var response = httpRequest.responseText;
			}
		break;
	}
	return response;
}

function AutoComplete_Request_getCount() 
{	
	return this.data.length;
}

function AutoComplete_Request_getStrings() 
{
	/*this.data = new Array();
	this.data.push("A");
	this.data.push("ABCD");
	this.data.push("Better");
	this.data.push("Tomorrow");
	this.data.push("離婚及婚姻");	*/
	var oText = document.getElementById('theTxt');
	var curText= oText.value.toLowerCase();
	curText = Trim(curText);
	var filtered = new Array();
	
	if (curText.length > 0)
	{
		var i, n = this.data.length;
		for (i = 0; i<n; i++)
		{
			if (data[i].toLowerCase().indexOf(curText) == 0)
				filtered.push(data[i]);
		}
	}
	return filtered;
	
}

function AutoComplete( oText, oDiv, nMaxSize)
{
	// initialize member variables
	this.oText = oText; // the text box
	this.oDiv = oDiv; // a hidden <div> for the popup auto-complete
	this.nMaxSize = nMaxSize;
	// preprocess the texts for fast access

	// 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();
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

AutoComplete.prototype.onchange = function()
{
	var txt = this.oText.value;
	this.data = new Array();
	var aStr = AutoComplete_Request_getStrings();
	// count the number of strings that match the text-box value.
	var nCount = aStr.length;

	// 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
		
	  	// add each string to the popup-div
	  	var i, n = aStr.length;
	  	var header = document.createElement('div');
	  	this.oDiv.appendChild(header);
	  	header.innerHTML="<B>Common Keywords:</B>";
	  	for ( i = 0; i < n; i++ )
	  	{
		  	var oDiv = document.createElement('div');
		  	this.oDiv.appendChild(oDiv);
		  	oDiv.innerHTML = aStr[i];
		  	
		  	oDiv.style.width='250';		  	
		  	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";
	}
}    

AutoComplete.prototype.onDivMouseDown = function()
{
	// set the text-box value to the word
	this.AutoComplete.oText.value = this.innerHTML;
}

AutoComplete.prototype.onDivMouseOver = function()
{
	// assumes the existence of a CSS style called AutoCompleteHighlight
	this.className = "AutoCompleteHighlight";
}

AutoComplete.prototype.onDivMouseOut = function()
{
	// assumes the existence of a CSS style called AutoCompleteBackground
	this.className = "AutoCompleteBackground";
} 

function createAutoComplete()
{
    this.data = new Array();
    
    AutoComplete_Request("http://" + document.domain, "/worddict.txt");
    AutoComplete_Request("http://" + document.domain, "/wordictch.txt");
	new AutoComplete(
					 document.getElementById('theTxt'),
					 document.getElementById('theDiv'),
					 25
					 );

}

// End of AutoComplete.js

