/*
Versione 1.4
Possibili dati restituiti dalla classe
responseText= html generico restituito
responseXML = XML generico restituito
stato       = stato attuale richiesta (intero da 0=inattivo a 4=completato)
se eseguiJs=true esegue codice javascript restituito dal file remoto
se settato oggDest scrive html restituito dal file remoto direttamente nell'oggetto (Senza usare funzione completato())

RICHIESTE
.metodo   = "GET" o "POST" (� una stringa di testo)
.fileDati = file richiamato
.parametri= query al file
.nocache  = Solo per metodo GET. se true accoda all'url la varibile settata tramite ".nocachepar" contente il timestamp attuale. Con il POST bisogna usare la gestione via funzione php header() della cache
.nocachepar = nome variabile accodata all'url per evitare caching.
.eseguiJs = se true esegue codice javascript
.oggDest  = oggetto HTML. scrittura diretta su un tag HTML specificato senza uso di altre funzioni

Funzioni richiamabili all'esterno
inizializzato()
richiesta()
completato()
noSuppAjax()
errore(codice,descr)
*/
function classeAjax(){
	this.fileDati="";//file oggetto richiesta
	this.parametri="";//parametri richiesta (es file=testo.txt)
    this.metodo="GET";//GET o POST
    this.nocache=true;
    this.nocachepar="rdm";//Nome del Parametro aggiunto alla query per evitare caching del browser (viene usata la data in secondi) - usare un nome mai utilizzato tra i parametri inviati
    this.eseguiJs=false;//Se il testo in arrivo � javascript lo posso eseguire al volo
    //funzioni di stato Ajax - per far eseguire fuorni dalla classe funzioni varie
	this.inizializzato=function(){return}; //stato=1
	this.richiesta=function(){return};     //stato=2
	this.risposta=function(){return};      //stato=3
	this.completato=function(){return};    //stato=4
    this.noSuppAjax=function(){return};    //Ajax non supportato
    this.errore=function(codice,descr){return};        //Errore connessione
    this.stato=0;
    this.abortisci=function(){
        if(this.ajax){//se ajax creato correttamente
            this.ajax.onreadystatechange=function(){return};// riassego onreadystatechange ad una funzione vuota, poich� quest'evento sar� sollevato chiamando il metodo abort()
            this.ajax.abort();
        }
    };
	this.creaAjax=function(){//crea l'oggetto
        this.riuscito=true;
        if(typeof(XMLHttpRequest)==="function"||typeof(XMLHttpRequest)==="object"){
            this.ajax=new XMLHttpRequest();
        }else if(window.ActiveXObject && navigator.userAgent.toUpperCase().indexOf("MSIE 4")<0){
    		if(navigator.userAgent.toUpperCase().indexOf("MSIE 5")<0){
    			this.ajax=new ActiveXObject("Msxml2.XMLHTTP");
    		}else{
    			this.ajax=new ActiveXObject("Microsoft.XMLHTTP");
    		}
        }else{
            this.riuscito=false;
        }
        return this.riuscito;
    };
	this.eseguiAjax=function(){//richiesto dall'esterno - fa partire la richiesta vera e propria
		if(this.riuscito){
			if(this.ajax){//se ajax creato correttamente
                var self=this;//devo recuperare "this" in modo asincrono
                if(this.metodo=="GET"){
                    if(this.nocache){param=this.nocachepar+"="+new Date().getTime()+"&"+this.parametri}else{param=this.parametri}
    				this.ajax.open("GET",this.fileDati+"?"+param,true);//open (method,uri[,async][,user][,password])
                    this.ajax.setRequestHeader("connection","close");// rimozione dell'header "connection" come "keep alive"
                    this.ajax.send(null);
                }else{
                    this.ajax.open("POST",this.fileDati,true);
                    this.ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    this.ajax.overrideMimeType('text/html');
                    this.ajax.setRequestHeader("Content-length", this.parametri.length);
                    this.ajax.setRequestHeader("Connection", "close");
                    this.ajax.send(this.parametri);
                }
                this.ajax.onreadystatechange=function(){
                    self.stato=self.ajax.readyState;
                    switch(self.ajax.readyState){//rihiamo eventuali funzioni esterne - per debug o messaggi all'utente
                		case 1://inizializzazione
							self.inizializzato();
						    break;
						case 2://richiesta
							self.richiesta();
						    break;
						case 3://risposta
							self.risposta();
						    break;
						case 4://completato
                            //uso self perch� this non sarebbe definito (� ascincrono...)
                            if(self.ajax.status==200){//OK
    							self.rispostaTesto=self.ajax.responseText;//se ho chiesto testo
    							self.rispostaXml=self.ajax.responseXML;//se ho chiesto xml (non testato)
                                if(self.eseguiJs){eval(self.ajax.responseText)};//compilo il javscript se serve
                                if(self.oggDest){//scrittura diretta su un tag HTML specificato
    								var tag="";
                                    try{tag=self.oggDest.nodeName.toLowerCase()}catch(e){};
    								if(tag=="input"||tag=="select"||tag=="option"||tag=="textarea"){
    									self.oggDest.value=self.ajax.responseText;
    								}else{
    									self.oggDest.innerHTML=self.ajax.responseText;
    								}
    							}
    							self.completato();//eventuale funzione esterna
                            }else{
                                self.errore(self.ajax.status,self.ajax.statusText);
                            }
						    break;
					}
				};
			}
		}else{
            this.noSuppAjax();
        }
	};
    this.creaAjax();
}


////////////////////////////////////////////////////////////////////////////////

function confirmLink(theLink, msg)
{
    var is_confirmed=confirm(msg);
    if(is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
}

function conf(link,msg){
  if(confirm(msg))
    {
	location.href=link;
	}
  }

function current_rating(id, rating) {
  post_id = id;
  post_rating = rating;
  var images_path='skins/default_skin/ita/images';
  for(i = 1; i <= rating; i++)
    {
    document.images['rating_' + post_id + '_' + i].src = images_path + '/rating_over.gif';
    }
  }

function ratings_off(rating_score, insert_half) {
  var ratings_max=5;
  var images_path='skins/default_skin/ita/images';
  for(i = 1; i <= ratings_max; i++)
    {
    if(i <= rating_score)
      {
      document.images['rating_' + post_id + '_' + i].src =  images_path + '/rating_on.gif';
      }
      else if(i == insert_half)
      {
      document.images['rating_' + post_id + '_' + i].src =  images_path + '/rating_half.gif';
      }
      else
      {
      document.images['rating_' + post_id + '_' + i].src =  images_path + '/rating_off.gif';
      }
    }
  }

function toggleLayer(whichLayer) {
  if (document.getElementById)
    {
    // this is the way the standards work
    var style2 = document.getElementById(whichLayer).style;
    style2.display = style2.display? "":"block";
    }
    else if (document.all)
    {
    // this is the way old msie versions work
    var style2 = document.all[whichLayer].style;
    style2.display = style2.display? "":"block";
    }
    else if (document.layers)
    {
    // this is the way nn4 works
    var style2 = document.layers[whichLayer].style;
    style2.display = style2.display? "":"block";
    }
  }
