var prevSrchTxt;
var wordSearched;

function showHint(txt)	{
	str = trim(txt.value);
	if (str.length < 2) {
		prevSrchTxt	 = str;
		return;
	}

	if (str == prevSrchTxt)
	   return;

	curwrd = getEditingWord(str);

	if (curwrd.length >3) {
		document.getElementById("txtHint").style.height=25;
		document.getElementById("txtHint").innerHTML="<font size=1 face='tahoma,arial' color=red>enter only the 2 or 3 characters.</font>";
		prevSrchTxt = str;
		return;
	}

	if (curwrd.length > 1)	{
		wordSearched = curwrd;
		cbx = document.qsrchfrm.srchlang;
		qlang = cbx.options[cbx.selectedIndex].value;
		var url="/qsearch.php?type=phonetic&qw="+curwrd+"&ql="+qlang;
		xmlHttp=GetXmlHttpObject(stateChanged);

		if (!xmlHttp) {
			document.getElementById("txtHint").style.height=25;
			document.getElementById("txtHint").innerHTML="Please upgrade your browser to use this service.";
			return;
		}
		document.getElementById("txtHint").style.display="";
		document.getElementById("txtHint").style.height=25;
		document.getElementById("txtHint").innerHTML="&nbsp;<font size=1 face='tahoma,arial' color=blue>searching. please wait....";
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
		prevSrchTxt = str;
	} else	{
		document.getElementById("txtHint").style.height=0;
		document.getElementById("txtHint").style.display="none";
		document.getElementById("txtHint").innerHTML="";
	}
}


function getEditingWord(words) {
	ind = words.lastIndexOf(' ');

	if (ind < 0) {
		return words;
	}
	return words.substring(ind+1,words.length);
}

function b_lst(lst) {
	var words =  new Array();
	words = lst.split(' ');
	wordhtml = "<ol style='cursor:pointer'>";
	for (i=0;i<words.length;i++) {
		wordhtml += "<li><a href=\"javascript:sw('" + words[i] + "')\">" + words[i] + "</li>";
	}
	wordhtml+="</ol>";
	return wordhtml;
}

function checksearch(frm) {
   srcTxt = trim(frm.srchtxt.value);
   if (srcTxt.length < 3) {
	alert("search word should have 3 or more characters");
	frm.srchtxt.focus;
	return false;
   }  
   return true;
   
}

function sw(lst){
	index = lst.indexOf("...");
	word = lst;
	if (index>-1) {
		word = lst.substr(0,index);
	}
	//replace part of the word that
	//was used to search with the word
	//selected from the list.
	curTxt = document.getElementById("txt1").value;

	//start with this
	newval = curTxt;

	lpos = curTxt.lastIndexOf(wordSearched);
	if (lpos < 0) {
		//should not happen.
	} else {
		newval = curTxt.substr(0,lpos);
		newval = trim(newval) + " " + word;
	}
	
	prevSrchTxt = newval;

	document.getElementById("txtHint").style.display="none";
	document.getElementById("txt1").value = trim(newval)+" ";
	document.qsrchfrm.txt1.focus();
}


function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

		document.getElementById("txtHint").style.display = "";

		if (xmlHttp.status != 200) {
			document.getElementById("txtHint").style.height=25;
			document.getElementById("txtHint").innerHTML="<font size=2 face=arial color=red>Failed to connect to search engine.</font>";
			return;
		}

		if (trim(xmlHttp.responseText) == '') {
			document.getElementById("txtHint").style.height=25;
			document.getElementById("txtHint").innerHTML="<font class=notfound>word not found.</font>";
		} else  {
			respTxt = xmlHttp.responseText;
			var tmpAry =  new Array();
			tmpAry = respTxt.split(' ');
			if (tmpAry.length ==1) {
				document.getElementById("txtHint").style.height=40;
			} else if (tmpAry.length > 8) {
				document.getElementById("txtHint").style.height=125;
			} else if (tmpAry.length > 3) {
				document.getElementById("txtHint").style.height=25*tmpAry.length;
			} else {
				document.getElementById("txtHint").style.height=30*tmpAry.length;
			}
			document.getElementById("txtHint").innerHTML="<font class=wordlist>"+b_lst(xmlHttp.responseText)+"</font>";
		}

	}
}


function GetXmlHttpObject(handler) {
	var objXmlHttp=null;

	if (navigator.userAgent.indexOf("Opera")>=0) {
		alert("Opera browser is not supported.");
		return;
	}

	if (window.ActiveXObject) { 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
			strName="Microsoft.XMLHTTP";
		}

		try {
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler;
			return objXmlHttp;
		} catch(e) {

			return;
		}
	}

	if (window.XMLHttpRequest) {
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler;
		return objXmlHttp;
	}
}


function trim(strVal) {

    if (strVal == null || strVal == "")
        return "";

    // Trim whitespace from left end of string
    var i = 0;
    var c = null;

    do {
        c = strVal.charAt (i++);
    } while (c == " " && i < strVal.length);

    if (c == " " && i == strVal.length)
        return "";

    strVal = strVal.substr (i - 1);
   
    // Trim whitespace from right end of string
    i = strVal.length - 1;
    c = null;
    do {
        c = strVal.charAt (i--);
    } while (c == " " && i > -1);
   
    strVal = strVal.substring (0, i + 2);
   
    return strVal;
}

