
var targetID = new Array() ;
var searchID = new Array() ;
var inputID = new Array() ;
var hiddenID = new Array() ;
var autoSugPointer = new Array();
var countSuggestions = new Array();

function keyBoardNav(e, id)
{  

   var keyCode = getKeyCode(e, 'keydown');
   
	if (keyCode == 9)
	{
		if(document.getElementById(targetID[id])) 
			document.getElementById(targetID[id]).style.display = "none";
		if(document.getElementById(searchID[id]))
			document.getElementById(searchID[id]).style.display = "none";
	}   
   else if (keyCode == 40)
   {
	//alert(autoSugPointer[id]);	 
	 
	  var tmpCount = (parseInt(countSuggestions[id])-1);  
      if (autoSugPointer[id] >= 0 && autoSugPointer[id] < tmpCount)
	  { 
	  	 factor=autoSugPointer[id]*25;  
		 if (autoSugPointer[id] != 0 && autoSugPointer[id] != tmpCount)
		 {
		    revertAutoSuggestKeyNav(autoSugPointer[id], id);
		 }
		 autoSugPointer[id] ++;
		 changeAutoSuggestKeyNav(autoSugPointer[id], id);
		 getElemId(searchID[id]).scrollTop = factor;
	  }

   }
   else if (keyCode == 38)
   {
	//alert(autoSugPointer[id]);
	  if (autoSugPointer[id] > 1)
	  {
		 revertAutoSuggestKeyNav(autoSugPointer[id], id);
		 autoSugPointer[id] --;
		 factor=factor-25;
		 changeAutoSuggestKeyNav(autoSugPointer[id], id);
		 getElemId(searchID[id]).scrollTop = factor;

	  } 
   }
   else if (keyCode == 13 && autoSugPointer[id])
   {
	   
	    var divStr=id + "-" + autoSugPointer[id];
		var strId = document.getElementById(divStr).name;
		var str   = document.getElementById(divStr).innerHTML;
		insertKeyword(str, id, strId);
		document.getElementById(targetID[id]).style.display = "none";
		document.getElementById(searchID[id]).style.display = "none";	
   }

}

function changeAutoSuggestKeyNav(id, ID)
{
   getElemId(ID + "-" + id).style.backgroundColor = "#555";
   getElemId(ID + "-" + id).style.color = "#FFF"; 
}

function revertAutoSuggestKeyNav(id, ID)
{
   getElemId(ID + "-" + id).style.backgroundColor = "#F9F9F9";
   getElemId(ID + "-" + id).style.color = "#006"; 
}


function hideSuggestions(id)
{
   try
   {
	   var listWrapID = getElemId(targetID[id]);
	   listWrapID.style.display = "none";	
   } catch(e) {
	   
	}
}

function insertKeyword(str, id, strId)
{

	
	getElemId(searchID[id]).scrollTop=0;
	hideSuggestions(id);
	getElemId(inputID[id]).value = str;
	getElemId(hiddenID[id]).value = strId;
	//displaying postcode description from postcode id 
	try{
		if(getElemId(inputID[id]).id=='postCode') {
			put_post_code(strId);
		}
	} catch(err1) {
	}
	
	//for displaying the activity
	try{
		if(getElemId(inputID[id]).id=='keyword') {
			document.getElementById('contettext').innerHTML=str;
			
			//alert(str);
			
			try{
				enable();
			} catch(err5) {
							
			}
			
		}
	} catch(err2) {
		
	}	
	
	//for displaying the single postcode from city id
	
	try{
		if(getElemId(inputID[id]).id=='city') {
			put_post_code_from_cityId(strId);
		}
	} catch(err3) {
		
	}	
	getElemId(inputID[id]).focus();
	document.getElementById(targetID[id]).style.display = "none";
	document.getElementById(searchID[id]).style.display = "none";
	
	try{
	  showSelects();
		
	} catch(err4) {
		
			
	}
	
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
