﻿  <!---AJAX LOOKUP --->
  function showElem(id)
{
	document.getElementById(id).style.display='inline';
}
function hideElem(id)
{
		
		document.getElementById(id).style.display='none';
		document.getElementById(id).style.position='absolute';
		
}
var xmlHttp
function findPos(obj)
 {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function fillForm(obj)
{
	var tForm = obj.parentNode;
	document.getElementById('submitButton').click();
	//alert(tForm.outerHTML);
}
//Function to trim the space in the left side of the string
function ltrim ( s ){
//return s.replace( /^s*/, “” );
s = s.replace(/^s*/, "");
return s;
}
//Function to trim the space in the right side of the string
function rtrim ( s ){
s = s.replace( /s*$/, "" );
return s;
}
function trim(s)
{
	return ltrim(rtrim(s));
}

function showHint(e)
{


	var str =e.value;
	var suggest;
	var e = document.getElementById("srchKey");
	//alert(e.outerHTML);
	suggest = document.getElementById("srchKeySuggest");
	

	var coord;
	coord=findPos(e);

	posx = coord[0];
	posy=coord[1];

	if(e.value=="")
	{
		document.getElementById(suggest.id).style.display='none';
	}
	else
	{
		
	}
	suggest.style.left=posx;
	suggest.style.top=posy+17;
	if (str.length==0)
  	{ 
 		 hideElem(suggest.id);
  		return;
 	 }
	 //xmlHttp=null;
	

	xmlHttp=GetXmlHttpObject();

	if(!xmlHttp && typeof XMLHttpRequest!= 'undefined'){ 
xmlHttp = new XMLHttpRequest(); 
} 


	


	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url=e.getAttribute("suggestPage");
//alert(url);
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	
	xmlHttp.onreadystatechange=function()
	{ 
		//document.getElementById(suggest.id).style.display='block';
		if ( (xmlHttp.readyState==4) )
		{ 
			var t = trim(xmlHttp.responseText.toString());
			//alert(t);
			if(t=='')
			{
				//alert('oh oh');
				document.getElementById(suggest.id).style.display='none'; 
			}
			else
			{
				//document.getElementById(suggest.id).style.display='block';
				//alert('here we are');
				document.getElementById(suggest.id).style.display="inline";
				//Effect.SlideDown(suggest.id);
				var tReg =new RegExp(str,"img");

				var newHTML = xmlHttp.responseText.replace(tReg,"" + str + "");
				newHTML = xmlHttp.responseText;
					//alert(newHTML);
				//document.getElementById(suggest.id).innerHTML=xmlHttp.responseText;
				document.getElementById(suggest.id).innerHTML=newHTML;
			}
			

			
		{	
		//document.getElementById(suggest.id).style.display='none';
		e.setAttribute("suggestID",'');
	}
}

}
//xmlHttp.onreadystatechange=function stateChanged(suggest.id);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
} 

var x =null;

var selectedItem = null;
function hover(obj)
{
	obj.style.backgroundColor='#99ccff';
	obj.style.cursor='hand';
	selectItem(obj);


}
function hoverOut(obj)
{
	obj.style.backgroundColor='transparent';
	obj.style.cursor='hand';
	unSelectItem(obj);
	//showElem(obj.parentNode.id);
}

function stripHTML(oldString) {

// alert('oldString: ' + oldString + ' New String: ' + oldString.replace(/<\/?[^>]+(>|$)/g, ""));
 return oldString.replace(/<\/?[^>]+(>|$)/g, "");
  
}

function handleClick(obj,id)
{
	
	var name =  document.getElementById(id);
	var toSearch =obj.getAttribute('suggest');
	toSearch = stripHTML(toSearch);
	//alert(toSearch);
	
	name.setAttribute('value',toSearch );
	name.setAttribute('text',toSearch );
	name.setAttribute('suggestID',obj.getAttribute('suggestID'));
	hideElem(obj.id);
	handleChange(name);

}
function handleLR(obj,id)
{
	
	var name =  document.getElementById(id);
	name.setAttribute('value',obj.getAttribute('suggest'));
	name.setAttribute('text',obj.getAttribute('suggest'));
	name.setAttribute('suggestID',obj.getAttribute('suggestID'));
	hideElem(obj.id);
	//handleChange(name);

}
function handleOver(obj)
{
if (x) 
 {
  clearTimeout(x);
  x = null;
}
 
	showElem(obj.id);
}
function handleHoverOut(obj)
{
	x =  setTimeout("hideElem('" + obj.id + "');selectedItem=null;",900);	

}
function processKeyDown(k,suggestBoxID,me) {

	if (window.event) {
    	key=event.keyCode;
    } else {
    	key = k.which;
    }
	//alert(key);
	if( (key==37) || (key==39) )
	{
		var obj = document.getElementById(suggestBoxID)
		if(selectedItem!=null)
		{
			
			var toSearch =obj.getAttribute('suggest');
			toSearch = stripHTML(toSearch);
			me.value=toSearch;
	
			
			
			me.setAttribute('suggestID',obj.getAttribute('suggestID'));
			selectedItem=null;
			hideElem(suggestBoxID);
		}
		
	}
	if(key==40) 
	{
		
		var obj = document.getElementById(suggestBoxID);
		//alert(obj.outerHTML);
		if(selectedItem==null)
		{selectFirstItem(obj);}
		else{
		selectNextItem(obj);}
	}
	if(key==38)
	{
		var obj = document.getElementById(suggestBoxID);
		if(selectedItem==null)
		{selectLastItem(obj);}
		else{
		selectPrevItem(obj);}
	}
	if( (key==9) || (key==13) )
	{
		
		var obj = document.getElementById(suggestBoxID)
		if(selectedItem!=null)
		{
			
			var toSearch =obj.getAttribute('suggest');
			toSearch = stripHTML(toSearch);
			me.value=toSearch;
			me.setAttribute('suggestID',obj.getAttribute('suggestID'));
			selectedItem=null;
			hideElem(suggestBoxID);
		}
		window.event.keyCode=9;
		document.getElementById('submitButton').click();
	}
	
}
function noenter() {
  return !(window.event && window.event.keyCode == 13); }

function selectNextItem(suggestBox)
{

	var test = suggestBox.childNodes[0].firstChild.childNodes[selectedItem];
	unSelectItem(test);
	if(selectedItem==suggestBox.childNodes[0].firstChild.childNodes.length-1)
	{
		
		selectFirstItem(suggestBox);
	}
	else
	{
	selectItem(suggestBox.childNodes[0].firstChild.childNodes[selectedItem+1]);
	//selectedItem = selectedItem +1;
	}
	
}
function selectPrevItem(suggestBox)
{
	var test = suggestBox.childNodes[0].firstChild.childNodes[selectedItem-1];

	unSelectItem(test);
	if(selectedItem==1)
	{
		
		selectLastItem(suggestBox)
	}
	else
	{
	 
	
	selectItem(test);
	//selectedItem = selectedItem -1;
	}
	
}
function selectFirstItem(suggestBox)
{	
	
	var test = suggestBox.childNodes[0].firstChild.firstChild.nextSibling;
	//alert(test.outerHTML);
	selectItem(test);
	selectedItem = 1;
	
}
function selectLastItem(suggestBox)
{
	var lastItem = suggestBox.childNodes[suggestBox.childNodes.length-1].lastChild.lastChild;
	//alert(lastItem.outerHTML);
	selectItem(lastItem);
	//selectedItem = suggestBox.childNodes.length-1;
}	
function selectItem(obj)
{
	var value = obj.firstChild.firstChild.getAttribute('value');
	//var obj = obj.parentNode.parentNode;
	obj.style.backgroundColor="#ffffcc";
	//obj.style.color="white";
	
	obj.parentNode.parentNode.parentNode.setAttribute('suggest',value);
	obj.parentNode.parentNode.parentNode.setAttribute('suggestID',obj.id);
	
	selectedItemx=0;
	while(obj.parentNode.childNodes[selectedItemx]!=obj)
	{unSelectItem(obj.parentNode.childNodes[selectedItemx]);++selectedItemx;}
	selectedItem=selectedItemx;
	selectedItemx=obj.parentNode.childNodes.length-1;
	while(obj.parentNode.childNodes[selectedItemx]!=obj)
	{unSelectItem(obj.parentNode.childNodes[selectedItemx]);--selectedItemx;}
	selectedItem=selectedItemx;
	
	obj.parentNode.parentNode.parentNode.setAttribute('suggest',value);
	obj.parentNode.parentNode.parentNode.setAttribute('suggestID',obj.id);
}
function unSelectItem(obj)
{
	obj.style.backgroundColor='transparent';
}
function processKeyUp(k,suggestBoxID)
{
	
	if (window.event) {
    	key=event.keyCode;
    } else {
    	key = k.which;
    }
	
	if( (key==40) || (key==38) || (key==9))
	{
		
	}
	else
	{
		
	showHint(document.getElementById(suggestBoxID));
	}
}


function handleChange(obj)
{
	if(obj.id=='srchKey')
	{
		fillForm(obj);
	
	}
	else
	{
	var theID=(obj.getAttribute('suggestID'));
	//obj.value = obj.value + theID;
	if(theID==null)
	{
		theID='';
	}
	//document.CustomerSearch.CustNo.value = theID;
	
	if(theID!=null){
	//fillForm(theID);
	
	}
	}
	
	
}

  
 
