  var lock=0;
  var enableCitySelect=false;
  var curr_item=0;
  var item_cnt=0;
        function xmlhttpPost(strURL,qstr) {
            var xmlHttpReq=false;
            var self=this;
            if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest(); }
            else if (window.ActiveXObject) { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); }
            self.xmlHttpReq.onreadystatechange = function() {
                if (self.xmlHttpReq.readyState==4) { parseDst(self.xmlHttpReq.responseText); }
            }
	  if(!(navigator.oscpu && document.getElementsByClassName))
	  {
            self.xmlHttpReq.open('POST',strURL,true);
            self.xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            self.xmlHttpReq.send(qstr);
	  }
	  else
	  {
	    if (self.xmlHttpReq.overrideMimeType) {
	    	self.xmlHttpReq.overrideMimeType('text/plain');
	    }
	    self.xmlHttpReq.open('GET',strURL+'?'+qstr,true);
            self.xmlHttpReq.send(null);
	  }
        }
        function getquerystring() {
            var word = form.word.value;
            qstr='v=3&'+'w='+escape(word);
            return qstr;
        }
  function getDst(city)
  {
    document.getElementById('cityflag').src='img/wait16.gif';
    lock=0;
    xmlhttpPost("gc.php",'v=4&c='+escape(city.value));
  }

  function resetAns(s)
  {
      document.getElementById('cityanswer').innerHTML=s;
      document.getElementById('cityflag').src='img/nocountry.gif';
      document.getElementById('cityselect').style.visibility="hidden";
      enableCitySelect=false;
      lock=0;
      curr_item=0;
      item_cnt=0;
  }
  function parseDst(resp)
  {
    if(resp.substr(0,1)!="0")
    {
      var ls=resp.split("\n");
      if(parseInt(ls[0])==1) {
        setAnswer(ls[1]);
      } else {
        // multiline answer
        // C220016652336Jardim Helena:BRBrazil:0---------:BRL
        // 01234567890123             0123     01234      012
        if(lock==0) {
          resetAns("&nbsp;");
        lock=1;
        var l="";
        for(var i=1;i<ls.length-1;i++)
        {
          var ll=ls[i].split(":");
          if(typeof(ll[0])!="undefined"&&typeof(ll[1])!="undefined")
          {
            l=l+'<div onmouseover="curr_item='+i+'; itemSel();" tabindex="'+i+'" id="csitem'+i+'" onclick="setAnswer('+"'"+ls[i]+"'"+');">';
            l=l+ll[0].substring(13,ll[0].length)+" ["+ll[1].substr(0,2)+"] "+ll[1].substring(2,ll[1].length);
            l=l+"</div>";
          }
        }
        item_cnt=ls.length-1;
        document.getElementById('cityselect').innerHTML=l;
        document.getElementById('cityselect').style.visibility="visible";
        enableCitySelect=true;
        }
      }
    } else { resetAns("?"); }
  }

  function setAnswer(line)
  {
      var nl=line.split(":");
      resetAns(nl[2]);
      document.getElementById('city').value=nl[0].substring(13,nl[0].length);
      document.getElementById('cityflag').src='img/flag-gif/'+nl[1].substr(0,2).toLowerCase()+'.gif';
  }
  function itemSel()
  {
    if(enableCitySelect)
    {
      for(var i=1;i<item_cnt;i++)
      {
        if(i!=curr_item)
        {
          document.getElementById('csitem'+i).style.backgroundColor="#eee";
          document.getElementById('csitem'+i).style.color="black";
        } else {
          document.getElementById('csitem'+i).style.backgroundColor="#8A1109";
          document.getElementById('csitem'+i).style.color="#FFB311";
        }
      }
    }
  }
function fireClickEvent(el) {
var target = document.getElementById(el);
if(target)
{
if(document.dispatchEvent) { // W3C
var oEvent = document.createEvent( "MouseEvents" );
oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
target.dispatchEvent( oEvent );
}
else if(document.fireEvent) { // IE
target.fireEvent("onclick");
}
}
}
  function handleKeyDown(e)
  {
    var ret=true;
    if(!e) e=window.event;
    if(!e) return true;
    var key=(typeof e.keyCode!='undefined'?e.keyCode:e.charCode);
    if(enableCitySelect)
    {
      if(key==38) {
        if(curr_item>1) curr_item-=1;
        itemSel();
      } else if(key==40) {
        if(curr_item<item_cnt-1) curr_item+=1;
        itemSel();
      } else if(key==13) {
        fireClickEvent('csitem'+curr_item);
      }
      ret=false;
    }
    return(ret);
  }
