var xmlHttp = false;
var isBusy = false;
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

function checkIt(string){
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*
ho definito l'oggetto xmlHttp in cima al file
*/
if (browser=="Internet Explorer"){
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlHttp = false;
		}
	}
}else{
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
}

function carica_comuni(caricamento, id_provincia, id_comune){
	
	reset_select(document.getElementById('id_comune'));
	/*
	if ( caricamento == 1 ){
		assign_value('cap', '');
		assign_value('telefono', '');
	}
	*/
	
	if ( id_provincia == null || id_provincia == "" || id_provincia == 0 || id_provincia.length == 0 ) return;
	
	var url = "../include/getData.php?opt=carica_comuni&id_provincia="+id_provincia+"&id_comune="+id_comune;

	if (isBusy){
		xmlHttp.onreadystatechange = null; // no go in IE
		xmlHttp.abort();
	}
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	isBusy = true;

	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = result_carica_comuni;

	// Send the request
	xmlHttp.send(null);

}

function result_carica_comuni(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		eval(response);
		return;
	}
}

function carica_cap_telefono(valore){
	
	assign_value('cap', '');
	assign_value('telefono', '');
	
	if ( valore == null || valore == "" || valore == 0 || valore.length == 0 ) return;
	
	var url = "../include/getData.php?opt=carica_cap_telefono&valore="+valore;

	if (isBusy){
		xmlHttp.onreadystatechange = null; // no go in IE
		xmlHttp.abort();
	}
	
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	
	isBusy = true;

	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = result_carica_cap_telefono;

	// Send the request
	xmlHttp.send(null);

}

function result_carica_cap_telefono(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		var risultati = response.split('::');
		var obj1 = 'cap';
		var obj2 = 'telefono';
		
		assign_value(obj1, risultati[0]);
		assign_value(obj2, risultati[1]);
		return;
	}
}

//assegna ad un campo(id) un valore
function assign_value(id, valore){
	var obj = document.getElementById(id);
	if ( obj != null ){
		obj.value = valore;
	}
}

//recupera un valore da un campo(id)
function get_value(id){
	return document.getElementById(id).value;
}

//recupera un valore da un campo(id)
function get_html(id){
	return document.getElementById(id).innerHTML;
}

//imposta il focus su un campo(id)
function get_focus(id){
	document.getElementById(id).focus();
}

function seleziona_select(oggetto){
	if ( oggetto != null ){
		oggetto.options[0] = new Option('Seleziona...',0);
		oggetto.options[0].selected = true;
	}
}

function seleziona_prima_option(oggetto){
	if ( oggetto != null ){
		oggetto.options[0].selected = true;
	}
}

function reset_select(oggetto){
	if ( oggetto != null ){
		oggetto.options.length = 0;
	}
}

function reset_input(oggetto){
	if ( oggetto != null ){
		oggetto.value = "";
	}
}

function show_hide(oggetto){
	var obj = document.getElementById(oggetto);
	if ( obj != null ){
		var status = obj.style.display;
		if ( status == 'block' ){
			obj.style.display = 'none';
		}else{
			obj.style.display = 'block';
		}
	}
}

function Vedi(){
	document.getElementById("01").style.display = 'block';
}

function Nascondi(){
	document.getElementById("01").style.display = 'none';
}

function Vedi2(){
	document.getElementById("02").style.display = 'block';
}

function Nascondi2(){
	document.getElementById("02").style.display = 'none';
}

