﻿// JavaScript per recupero Comuni
var myRequest2 = null;
function CreateXmlHttpReq2(handler) {
	var xmlhttp2 = null;
	try {
		xmlhttp2 = new XMLHttpRequest();
	} 
	catch(e) {
		try {
			xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) {
			xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	xmlhttp2.onreadystatechange = handler;
	return xmlhttp2;
}
function myHandler2() {
	if (myRequest2.readyState == 4 && myRequest2.status == 200) {
		e = document.getElementById("divcomuni");
		e.innerHTML = myRequest2.responseText;
		s = document.getElementById('selcomune')
		if ( s.options.length > 0 )
			e.style.display = "block";
		else
			e.style.display = "none";
		//alert(myRequest2.responseText);
	}
}

function key(e) { e = e || window.event; return e.keyCode; }

function cercacomune(stringa,keyCode) {
	e = document.getElementById("divcomuni");
	s = document.getElementById('selcomune')
	if(keyCode==40) {
		if (s!=null && e.style.display=='block') {
			s.focus();
			s.selectedIndex=0;
			return;
		}
	}
	if(stringa.length >= 2){
		var r = Math.random();
		myRequest2 = CreateXmlHttpReq2(myHandler2);
		myRequest2.open("GET","blocchi/getcomune.cfc?method=f_init&stringa="+encodeURI(stringa)+"&rand="+escape(r));
		myRequest2.send(null);
	}else{
		e = document.getElementById("divcomuni");
		e.innerHTML = '';
		e.style.display = "none";
	}
}
function azzerraCampi(){
	document.getElementById("comune").value = 'Cerca Comune';
}
function trovacomune(arg,keyCode){
	s = document.getElementById('selcomune')
//alert((arg.tagName == 'FORM')+','+ (s!=null )+','+ (s.selectedIndex == -1))
	if ( arg.tagName == 'SELECT' && keyCode==13 ){
		document.getElementById('comune').value = arg.options[arg.selectedIndex].value;
		document.getElementById('istat').value = arg.options[arg.selectedIndex].id;
		document.forms.cerca_comune.submit();
	}else if ( arg.tagName == 'FORM' && s!=null && s.selectedIndex <= 0){
		if ( s.options.length > 0 ){
 			document.getElementById('comune').value = s.options[0].value;
			document.getElementById('istat').value = s.options[0].id;
		}else{return false}
	}else{return false}
}
