var http_signal_comment = getHTTPObject();
var url_signal_comment = "signal_new_comment.php?";

function handleHttpResponse_signal_comment() {
	if (http_signal_comment.readyState == 4) { 
		clearTimeout(timeout_signal_comment);
		if(http_signal_comment.status == 200 || http_signal_comment.status == 304 || http_signal_comment.status == 307){ 
			document.getElementById('clientarea').innerHTML = "<table style='border: 0;'><tr style=''><td>" + http_signal_comment.responseText + "</td></tr></table>";
			if(document.getElementById("campo_textarea")) document.getElementById("campo_textarea").value = '';
			occupato_signal_comment = false;
			document.body.style.cursor = "auto";
		}
		else {
			document.getElementById('clientarea').innerHTML = "<table style='border: 0;'><tr style=''><td>" + "Si sono verificati dei problemi. Si prega di ripetere l'operazione." + "</td></tr></table>";
			occupato_signal_comment = false;
			document.body.style.cursor = "auto";
		}
	}
	else if(max_wait < 1000) {
		max_wait *= 1000;
		check = check_elapsed_time(); 
	}
}


var occupato_signal_comment = false;
function signal_comment_xmlhttprequest(class_name,comment) {
	if (!occupato_signal_comment && http_signal_comment) { 
		document.getElementById('clientarea').innerHTML = "<table style='border: 0;'><tr style=''><td>" + "<img src='./immagini/indicator_waitanim.gif' width='85' height='81' alt='' /><br />Attendere prego..." + "</td></tr></table>";
		
		new_date = new Date();
		initial_time = new_date.getTime(); // tempo in millisecondi dell'inizio
		max_wait = 5; //tempo di attesa massima espresso in secondi
		
		document.body.style.cursor = "wait";
		var url_completo = url_signal_comment+"classe="+encodeURIComponent(class_name)+"&commento="+encodeURIComponent(comment);
		http_signal_comment.open("GET", url_completo, true);
		http_signal_comment.onreadystatechange = handleHttpResponse_signal_comment;
		occupato_signal_comment = true;
		http_signal_comment.send(null);
	}
}

function check_elapsed_time () {
	new_date = new Date();
	if((new_date.getTime() - initial_time) > max_wait) {
		http_signal_comment.onreadystatechange = function(){return;};
		http_signal_comment.abort();
		document.getElementById('clientarea').innerHTML = "<table style='border: 0;'><tr style=''><td>" + "Spiacente, richiesta fallita. Si prega di ritentare tra qualche istante." + "</td></tr></table>";
		occupato_signal_comment = false;
		document.body.style.cursor = "auto";
	}
	else {
		timeout_signal_comment = setTimeout(check_elapsed_time, 500);
	}
}
