function urlencode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    str = (str + '').toString();

    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}


EmergingTips = function()
{

var oDiv=null;
var oElement=null;
var oRegionElement=null;
var Name=null;
var Way=null;
var focused=false;
var focus_on=true;
var changed=false;
var oldValue=null;
var currentSelect=-1;
var gCities = null;
var fakecounter = 0;

    var getWidth = function ()
    {
      if (navigator && navigator.userAgent.toLowerCase().indexOf("msie")==-1)
      {
        return oElement.offsetWidth-1*2;
      }
      else
      {
        return oElement.offsetWidth;
      }
    }

    var getParameter = function (s,na)
    {
      var wb=0;
      while(s)
      {
        wb+=s[na];
        s=s.offsetParent
      }
      return wb;
    }

    var getOffsetLeft = function (s)
    {
      return getParameter(s,"offsetLeft")
    }

    var getOffsetTop = function (s)
    {
      return getParameter(s,"offsetTop")
    }

    var setPosition = function ()
    {
      if(oDiv)
      {
        oDiv.style.left=getOffsetLeft(oElement)+"px";
        oDiv.style.top=getOffsetTop(oElement)+oElement.offsetHeight-1+"px";
        oDiv.style.width=getWidth()+"px";
      }
    }

    var setObj = this.setObj = function(extObj)
    {
      oElement=extObj;
    }

    var setRegionObj = this.setRegionObj = function(extObj)
    {
      oRegionElement=extObj;
    }

    var setName = this.setName = function(extName)
    {
      Name=extName;
    }
	 var setWay = this.setWay = function(extWay)
    {
      Way=extWay;
    }

    var addto = this.addto = function (str)
    {
      oElement.value=str;
    }

    var create = this.create = function()
    {
		//alert('create');
      //oDiv = document.getElementById("completeDiv");
     	
    	/*if ( oDiv.hasChildNodes() )
    	{
    	    while ( oDiv.childNodes.length >= 1 )
    	    {
    	    	oDiv.removeChild( oDiv.firstChild );       
    	    } 
    	}
    	*/
      var a = document.getElementById("completeDiv");
      try
      {
      if (typeof(a) != 'undefined')
      {
    	 // console.log(typeof(a));
    	  document.removeChild(a);
      }
      }
      catch(e)
      {}
      
    	
      oDiv = document.createElement("DIV");
      oDiv.style.borderRight="black 1px solid";
      oDiv.style.borderLeft="black 1px solid";
      oDiv.style.borderTop="black 1px solid";
      oDiv.style.borderBottom="black 1px solid";
      oDiv.setAttribute("id","completeDiv");
      setPosition();
      oDiv.style.zIndex="255";
      oDiv.style.paddingRight="0";
      oDiv.style.paddingLeft="0";
      oDiv.style.paddingTop="0";
      oDiv.style.paddingBottom="0";
      oDiv.style.visibility="visible";
      oDiv.style.position="absolute";
      oDiv.style.backgroundColor="white";
      document.body.appendChild(oDiv);
    }

    var setFocused = this.setFocused = function (v)
    {
      focused=v;
      focus_on=true;
    }

    var loadText = this.loadText = function (str)
    {
		//alert('load text' +str);
		var request             = getRequestObj();
        //request.getData('/_fetchers/getFio.php?str=' + str  + '&rand=' + Math.random(), getResult);
		
		//alert('vibiray svoe chudo='+urlencode(str));
		request.getData(Way + str  + '&rand=' + Math.random(), getResult);
      
    }
	var getResult	= this.getResult = function (obj)
	{
		//alert('loaded text');
		//alert('got_response'+obj.responseText);
		if (obj.responseText.length > 0)
		{
			setText(obj.responseText);
		}
		else
		{
			setText('');
		}
        changed=false;
	}
	
    var setText = this.setText = function (oCities)
    {
       var arr 	= ['red','green','blue']
	   if (oCities.length > 0)
	   {
		//alert('woot' + oCities);
		eval('oCities	='+oCities);
		
		gCities	= oCities;
			
		currentSelect = -1;
	  
			oDiv.innerHTML='';

			for (i = 0, count = oCities.length; i < count; i ++)
			{
				if(oCities[i] != null)
				{
					//alert(oCities[i]);
					oNewElement = document.createElement("div");
					oNewElement.setAttribute("id","tips_"+fakecounter+"_"+i);
					oNewElement.setAttribute("onclick","javascript:"+Name+".addto('"+oCities[i]+"');doSubmit();");
					oNewElement.setAttribute("className","search_tips_nonactive");
					oNewElement.setAttribute("onMouseOver","movecursorAt(this.id)");
				//	oNewElement.setAttribute("onMouseOut","this.className='search_tips_nonactive'");
					
					//oNewElement.setAttribute("style","font:normal 50px Arial;color:black;");
					oNewElement.appendChild(document.createTextNode(oCities[i]));
					oDiv.appendChild(oNewElement);
					//oNewElement = document.createElement("BR");
					//oDiv.appendChild(oNewElement);
				}
				
			}
		}
      //alert(oDiv.innerHTML);
       
       fakecounter++;
    }

    var isChange = this.isChange = function ()
    {
		
      setPosition();
      if (oElement.value!=oldValue) {changed=true; oldValue=oElement.value;}
      else {changed=false;}
      
      //console.log('changed='+changed+ '  '+oElement.value+' != ' +oldValue);

      if(focused){
        if (oElement.value.length > 1)
        {
		
          if((changed)||(focus_on)) {loadText(oElement.value); focus_on=false;}
          oDiv.style.visibility="visible";
        }
        else
          oDiv.style.visibility="hidden";
      }
      else oDiv.style.visibility="hidden";
	  //alert('woot' + Name);
      setTimeout(Name+'.isChange()',800);
    }
    
    
    var movecursor	= this.movecursor = function (direction)
    {
    	currentSelect += direction;
    }
    
    var movecursorAt	= this.movecursorAt = function (position)
    {
    	hideItem();
    	currentSelect =parseInt(position);
    	selectItem();
    }

    
    var hideItem	= this.hideItem = function ()
    {
    	try
    	{
    		document.getElementById('tips_'+(fakecounter-1)+'_'+currentSelect).className = "search_tips_nonactive";
    	}
    	catch(e)
    	{
    //		console.log('err1'+e)
    	}
    	
    }
    
    var getItemByNumber	= this.getItemByNumber = function (number)
    {
    	try
    	{
    		var a = gCities[number];
    		return a;
    	}
    	catch(e)
    	{
    		return null
    	}
    }
    
    var getCurrentItem	= this.getCurrentItem = function ()
    {
    	try
    	{
    		var a = gCities[currentSelect];
    		return a;
    	}
    	catch(e)
    	{
    		return null
    	}
    }
    
    var selectItem	= this.selectItem = function ()
    {
    	try
    	{
    		//console.log('trying to color:tips_'+currentSelect);
    		if (currentSelect >= gCities.length)
    			currentSelect = gCities.length-1;
    		
        	//console.log('tips_'+(fakecounter-1)+'_'+currentSelect);
    		document.getElementById('tips_'+(fakecounter-1)+'_'+currentSelect).className = "search_tips_active";
    	}
    	catch(e)
    	{
    		console.log('tips_'+(fakecounter-1)+'_'+currentSelect +' |err2'+e)
    	}
    }
    
    var getFocusOn	= this.getFocusOn = function()
    {
    	return focus_on;
    }
}

