var _adresseRecherche = "includes/texte_libre_options.php"; //l'adresse à interroger pour trouver les suggestions
var texte_libre_table = "texte_libre";

var datas;
var textelibre;
var old_store;
var _eventKeycode = null;
var keyNull;
var old_value;

function initTexteLibre(){
		datas = new dojo.data.ItemFileReadStore({url: 'includes/texte_libre_options.php?debut=_&table=texte_libre' });
		textelibre = dijit.byId('textelibre');	
		textelibre.focus();
		old_store = datas;
		old_value = "";
		keyNull = false;
		//console.log(textelibre);
		textelibre.store = datas;
		dojo.connect(textelibre,'onKeyUp',reloadDatas);
		
}



function reloadDatas() {
		/* reinitialisation of the ComboBox Store */		
		document.getElementById('textelibre').onkeyup = onKeyUpHandler;
		var value = document.getElementById('textelibre').value;		
		// console.log("value : "+value+" length :"+document.getElementById('textelibre').value.length+" url :"+'includes/texte_libre_options.php?debut='+value+'&table='+texte_libre_table);
		if(value.length>0 && !keyNull && value!=old_value){
			// console.log("reload");			
			if(old_store._loadFinished!=false){
				// console.log("not over");
				// old_store.close();
				//console.log(old_store._loadFinished);
			}
			var store = new dojo.data.ItemFileReadStore({url: 'includes/texte_libre_options.php?debut='+encodeURIComponent(value)+'&table='+texte_libre_table });			
			//console.log(store._fetchItems);
			textelibre.store = store;
			old_store = store;
			old_value = value;
		}		
}

var onKeyUpHandler=function(event){
	// accès evenement compatible IE/Firefox
	if(!event&&window.event) {
	event=window.event;
	}
	_eventKeycode=event.keyCode;
	// console.log(_eventKeycode);
	switch(_eventKeycode){
		 // cursor left, cursor right, page up, page down, others??	
		case 3:
		case 13:
		case 27:
		case 38:
		case 40:
		  // on supprime la suggestion du texte utilisateur
		  keyNull=true;
		  break;
		default:
		  keyNull=false;
		  break
	}
}


function utf8_encode ( string ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}

var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

// echappe les caractère spéciaux
function escapeURI(La){
  if(encodeURIComponent) {
    return encodeURIComponent(La);
  }
  if(escape) {
    return escape(La);
  }
}

function no_accent(my_string) {
	var new_string = "";
	var pattern_accent = new Array("é", "è", "ê", "ë", "ç", "à", "â", "ä", "î", "ï", "ù", "ô", "ó", "ö");
	var pattern_replace_accent = new Array("e", "e", "e", "e", "c", "a", "a", "a", "i", "i", "u", "o", "o", "o");
	if (my_string && my_string!= "") {
		new_string = preg_replace(pattern_accent, pattern_replace_accent, my_string);
	}
	return new_string;
}

function preg_replace (array_pattern, array_pattern_replace, my_string)  {
	var new_string = String (my_string);
	for (i=0; i<array_pattern.length; i++) {
		var reg_exp= RegExp(array_pattern[i], "gi");
		var val_to_replace = array_pattern_replace[i];
		new_string = new_string.replace (reg_exp, val_to_replace);
	}
	return new_string;
}
