function autoCompleteDB()
 {
    this.aNames=new Array();
 }
 
 autoCompleteDB.prototype.assignArray=function(aList)
 {
    this.aNames=aList;
 };
 
 autoCompleteDB.prototype.getMatches=function(str,aList,maxSize)
 {
    /* debug */ //alert(maxSize+"ok getmatches");
    var ctr=0;
    for(var i in this.aNames)
    {
         if(this.aNames[i].toLowerCase().indexOf(str.toLowerCase())==0) /*looking for case insensitive matches */
       {
          aList.push(this.aNames[i]);
          ctr++;
       }
       if(ctr==(maxSize-1)) /* counter to limit no of matches to maxSize */
          break;
    }
 };
 
 function autoComplete(aNames,oText,oDiv,maxSize)
 {
 
    this.oText=oText;
    this.oDiv=oDiv;
    this.maxSize=maxSize;
    this.cur=-1;
 
    this.db=new autoCompleteDB();
    this.db.assignArray(aNames);
    
    oText.onkeyup=this.keyUp;
    oText.onkeydown=this.keyDown;
    oText.autoComplete=this;
    oText.onblur=this.hideSuggest;
 }
 
 autoComplete.prototype.hideSuggest=function()
 {
    this.autoComplete.oDiv.style.visibility="hidden";
 };
 
 autoComplete.prototype.selectText=function(iStart,iEnd)
 {
    if(this.oText.createTextRange) /* For IE */
    {
       var oRange=this.oText.createTextRange();
       oRange.moveStart("character",iStart);
       oRange.moveEnd("character",iEnd-this.oText.value.length);
       oRange.select();
    }
    else if(this.oText.setSelectionRange) /* For Mozilla */
    {
       this.oText.setSelectionRange(iStart,iEnd);
    }
    this.oText.focus();
 };
 
 autoComplete.prototype.textComplete=function(sFirstMatch)
 {
    if(this.oText.createTextRange || this.oText.setSelectionRange)
    {
       var iStart=this.oText.value.length;
       //this.oText.value=sFirstMatch;
       this.selectText(iStart,sFirstMatch.length);
    }
 };
 
 autoComplete.prototype.keyDown=function(oEvent)
 {
    oEvent=window.event || oEvent;
    iKeyCode=oEvent.keyCode;
 
    switch(iKeyCode)
    {
       case 38: //up arrow
          this.autoComplete.moveUp();
          break;
       case 40: //down arrow
          this.autoComplete.moveDown();
          break;
       case 13: //return key
          window.focus();
          break;
    }
 };
 
 autoComplete.prototype.moveDown=function()
 {
    if(this.oDiv.childNodes.length>0 && this.cur<(this.oDiv.childNodes.length-1))
    {
       ++this.cur;
       for(var i=0;i<this.oDiv.childNodes.length;i++)
       {
          if(i==this.cur)
          {
             this.oDiv.childNodes[i].className="over";
             this.oText.value=this.oDiv.childNodes[i].innerHTML;
          }
          else
          {
             this.oDiv.childNodes[i].className="";
          }
       }
    }
 };
 
 autoComplete.prototype.moveUp=function()
 {
    if(this.oDiv.childNodes.length>0 && this.cur>0)
    {
       --this.cur;
       for(var i=0;i<this.oDiv.childNodes.length;i++)
       {
          if(i==this.cur)
          {
             this.oDiv.childNodes[i].className="over";
             this.oText.value=this.oDiv.childNodes[i].innerHTML;
          }
          else
          {
             this.oDiv.childNodes[i].className="";
          }
       }
    }
 };
 
 autoComplete.prototype.keyUp=function(oEvent)
 {
    oEvent=oEvent || window.event;
    var iKeyCode=oEvent.keyCode;
    if(iKeyCode==8 || iKeyCode==46)
    {
       this.autoComplete.onTextChange(false); /* without autocomplete */
    }
 else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123))
    {
       //ignore
    }
    else
    {
       this.autoComplete.onTextChange(true); /* with autocomplete */
    }
 };
 
 autoComplete.prototype.positionSuggest=function() /* to calculate the appropriate poistion of the dropdown */
 {
    var oNode=this.oText;
    var x=0,y=oNode.offsetHeight;
 
    while(oNode.offsetParent && oNode.offsetParent.tagName.toUpperCase() != 'BODY')
    {
       x+=oNode.offsetLeft;
       y+=oNode.offsetTop;
       oNode=oNode.offsetParent;
    }
 
    x+=oNode.offsetLeft;
    y+=oNode.offsetTop;
 
    this.oDiv.style.top=y+"px";
    this.oDiv.style.left=x+"px";
 }
 
 autoComplete.prototype.onTextChange=function(bTextComplete)
 {
    var txt=this.oText.value;
    var oThis=this;
    this.cur=-1;
    
    if(txt.length>0)
    {
       while(this.oDiv.hasChildNodes())
          this.oDiv.removeChild(this.oDiv.firstChild);
       
       var aStr=new Array();
       this.db.getMatches(txt,aStr,this.maxSize);
       if(!aStr.length) {this.hideSuggest ;return}
       if(bTextComplete) this.textComplete(aStr[0]);
       this.positionSuggest();

       for(i in aStr)
       {
          var oNew=document.createElement('div');
          this.oDiv.appendChild(oNew);
          oNew.onmouseover=
          oNew.onmouseout=
          oNew.onmousedown=function(oEvent)
          {
             oEvent=window.event || oEvent;
             oSrcDiv=oEvent.target || oEvent.srcElement;
 
             if(oEvent.type=="mousedown")
             {
                oThis.oText.value=this.innerHTML;
             }
             else if(oEvent.type=="mouseover")
             {
                this.className="over";
             }
             else if(oEvent.type=="mouseout")
             {
                this.className="";
             }
             else
             {
                this.oText.focus();
             }
          };
          oNew.innerHTML=aStr[i];
       }
       
       this.oDiv.style.visibility="visible";
    }
    else
    {
       this.oDiv.innerHTML="";
       this.oDiv.style.visibility="hidden";
    }
 };
 
 function createAutoComplete()
 {
 var aNames =
    [
"Aberdeen, WA", 
"Admiral - Seattle Neighborhood", 
"Airway Heights, WA", 
"Alaska Junction - Seattle Neighborhood", 
"Albion, WA", 
"Algona, WA", 
"Alki - Seattle Neighborhood", 
"Almira, WA", 
"Anacortes, WA", 
"Arbor Heights - Seattle Neighborhood", 
"Arlington, WA", 
"Asotin, WA", 
"Auburn, WA", 
"Bainbridge Island, WA", 
"Ballard - Seattle Neighborhood", 
"Battle Ground, WA", 
"Beacon Hill - Seattle Neighborhood", 
"Beaux Arts Village, WA", 
"Belfair, WA", 
"Bellevue, WA", 
"Bellingham, WA", 
"Belltown - Seattle Neighborhood", 
"Belvidere - Seattle Neighborhood", 
"Benton City, WA", 
"Bingen, WA", 
"Black Diamond, WA", 
"Blaine, WA", 
"Blue Ridge - Seattle Neighborhood", 
"Bonney Lake, WA", 
"Bothell, WA", 
"Boulevard Park - Seattle Neighborhood", 
"Bow, WA", 
"Bremerton, WA", 
"Brewster, WA", 
"Bridgeport, WA", 
"Brier, WA", 
"Brighton - Seattle Neighborhood", 
"Broadmoor - Seattle Neighborhood", 
"Broadview - Seattle Neighborhood", 
"Bryant - Seattle Neighborhood", 
"Bryn Mawr - Seattle Neighborhood", 
"Buckley, WA", 
"Bucoda, WA", 
"Burien, WA", 
"Burlington, WA", 
"Camano Island, WA", 
"Camas, WA", 
"Capitol Hill - Seattle Neighborhood", 
"Carbonado, WA", 
"Carnation, WA", 
"Cashmere, WA", 
"Castle Rock, WA", 
"Cathlamet, WA", 
"Cedar Park - Seattle Neighborhood", 
"Centralia, WA", 
"Chehalis, WA", 
"Chelan, WA", 
"Cheney, WA", 
"Chewelah, WA", 
"Chinook, WA", 
"Clarkston, WA", 
"Cle Elum, WA", 
"Clipper, WA", 
"Clyde Hill, WA", 
"Colfax, WA", 
"College Place, WA", 
"Colton, WA", 
"Columbia City - Seattle Neighborhood", 
"Colville, WA", 
"Conconully, WA", 
"Concrete, WA", 
"Connell, WA", 
"Cosmopolis, WA", 
"Coulee City, WA", 
"Coulee Dam, WA", 
"Coupeville, WA", 
"Covington, WA", 
"Creston, WA", 
"Crown Hill - Seattle Neighborhood", 
"Cusick, WA", 
"Custer, WA", 
"Darrington, WA", 
"Davenport, WA", 
"Dayton, WA", 
"Deer Park, WA", 
"Delridge - Seattle Neighborhood", 
"Deming, WA", 
"Des Moines, WA", 
"Dodge, WA", 
"Downtown Condos - Seattle Neighborhood", 
"DuPont, WA", 
"Duvall, WA", 
"East Wenatchee, WA", 
"Eastern Washington", 
"Eastlake - Seattle Neighborhood", 
"Eastsound, WA", 
"Eatonville, WA", 
"Edgewood, WA", 
"Edmonds, WA", 
"Ellensburg, WA", 
"Elma, WA", 
"Endicott, WA", 
"Entiat, WA", 
"Enumclaw, WA", 
"Ephrata, WA", 
"Everett, WA", 
"Everson, WA", 
"Fairfield, WA", 
"Fall City, WA", 
"Farmington, WA", 
"Fauntleroy - Seattle Neighborhood", 
"Federal Way, WA", 
"Ferndale, WA", 
"Fife, WA", 
"Fircrest, WA", 
"First Hill - Seattle Neighborhood", 
"Forks", 
"Freeman, WA", 
"Fremont - Seattle Neighborhood", 
"Friday Harbor, WA", 
"Garfield, WA", 
"George, WA", 
"Georgetown - Seattle Neighborhood", 
"Gig Harbor, WA", 
"Glacier, WA", 
"Gold Bar, WA", 
"Goldendale, WA", 
"Graham, WA", 
"Grand Coulee, WA", 
"Grandview, WA", 
"Granger, WA", 
"Granite Falls, WA", 
"Green Lake - Seattle Neighborhood", 
"Greenwood - Seattle Neighborhood", 
"Hamilton, WA", 
"Harrah, WA", 
"Harrington, WA", 
"Hartline, WA", 
"Hatton, WA", 
"Hawthorne Hills - Seattle Neighborhood", 
"Hazel Dell", 
"Highland Park - Seattle Neighborhood", 
"Highpoint - Seattle Neighborhood", 
"Holly, WA", 
"Hoquiam, WA", 
"Hunts Point, WA", 
"Illahee, WA", 
"Ilwaco, WA", 
"Index, WA", 
"Interbay - Seattle Neighborhood", 
"Ione, WA", 
"Issaquah, WA", 
"Judkins - Seattle Neighborhood", 
"Kahlotus, WA", 
"Kalama, WA", 
"Kapowsin, WA", 
"Keller, WA", 
"Kelso, WA", 
"Kenmore, WA", 
"Kennewick, WA", 
"Kent, WA", 
"Kettle Falls, WA", 
"Keyport, WA", 
"Kingston, WA", 
"Kirkland", 
"Kittitas, WA", 
"Klickitat, WA", 
"Krupp, WA", 
"La Center, WA", 
"La Conner, WA", 
"La Crosse, WA", 
"La Push, WA", 
"Lacey, WA", 
"Lake City - Seattle Neighborhood", 
"Lake Forest Park, WA", 
"Lake Stevens, WA", 
"Lakewood, WA", 
"Lamont, WA", 
"Langley, WA", 
"Latah, WA", 
"Laurelhurst - Seattle Neighborhood", 
"Leavenworth, WA", 
"Leschi - Seattle Neighborhood", 
"Liberty, WA", 
"Liberty Lake, WA", 
"Lind, WA", 
"Long Beach, WA", 
"Longview, WA", 
"Loyal Heights - Seattle Neighborhood", 
"Lyman, WA", 
"Lynden, WA", 
"Lynnwood, WA", 
"Mabton, WA", 
"Madison Park - Seattle Neighborhood", 
"Madrona - Seattle Neighborhood", 
"Magnolia - Seattle Neighborhood", 
"Malden, WA", 
"Mansfield, WA", 
"Maple Falls, WA", 
"Maple Leaf - Seattle Neighborhood", 
"Maple Valley, WA", 
"Marcus, WA", 
"Martha Lake - Seattle Neighborhood", 
"Marysville, WA", 
"Mattawa, WA", 
"Matthews Beach - Seattle Neighborhood", 
"McCleary, WA", 
"Medical Lake, WA", 
"Medina, WA", 
"Menlo, WA", 
"Mercer Island, WA", 
"Mesa, WA", 
"Metaline, WA", 
"Metaline Falls, WA", 
"Mill Creek, WA", 
"Millwood, WA", 
"Milton, WA", 
"Monroe, WA", 
"Montesano, WA", 
"Montlake - Seattle Neighborhood", 
"Morton, WA", 
"Moses Lake, WA", 
"Mossyrock, WA", 
"Mount Vernon, WA", 
"Mountlake Terrace, WA", 
"Moxee, WA", 
"Mount Baker - Seattle Neighborhood", 
"Mukilteo, WA", 
"Naches, WA", 
"Napavine, WA", 
"Naselle, WA", 
"Nespelem, WA", 
"Newcastle, WA", 
"Newport, WA", 
"Nooksack, WA", 
"Normandy Park, WA", 
"North Bend, WA", 
"North Bonneville, WA", 
"Northgate - Seattle Neighborhood", 
"Northport, WA", 
"Oak Harbor, WA", 
"Oakesdale, WA", 
"Oakville, WA", 
"Ocean Park, WA", 
"Ocean Shores, WA", 
"Odessa, WA", 
"Okanogan, WA", 
"Olympia, WA", 
"Omak, WA", 
"Onalaska, WA", 
"Oroville, WA", 
"Orting, WA", 
"Othello, WA", 
"Oysterville, WA", 
"Pacific, WA", 
"Palouse, WA", 
"Parkland, WA", 
"Pasco, WA", 
"Pateros, WA", 
"Pe Ell, WA", 
"Phinney Ridge - Seattle Neighborhood", 
"Pidgeon Point - Seattle Neighborhood", 
"Pomeroy, WA", 
"Port Angeles, WA", 
"Port Orchard, WA", 
"Port Townsend, WA", 
"Poulsbo, WA", 
"Prescott, WA", 
"Preston, WA", 
"Prosser, WA", 
"Pullman, WA", 
"Puyallup, WA", 
"Queen Anne - Seattle Neighborhood", 
"Quincy, WA", 
"Rainier, WA", 
"Rainier Beach - Seattle Neighborhood", 
"Ravenna - Seattle Neighborhood", 
"Raymond, WA", 
"Reardan, WA", 
"Redmond, WA", 
"Renton, WA", 
"Republic, WA", 
"Richland, WA", 
"Richmond Beach - Seattle Neighborhood", 
"Ridgefield, WA", 
"Ritzville, WA", 
"Riverside, WA", 
"Riverton - Seattle Neighborhood", 
"Rock Island, WA", 
"Rockford, WA", 
"Rosalia, WA", 
"Roslyn, WA", 
"Roy, WA", 
"Royal City, WA", 
"Ruston, WA", 
"Sammamish, WA", 
"Seahurst - Seattle Neighborhood", 
"SeaTac, WA", 
"Seattle, WA", 
"Seaview - Seattle Neighborhood", 
"Sedro-Woolley, WA", 
"Selah, WA", 
"Sequim, WA", 
"Seward Park - Seattle Neighborhood", 
"Shelton, WA", 
"Sheridan Beach - Seattle Neighborhood", 
"Shoreline, WA", 
"Shorewood - Seattle Neighborhood", 
"Sifton, WA", 
"Silverdale, WA", 
"Skykomish, WA", 
"Skyway - Seattle Neighborhood", 
"Smokey Point, WA", 
"Snohomish, WA", 
"Snoqualmie, WA", 
"Soap Lake, WA", 
"South Bend, WA", 
"South Cle Elum, WA", 
"South Hill, WA", 
"South Lake Union - Seattle Neighborhood", 
"South Prairie, WA", 
"Spanaway, WA", 
"Spangle, WA", 
"Spokane, WA", 
"Spokane Valley, WA", 
"Sprague, WA", 
"Springdale, WA", 
"St. John, WA", 
"Stanwood, WA", 
"Starbuck, WA", 
"Stehekin, WA", 
"Steilacoom, WA", 
"Stevenson, WA", 
"Sultan, WA", 
"Sumas, WA", 
"Sumner, WA", 
"Sunnyside, WA", 
"Sunset Hill - Seattle Neighborhood", 
"Tacoma, WA", 
"Tekoa, WA", 
"Tenino, WA", 
"The Highlands - Seattle Neighborhood", 
"Tieton, WA", 
"Toledo, WA", 
"Tonasket, WA", 
"Toppenish, WA", 
"Tukwila, WA", 
"Tumwater, WA", 
"Twisp, WA", 
"Union Gap, WA", 
"Uniontown, WA", 
"University District - Seattle Neighborhood", 
"University Place, WA", 
"Uptown (Lower QA) - Seattle Neighborhood", 
"Vader, WA", 
"Vancouver, WA", 
"Vashon, WA", 
"View Ridge - Seattle Neighborhood", 
"Waitsburg, WA", 
"Walla Walla, WA", 
"Wallingford - Seattle Neighborhood", 
"Wapato, WA", 
"Warden, WA", 
"Washington Park - Seattle Neighborhood", 
"Washougal, WA", 
"Washtucna, WA", 
"Waterville, WA", 
"Waverly, WA", 
"Wedgewood - Seattle Neighborhood", 
"Wellpinit, WA", 
"Wenatchee, WA", 
"West Richland, WA", 
"Western Washington", 
"Westport, WA", 
"White Center - Seattle Neighborhood", 
"White Salmon, WA", 
"Wilbur, WA", 
"Wilkeson, WA", 
"Wilson Creek, WA", 
"Windermere - Seattle Neighborhood", 
"Winlock, WA", 
"Winthrop, WA", 
"Woodinville, WA", 
"Woodland", 
"Woodway, WA", 
"Yacolt, WA", 
"Yakima, WA", 
"Yarrow Point, WA", 
"Yelm, WA", 
"Zillah, WA"
];
 
	new autoComplete(aNames,document.getElementById('txt'),document.getElementById('suggest'),50);
	new autoComplete(aNames,document.getElementById('txt_second'),document.getElementById('suggest_second'),50);
 }

