
	var _req;
	var _div;
	var _keyCode;
	var _highlightedSuggestionIndex = -1;
	var _suggestionCount = 0;
	var _highlightedDiv = null;
	var _txtFld = null;
	var _originalVal;
	var _PageAppelante='';
	var _NbCarMin=1;
	
//			document.onkeydown= keydownHandler;

	document.onmousedown = function(){
		HideDiv("autocomplete");
		//event.cancelBubble=true;
	}
	
	String.prototype.trim=function(){
		var
			r=/^\s+|\s+$/,
			a=this.split(/\n/g),
			i=a.length;
		while(i-->0)
			a[i]=a[i].replace(r,'');
		return a.join('\n');
	}
	

function Init() {
  var xmlhttp;
  /*NE PAS enlever les commentaires ci-dessous, il sont nécessaires ŕ IE*/
  
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      _req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      try {
        _req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
        _req = false;
        }
      }
  @else
  _req = false;
  @end @*/
  
  if (!_req && typeof XMLHttpRequest != 'undefined') {
    try 
		{_req = new XMLHttpRequest();} 
    catch (e) 
      {_req = false;}
    }
  }
  
  /*
	function Init()	{
		if (window.XMLHttpRequest) { // Non-IE browsers
			_req = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject){ // IE
			_req = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} 
*/
	SendQuery=function(key,CheminAjax,PageAppelante,e)
	{
		Init(); 
	
		_PageAppelante=PageAppelante;
		
		
		if (e.which){_keyCode=e.which;}
		else {_keyCode=event.keyCode;} 
		
		_txtFld = document.getElementById('Lieu');
		//alert("key=" + key + "   _keyCode=" + _keyCode + "   _txtFld=" + _txtFld  );		
				
		
		
		var url=CheminAjax + "lieu.aspx?l="+key;
		//url = escapeURI(url);
		//alert (url);
		if (_keyCode==38 || _keyCode==40) //// 38 is up cursor key, 40 is down cursor key...
		{
			upDownCursorHandler();
		}
		else if (_keyCode == 13)
		{
			_originalVal = key;
			HideDiv("autocomplete");
			//move cursor to last.
			setRange(_txtFld,_txtFld.value.length,_txtFld.value.length);
		}
		else if(_req!=null)
		{
			createDiv(_txtFld);
			//alert('lancement requete');
			if (key.length>_NbCarMin)
			{
				_originalVal = key;
				_highlightedSuggestionIndex = -1;
				_req.onreadystatechange = processStateChange;
				_req.open("GET", url, true);
				_req.send(null);
			}
		}
	}
	
	function escapeURI(uri)
	{
		if (encodeURIComponent)
			return encodeURIComponent(uri);
		if (escape)
			return escape(uri);
	}

	processStateChange = function(){
		if (_req.readyState == 4){// only if "OK"
			if (_req.status == 200)	{
				if(_req.responseText=="")
					HideDiv("autocomplete");
				else{
					eval(_req.responseText);
				}
			}
			else{
				document.getElementById("autocomplete").innerHTML=
					"errors occured:<br>"+_req.statusText;
			}
		}
	}

	ShowDiv = function(divid){
		if (document.layers) 
		{
		document.layers[divid].visibility="show";
		document.layers['DdlLieu'].visibility="hide";
		document.layers['DebutSejour'].visibility="hide";
		document.layers['DureeSejour'].visibility="hide";
		document.layers['TypeLogmnt'].visibility="hide";
		document.layers['NbPers'].visibility="hide";
		if (_PageAppelante=='RechAv')
			{
			document.layers['PrixMax'].visibility="hide";
			document.layers['nbrPieces'].visibility="hide";
			}
		}
		else 
		{
		document.getElementById(divid).style.visibility="visible";
		document.getElementById('DdlLieu').style.visibility="hidden";
		document.getElementById('DebutSejour').style.visibility="hidden";
		document.getElementById('DureeSejour').style.visibility="hidden";
		document.getElementById('TypeLogmnt').style.visibility="hidden";
		document.getElementById('NbPers').style.visibility="hidden";
		if (_PageAppelante=='RechAv')
			{
			document.getElementById('PrixMax').style.visibility="hidden";
			document.getElementById('nbrPieces').style.visibility="hidden";
			}
		}
	}
	HideDiv = function(divid){
		try{
			if (document.layers) 
			{
			document.layers[divid].visibility="hide";
			document.layers['DdlLieu'].visibility="show";
			document.layers['DebutSejour'].visibility="show";
			document.layers['DureeSejour'].visibility="show";
			document.layers['TypeLogmnt'].visibility="show";
			document.layers['NbPers'].visibility="show";
			if (_PageAppelante=='RechAv')
				{
				document.layers['PrixMax'].visibility="show";
				document.layers['nbrPieces'].visibility="show";
				}
			}
			else 
			{
			document.getElementById(divid).style.visibility="hidden";
			document.getElementById('DdlLieu').style.visibility="visible";
			document.getElementById('DebutSejour').style.visibility="visible";
			document.getElementById('DureeSejour').style.visibility="visible";
			document.getElementById('TypeLogmnt').style.visibility="visible";
			document.getElementById('NbPers').style.visibility="visible";
			if (_PageAppelante=='RechAv')
				{
				document.getElementById('PrixMax').style.visibility="visible";
				document.getElementById('nbrPieces').style.visibility="visible";
				}
			}
		}
		catch(e){}
	}

	window.onload = function(){
		//on cache le div quand on charge la page
		if (document.getElementById("autocomplete"))
			HideDiv("autocomplete");
		//document.forms[0].TextBox1.focus();
	}
	
	function createDiv(txtFld)
	{	//alert('creation du div...');
		if (document.getElementById("autocomplete"))
			HideDiv("autocomplete");
		_div = document.createElement("DIV");
		_div.id = "autocomplete";
		_div.style.borderRight = "black 1px solid";
		_div.style.borderLeft = "black 1px solid";
		_div.style.borderTop = "black 1px solid";
		_div.style.borderBottom = "black 1px solid";
		_div.style.zIndex = "100";
		_div.style.backgroundColor = "white";
		_div.style.position="absolute";
		_div.style.visibility="hidden";
		_div.style.paddingLeft = "3";
		
		setDivSizePosition(txtFld);
		document.body.appendChild(_div);
		return false;
	}

	function setDivSizePosition(txtFld)
	{
		if(_div)
		{
			//alert('positionner le div...');
			_div.style.left=calculateOffsetLeft(txtFld)+"px";
			_div.style.top=calculateOffsetTop(txtFld)+txtFld.offsetHeight-1+"px";
			_div.style.width=calculateWidth(txtFld)+"px"
		}
	}

	function calculateWidth(txtFld)
	{
		if(navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1)
		{
			return txtFld.offsetWidth-2
		}
		else
		{
			return txtFld.offsetWidth
		}
	}

	calculateOffsetLeft=function(r){
		return calc(r,"offsetLeft")
	}

	calculateOffsetTop = function(r){
		return calc(r,"offsetTop")
	}
	calc = function(r,attr)
	{
		var kb=0;
		while(r)
		{
			kb+=r[attr]; 
			r=r.offsetParent
		}
		return kb
	}
	omover = function(){
		this.style.backgroundColor="#55BBEE";
		this.style.color="red";
		this.style.cursor="pointer";
	}
	omout = function(){
		this.style.color="black";
		this.style.backgroundColor="white";
	}
	callback1 = function(a,b,c){
		_div = document.getElementById("autocomplete");
		while (_div.childNodes.length >0){
			_div.removeChild(_div.childNodes[0]);
		}
		_suggestionCount = a.length;
		if (_suggestionCount == 0) HideDiv("autocomplete");
		else{
			//alert('creation des _div');
			for (var i=0; i<a.length; ++i){
				
				var div = document.createElement("DIV");
				var sp =  document.createElement("SPAN");
				sp.style.height = "auto";
				sp.style.display="block";
				sp.style.paddingLeft="2";
				sp.style.paddingRight="2";
				sp.style.width = "100%";
				
				var sp1 = document.createElement("SPAN");
				sp1.style.height = "auto";
				sp1.style.fontSize = "11px";
				sp1.style.cssFloat = "left";
				sp1.style.fontFamily = "arial,sans-serif";
				var nomVille = a[i]
				if (_originalVal.indexOf(" ")>0)
					{
					_originalVal=_originalVal.substring(0,_originalVal.indexOf(" ")+1) + _originalVal.substring(_originalVal.indexOf(" ")+1,_originalVal.indexOf(" ")+2).toUpperCase() + _originalVal.substring(_originalVal.indexOf(" ")+2);
					}
				sp1.innerHTML = a[i].replace(_originalVal.substring(0,1).toUpperCase() + _originalVal.substring(1) ,"<b>" + _originalVal.substring(0,1).toUpperCase() + _originalVal.substring(1) + "</b>").replace(_originalVal,"<b>" + _originalVal + "</b>") ;
				sp1.style.width = "58%";
				sp1.style.wordWrap="break-word";
				sp1.className = "Suggest";

				var sp2 = document.createElement("SPAN");
				sp2.style.height = "auto";
				sp2.style.fontSize = "11px";
				sp2.style.fontFamily = "arial,sans-serif";
				sp2.style.width = "41%";
				//sp2.style.textAlign="right";
				sp2.style.valign="center";
				sp2.style.color="#104A5D";
				sp2.style.cssFloat = "right";
				sp2.innerHTML = '&nbsp;&nbsp;&nbsp;(' + b[i] + ')' + '&nbsp;&nbsp;&nbsp;' + c[i];
				sp2.style.wordWrap="break-word";
				sp2.className = "Suggest";
							
				sp.appendChild(sp1);
				sp.appendChild(sp2);
				
				div.style.width = "100%";
				div.style.height = "auto";
				div.id = "_div" + i.toString();
				div.onmouseover=omover;
				div.onmouseout=omout;
				div.onmousedown=omdown;
				div.appendChild(sp);
				_div.appendChild(div);
			}
			ShowDiv("autocomplete");
		}
	}
	getSpanValue = function(i,className){
		//alert('avant affectation');
		var sp=i.getElementsByTagName("SPAN"); //plantage ici
		//alert('sp.length=' + sp.length);
		if(sp){
			//alert('sp existe');
			for(var f=0; f<sp.length; ++f){
				if(sp[f].className==className){
					/*modif FG replace les B*/
					//alert('avant premier replace');
					var value=sp[f].innerHTML.replace("<B>","").replace("</B>","").replace("<b>","").replace("</b>","");
					//alert('avant deuxieme replace value=' + sp[f].innerHTML);
					value=value.replace("<B>","").replace("</B>","").replace("<b>","").replace("</b>","");
					if(value=="&nbsp;") 
						{
						return "";
						} 
					else
						{
						//alert('avant stripCR');
						var s=stripCR(value);
						return s;
						}
				}
			}
		}
		else
			{
			return "" 
			}
	}
	function stripCR(va){
		for(var i=0,s="",zb="\n\r"; i<va.length; i++) {
			if (zb.indexOf(va.charAt(i))==-1) {
			s+=va.charAt(i);
			} else {
			s+=" ";
			}
		}
		return s
	}
	var omdown=function(){
		v=getSpanValue(this,"Suggest");
		//document.forms[0].TextBox1.value = v.trim();
		v=decode(v);
		_txtFld.value = v.trim();
		HideDiv("autocomplete");
	}
	
	function upDownCursorHandler(){
		// 38 is up cursor key, 40 is down cursor key...
		if(_keyCode==40){
			highlit(_highlightedSuggestionIndex+1);
		}else if(_keyCode==38){
			highlit(_highlightedSuggestionIndex-1);
		}
	}
	function highlit(indx){
		if (indx < 0) return;
		if (_suggestionCount <= indx) return;
		_highlightedSuggestionIndex = indx;
		var completeList = document.getElementById("autocomplete");
		
		if (completeList)
		{
			if (_highlightedDiv)  //Set previously highlighted item to un-highlighted
			{
				_highlightedDiv.style.color="black";
				_highlightedDiv.style.backgroundColor="white";
			}
			//alert('avant, completeList=' + completeList);
			
			_highlightedDiv = document.getElementById('_div' + _highlightedSuggestionIndex);//completeList.childNodes[_highlightedSuggestionIndex];
			//alert('_highlightedDiv=' + _highlightedDiv + 'grace au document.getElementById');
			var v = getSpanValue(_highlightedDiv,"Suggest");
						
			//document.forms[0].TextBox1.value = v.trim();
			v=decode(v);
			//alert('apres decode');
			//_oldVal = _txtFld.value;
			_txtFld.value = v.trim();
			setRange(_txtFld,_originalVal.length,_txtFld.value.length);
			_highlightedDiv.style.color="red";
			_highlightedDiv.style.backgroundColor="#55BBEE";
		}
	}
	function decode(s)  {return s.replace("&amp;","&");}
	