

function bbcodeHandler(formName, areaName) {
  this.formName = formName;
  this.areaName = areaName;
  this.field = '';
}


bbcodeHandler.prototype.getField = function() {
  if(this.field == '') {
    var form = document.forms[this.formName];
    var childs = form.getElementsByTagName('textarea');
    for(var i =0; i<childs.length; i++) {
      if(childs[i].name == this.areaName) {
        this.field = childs[i];
        break;
      }
    }
  }
  return this.field;  
}


bbcodeHandler.prototype.setBold = function() {
  this.setStyle('[b]','[/b]');
  return false;
}

bbcodeHandler.prototype.setItalic = function() {
  this.setStyle('[i]','[/i]');
}

bbcodeHandler.prototype.setUnderline = function() {
  this.setStyle('[u]','[/u]');
}

bbcodeHandler.prototype.setLeft = function() {
  this.setStyle('\r[left]','[/left]\r');
}

bbcodeHandler.prototype.setCen = function() {
  this.setStyle('\r[cen]','[/cen]\r');
}

bbcodeHandler.prototype.setRight = function() {
  this.setStyle('\r[right]','[/right]\r');
}

bbcodeHandler.prototype.setJst = function() {
  this.setStyle('\r[jst]','[/jst]\r');
}

bbcodeHandler.prototype.setOL = function() {
  this.setStyle('\r[ol][li]','[/li][/ol]\r');
}

bbcodeHandler.prototype.setTable = function() {
  this.setStyle('' ,'\r[table]\r[tr]\r  [td][/td][td][/td]\r[/tr]\r[tr]\r  [td][/td][td][/td]\r[/tr]\r[/table]\r');
}

bbcodeHandler.prototype.setUL = function() {
  this.setStyle('\r[ul][li]','[/li][/ul]\r');
}

bbcodeHandler.prototype.setLink = function() {
  this.setStyle('[url title="titolo" href="http://..." ]nome da cliccare','[/url]');
}

bbcodeHandler.prototype.setMail = function() {
  this.setStyle('[mail to="address@host" ]nome da cliccare[/mail]','');
}

bbcodeHandler.prototype.setImg = function(imageid, thumb) {
  if(thumb == '1')
  	this.setStyle('[img id="'+imageid+'" thumb="1"][/img]','');
  else
	this.setStyle('[img id="'+imageid+'"][/img]','');
}

bbcodeHandler.prototype.setH = function(formSelect) {
  if(formSelect.selectedIndex != 0) {
    startTag = '['+formSelect.options[formSelect.selectedIndex].value+']';
    endTag = '[/'+formSelect.options[formSelect.selectedIndex].value+']';
    formSelect.selectedIndex = 0;
    this.setStyle(startTag, endTag);
  }
}

bbcodeHandler.prototype.setColor = function(color) {
  this.setStyle('[color color="#'+color+'"]','[/color]');
}

bbcodeHandler.prototype.setCollapse = function() {
  this.setStyle('[collapse trigger="Altre informazioni"]','[/collapse]');
}



bbcodeHandler.prototype.setStyle = function(startTag, endTag) {
  var field = this.getField();
  if(document.selection) {
  	field.focus();
    range = document.selection.createRange();
    range.text = startTag + range.text + endTag;
    //scroll text area to 
  	range.collapse();
  	range.select();
  }
  else if((document.getSelection) && (field.selectionStart || field.selectionStart=='0')) {
  	field.focus();
  	
    var startPos = field.selectionStart;
    var endPos = field.selectionEnd;
    field.value = field.value.substring(0, startPos)
                  + startTag + field.value.substring(startPos,endPos) + endTag
                  + field.value.substring(endPos, field.value.length);
    
    
    field.selectionStart = startPos + startTag.length+(endPos-startPos);
    field.selectionEnd = startPos + startTag.length+(endPos-startPos);
        
    var currLine = this.getLineCount(field.value.substring(0,field.selectionStart));
    var totLine = this.getLineCount(field.value);
    
    field.scrollTop = Math.floor(field.scrollHeight / totLine * (currLine-1));
    
  }
}


bbcodeHandler.prototype.getLineCount = function(text) {
  var linePattern = /.*(\r\n|\r|\n)/g;
  linePattern.lastIndex = 0;
  var lineCount = 0;
  var match, index=0, matchText='';
  while ((match = linePattern.exec(text))) {
    lineCount++;
    index = match.index;
    matchText = match[0];
  }
  if (matchText.length && text.length && (index + matchText.length < text.length)) {
    lineCount++;
  }
  return lineCount;
}


bbcodeHandler.prototype.showPreview = function(destination) {
  var field=this.getField();
  var w = window.open(destination,'','width=512, height=384,status=1,dependent=1');
}
