// version of library
var isMacIE = document.all && !window.print;
var isIE = document.all;
LCB_version = 2.8;
_mac=navigator.userAgent.indexOf('Mac')!=-1;
_ie512=navigator.userAgent.indexOf('MSIE 5.12')!=-1;
_dom = document.all?(document.getElementById?2:1)
                   :(document.getElementById?4:(document.layers?3:0));

function AutoSuggest(elem, suggestions)
{
	var me = this;
	var urlredir = null;
	this.elem = elem;
	this.suggestions = suggestions;
	this.inputText = null;
	this.highlighted = -1;
	this.div = document.getElementById("autosuggest");
	this.eligible=new Array();
	var TAB = 13;
	var ESC = 27;
	var KEYUP = 38;
	var KEYDN = 40;
	elem.setAttribute("autocomplete","off");

	if(!elem.id)
	{
		var id = "autosuggest" + idCounter;
		idCounter++;
		elem.id = id;
	}
	elem.onkeydown = function(ev)
	{
		var key = me.getKeyCode(ev);
		switch(key)
		{
			case TAB:
			return;

			case ESC:
			me.hideDiv();
			break;

			case KEYUP:
			if (me.highlighted > 0)
			{
				me.highlighted--;
			}
			me.changeHighlight(key);
			break;

			case KEYDN:
			if (me.highlighted < (me.eligible.length - 2))
			{
				me.highlighted++;
			}
			me.changeHighlight(key);
			break;
		}
	};
	elem.onkeyup = function(ev) 
	{
		if ( !isIE ){
		    me.FF(this, ev);
		}else{
			me.IE();
		}
		var key = me.getKeyCode(ev);
		switch(key)
		{
		case TAB:
		case ESC:
		case KEYUP:
		case KEYDN:
			return;
		default:
			if (this.value != me.inputText)
			{
				document.getElementById('hidcp').value = me.inputText = this.value;
				if( this.value.length > 2 && this.value.length < 6){
					me.getEligible();
				}else{
					me.hideDiv();
				}
			}
			else
			{
			}
		}
	};
	this.makeandshowDiv= function() {
				me.createDiv();
				me.positionDiv();
				setTimeout(showDivDelayed,50);
	}
	function showDivDelayed(){
		me.showDiv();
	}
	this.IE=function() //~~ pour Internet Explorer ~~
	{
	 var champ = document.getElementById('codePostal');
	 if ( (event.keyCode<96 || event.keyCode>105) && event.keyCode!=13 )
	 {
	  champ.value=champ.value.replace(/[^0-9]/g,"");
	 }
	}
	
	this.FF=function(zone,evt) //~~ pour FireFox ~~
	{
	 if ( (evt.which<96 || evt.which>105) && evt.which!=13 )
	 {
	  zone.value=zone.value.replace(/[^0-9]/g,"");
	 }
	 if(evt.which==undefined){ // pour le déclenchement par script
	  zone.value=zone.value.replace(/[^0-9]/g,"");
	 }
	}
	this.useSuggestion = function()
	{
		if (this.highlighted > -1)
		{
			var choisi=me.eligible[this.highlighted];
			this.elem.value = HtmlDecode(choisi.l);
			this.elem.className="codePostal";
			document.getElementById('hidcp').value=choisi.cp;
			this.hideDiv();
			setTimeout("document.getElementById('" + this.elem.id + "').focus()",0);
		}
	};
	this.getDivWidth = function(){
		if(_dom==4 || _dom==2) return this.div.offsetWidth;
		if(_dom==1)            return this.div.style.pixelWidth;
		if(_dom==3)            return this.div.clip.width;
		return 0;
	}
	this.getDivLeft = function(div){
	  if(_dom==4 || _dom==2) return div.offsetLeft;
	  if(_dom==1)            return div.style.pixelLeft;
	  if(_dom==3)            return div.left;
	  return 0;
	}	
	this.getDivTop = function (div){
	  if(_dom==4 || _dom==2) return div.offsetTop;
	  if(_dom==1)            return div.style.pixelTop;
	  if(_dom==3)            return div.top;
	  return 0;
	}
	this.showDiv = function()
	{
		this.div.style.display = 'block';
	};
	this.hideDiv = function()
	{
		this.div.style.display = 'none';
		this.highlighted = -1;
	};
	this.changeHighlight = function()
	{
		var lis = this.div.getElementsByTagName('LI');
		for (i=0;i<lis.length;i++)
		{
			var li = lis[i];
			if (this.highlighted == i)
			{
				li.className = "selected";
			}
			else
			{
				li.className = "";
			}
		}
	};
	this.positionDiv = function()
	{
		var el = this.elem;
		var x = 0;
		var y = el.offsetHeight;
		while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
		{
			x += el.offsetLeft;
			y += el.offsetTop;
			el = el.offsetParent;
		}

		x += el.offsetLeft;
		y += el.offsetTop;
		this.div.style.left = x + 'px';
		this.div.style.top = y  + 'px';
	};
	this.createDiv = function()
	{
		var ul = document.createElement('ul');
		for (i=0;i<me.eligible.length;i++)
		{
			var word = me.eligible[i].l;
			if (word!=null) {
				var li = document.createElement('li');
				var a = document.createElement('a');
				a.href="javascript:false";
				a.innerHTML = word;
				li.appendChild(a);
		
				if (me.highlighted == i)
				{
					li.className = "selected";
				}
				else
					li.className="";		
				ul.appendChild(li);
			}
		}
	
		this.div.replaceChild(ul,this.div.childNodes[0]);
		ul.onmouseover = function(ev)
		{
			var target = me.getEventSource(ev);
			while (target.parentNode && target.tagName.toUpperCase() != 'LI')
			{
				target = target.parentNode;
			}		
			var lis = me.div.getElementsByTagName('LI');	
			for (i in lis)
			{
				var li = lis[i];
				if(li == target)
				{
					me.highlighted = i;
					break;
				}
			}
			me.changeHighlight();
		};
		ul.onclick = function(ev)
		{
			me.useSuggestion();
			me.hideDiv();
			me.cancelEvent(ev);
			return false;
		};	
		this.div.className="suggestion_list";
		this.div.style.position = 'absolute';
	};
	this.setEligible = function(arr) {
		me.eligible=arr;
	}
	this.getEligible = function()
	{
		getSuggestions(this.inputText);
	};
	this.getKeyCode = function(ev)
	{
		if(ev)			//Moz
		{
			return ev.keyCode;
		}
		if(window.event)	//IE
		{
			return window.event.keyCode;
		}
	};
	this.getEventSource = function(ev)
	{
		if(ev)			//Moz
		{
			return ev.target;
		}
	
		if(window.event)	//IE
		{
			return window.event.srcElement;
		}
	};
	this.cancelEvent = function(ev)
	{
		if(ev)			//Moz
		{
			ev.preventDefault();
			ev.stopPropagation();
		}
		if(window.event)	//IE
		{
			window.event.returnValue = false;
		}
	}
}
var idCounter = 0;