/*
Funzione che autocompila un campo sull'inserimento
Il campo da autocompilare deve essere strutturato nel modo seguente:
<input type='text' name='destinazione' [class="classe_destinazione"] [size...maxlenght...] OnKeyUp="autoComplete(this,this.form.destinazione_src,'value',true, text_color)" autocomplete=off>
Pił un campo nascosto con lo stesso colore del testo originale del campo destinazione:
<input type="text" name="destinazione_textcolor" [class="classe_destinazione"] style="display:none;">
Pił una combo sorgente con i valori da prelevate per l'autocompilazione:

		<select name="destinazione_src" style="display:none;">
			<option value="valore">label
			...
		</select>

-------------------
Compatibile con:
IE 6.0 +
Per gli altri non funziona ma non restituisce errori:
-------------------
*/

function autoComplete (field, select, property, text_color) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		found=true; break;
		}
	}
	if (found) { select.selectedIndex = i;}
	else { select.selectedIndex = -1; field.style.color='#FF7676';return;}
	if (field.createTextRange) {
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
				}
			}
		}
		// Nel caso prima il testo fosse rosso lo ripristino a quello corretto:
		field.style.color = text_color;
	}
