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();
	}
}

/************************************************************************/

/**************************
 * FUNZIONI DISPONIBILITA *
 *************************/
function check_data_disponibilita(data){
	if ( data == null || data == "" || data.length < 10 ) return;
	
	var url = "/include/getData.php?opt=check_data_disponibilita&data="+data;

	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_check_data_disponibilita;

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

function result_check_data_disponibilita(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		var risultati = response.split('::');
		if ( risultati[0].length > 0 ){
			alert(risultati[0]);
			assign_html(risultati[1], risultati[2]);
			assign_value(risultati[3], risultati[4]);
			return false;
		}
	}
}

function cerca_disponibilita(id_itinerario, data){
	if ( data == null || data == "" || data.length < 10 ) return;
	
	var url = "/include/getData.php?opt=check_disponibilita&id_itinerario="+id_itinerario+"&data="+data;

	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_cerca_disponibilita;

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

function result_cerca_disponibilita(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		document.getElementById('risultati').innerHTML = response;
	}
}

/******************
 * FUNZIONI SITO *
 *****************/
function abilita_prosegui(id_disponibilita){
	document.getElementById('submit_prosegui').disabled = true;
	if ( id_disponibilita == null || id_disponibilita == "" || id_disponibilita == 0 ) return;
	
	var url = "/include/getData.php?opt=visualizza_prezzi&id_disponibilita="+id_disponibilita;

	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_abilita_prosegui;

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

function result_abilita_prosegui(){
	if (xmlHttp.readyState != 4){
		return;
	}else if (xmlHttp.readyState == 4){
		isBusy = false;
		
		var response = xmlHttp.responseText;
		document.getElementById('visualizza_prezzi').innerHTML = response;
		document.getElementById('submit_prosegui').disabled = false;
	}
}

function check_form_scelta(theForm){
	
	var controllo_soluzione = false;
	var controllo_metodo = false;
	
	var radio_soluzione = theForm.soluzione;
	var radio_metodo = theForm.metodo;
	
	var testo = "Confermi le scelte effettuate?";

	for(var i=0; i < radio_soluzione.length; i++) {
		if( radio_soluzione[i].checked ){
			controllo_soluzione = true;
			break;
		}
	}

	for(var i=0; i < radio_metodo.length; i++) {
		if( radio_metodo[i].checked ){
			controllo_metodo = true;
			break;
		}
	}
	
	if ( !controllo_soluzione ){
		alert("E' obbligatorio selezionare una soluzione di pagamento.");
		return false;
	}

	if ( !controllo_metodo ){
		alert("E' obbligatorio selezionare un metodo di pagamento.");
		return false;
	}

	if ( confirm(testo) ){
		return true;
	}else{
		return false;
	}

}

function cambia_posti_disponibili(){
	var posti_disponibili_iniziale = parseInt(get_value('posti_disponibili_iniziale'));
	var adulti = parseInt(get_value('adulti'));
	var bambini = parseInt(get_value('bambini'));
	var scuole = parseInt(get_value('scuole'));
	var totale = adulti + bambini + scuole;
	var differenza = 0;
	
	var posti_disponibili = posti_disponibili_iniziale - adulti - bambini - scuole;
	assign_value('posti_disponibili', posti_disponibili);
	
	if ( posti_disponibili < 0 ){
		alert('Numero massimo posti disponibili raggiunto');
		assign_value('posti_disponibili', 0);
		
		differenza = totale - posti_disponibili_iniziale;
		
		if ( adulti == bambini && adulti == scuole ){
			
			adulti = adulti - differenza;
			assign_value('adulti', adulti);
			
		}else{
		
			if ( adulti > bambini){
				if ( adulti > scuole ){
					adulti = adulti - differenza;
					assign_value('adulti', adulti);
				}else{
					scuole = scuole - differenza;
					assign_value('scuole', scuole);
				}
			}else{
				if ( bambini > scuole ){
					bambini = bambini - differenza;
					assign_value('bambini', bambini);
				}else{
					scuole = scuole - differenza;
					assign_value('scuole', scuole);
				}
			}
		}
	}
}

/******************
 * FUNZIONI VARIE *
 *****************/
//assegna ad un campo(id) un valore
function assign_value(id, valore){
	document.getElementById(id).value = valore;
}

function assign_html(id, valore){
	return document.getElementById(id).innerHTML = 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 resetta_select(oggetto){
	if ( oggetto != null ){
		/*
		oggetto.options.length = 0;
		oggetto.disabled = true;
		*/
		oggetto.options[0] = new Option('Seleziona...',0);
		oggetto.options[0].selected = true;
		
	}
}

function resetta_input(oggetto){
	oggetto.value = "";
}

function apriwin(indirizzo, nwidth, nheight){
	NuovaWin = window.open(indirizzo, "NewWin", "width=" + nwidth + ", height=" + nheight + ", scrollbars=no, toolbar=no, menubar=no, location=no, directories=no, resizable=no");
	NuovaWin.focus();
}
