LiveSearchCities = Class.create();
LiveSearchCities.prototype = {
	initialize: function(searchItem,resultsItem,activityItem,activityHelp) {
		this.searchItem		= searchItem;
		this.resultsItem	= resultsItem;
		this.activityItem	= activityItem;
		this.activityHelp	= activityHelp;
		this.injectBehavior();
		this.updateSearchResults();
	},
	injectBehavior: function() {
		Event.observe(
			this.resultsItem,
			'change',
			function () {
				var loc=$F('scitychoice').split("||");
				$('sinsee').value = loc[1];
				$('spostcode').value = loc[0];
			});
		Event.observe(
			this.searchItem,
			'keyup',
			this.updateSearchResults.bindAsEventListener(this));
	},
	updateSearchResultsCity: function(city) {
		$(this.searchItem).value=$(citychoice);
	},
	updateSearchResults: function() {
		if(Ajax.activeRequestCount == 0 && $F(this.searchItem).length > 3 ) {
			$(this.resultsItem).options.length = 0;
			var pars = 'cp=' + $F(this.searchItem);
			var url = '/api/cities.php';
			$(this.activityItem).style.display = '';
			$(this.activityHelp).style.display = 'none';
			var myAjax = new Ajax.Request(url,{method: 'post',parameters: pars,onComplete: this.handleSearch.bind(this)});
		}
	},
	handleSearch: function(originalRequest) {
		sel=$(this.resultsItem);
		insee=$('sinsee').value;
		var obj = new Option('choisir votre ville/village','',0,0);
		sel.options[ sel.options.length ] = obj;
		var json_object = eval( '(' + originalRequest.responseText + ')' );
		json_object.each(function(item, index) {
			var obj = new Option( item.cp+' '+ item.ville,item.cp+'||'+item.id,0, (item.id==insee?1:0));
			sel.options[ sel.options.length ] = obj;
		});
		$(this.activityItem).style.display = 'none';
		$(this.resultsItem).disabled = false;
	}
}
function index(){sel.selectedIndex = sel.length-1;}

function ajoute_opt(){
	pause=setTimeout("index()",10)
}
