//Creazione dell'oggetto xmlhttp
function assegnaXMLHttpRequest() {
	var
		XHR = null,
		browserUtente = navigator.userAgent.toUpperCase();

	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		XHR = new XMLHttpRequest();
		else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
			if(browserUtente.indexOf("MSIE 5") < 0)
				XHR = new ActiveXObject("Msxml2.XMLHTTP");
			else
				XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return XHR;
};


function leggiXML() {

	//Creo l'oggetto xmlhttp
	var ajax = assegnaXMLHttpRequest();
 
	// elemento è il div dove visualizzare il risultato della pagina
	var elemento = prendiElementoDaId('chatboard');

	// se l'oggetto è creato	
	if(ajax)
	{
		// apre la connessione in GET con la pagina prova.php passando la variabile id
		ajax.open("get", "include/chat/leggiXML.php", true);
	
		ajax.setRequestHeader("connection", "close");
		
		// al cambio di stato
		ajax.onreadystatechange = function()
		{
			// verifica dello stato
			if(ajax.readyState === readyState.COMPLETATO)
			{
			
				// verifica della risposta da parte del server
				if(statusText[ajax.status] === "OK")
				{
					// riempie il div con dati printati dalla pagina php						
					elemento.innerHTML = ajax.responseText;
					return true;
				} else {
					 // errore di caricamento
					elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.<br /=Errore riscontrato: " + statusText[ajax.status];
					return false;
				}
			/*} else if(ajax.readyState === readyState.INATTIVO) {
				elemento.innerHTML = "Stato: Inattivo...";
			} else if(ajax.readyState === readyState.INIZIALIZZATO) {
				elemento.innerHTML = "Comando Inizializzato...";
			} else if(ajax.readyState === readyState.RICHIESTA) {
				elemento.innerHTML = "Invio Richiesta...";
			} else if(ajax.readyState === readyState.RISPOSTA) {
				elemento.innerHTML = "Ricezione Risposta..."; */
			} 
		}
		// invio richiesta
		ajax.send(null);
	}
	setTimeout('leggiXML()', 5000);
};

function sendXML() {

	//Creo l'oggetto xmlhttp
	var ajax = assegnaXMLHttpRequest();
 
	// elemento è il div dove visualizzare il risultato della pagina
	var elemento = prendiElementoDaId('chatboard');
	var text_chat = prendiElementoDaId('testo_chat');
	var tot_text = escape(text_chat.value);

	// se l'oggetto è creato	
	if(ajax)
	{
		// apre la connessione in GET con la pagina prova.php passando la variabile id
		ajax.open("post", "include/chat/sendXML.php", true);
	
		ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("connection", "close");
		
		// al cambio di stato
		ajax.onreadystatechange = function()
		{
		
			// verifica dello stato
			if(ajax.readyState === readyState.COMPLETATO)
			{
				// verifica della risposta da parte del server
				if(statusText[ajax.status] === "OK")
				{
					// riempie il div con dati printati dalla pagina php							  
					//elemento.innerHTML = ajax.responseText;
					leggiXML();
					text_chat.value = "";
					return true;
				} else {
					 // errore di caricamento
					elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.<br /=Errore riscontrato: " + statusText[ajax.status];
					return false;
				}
			/*} else if(ajax.readyState === readyState.INATTIVO) {
				elemento.innerHTML = "Stato: Inattivo...";
			} else if(ajax.readyState === readyState.INIZIALIZZATO) {
				elemento.innerHTML = "Comando Inizializzato...";
			} else if(ajax.readyState === readyState.RICHIESTA) {
				elemento.innerHTML = "Invio Richiesta...";
			} else if(ajax.readyState === readyState.RISPOSTA) {
				elemento.innerHTML = "Ricezione Risposta..."; */
			}
		}
		// invio richiesta
		ajax.send("testo_chat="+tot_text);
	}
};



	// oggetto di verifica stato
var readyState = {
	INATTIVO:	0,
	INIZIALIZZATO:	1,
	RICHIESTA:	2,
	RISPOSTA:	3,
	COMPLETATO:	4
};

// array descrittivo dei codici restituiti dal server
// [la scelta dell' array è per evitare problemi con vecchi browsers]
var statusText = new Array();
statusText[100] = "Continue";
statusText[101] = "Switching Protocols";
statusText[200] = "OK";
statusText[201] = "Created";
statusText[202] = "Accepted";
statusText[203] = "Non-Authoritative Information";
statusText[204] = "No Content";
statusText[205] = "Reset Content";
statusText[206] = "Partial Content";
statusText[300] = "Multiple Choices";
statusText[301] = "Moved Permanently";
statusText[302] = "Found";
statusText[303] = "See Other";
statusText[304] = "Not Modified";
statusText[305] = "Use Proxy";
statusText[306] = "(unused, but reserved)";
statusText[307] = "Temporary Redirect";
statusText[400] = "Bad Request";
statusText[401] = "Unauthorized";
statusText[402] = "Payment Required";
statusText[403] = "Forbidden";
statusText[404] = "Not Found";
statusText[405] = "Method Not Allowed";
statusText[406] = "Not Acceptable";
statusText[407] = "Proxy Authentication Required";
statusText[408] = "Request Timeout";
statusText[409] = "Conflict";
statusText[410] = "Gone";
statusText[411] = "Length Required";
statusText[412] = "Precondition Failed";
statusText[413] = "Request Entity Too Large";
statusText[414] = "Request-URI Too Long";
statusText[415] = "Unsupported Media Type";
statusText[416] = "Requested Range Not Satisfiable";
statusText[417] = "Expectation Failed";
statusText[500] = "Internal Server Error";
statusText[501] = "Not Implemented";
statusText[502] = "Bad Gateway";
statusText[503] = "Service Unavailable";
statusText[504] = "Gateway Timeout";
statusText[505] = "HTTP Version Not Supported";
statusText[509] = "Bandwidth Limit Exceeded";

onload = function()
{
leggiXML();
}

function showHelp()
{

	var testo = "Tag Utilizzabili per FlatChat:\r\n\r\n"
	+ "[:BR:] == A Capo\r\n"
	+ "[:ECOM:] == 'E' Commerciale\r\n"
	+ "[b]testo[/b] == Grassetto\r\n"
	+ "[u]testo[/u] == Sottolineato\r\n"
	+ "[code]testo[/code] == Codice\r\n"
	+ "[red]testo[/red] == Testo Rosso\r\n"
	+ "[green]testo[/green] == Testo Verde\r\n"
	+ "[blue]testo[/blue] == Testo Blu\r\n"
	+ "[pink]testo[/pink] == Testo Rosa\r\n"
	+ "[yellow]testo[/yellow] == Testo Giallo\r\n\r\n\r\n"
	+ "Smile Tags per FlatChat:\r\n\r\n"
	+ "[:)] == Smile Sorriso\r\n"
	+ "[:(] == Smile Triste\r\n"
	+ "[:o] == Smile Arrabbiato\r\n"
	+ "[:p] == Smile Linguetta\r\n"
	+ "[:D] == Smile Risata\r\n"
	+ "[:!] == Smile Indeciso\r\n"
	+ "[:O] == Smile Sbalordito\r\n"
	+ "[8)] == Smile Fighetto\r\n"
	+ "[;)] == Smile Ammiccante\r\n"
	+ "[8-)] == Smile Pensieroso\r\n"
	+ "[2-)] == Smile Imbarazzato\r\n"
	+ "[%-)] == Smile Rimbambito\r\n";
	
	alert(testo);
}

function prendiElementoDaId(id_elemento) {
 // elemento da restituire
 var elemento;
 
 // se esiste il metodo getElementById
 // questo if sarà diverso da false, null o undefined
 // e sarà quindi considerato valido, come un true
 if(document.getElementById)
  elemento = document.getElementById(id_elemento);
 
 // altrimenti è necessario usare un vecchio sistema
 else
  elemento = document.all[id_elemento];

 // restituzione elemento
 return elemento;
}

function leggi_thx(utente)
{
	var elemento = prendiElementoDaId("livo");
	
	//Creo l'oggetto xmlhttp
	var ajax = assegnaXMLHttpRequest();
	
	// se l'oggetto è creato	
	if(ajax)
	{
		// apre la connessione in GET con la pagina prova.php passando la variabile id
		ajax.open("get", "include/thx/leggiLiv.php?user="+utente, true);
		
		ajax.setRequestHeader("connection", "close");
		
		// al cambio di stato
		ajax.onreadystatechange = function()
		{
		
			// verifica dello stato
			if(ajax.readyState === readyState.COMPLETATO)
			{
				// verifica della risposta da parte del server
				if(statusText[ajax.status] === "OK")
				{
					// riempie il div con dati printati dalla pagina php		
					
					elemento.value = ajax.responseText;
					return true;
				} else {
					 // errore di caricamento
					elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.<br /=Errore riscontrato: " + statusText[ajax.status];
					return false;
				}
			/*} else if(ajax.readyState === readyState.INATTIVO) {
				elemento.innerHTML = "Stato: Inattivo...";
			} else if(ajax.readyState === readyState.INIZIALIZZATO) {
				elemento.innerHTML = "Comando Inizializzato...";
			} else if(ajax.readyState === readyState.RICHIESTA) {
				elemento.innerHTML = "Invio Richiesta...";
			} else if(ajax.readyState === readyState.RISPOSTA) {
				elemento.innerHTML = "Ricezione Risposta..."; */
			}
		}
		// invio richiesta
		ajax.send(null);
	}
}

function addScreen()
{

	var numero = prendiElementoDaId("numss");
	
	var sin = prendiElementoDaId("left"+numero.value);
	
	var des = prendiElementoDaId("right"+numero.value);
	
	var dopo = Number(numero.value) + 1;
	
	sin.innerHTML = '<span class="titolo">ScreenShot </span>('+dopo+')<span class="titolo">:</span>';
	
	des.innerHTML = '<input class="dati" name="screenshot'+numero.value+'" type="text" value="http://" />';
	
	numero.value = dopo;

}


function removeScreen()
{
	var numero = prendiElementoDaId("numss");
	
	var prima = Number(numero.value) - 1;
	
	var sin = prendiElementoDaId("left"+prima);
	
	var des = prendiElementoDaId("right"+prima);
	
	sin.innerHTML = '';
	
	des.innerHTML = '';
	
	numero.value = prima;
}

function addFile()
{

	var numero = prendiElementoDaId("numfiles");
	
	var sin = prendiElementoDaId("fleft"+numero.value);
	
	var des = prendiElementoDaId("fright"+numero.value);
	
	var dopo = Number(numero.value) + 1;
	
	sin.innerHTML = '<input class="dati" name="nomelink'+numero.value+'" type="text" value="Download" />';
	
	des.innerHTML = '<input class="dati" name="link'+numero.value+'" type="text" value="http://" />';
	
	numero.value = dopo;
	
	var cc = prendiElementoDaId("conta");
	
	cc.innerHTML = '('+dopo+')';

}

function removeFile()
{
	var numero = prendiElementoDaId("numfiles");
	
	var prima = Number(numero.value) - 1;
	
	var sin = prendiElementoDaId("fleft"+prima);
	
	var des = prendiElementoDaId("fright"+prima);
	
	sin.innerHTML = '';
	
	des.innerHTML = '';
	
	numero.value = prima;
	
	var cc = prendiElementoDaId("conta");
	
	cc.innerHTML = '('+prima+')';
}

function ctrlRelease()
{
	var nome = prendiElementoDaId("nome");
	
	var recensione = prendiElementoDaId("recensione");
	
	var nomelink = prendiElementoDaId("nomelink0");
	
	var link = prendiElementoDaId("link0");
	
	var form = prendiElementoDaId("formr");
	
	if(nome.value == '')
	{
		alert("Inserire un Nome per la Release!");
		return false;
	}
	else if(recensione.value == '')
	{
		alert("Inserire una Recensione per la Release!");
		return false;
	}
	else if(nomelink.value == '')
	{
		alert("Inserire un Nome per il Link!");
		return false;
	}
	else if(link.value == '' || link.value == 'http://')
	{
		alert("Inserire un Link per il Download!");
		return false;
	}
	else
	{
		form.submit();
		return true;
	}
}

function goDel(numero)
{
	var elemento = prendiElementoDaId("formdel"+numero);
	
	if(confirm('Vuoi Eliminare la Richiesta?'))
	{
		elemento.submit();
		return true;
	}
	else
	{
		return false;
	}
}

function checkReq()
{
	var nome = prendiElementoDaId("nom");
	var descrizion = prendiElementoDaId("descrizion");
	var formadd = prendiElementoDaId("formadd");
	
	if(nome.value == '')
	{
		alert("Inserire un Nome!");
		return false;
	}
	else if(descrizion.value == '')
	{
		alert("Inserire una Descrizione!");
		return false;
	}
	else
	{
		formadd.submit();
		return true;
	}
}