var editor;
var httpRequest;

// classe per la codifica/decodifica UTF8
var Utf8 = {
    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

function newEditor(name, level, style) {
   eval("window.h_edi_"+name+" = new H_editor('"+name+"',"+level+");");
   eval("h_edi_"+name+".edit('"+style+"')");
}

// --- costruttore della classe H_editor ---
function H_editor (id, level) {
   
   // --- Inizializza le proprietà
   this.status = false;
   this.id = id;
   this.level = level;
   this.docId = 'h_edi_'+id;
   this.code = '';

   // --- Inizializza i metodi ---
   this.edit = editArea;
   this.save = saveArea;
}

// --- metodi della classe H_editor ---
function editArea(style){
   if(this.status) return;

// Recupero via Ajax del block-code originale
   editor = this;
   getCode('POST','?','h_getcode='+this.id+'&h_level='+this.level);

// Costruzione della TEXTAREA per l'edit del block-code
   var text = '<textarea id="'+this.docId+'_txtarea" onDblClick="event.cancelBubble = true;" onBlur="eval(\''+this.docId+'.save()\');" wrap="off" style="'+style+'">';
   text += this.code;
   text += '</textarea>';
   text += '<br/>';
   text += '<div class="edit_area"><input type="button" onClick="eval(\''+this.docId+'.save()\');" value="modifica" class="button"/></div>';
  
// Scrittura della TEXTAREA
   document.getElementById(this.docId).innerHTML = text;

// Attivazione dello stato 'IN EDIT'
   this.status = true;
}

function saveArea(){
   if(!this.status) return;
   var content="";
   if (document.all) content = document.getElementById(this.docId+'_txtarea').innerHTML;
   else content = document.getElementById(this.docId+'_txtarea').value;
   if (document.all) document.getElementById(this.docId).innerHTML = content;
   else document.getElementById(this.docId).value= content;
   editor = this;
   pars = 'h_setcode='+this.id+'&h_level='+this.level+'&h_bodycode='+escape(Utf8.encode(content));

   send('POST', '?', true, pars); // Richiesta Ajax ASINCRONA
}
// FINE metodi della classe 'H_editor'


// Metodo CALLBACK request AJAX
function setCode() {
   if(httpRequest.readyState == 4 && httpRequest.status == 200) {
         var text = unescape(httpRequest.responseText);
         editor.code = text;
   }
}

// Metodo CALLBACK request AJAX
function displayArea() {
   if(httpRequest.readyState == 4 && httpRequest.status == 200) {
         var text = unescape(httpRequest.responseText);
         document.getElementById(editor.docId).innerHTML = text;
         editor.status = false;
   }
}

// metodo di LANCIO request AJAX
function getCode(mode, url, pars) {
      httpRequest = new XMLHttpRequest();

      httpRequest.onreadystatechange=setCode;
      httpRequest.open(mode,url,false)
      httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      httpRequest.send(pars)
}

// metodo di LANCIO request AJAX
function send(mode, url, flag, pars) {
      httpRequest = new XMLHttpRequest();

      httpRequest.onreadystatechange=displayArea
      httpRequest.open(mode,url,flag)
      httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      httpRequest.send(pars)
}

function writetostatus(text){
    window.status = text;
    return true;
}