// DEFINE
var ROSSO=0;
var GIALLO=1;
var VERDE=2;

// VARIABILI
var obj;
var semaforo=ROSSO;
var utente;
var firsttime=0;

var w3c=(document.getElementById)?true:false;
var ns4=(document.layers)?true:false;
var ie4=(document.all && !w3c)?true:false;
var ie5=(document.all && w3c)?true:false;
var ns6=(w3c && navigator.appName.indexOf("Netscape")>=0)?true:false;

function init(){
	adatta();
	semaforo=GIALLO;
	obj=istanzaXHR();
	semaforo=VERDE;
//	fmettionline();
//	window.setTimeout("fmettionline();",5000);
	fmettionline();

}

function istanzaXHR() {
	var tmpobj;
	if (window.XMLHttpRequest) {	// Non Internet Explorer
		tmpobj = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // Internet Explorer
		tmpobj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return tmpobj;
}

function onStateChangeUtente() {	// Cambiamento Stato
	if (obj.readyState == 4) { // Caricamento Avvenuto
		if (obj.status == 200) { // con successo
			var stringatotale = obj.responseText; //controllo cosa sto ricevenfo
//			stringatotale = stringatotale.substring(1,(stringatotale.length-2));
			if( stringatotale == "NDN") {
				document.getElementById("ricercautente").innerHTML="<font size='-2' color='green'>Nessun&nbsp;utente&nbsp;disponibile</font>";
			} else if( stringatotale == "NNN") {
				document.getElementById("ricercautente").innerHTML="<font size='-2' color='green'>Inserisci&nbsp;almeno&nbsp;due&nbsp;lettere.</font>";
			} else {
				document.getElementById("ricercautente").innerHTML="<font size='-1' color='green'>"+stringatotale+"</font>";
			}
		} else {
			alert("Errore: "+obj.statusText);
		}
	}
}

function inviaXHR(obj,url) {
	try {
		obj.open("POST", url, true);	// Preparazione comunicazione
		obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		obj.onreadystatechange = onStateChange;
		obj.send("vai=ok");
	} catch (e) {
		alert("Errore: "+e);
	}
}

function inviautenteXHR(obj,url) {

	var utente;

	utente=document.getElementById("cercautente").value;
	try {
		obj.open("POST", url, true);	// Preparazione comunicazione
		obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		obj.onreadystatechange = onStateChangeUtente;
		obj.send("user="+utente);	// Trasferimento
	} catch (e) {
		alert("Errore: "+e);
	}
}

/*******************************************************
// Cerca Utenti
*******************************************************/

function fcercautente() {

	var utente;
	utente=document.getElementById("cercautente").value;

	if( utente.length != 0 ) {
		inviautenteXHR(obj,"./caronte.php");
	}
}

//-----------------------------------------------------

/*******************************************************
// Mostra Post
*******************************************************/

/*function MostraPost(id)
{*/
	/*var post;
	post=document.getElementById("mostrapost").value;*/

	//if(post.length != 0)
	/*if(id.length != 0)
	{
		inviapostXHR(id,obj,"./caronte.php");
	}
}*/

function MostraPost(id)
{
	$("#leggitutto_" + id).html("<img src=\"img/loading.gif\" border=\"0\" alt=\"Caricamento in corso...\"/>");
	var letture = parseInt($("#letture_" + id).attr("innerHTML").split(" ")[0]) + 1;
	$("#letture_" + id).html(letture + " Letture");

	if(id.length != 0)
	{
		inviapostXHR(id,obj,"./caronte.php");
	}
}


function inviapostXHR(id,obj,url) {

	/*var post;

	post=document.getElementById("mostrapost").value;*/
	try {
		obj.open("POST", url, true);	// Preparazione comunicazione
		obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //dichiarazione per POST
		obj.onreadystatechange = function () {	// Cambiamento Stato
	if (obj.readyState == 4) 
	{ // Caricamento Avvenuto
		if (obj.status == 200) 
		{ // con successo
			var ricevo = obj.responseText; 
			if(ricevo.length != 0)
			{
				document.getElementById("mostrami_"+id).innerHTML=URLDecode(ricevo);
			}
		} 
		else 
		{
			alert("Errore: "+obj.statusText);
		}
	}
}
		//obj.send("post="+post);	// Trasferimento
		//obj.send(id+post);	// Trasferimento
		obj.send("?commenta&id="+id);	// Trasferimento
		//alert("?id="+id);
	} catch (e) {
		alert("Errore: "+e);
	}
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}


//------------------------------------------------------

/*******************************************************
// Cambio Pagina
*******************************************************/

function CaricaPagina(pg)
{
	var pag;

	pag=document.getElementById("pagina").value;
	
	if(pg.length != 0)
	{
		inviapaginaXHR(pg,obj,"./caronte.php");
	}
}


function inviapaginaXHR(pg,obj,url) {

	var pag;

	pag=document.getElementById("pagina").value;

	try {
		obj.open("POST", url, true);	// Preparazione comunicazione
		obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //dichiarazione per POST
		obj.onreadystatechange = function () {	// Cambiamento Stato
	if (obj.readyState == 4) 
	{ // Caricamento Avvenuto
		if (obj.status == 200) 
		{ // con successo
			var ricevo = obj.responseText; 
			if(ricevo.length != 0)
			{
				document.getElementById("pagina").innerHTML=ricevo;
			}
		} 
		else 
		{
			alert("Errore: "+obj.statusText);
		}
	}
}
		//obj.send("post="+post);	// Trasferimento
		//obj.send(id+post);	// Trasferimento
		obj.send("pagina="+pg);	// Trasferimento
		//alert("?id="+id);
	} catch (e) {
		alert("Errore: "+e);
	}
}


//------------------------------------------------------

/*******************************************************
// Visualizzo gli utenti online
*******************************************************/

function fmettionline() {

	if( semaforo == VERDE ) {
		semaforo=GIALLO;
		inviaonlineXHR(obj,"./caronte.php");
		window.setTimeout("fmettionline();",5000);
	} else if( semaforo == GIALLO ) {
		window.setTimeout("fmettionline();",1000);
	}
}

function inviaonlineXHR(obj,url) {

	try {
		obj.open("POST", url, true);	// Preparazione comunicazione
		obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		obj.onreadystatechange = onStateChangeOnLine;
		obj.send("mioidinviato=dummy");	// Trasferimento
	} catch (e) {
//		alert("Errore: "+e);
		semaforo=ROSSO;
	}
}

function onStateChangeOnLine() {	// Cambiamento Stato

	if (obj.readyState == 4) { // Caricamento Avvenuto
		if (obj.status == 200) { // con successo
			var stringatotale = obj.responseText;
			document.getElementById("useronline").innerHTML="<hr><font size='-1' color='white'>"+stringatotale+"</font><hr>";
			semaforo=VERDE;
		} else {
//			alert("Errore: "+obj.statusText);
			semaforo=ROSSO;
		}
	}
}

function onStateChangeIP() {	// Cambiamento Stato
	if (obj.readyState == 4) { // Caricamento Avvenuto
		if (obj.status == 200) { // con successo
			var stringatotale = obj.responseText;
/*
			if( stringatotale == "ND") {
				document.getElementById("presenza").innerHTML="<font size='-1' color='green'>IP BANNABILE</font>";
			} else {
*/
				document.getElementById("presenza").innerHTML=stringatotale;
//			}
			semaforo=VERDE;
		} else {
			document.getElementById("presenza").innerHTML="?";
//			alert("Errore: "+obj.statusText);
			semaforo=ROSSO;
		}
	}
}

function inviaIPXHR(obj,url) {
	try {
		obj.open("POST", url, true);	// Preparazione comunicazione
		obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		obj.onreadystatechange = onStateChangeIP;
		obj.send("checkip="+utente);	// Trasferimento
	} catch (e) {
		document.getElementById("presenza").innerHTML="??";
//		alert("Errore: "+e);
		semaforo=ROSSO;
	}
}


function aggiorna() {
	if(firsttime==0) {
		init();
		firsttime=1;
	}

	if( semaforo == VERDE ) {
		semaforo=GIALLO;
		utente=document.getElementById("ip").value;
		inviaIPXHR(obj,"./caronte.php");
	} else if( semaforo == GIALLO ) {
		window.setTimeout("aggiorna();",1000);
	}

}



function adatta(){

	if (screen.width > 1025 ) {
		document.getElementById("ida1").style.width = "80%";
	}else{
		document.getElementById("ida1").style.width = "68%";
	}
/*	alert(document.getElementById("tad").style.width);
	alert(1);
*/
}
window.onload=function(){
	init();
}

window.onresize=function(){
	if(ns4)setTimeout('history.go(0)',400);
}
/*
Errore: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.statusText]"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: http://www.capterstux.com/ajax/ajax.js :: onStateChangeUtente :: line 52"  data: no]
File sorgente: http://www.capterstux.com/ajax/ajax.js
Riga: 52
*/