var autos = new Array();// array of autoforms
var tid = 0; // timer id
var httpBaseURL;
var startswithServlet;
var resultCache=new Object(); // This is a cache of results from the server...
var keycode=0; //holds the last keycode pressed
var req = null;
var enter = false;
var textkeyword="";
var currfid;

function AutoForm(fid,aid,inputfield,pagetype)
{
this.id = fid;
this.autoid = aid;
var thisform = document.getElementById(fid);
this.inputfield = inputfield;
this.pagetype=pagetype;
this.ftype="2";
this.rows = 0;
this.rowDivList = null;
this.highlightedSuggestionIndex = -1;
this.highlightedSuggestionDiv= null;
this.topicArray = new Array(10); 
this.countArray = new Array(10);
this.cursorUpDownPressed = false;
this.currentInputFieldValue=""; 
this.partialquery="";
this.currTopic = ""; 
this.currCount = ""; 
this.keycode = 0;
if (this.inputfield.addEventListener)
this.inputfield.addEventListener("keypress", KeyPress, true);
thisform.onsubmit= returnFalse;
if(pagetype=="1"){
document.getElementById(aid).style.margin = (!window.XMLHttpRequest)?"-5px 0px 0px 0px":"-5px 0px 0px 0px";
}else if(pagetype=="2") {
document.getElementById(aid).style.margin = (!window.XMLHttpRequest)?"-200px 0px 0px 80px":"-200px 0px 0px 80px";
document.getElementById(aid).style.width = "289px";
}
}
function returnFalse() {return false;}
function returnTrue(){return true;}
function InitForm(fid,aid,inputfield,pagetype)
{
var autoform = new AutoForm(fid,aid,document.getElementById(inputfield),pagetype);
autos[fid] = autoform;
}
function BodyLoad(HttpBaseURL, StartsWithServlet,s, fid,setFocus)
{
	return 0;
httpBaseURL = HttpBaseURL;
startswithServlet = StartsWithServlet;
currfid=fid;
    var queryfield = document.getElementById(s);
    //Don't set focus for sitePages - check the value of boolean setFocus
    if ((queryfield && queryfield.value=="") && ((setFocus != null && setFocus == true) || setFocus == null)){   
        queryfield.focus(); // focus on first inputfield if available and empty
    }
    textkeyword=queryfield.value;
    setTimeout("everytenms()",10);
}
function KeyDown(fid,evt)
{
    var ctrl = 0;
    if(!evt && window.event)
{
        evt=window.event;
    }
    
    if(document.layers)
        ctrl = (evt.modifiers & Event.CONTROL_MASK) > 0;
    else
        ctrl = evt.ctrlKey;
        
    if (tid)
return;

keycode = evt.keyCode;
    if (ctrl)
       return true;
    if (isCtrlKey(keycode))
        return true;
return UpDown(fid,keycode);
}
function KeyPress(event)
{
if (event.which == 0 && !isShowKey(keycode)) {
if (event.preventDefault)
event.preventDefault();
return false;
}
}
UpDown = function(fid,keycode)
{
var inputfield = autos[fid].inputfield;
var inputfieldValue = inputfield.value;
autos[fid].keycode = keycode;
if(handleCursorUpDownEnter(keycode,fid,inputfieldValue) || isShowKey(keycode))
return true;
else
return false;
}
// returns false if cursor up / cursor down or enter pressed...
function handleCursorUpDownEnter(eventCode,fid,inputfieldValue)
{
var highlightedSuggestionIndex = autos[fid].highlightedSuggestionIndex;
var autodiv = document.getElementById(autos[fid].autoid);
    if (eventCode == 33) // page up
{
if (autodiv.style.visibility=="visible")
{
setStyleForElement(autos[fid].highlightedSuggestionDiv,"1");
autos[fid].highlightedSuggestionIndex = 0;
highlightNewValue(fid,0);
}
return false;
}
else if (eventCode == 34) // page down
{
if (autodiv.style.visibility=="visible")
{
setStyleForElement(autos[fid].highlightedSuggestionDiv,"1");
autos[fid].highlightedSuggestionIndex = autos[fid].rows-1;
highlightNewValue(fid,autos[fid].rows-1);
}
return false;
}
else if(eventCode==40)
{
if (autodiv.style.visibility=="visible")
{
highlightNewValue(fid,highlightedSuggestionIndex+1);
return false;
}
}
else if(eventCode==38)
{
if (autodiv.style.visibility=="visible")
{
highlightNewValue(fid,highlightedSuggestionIndex-1);
return false;
}
}
else if(eventCode==13||eventCode==3)
{
if (eventCode==13) {
if(autos[fid].currTopic!=""){
autos[fid].inputfield.value=autos[fid].currTopic;
textkeyword=autos[fid].currTopic;
}
document.getElementById(autos[fid].autoid).style.visibility="hidden";
return submitForm(fid);
}
return false;
}
if (keycode != 0)
ForEachKeyPressed(inputfieldValue,fid);
return true;
}
// Called when cursor up/down pressed... selects new entry in autodiv...
function highlightNewValue(fid,C)
{
autos[fid].currentInputFieldValue= autos[fid].partialquery;
//autos[fid].inputfield.value=autos[fid].partialquery;
if(!autos[fid].rowDivList)
{
return;
}
ShowDiv(fid);
if(C>=autos[fid].rows)
{
        if (autos[fid].dir=="up")
        {
            C=autos[fid].rows;
        }
        else
        {
            C=autos[fid].rows-1
            autos[fid].highlightedSuggestionDiv=autos[fid].rowDivList.item(C);
        }
    }
if(C<0)
{
        if (autos[fid].dir=="up")
        {
            C=0;
            autos[fid].highlightedSuggestionDiv=autos[fid].rowDivList.item(C);
        }
        else
        {
            C=-1;
            autos[fid].highlightedSuggestionIndex=-1
            autos[fid].highlightedSuggestionDiv= null;
            setStyleForElement(autos[fid].rowDivList.item(0),"1");
        }
    }
 if(autos[fid].highlightedSuggestionIndex!=-1&&C!=autos[fid].highlightedSuggestionIndex)
 {
setStyleForElement(autos[fid].highlightedSuggestionDiv,"1");
autos[fid].highlightedSuggestionIndex=-1
}

if (C>=0)
{
autos[fid].highlightedSuggestionIndex=C;
autos[fid].highlightedSuggestionDiv=autos[fid].rowDivList.item(C);
setStyleForElement(autos[fid].highlightedSuggestionDiv,"0");
currentInputFieldValue=autos[fid].partialquery;
var q = valueOfAutodiv(autos[fid].highlightedSuggestionDiv);
autos[fid].currTopic = autos[fid].topicArray[C];
autos[fid].currCount = autos[fid].countArray[C];
}
else
{
autos[fid].currTopic ="";
autos[fid].currCount ="";
}
}
// This function gets called for every keypress I make...
function ForEachKeyPressed(partialquery,fid)
{
var localInputField = autos[fid].inputfield;
var autoid = autos[fid].autoid;
var autodiv = document.getElementById(autoid);
var highlightedSuggestionIndex= autos[fid].highlightedSuggestionIndex;
    // This becomes the rows in our suggestion list...
var J=autodiv.getElementsByTagName("LI");

// # of rows in list...
var rows = J.length;
autos[fid].rows = rows;
autos[fid].rowDivList=J;
autos[fid].partialquery=partialquery;
autos[fid].currTopic = ""; //reset
autos[fid].currCount = ""; //reset
var s= localInputField.value;
if(s=="" || rows==0 || keycode==27 || s.search(/\S/)==-1 || (autodiv.style.visibility=="hidden" && ( keycode==35 || keycode==36))) //esc
HideDiv(fid);
for(var f=0; f<rows; f++)
{
setStyleForElement(J.item(f),"1");
}
var highlightedSuggestionDiv = autos[fid].highlightedSuggestionDiv;
var noncharkey = false;
switch(autos[fid].keycode)
{
case 8: // back space
case 33: //page up
case 34: // page down
case 35: // end
case 36: // home
case 37: // left arrow
case 39: // right arrow
case 45: //????
case 46: // delete
noncharkey=true;
break;
default:
// regular keypress ...
break
}
if(!noncharkey && highlightedSuggestionDiv)
{
setStyleForElement(highlightedSuggestionDiv,"0");
var z;
z=valueOfAutodiv(highlightedSuggestionDiv);
if(z!=localInputField.value)
{
if(localInputField.value!=partialquery)
{
return;
}
}
}
else
{
highlightedSuggestionIndex=-1;
    }
}
// Return null if i undefined...
// otherwise return value of span 3...
function valueOfAutodiv(i)
{
if(!i)
{
return null;
}
return findSpanValueForClass(i,"5")
}
// Find span value with className = dc...
function findSpanValueForClass(i,dc){
var ga=i.getElementsByTagName("span");
if(ga){
for(var f=0; f<ga.length; ++f){
if(ga[f].className==dc){
var value=ga[f].innerHTML;
if(value=="&nbsp;") {
return"";
} else{
var z=stripCRFromString(value);
return z
}
}
}
}else{
return""
}
}
// strip CR from string...
function stripCRFromString(va)
{
for(var f=0,oa="",zb="\n\r"; f<va.length; f++)
{
if (zb.indexOf(va.charAt(f))==-1)
oa+=va.charAt(f);
else
oa+=" ";
}
return oa
}

function SendQuery(fid)
{
tid =0;
var autoid = autos[fid].autoid;
var s = autos[fid].inputfield;
var query = s.value;
if(query.length!=0 && query.search(/\S/)!=-1)
{

var result=resultCache[autos[fid].ftype+query];
if(result)
{
// Found in our cache...

ShowDiv(autoid);
startswithResult(query,result[0],result[1],fid);
}
else
{
var url= httpBaseURL+startswithServlet+"?w=" + query +"&c="+document.getElementById("scate").value;
//alert(url);
if (req)
req.abort();
req = (!window.XMLHttpRequest)? (window.ActiveXObject ? (new ActiveXObject("Microsoft.XMLHTTP")):""):(new XMLHttpRequest());
req.open("GET", url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.onreadystatechange = new Function("Process('" + fid + "')");
req.send(null);
}
}
else
HideDiv(fid);
}
function Process(fid)
{

if (req.readyState == 4)
{
try // ff throws an exception sometimes
{

if (req.status == 200)
{
if(req.responseText=="")
HideDiv(fid);
else
{
//alert(req.responseText);
eval('startswithResult(' + req.responseText + '"' + fid + '");');
}
}
}
catch(e){}
}
}
function ShowDiv(fid)
{
if (autos[fid])
{
document.getElementById(autos[fid].autoid).style.visibility="visible";
showHideSelects(fid,false);
}
}

function HideDiv(fid)
{
if (autos[fid])
{
document.getElementById(autos[fid].autoid).style.visibility="hidden";
showHideSelects(fid,true);
}

}
function onBlurHandler( fid, evt )
{
if(!evt && window.event)
{
evt=window.event;
}
eval("HideDiv('" + fid + "')");
}
// We get the following javascript code dynamically returned from the server:
// "bill gates", new Array("Bill Gates", "Bill Gates tax"), new Array("Computer Tycoon", "neologisms"), new Array("bill-gates", "bill-gates-tax"),
startswithResult=function(partialquery,topicArray,countArray,fid)
{
resultCache[autos[fid].ftype+partialquery]=new Array(topicArray,countArray);
autos[fid].topicArray = topicArray;
autos[fid].countArray = countArray;
 // alert(topicArray.length);
if (topicArray.length==0)
{
HideDiv(fid);
autos[fid].rows=0;
autos[fid].rowDivList=null;
}
else
{
        
        ShowDiv(fid); // ShowDiv has to be before displayFoundList
        
         if (autos[fid].dir=="up")
            autos[fid].highlightedSuggestionIndex= topicArray.length;
        else
            autos[fid].highlightedSuggestionIndex= -1;
        displayFoundList(topicArray, countArray, fid);
        var dir = autos[fid].dir;
        ForEachKeyPressed(partialquery, fid);
}
}
function displayFoundList(topicArray, countArray, fid)
{
var autoid = autos[fid].autoid;
var autoDIV = document.getElementById(autoid);
while(autoDIV.childNodes.length > 0)
autoDIV.removeChild(autoDIV.childNodes[0]);
if (topicArray.length!=0)
{

var pp = document.createElement("DIV");
var mm = document.createElement("UL");
for(var f = 0; f < topicArray.length; ++f)
{
var u = document.createElement("LI");
setStyleForElement(u, "1");
u.onmouseover=new Function("mover('" + fid + "',"+f +")");
u.onmouseout=new Function("mout('" +fid +"')");
if (typeof(document.addEventListener) !="undefined")
u.addEventListener("mousedown",mdown ,false);
else
u.onmousedown=mdown;
var ua = document.createElement("SPAN");
u.displaySpan = ua;
var text = topicArray[f];
var text2 = "";
if (countArray[f].length>0)
{
text2= " <span style='float:right;text-decoration:none;color:black;font-size:12px;'>" + addComma(countArray[f]) + "ΜυΠΕΟ’</span>";
}
ua.innerHTML = "<span style='float:left;color:green;font-size:12px;text-decoration:underline'>" +text +"</span>"+ text2+"<br/>";
u.appendChild(ua);
mm.appendChild(u);
}

pp.appendChild(mm);
autoDIV.appendChild(pp);
}
}
var mdown=function(event)
{
if (isLeftButton(event))
{
autos[currfid].inputfield.value=autos[currfid].currTopic;

textkeyword=autos[currfid].currTopic;
document.getElementById(autos[currfid].autoid).style.visibility="hidden";
return submitForm(currfid);
}
}
var mover=function(fid,index)
{
if(autos[fid].highlightedSuggestionIndex!=-1 && index!=autos[fid].highlightedSuggestionIndex)
{
setStyleForElement(autos[fid].highlightedSuggestionDiv,"1");
autos[fid].highlightedSuggestionIndex=-1
}
var autoid = autos[fid].autoid;
var autodiv = document.getElementById(autoid);
J=autodiv.getElementsByTagName("LI");
autos[fid].highlightedSuggestionIndex=index;
autos[fid].rowDivList=J;
autos[fid].highlightedSuggestionDiv=autos[fid].rowDivList.item(index);
autos[fid].currTopic= autos[fid].topicArray[index];
autos[fid].currCount= autos[fid].countArray[index];
setStyleForElement(autos[fid].highlightedSuggestionDiv,"0");
};

var mout=function(fid)
{
    autos[fid].currTopic= "";
autos[fid].currCount= "";
    setStyleForElement(autos[fid].highlightedSuggestionDiv,"1");
}
function setStyleForElement(c,name)
{
    if (c!= null && typeof(c) !="undefined")
{
c.className=name;
switch(name.charAt(0))
{
 case "0":
c.style.backgroundColor="#3366cc";
c.style.color="black";
            c.style.cursor= "pointer";
            if(c.displaySpan)
{
var linkarray = c.displaySpan.getElementsByTagName("span");
if (linkarray.item(0))linkarray.item(0).style.color= "white";
if (linkarray.item(1))linkarray.item(1).style.color= "white";
}
break;
 case "1":
c.style.backgroundColor="white";
c.style.color="black";
if(c.displaySpan)
{
var linkarray = c.displaySpan.getElementsByTagName("span");
if (linkarray.item(0))linkarray.item(0).style.color= "green";
if (linkarray.item(1))linkarray.item(1).style.color= "black";
}
break;
case "2":
c.style.display="block";
c.style.paddingRight="3";
c.style.height="15px";
c.style.overflow="hidden";
break;
case "6":
c.style.styleFloat = "right";
c.style.cssFloat="right";
c.style.width=40+"%";
c.style.fontSize="11px";
c.style.textAlign="right";
c.style.whiteSpace="nowrap";
break;
case "7":
c.style.width=60+"%";
c.style.cssFloat="left";
c.style.textAlign="left";
c.style.fontSize="11px";
c.style.whiteSpace="nowrap";
break;
case "8":
c.style.textAlign="right";
c.style.whiteSpace="nowrap";
c.style.fontSize="11px";
break;
}
}
}
/* In order to fix an IE bug for Incremental search
 The div is always below the select box, so every time you display the div
 you should hide the select boxes that overlap
*/
function showHideSelects(fid,bShow)
{
var selObjects=document.getElementsByTagName("SELECT");
var autodiv = document.getElementById(autos[fid].autoid);
for(var i=0;i<selObjects.length;i++)
{
if(ObjectOverlap(autodiv, selObjects[i]) || selObjects[i].id=="menu" || selObjects[i].id=="topic" || selObjects[i].id=="categories")
{
selObjects[i].style.visibility = (bShow == true ? "visible" : "hidden");
}
}
}
function ObjectOverlap(obj1, obj2)
{
var obj1TopX = getLeft(obj1);
var obj1TopY = getTop(obj1);
var obj1BottomX = getLeft(obj1)+obj1.offsetWidth;
var obj1BottomY = getTop(obj1)+obj1.offsetHeight;

var obj2TopX = getLeft(obj2);
var obj2TopY = getTop(obj2);
var obj2BottomX = getLeft(obj2)+obj2.offsetWidth;
var obj2BottomY = getTop(obj2)+obj2.offsetHeight;

var overlapOnX = (obj1TopX < obj2BottomX && obj2TopX < obj1BottomX);
var overlapOnY = (obj1TopY < obj2BottomY && obj2TopY < obj1BottomY);

return (overlapOnX && overlapOnY);
}
//Positioning functions
function getObjLeft(anObject)
{
return(anObject.offsetParent ? (getObjLeft(anObject.offsetParent) + anObject.offsetLeft) : anObject.offsetLeft);
}

function getObjTop(anObject)
{
return(anObject.offsetParent ? (getObjTop(anObject.offsetParent) + anObject.offsetTop) : anObject.offsetTop);
}


function getLeft(anObject)
{
return(getObjLeft(anObject));
}

function getTop(anObject)
{
return(getObjTop(anObject));
}

function isSendKey(keycode)
{
var sendkey = true;
switch(keycode)
{
//case 8: // back space
case 10: //shift
case 11: //ctrl
case 12: //alt
case 13: //enter
case 27: //esc
case 33: //page up
case 34: // page down
case 35: // end
case 36: // home
case 37: // left arrow
case 38: // up arrow
case 39: // right arrow
case 40: // down arrow
case 45: //????
//case 46: // delete
sendkey=false;
break;
default:
// regular keypress ...
break;
}
return sendkey;
}
function isShowKey(keycode)
{
// 38 is up cursor key, 40 is down cursor key...
if(keycode==40 || keycode==38 || keycode == 33 || keycode== 34 || keycode==13)
return false;
else
return true;
}
function isCtrlKey(keycode)
{
if(keycode==16 || keycode==17 || keycode == 18)// shift,ctrl,alt
return true;
else
return false;
}
function isLeftButton(e)
{
var leftclick;
if (!e) var e = window.event;
if (e.which) leftclick = (e.which == 1);
else if (e.button) leftclick = (e.button == 0 || e.button==1);
return leftclick;
}

function addComma(aCount) {
	var retStr = "";
	var len = aCount.length;
	var first = true;
	while(len>3) {		
		retStr=","+aCount.substring(len-3,len)+retStr;
		aCount = aCount.substring(0,len-3);
		len = aCount.length;
	}
	return (aCount+retStr);
}

function everytenms(){
	var qnowvalue=autos[currfid].inputfield.value;
	if(qnowvalue==""){
		document.getElementById(autos[currfid].autoid).style.visibility="hidden";		
	} else if (textkeyword==qnowvalue) {
		
	} else {
		SendQuery(currfid);
		textkeyword=qnowvalue;
	}
	if(qnowvalue!=null){
	setTimeout("everytenms()",10);
	//alert(qnowvalue);
	}
	return true;
}
function submitForm(fid){
var terms = document.getElementById('seek1').value;
var c;
var c1;
var c2;
var d;
var citem = document.getElementsByName('c');
if(citem==undefined||citem==null){
	 c="";
}else {
   for(var i=0;i<citem.length;i++) {
    	  if(citem[i].checked) {
    	     c=citem[i].value;
    	  }
   }
}
if(c==undefined)
  c="";
var c1item = document.getElementById('c1');
for(var j=0;j<c1item.length;j++) {
    	if(c1item[j].selected) {
    	c1=c1item[j].value;
    	}
}

var c2item = document.getElementById('c2');
if(c2item==undefined||c2item==null){
c2="";
}else {
  for(var k=0;k<c2item.length;k++) {
    	if(c2item[k].selected) {
    	  c2=c2item[k].value;
    	}
  }
}



var ditem = document.getElementById('day');
if(ditem==undefined||ditem==null){
  d="";
}else {
  for(var n=0;n<ditem.length;n++) {
    	if(ditem[n].selected) {
    	  d=ditem[n].value;
    	}
   }
} 
 
 if(document.getElementById("form1").acbb) {
    var ac_value=document.getElementById("form1").acbb.value;	
     if("comp"==ac_value)
        document.getElementById(fid).action="http://so.netsun.com/web/comp.htm";
     else
     	  document.getElementById(fid).action="http://so.netsun.com/web/prod.htm";

}


var action=document.getElementById(fid).action;
var count = action.indexOf("?");
if(count>0) {
   document.getElementById(fid).action=action.substr(0,count)+"?terms="+terms+"&c="+c+"&c1="+c1+"&c2="+c2+"&d="+d;
} else {
  document.getElementById(fid).action=action+"?terms="+terms+"&c="+c+"&c1="+c1+"&c2="+c2+"&d="+d;
}

document.getElementById(fid).submit();
}

function changeft(fid,ftype){
autos[fid].ftype=ftype;
}  