var webSearch=new Array();var text=new Array();var newsearch=false;var NFIELDS;var TXT_DEFAULT="insert text here";var RES1_INIT="--";var RES1_SEARCHING="??";var RES2_INIT="--";var RES2_SEARCHING="counting...";var BAR_HEIGHT=10;var LONG_TEXT_TH=25;var ZERO_TIP='<b>TIP:</b> try shorter text. <a href="#zerohits"><b>More...</b></a>';var EXAMPLES_SEARCHING="searching...";var EXAMPLES_INIT="examples sentences will be displayed as you use the sentence checker tool...";function SearchBox(a){this.ind=a;this.text=TXT_DEFAULT;this.state=SearchBox.IDLE;this.tic=0;this.count=0;this.initialized=false}SearchBox.prototype.Reset=function(){this.GetTxtObj().value=TXT_DEFAULT;this.GetTxtObj().style.color="gray";this.initialized=false;this.GetRes1Obj().innerHTML=RES1_INIT;this.GetRes2Obj().innerHTML=RES2_INIT;this.GetHintObj().innerHTML=""};SearchBox.prototype.Init=function(){if(!this.initialized&&this.GetTxtObj().value==TXT_DEFAULT){this.initialized=true;this.GetTxtObj().value="";this.GetTxtObj().style.color="black"}};SearchBox.prototype.SetText=function(a){if(a!=null){this.initialized=true;this.GetTxtObj().value=a;this.GetTxtObj().style.color="black"}else{this.initialized=false;this.GetTxtObj().value=""}};SearchBox.prototype.Search=function(){if(!this.initialized){this.GetTxtObj().value="";this.initialized=true;this.GetTxtObj().style.color="black"}query=this.GetTxtObj().value;this.GetRes1Obj().innerHTML=RES1_SEARCHING;this.GetRes2Obj().innerHTML=RES2_SEARCHING;this.GetHintObj().innerHTML="";if(query==""){this.state=SearchBox.COMPLETED}else{dbg("Query called: "+query+" ind ="+this.ind);webSearch[this.ind].execute('allintext:"'+query+'"');this.state=SearchBox.PENDING}this.tic=0;this.count=0};SearchBox.prototype.OnSearchComplete=function(){this.state=SearchBox.COMPLETED;if(webSearch[this.ind].results&&webSearch[this.ind].results.length>0){dbg("complete - res");this.count=parseInt(webSearch[this.ind].cursor.estimatedResultCount);this.GetRes1Obj().innerHTML=webSearch[this.ind].cursor.estimatedResultCount;this.GetRes2Obj().innerHTML=""}else{dbg("complete - nores");this.GetRes1Obj().innerHTML="0";if(this.GetTxtObj().value.length>LONG_TEXT_TH){this.GetHintObj().innerHTML=ZERO_TIP}}};SearchBox.prototype.CheckTimer=function(){with(this){if(state==SearchBox.PENDING){tic++;if(tic==SearchBox.MAXTIC){state=SearchBox.TIMEOUT}}}};SearchBox.prototype.IsDone=function(){if(this.state==SearchBox.TIMEOUT||this.state==SearchBox.COMPLETED){return true}else{return false}};SearchBox.prototype.GetTxtObj=function(){return document.getElementById("txt"+this.ind)};SearchBox.prototype.GetRes1Obj=function(){return document.getElementById("res"+this.ind+"_1")};SearchBox.prototype.GetRes2Obj=function(){return document.getElementById("res"+this.ind+"_2")};SearchBox.prototype.GetHintObj=function(){return document.getElementById("hint"+this.ind)};SearchBox.IDLE=0;SearchBox.PENDING=1;SearchBox.COMPLETED=2;SearchBox.TIMEOUT=3;SearchBox.MAXTIC=20;function Googlenglish(){newsearch=true;for(var a=0;a<NFIELDS;a++){text[a].Search()}setExamplesHTML(EXAMPLES_SEARCHING)}function logResults(){var a=GetXmlHttpObject();if(a==null){alert("Your browser does not support AJAX!");return}var b="http://textranch.com/php_log/log_queries_db.php";var d="";for(var c=0;c<NFIELDS;c++){d+="q"+(c+1)+"="+text[c].GetTxtObj().value+"&";d+="h"+(c+1)+"="+text[c].count;d+=(c<3)?"&":""}a.open("POST",b,true);a.setRequestHeader("Content-type","application/x-www-form-urlencoded");a.setRequestHeader("Content-length",d.length);a.setRequestHeader("Connection","close");a.onreadystatechange=function(){if(a.readyState==4&&a.status==200){}};a.send(d)}function GetXmlHttpObject(){var a=null;try{a=new XMLHttpRequest()}catch(b){try{a=new ActiveXObject("Msxml2.XMLHTTP")}catch(b){a=new ActiveXObject("Microsoft.XMLHTTP")}}return a}function dbg(a){}function processkey(b){var a=(b.which)?b.which:b.keyCode;if(a==13){Googlenglish()}}function OnTimer(){var d;if(newsearch){var f=true;for(d=0;d<NFIELDS;d++){text[d].CheckTimer();if(!text[d].IsDone()){f=false}}if(f){dbg("Query Completed");for(d=0;d<NFIELDS;d++){text[d].state=SearchBox.IDLE}var e=0;for(d=0;d<NFIELDS;d++){e+=text[d].count}var a="";for(d=0;d<NFIELDS;d++){var c=Math.floor((100*text[d].count)/e);if(c==0){text[d].GetRes2Obj().innerHTML=""}else{var g=(97*c)/100;text[d].GetRes2Obj().innerHTML='<img src="http://textranch.com/bar.gif" height="'+BAR_HEIGHT+'" width="'+g+'">'}if(webSearch[d].results&&webSearch[d].results.length>0){if(d>0){a+="<br />"}a+="<b><u>"+text[d].GetTxtObj().value+"</u></b><br/>";a+="<UL>";for(var b=0;b<webSearch[d].results.length;b++){a+="<LI>"+webSearch[d].results[b].content+"</LI>"}a+="</UL>"}}setExamplesHTML(a);logResults()}}else{}}function textranchInit(a){NFIELDS=a;var b;for(b=0;b<NFIELDS;b++){webSearch[b]=new GwebSearch()}for(b=0;b<NFIELDS;b++){text[b]=new SearchBox(b);webSearch[b].setSearchCompleteCallback(text[b],SearchBox.prototype.OnSearchComplete);text[b].Reset()}setInterval("OnTimer()",1000)}function getExamplesDiv(){return document.getElementById("example_sentences")}function setExamplesHTML(a){getExamplesDiv().innerHTML=a}function getExamplesHTML(){getExamplesDiv().innerHTML}function showExample(d,c,b,a){text[0].SetText(d);text[1].SetText(c);text[2].SetText(b);text[3].SetText(a);Googlenglish()}function clearForm(){for(var a=0;a<NFIELDS;a++){text[a].Reset()}};
