    var http_request = false;
		
		var arrSMS, arrWEB, smsOK, webOK;
	
	// 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";


// array codice faccine

	var smile = new Array();

	smile[0] =  new Array(";[\)]", ":ah:",":wow:",":wohhh:",":vom:",":up:",":testate:",":sun:",":smack:",":si:",":saint:",":rotolo:",":rosso:",":roll:",":reyes:",":regol:",":regan:",":P",":o",":no:",":met:",":mad:",":kal:",":italy:",":help:",":dj:",":D",":cul:",":crybaby:",":cool:",":comp:",":arg:",":aff:",":ador:",":[\?]:",":[\)]",":[\(]", ":ciao:", ":wofftopic:", ":leggi:", ":bravo:",":timid:",":rinco:",":pioggia:",":morte:",":coatto:",":applausi:",":censura",":hmm:",":yea:");
	smile[1] =  new Array("wink.gif", "lol.gif", "wow.gif", "ohhh.gif", "barfy.gif", "up.gif", "testate.gif", "sun.gif", "bisou.gif", "yes.gif", "saint2.gif", "rotolo.gif", "arb.gif", "roll.gif", "rolleyes.gif", "regolamento.gif", "regan.gif", "tongue.gif", "shocked.gif", "no.gif", "metallica.gif", "mad.gif", "smile_kal.gif", "italy.gif", "help.gif", "dj.gif", "biggrin.gif", "cul.gif", "crybaby.gif", "cool.gif", "computer.gif", "arg.gif", "affogo.gif", "prostrazione.gif", "puzzled.gif", "smile.gif", "sad.gif", "ciao.gif", "offtopic.gif", "leggi_rsvd.gif", "signs.gif","timid.gif","rinco.gif","pioggia.gif","morte.gif","coatto.gif","clapping.gif","censura.gif","hmm.gif","yea.gif");


	// restituisce il testo in un nodo
		function leggiContenuto(item, nomeNodo) {
			if(item.getElementsByTagName(nomeNodo).item(0).firstChild){
				var str = item.getElementsByTagName(nomeNodo).item(0).firstChild.nodeValue;
  			return str.replace('<',"&lt;").replace('>',"&gt;");
  		}
  		else
  			return "";
		};

	// restituisce un elemento dal suo id
		function prendiElementoDaId(id_elemento) {
			var elemento;
			if(document.getElementById)
				elemento = document.getElementById(id_elemento);
			else
				elemento = document.all[id_elemento];
			return elemento;
		};
	
	
	// restituisce un oggetto XMLHttpRequest
		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;
		};
	
	
	// esegue la "funzione" dopo la richiesta ajax
    function makeRequest(url,getvar,funzione) {

        http_request = false;
        
        http_request = assegnaXMLHttpRequest();
        
/*        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                //http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
*/

        if (!http_request) {
            alert('Errore ajax - No XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = funzione;
        http_request.open('POST', url, true);
				http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(getvar);

    }


	// credito SMS
    function alertContents() {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                //alert(http_request.responseText);
								prendiElementoDaId("result").innerHTML = http_request.responseText;
            } else {
                prendiElementoDaId("result").innerHTML = "Errore nella richiesta...";
            }
        }else{
								prendiElementoDaId("result").innerHTML = "Caricamento in corso...";
				}
    }





// Legge i messaggi da XML e restituisce un array - [1] data, [2] testo

function leggiMex(xml) {

 var
 	
	mex = new Array(),
	
	items = xml.getElementsByTagName("mex"),
	
	testo,	mitt, data;
	

 for(var a = 0, b = items.length; a < b; a++) {
 
 	mex[a] = new Array(2);

	testo = unescape(leggiContenuto(items[a], "text"));
	
	mitt = unescape(leggiContenuto(items[a], "mitt"));
	
	data = leggiContenuto(items[a], "date");
	
	for (var i = 0; i < smile[0].length; i++){
		var espr = new RegExp(smile[0][i],"g");
			testo = testo.replace(espr, '<img src="/forum/images/smilies/'+smile[1][i]+'" style="border:none" alt="'+smile[1][i]+'" />');
	}
	
	if(mitt != "")
		mitt = '(<a href="/forum/member.php?action=viewpro&member='+mitt+'" title="Data di invio: '+data+'"><b>'+mitt+'</b></a>)';
	
	mex[a][1] = data.split(' ').join('').split(':').join('').split('-').join('');
  
	mex[a][2] = "&nbsp;"+testo+' '+mitt;

 }
	
 return mex;
}



function ordina(sms, web){

	var s = 0, 
	w = 0,
	ordine = new Array();

	for (var i = 0; s < sms.length && w < web.length; i++){

		if(sms[s][1] < web[w][1]){
			ordine[i] = web[w][2];
			w++;
			continue;
		}
		else{
			ordine[i] = sms[s][2];
			s++;
		}	
	}
	
	for (var k = i; s == sms.length && w < web.length; k++, w++)
		ordine[k] = web[w][2];
		
	for (var j = k; s < sms.length && w == web.length; j++, s++)
		ordine[j] = sms[s][2];	
		
	return ordine;
}




function caricaTesto(nomeFile) {

	var kalaMex = prendiElementoDaId("cCMar");
	
  var arrTmp;
  
  var ajax = assegnaXMLHttpRequest();


  if(ajax) {

    ajax.open("GET", nomeFile, true);

    ajax.setRequestHeader("connection", "close");
		ajax.setRequestHeader("Content-Type", "text/xml");
 		ajax.setRequestHeader("Cache-Control", "no-cache");

 
    ajax.onreadystatechange = function() {

		  if(ajax.readyState === readyState.COMPLETATO) {
    
    		if(statusText[ajax.status] === "OK") {

      		if(ajax.responseXML){

        		arrTmp = leggiMex(ajax.responseXML);

      		}
      		else{
      		
        		arrTmp[0][2] = ajax.responseText;
        	
      		}
      
      		current = 0;
      
      		if (arrTmp[0][1] === "0"){
      			
        		nodechange(kalaMex, "<nobr>"+contents.toString()+"</nobr>");
        		caric();
      		}
      		else{
      		
      			//if(running == false)
						//	tickerstart();
						//var kalaMex = prendiElementoDaId("cCMar");
						nodechange(kalaMex, "<nobr>"+contents.toString()+"</nobr>");
      		}
    		}
    		else{
    
      		arrTmp[0][2] = "Impossibile effettuare l'operazione richiesta. " + "Errore riscontrato: " + statusText[ajax.status];
    		}
    		
    		if (nomeFile.substr(0,30) == "http://www.kalauras.it/SMS/Mex"){
    			arrSMS = arrTmp;
    			smsOK = true;
    		}
    		else if (nomeFile.substr(0,36) == "http://www.kalauras.it/forum/Mex_web"){
      		arrWEB = arrTmp;
      		webOK = true;
    		}
    		if(smsOK && webOK){
    			
    			contents = ordina(arrSMS, arrWEB);
    		
					nodechange(kalaMex, "<nobr>"+contents.toString()+"</nobr>");
					//document.getElementById("cMar").style.height = (obj.offsetHeight+5)+"px";
						
    			smsOK = false;
    			webOK = false;
    		}
    	
  		}
		}

 
    ajax.send(null);
  }
   
  return null;
} 




function scrivi(questo) {

		var newmess = prendiElementoDaId("newmex");
		
		if(prendiElementoDaId(questo).style.display == "none"){
			aggiorna();
			nodechange(newmess, "Nascondi");
			prendiElementoDaId(questo).style.display = "block";
		}
		else{
		
			nodechange(newmess, "<b>New Message</b>");
			prendiElementoDaId(questo).style.display = "none";
		}

}


function vai(pos){

	var newpos = current+pos-1;

	if(newpos == -1)
		current = contents.length-1;
	else if(newpos == -2)
		current = contents.length-2;
	else if(newpos >= contents.length)
		current = 0;
	else	
		current = newpos;
	
	tickerrun();
	window.clearInterval(runid);
	runid = window.setInterval(tickerrun, delay, '');
}


function newMessage(){

	var nick = prendiElementoDaId("nick").value;
	var testo = prendiElementoDaId("testo").value;

	if(nick.toUpperCase() == "SMS" || nick == "")
		alert("Per lasciare un messaggio devi loggarti al forum!");
	else if (testo == "")
		alert("Testo non valido!");
	else{
		testo = testo.replace("#", "").replace("<", "").replace(">", "");
		caricaTesto("/forum/newMessage.php?nick="+escape(nick)+"&testo="+escape(testo)+"&r="+Math.random());
		alert("Messaggio inviato!");
	
		window.focus();
	}
	
}


function aggiorna(){

caricaTesto("/SMS/ws.php?t="+Math.random());
window.focus();

}



function caric(){

	if(!obj){
		startMarquee();
		window.onblur = function(){ clearTimeout(idMar); contr = -1;}
		window.onfocus = function(){if (contr == -1){marquee(); contr = 0; }}
	}

	caricaTesto("http://www.kalauras.it/SMS/Mex.xml?t="+Math.random());
	caricaTesto("http://www.kalauras.it/forum/Mex_web.xml?t="+Math.random());

	obj.style.left=(cWid+offset)+"px";

	if(prendiElementoDaId("message"))
		loadEls();
}

