//HTML Message Editor 
//by Heracleum 2004

var usag = navigator.userAgent.toLowerCase();
var is_opera = (usag.indexOf('opera') != -1);
var is_ie = ((usag.indexOf('msie') != -1) && (!is_opera));
var is_ie4 = ((is_ie) && (usag.indexOf("msie 4.") != -1));
var is_moz = ((navigator.product == 'Gecko'));


function previewPost(){
 var msgtxt = document.frm.txtcode.value;
 if (window.hme_hide_br)
  msgtxt = msgtxt.replace(/\n/g, "<br>");
 var pwin = window.open('','pwin','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,top=100,left=100,width=426,height=400'); 
 pwin.document.write('<html><head><title>Anteprima messaggio</title><link rel="stylesheet" href="skin/super.css"></head>\n');
 pwin.document.write('<body style="font-weight:normal"><div class="commentcontainer">Anteprima messaggio:<div class="commentbody">');
 pwin.document.write(msgtxt + '</div>');
 pwin.document.write('</div>(questo testo non deve apparire n&eacute; corsivo n&eacute; grassetto!)</body></html>');
 pwin.document.close();
 pwin.focus();
}

function frameTitle(title, iconsrc, iconmargin, id, show, w, pos, pane){
 var str = '';
 if (pos=="popup")
  pos = ' position:absolute; bottom:32px; right:0px;';
 str += '<div id="'+ id +'" style="visibility:'+((show)?'visible':'hidden')+';'+ pos +' width:'+ w +'; background-color: threedface; border-width: 2px; border-style: outset; padding: 2px;">';
 str += '<div style="width:100%; color:CaptionText; background-color: ActiveCaption; margin-bottom: 1px;"><nobr><img src="http://'+iconsrc+'" align=absmiddle style="margin:'+ iconmargin +';"> <b> '+ title +'</b></nobr></div>';
 if (pane)
  str += ' <div class="gui" style="padding: 2px;">';
 return str;
}

function iconSet(title, ifrom, ito, urlbase, fill, credits){
 var str = '';
 str += '<div style="font-size: 20px; border-top: 2px groove; padding-top: 4px; padding-bottom: 4px;">';
 str += '<span style="font-size: 12px">'+title+': </span>';
 var emopic;
 for (var i=ifrom;i<ito+1;i++){
  emopic = urlbase + ((i<10 && fill)? "0"+i : i) +'.gif';
  str += getTButton(emopic, "addSmile('"+emopic+"');", emopic, 'padding:2px;');
 }
 if (!credits){
  credits = urlbase.substring(0, urlbase.indexOf("/",8));
  str += '<br><div align=right style="font-size: 10px">di pubblico dominio - dal sito: <a target=_blank href='+credits+'>'+credits+'</a></div>';
 }else 
  str += '<br><div align=right style="font-size: 10px">credits: <a target=_blank href='+credits+'</a></div>';
 str += '</div>';
 return str;
}

function showEmotiCat(sel){  
 var epane = document.getElementById("emopane");
 if (sel == null)
  epane.innerHTML = '';
 else{
  var opt = sel.options[sel.selectedIndex];
  epane.innerHTML = eval('iconSet("'+ opt.text +'", '+ opt.value +');');
 }
}

//toolbar

function getTTextButton(txt, eventh, ttip, bstyle){
if (!bstyle)
 bstyle='';
else 
 bstyle='style="'+bstyle+'" ';
if (txt.charAt(txt.length-1) != '>') 
 txt = '<span style="vertical-align: middle; font-size: 12px;"><nobr>'+txt+'</nobr></span>';
var ret = '<div style="display:inline;" class=tb_button onclick="'+eventh+'" '+bstyle;
ret += ' onmousedown="buttonDown(this)" onmouseup="buttonOver(this, true)" onmouseover="buttonOver(this, true)" onmouseout="buttonOver(this, false)"';
ret += ' title="'+ttip+'" unselectable="on">'+txt+'</div>';
return ret;
}
function createTTextButton(txt, eventh, ttip, bstyle){
 document.write(getTTextButton(txt, eventh, ttip, bstyle));
} 

function getTButton(imgurl, eventh, ttip, bstyle){
 return getTTextButton('<img src='+imgurl+' align=absmiddle unselectable="on">', eventh, ttip, bstyle); 
}
function createTButton(imgurl, eventh, ttip, bstyle){
 createTTextButton('<img src='+imgurl+' align=absmiddle unselectable="on">', eventh, ttip, bstyle); 
}

function createTSeparator(groove){
document.write('<div style="display:inline;" class="'+((groove)?'tb_handle':'tb_separator')+'"></div>');
}

function buttonOver(img, isover){
 img.className = (isover)? "tb_raised" : "tb_button"; 
}
function buttonDown(img){
 img.className = "tb_pressed";
}




//eventi cursore
 function textareaEvent(kb, e){
  if (popup == null || popup == document.getElementById("popupsmile") ){
   if (kb==1)
    checkEnter(e);
   storeCaret();
  }
 }

 function checkEnter(e){
  if (!window.hme_hide_br && document.frm.autobr.checked){
   if(window.event){
    if(window.event.keyCode==13)
     addText('<br>\n', '');
   }else if(e && e.which==13){
    addText('<br>\n', '', true, '\n');
   }
  }
 }     

 function storeCaret() {
   var ta = document.frm.txtcode;
   if (ta.createTextRange) 
     ta.caretPos = document.selection.createRange().duplicate();
 }

 function isOK(){
  if (document.selection)
   return true;//(document.selection.createRange().parentElement() == document.getElementById("txtcode"));
  else
   return true;
 }
     
//inserimento tag

 var tags = new Array("b", "i", "u");
 var popup;

 function addTag(ti){
  addText('<'+tags[ti]+'>', '</'+tags[ti]+'>');
 }

  function addText(before, after, overwrite, del) {
    var ta = document.frm.txtcode;
    var text = '';
    if (document.selection){
      if (overwrite)
	document.selection.createRange().text = '';
      text = document.selection.createRange().text;
      if (text != '' && isOK()) {
	document.selection.createRange().text = before + text + after;
      }else{
        var caretPos = ta.caretPos;
        if (!caretPos){
         ta.focus();
         storeCaret();
         caretPos = ta.caretPos;
        }
	text = before + after;
	var ct = (overwrite)?'':caretPos.text;
	caretPos.text = (caretPos.text.charAt(caretPos.text.length-1)==' ') ? text + ct + ' ' : text + ct;
	caretPos.move('character', -after.length);
	caretPos.select(); 
      }
      ta.focus();
    }else if(ta.setSelectionRange){
      var ss = (del && ta.selectionStart>=del.length)?ta.selectionStart-del.length : ta.selectionStart;
      var se = ta.selectionEnd;  var sl = ta.textLength;
      var res = ta.value.substring(0, ss) + before;
      var selText = ta.value.substring(ss, se);
      if (!overwrite)
       res += selText;
      res += after + ta.value.substring(se, sl);
      ta.value = res;
      ta.focus();
      if (ss != se && !del){
        ta.setSelectionRange(ss, ((overwrite)?ss:se) + before.length + after.length);
      }else{
	var dlen = (del)? del.length: 0;
        ta.setSelectionRange(se + before.length -dlen, se + before.length -dlen);
      }
    }else{
     ta.value = before + after;
     ta.focus();
    }
  }

 function getSelText(){
  var ta = document.frm.txtcode;
  if (document.selection)
   return document.selection.createRange().text;
  else if (ta.setSelectionRange)
   return ta.value.substring(ta.selectionStart, ta.selectionEnd);
  else 
   return '';
 }

 function insertCol(f){
  popupClose();
  var coltag = '<font style="';
  if (f._forec.value.length > 0)
   coltag += 'color:'+f._forec.value+'; ';
  if (f._backc.value.length > 0)
   coltag += 'background-color:'+f._backc.value+'; ';
  if (f._font.selectedIndex > 0)
   coltag += 'font-family:'+f._font.options[f._font.selectedIndex].value+';';
  if (f._size.selectedIndex > 0)
   coltag += 'font-size:'+f._size.options[f._size.selectedIndex].text+';';
  addText(coltag+'">'+getSelText(), '</font>', true);
 }

 function popupCol(){
  if (popup != null)
   popupClose();
  popup = document.getElementById("popupcol");
  popup.style.visibility = "visible";
  document.frmcol._forec.focus();
 }  

 function insertImg(f){
  popupClose();
  var imgtag = '<img src="'+f._src.value+'"';
  if (f._title.value.length > 0)
   imgtag += ' title="'+f._title.value+'"';
  if (f._border.value.length > 0)
   imgtag += ' border='+f._border.value;
  if (f._align.selectedIndex > 0)
   imgtag += ' align='+f._align.options[f._align.selectedIndex].value;
  if (f._width.value.length > 0)
   imgtag += ' width='+f._width.value;
  if (f._height.value.length > 0)
   imgtag += ' height='+f._height.value;
  if (f._space.value.length > 0)
   imgtag += ' hspace='+f._space.value+' vspace='+f._space.value;
  addText(imgtag+'>', '', true);
 }

 function popupImg(){
  if (popup != null)
   popupClose();
  popup = document.getElementById("popupimg");
  popup.style.visibility = "visible";
  var dsel = getSelText();
  if (dsel.length>0){
   if (dsel.indexOf("http://")==0)
    document.frmimg._src.value = dsel;
   else
    document.frmimg._title.value = dsel;
  }
  document.frmimg._src.focus();
 }  

 function insertLink(f){
  popupClose();
  var linktag = '<a href="'+f._href.value+'"';
  if (f._title.value.length > 0)
   linktag += ' title="'+f._title.value+'"';
  if (f._target.checked)
   linktag += ' target=_blank';
  linktag += '>'+ f._text.value;
  addText(linktag, '</a>', true);
 }

 function popupLink(){
  if (popup != null)
   popupClose();
  popup = document.getElementById("popuplink");
  popup.style.visibility = "visible";
  var dsel = getSelText();
  if (dsel.length>0){
   if (dsel.indexOf("http://")==0){
    document.frmlink._href.value = dsel;
    document.frmlink._text.value = dsel;
   }else
    document.frmlink._text.value = dsel;
  }
  document.frmlink._href.focus();
 }  

 function addSmile(pic){ 
  addText('<img src="'+pic+'">', '', true);
 }

 function popupSmile(){
  if (popup != null)
   popupClose();
  showEmotiCat(document.frmsmile.emocat);
  popup = document.getElementById("popupsmile");
  popup.style.visibility = "visible";
 }  

 function popupClose(){
  popup.style.visibility = "hidden";
  popup = null;
 }