/**************************************************************
 * CalendarEvent
 *
 * Constructeur
 *
 * @param domNode_  domNode  Divi HTML
 * @param	calendarGrid_	CalendarGrid	calendrier auxquel est relié l'evenement
 *
 ***************************************************************/
function FormSimpleListCompletion(listNode_, url_){
	this.enable=false;
	this.minsize=1;
	this.domNode=listNode_;																				//le champ list <select>
	this.searchDomNode = document.getElementById('search_'+listNode_.id);		//le champ de recherche
	this.url=url_;																													//url qui va générer la listes des suggestions
	this.oldValue="";																											// valeur précédente du champ texte
	this.currentValue=""; 																									// valeur actuelle du champ texte
	if(this.domNode.options.length==0) this.domNode.disabled=true;

	// tourne en permanence pour suggérer suite à un changement du champ texte
	this.mainLoop();
}

FormSimpleListCompletion.prototype.mainLoop=function(){

	  this.currentValue = this.searchDomNode.value;

	  if(this.oldValue!= this.currentValue){
	  	this.enable=true;
	    this.callSuggestions();
	  }

	  if(!this.currentValue && this.enable){
	  	this.reinit();
  		this._reinitValue();
	  }


	  var obj=this;
	  setTimeout(function(){obj.mainLoop();},400); // la fonction se redéclenchera dans 300 ms
}

FormSimpleListCompletion.prototype.setMinSize=function(size_){
	this.minsize=size_;
}

FormSimpleListCompletion.prototype.getMinSize=function(){
	return this.minsize;
}


FormSimpleListCompletion.prototype.callSuggestions=function(){

	var val_=escapeURI(this.currentValue);
	if(val_ && this.currentValue.length>=this.getMinSize()){
		var url=this.url+'&search='+val_;
		var objet=this;
		NewAC(url).setMimeType("text/xml")
						 .addHandler({type:AC_METHOD, value:objet, method:'insertSuggestions'})
						 .connect('',this.domNode.id);
		return true;
	}else{
  	this.reinit();
  	this._reinitValue();
  }
}

FormSimpleListCompletion.prototype._reinitValue=function(val_){

	 this.currentValue = this.searchDomNode.value;
	 this.oldValue=val_;
}

FormSimpleListCompletion.prototype.insertSuggestions=function(val_){


	var newOptions = val_.getElementsByTagName('option');
	var search=val_.getElementsByTagName('search');
	var oldOptions=this.domNode.options;

	this.reinit();
	this._reinitValue(search[0].firstChild.data);
	if(newOptions.length>0){
		var newOptions = val_.getElementsByTagName('option');
		for(i=0; i<newOptions.length;i++){
				this.domNode.length++;
				var option=newOptions[i];
				this.domNode.options[i].value=option.getAttribute('value');
				this.domNode.options[i].text=option.firstChild.data;
		}
		this.domNode.disabled=false;
	}
}

FormSimpleListCompletion.prototype.reinit=function(){
		this.domNode.length=0;
		this.domNode.disabled=true;
}
/**************************************************************
 * escapeURI
 *
 * escapeURI est une méthode toute simple, permettant d'échapper les caractères
 * spéciaux du champ texte avant d'envoyer la requête au serveur.
 * Cette méthode se base sur des méthodes JavaScript natives des navigateurs.
 *
 * @param La String à échapper
 * @return  String échappée
 ***************************************************************/
function escapeURI(La){
  if(encodeURIComponent) {
    return encodeURIComponent(La);
  }
  if(escape) {
    return escape(La)
  }
}




