function bbcode_ins(fieldId, tag)
{
	field=document.getElementById(fieldId);
	if(tag=='b' || tag=='i' || tag=='u')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + ']' + selected + '[/' + tag+']';
		}
		
		//MOZILLA/NETSCAPE/SAFARI support
		
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + ']' + selected + '[/' + tag +']' + field.value.substring(endPos, field.value.length);
		}
}
	else if(tag == 'img')
	{
		var path = prompt('Enter image path', 'http://');
		if(!path)
		{
			return;
		}
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + ']' + path + '[/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + ']' + path + '[/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
	else if(tag == 'url')
	{
		var url = prompt('Enter link URL', 'http://');
		var linkText = prompt('Enter link text', '');
		if(!url || !linkText)
		{
			return;
		}
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + '='+url+']' + linkText + '[/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + '='+url+']' + linkText + '[/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
	else //For smilies
	{
		if (document.selection) 
		{
		field.focus();
		sel = document.selection.createRange();
		sel.text = tag;
		}
		
		//MOZILLA/NETSCAPE/SAFARI support
		
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.focus();
		field.value = field.value.substring(0, startPos) + tag + field.value.substring(endPos, field.value.length);
		}
	}
}

  var iconPath="/themes/inlove/images/editor/";
  var iconWidth=23;
  var iconHeight=25;
  var sHTML = ""
  
  function printButton(array,id){
  		for (i = 0; i < array.length; i++){
			document.getElementById(id).innerHTML = document.getElementById(id).innerHTML + writeButtonToggle(array[i][0],array[i][1],array[i][2],array[i][3],array[i][4])
		}
  }
  
  function writeButtonToggle(id,img,title,txtarea,tagin)
  {
	  w = iconWidth;
	  h = iconHeight;
	  imgPath=iconPath+img;
	  var 	sHTML = ""
  			sHTML = sHTML + "<td style='padding:0px;padding-right:0px;VERTICAL-ALIGN: top;margin-left:0;margin-right:0px;margin-bottom:0px;width:"+w+"px;height:"+h+"px;'>";
  			sHTML = sHTML + "<div name=\""+id+"\" id=\""+id+"\" style='background:url("+imgPath+") 0px 0px;width:"+w+"px;height:"+h+"px;' ";
  			sHTML = sHTML + "onmouseover='doOver(\""+id+"\")' ";
  			sHTML = sHTML + "onmouseout='doOut(\""+id+"\")' ";
  			sHTML = sHTML + " title=\""+title+"\" onclick=\"bbcode_ins('"+txtarea+"','"+tagin+"')\"></div>";
  			sHTML = sHTML + "</td><td><img src=\""+iconPath+"/brkspace.gif\"></td>";
  return sHTML;
  }
 
  function printSmile(array,id){
  		for (i = 0; i < array.length; i++){
			document.getElementById(id).innerHTML = document.getElementById(id).innerHTML + writeSmileToggle(array[i][0],array[i][1],array[i][2],array[i][3],array[i][4])
		}
  }
  
  function writeSmileToggle(id,img,title,txtarea,tagin)
  {
	  w = iconWidth;
	  h = iconHeight;
	  imgPath=img;
	  var sHTML = ""
	  sHTML = sHTML + "<td style='padding:0 4px 5px 0;VERTICAL-ALIGN: bottom;margin-left:0;margin-right:0px;margin-bottom:3px;'>";
	  sHTML = sHTML + "<img name=\""+id+"\" id=\""+id+"\" src='"+imgPath+"'";
	  sHTML = sHTML + "  title=\""+title+"\" onclick=\"bbcode_ins('"+txtarea+"','"+tagin+"')\">";
	  sHTML = sHTML + "</span></td>";
	  return sHTML;
  }

  
function doOver(btn)
 {
  btn = document.getElementById(btn)
     newposition = eval("'0px "+-iconHeight+"px'")
  if(btn) btn.style.backgroundPosition = newposition;//no.2
 }
 
 function doOut(btn)
 {
  btn = document.getElementById(btn)
  if(btn) btn.style.backgroundPosition = '0px 0px'//no.1 (remain inactive)
 }