﻿  
//NOTE: the cityTextBoxID and the vals array should be prepared in advance in the client page

function fillCity(e)
{

//if the cityTextBoxID or vals array is not defined, then this function will refuse to process
if (cityTextBoxID==null || vals==null)
	return;

//only functioning with IE or Mozila, other browser just don't have this feature
if ($(cityTextBoxID).createTextRange=='undefined' && $(cityTextBoxID).setSelectionRange=='undefined')
    return;

var iKeyCode;
if (window.event) 
    iKeyCode = window.event.keyCode;
else if (e) 
    iKeyCode = e.which;

if(iKeyCode)
{
    //call the GO button if key = ENTER
    if (iKeyCode==13)
    {
        $('citysearchbtn').click();
        return false;
    }
    
    //check if special characters entered => not function with them        
    if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) 
	    return;
}

//processing the auto suggestion feature
var i=0;
	for(i=0; i<vals.length; i++)
	{
		var pos=-1;
		var curVal=$(cityTextBoxID).value;
		pos = vals[i].toLowerCase().indexOf(curVal.toLowerCase());
		if(pos==0)
		{
			leftpart=$(cityTextBoxID).value;
			rightpart=vals[i].substring(pos+curVal.length,vals[i].length-pos);
  			if ($(cityTextBoxID).createTextRange) 
			{			 
				$(cityTextBoxID).value=vals[i];	
	        	var oRange = $(cityTextBoxID).createTextRange();
  				 oRange.moveStart("character", curVal.length);
 				 oRange.moveEnd("character", 0);
				 oRange.select();
			}
			else if ($(cityTextBoxID).setSelectionRange) 
			{			    
				$(cityTextBoxID).value=vals[i];	
		       	$(cityTextBoxID).setSelectionRange(leftpart.length,vals[i].length);
			}
			$(cityTextBoxID).focus(); 				
			break;
		}
	}	
}

function $(id)
{
return document.getElementById(id);
}