function editor_generate(objname,w,h) {


  var imgURL = _editor_url + 'images/';      

  var obj    = document.all[objname];
  if (!w) {
    if      (obj.style.width) { width = obj.style.width; }     
    else if (obj.cols)        { width = (obj.cols * 8) + 22; }  
    else                      { width = '100%'; }               
  }
    if (!h) {
    if      (obj.style.height) { height = obj.style.height; }  
    else if (obj.rows)         { height = obj.rows * 17 }       
    else                       { height = '200'; }             
  }

  var Agent, VInfo, MSIE, Ver, Win32, Opera;
  Agent = navigator.userAgent;
  VInfo = Array();                              
  VInfo = Agent.split(";")
  MSIE  = Agent.indexOf('MSIE') > 0;
  Ver   = VInfo[1].substr(6,3);
  Win32 = Agent.indexOf('Windows') > 0 && Agent.indexOf('Mac') < 0 && Agent.indexOf('Windows CE') < 0;
  Opera = Agent.indexOf('Opera') > -1;
  if (!MSIE || Opera || Ver < 5.5 || !Win32) { return; }

  var editor = ''
  + '<table border=1 cellspacing=0 cellpadding=0 bgcolor="buttonface" style="padding: 1 0 0 0" width=' + width + ' unselectable="on"><tr><td>\n'
  + '<table border=0 cellspacing=2 cellpadding=0 bgcolor="buttonface" style="float: left;"  unselectable="on">\n'
  + ' <tr>\n'
  + '  <td style="border-width: 0; padding: 2 0 0 3;" align=right dir=ltr>\n'
  + '   <select id="_' +objname+ '_FontName" onChange="editor_action(this.id)" unselectable="on">\n'
  + '   <option value="Tahoma, Unikurd Web">Tahoma</option>\n'
  + '   <option value="Unikurd Web, Tahoma">Unikurd Web</option>\n'
  + '   <option value="wingdings">WingDings</option>\n'
  + '   </select>'
  +    '<select id="_' +objname+ '_FontSize" onChange="editor_action(this.id)" style="width:38px"  unselectable="on">\n'
  + '   <option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option><option value=6>6</option><option value=7>7</option>\n'
  + '   </select>\n\n'
  +    '<button title="Bold" id="_' +objname+ '_Bold" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'format_bold.gif" unselectable="on"></button>'
  +    '<button title="Italic" id="_' +objname+ '_Italic" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'format_italic.gif" unselectable="on"></button>'
  +    '<button title="Underline" id="_' +objname+ '_Underline" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'format_underline.gif" unselectable="on">\n'
//+    '<button title="Strikethrough" id="_' +objname+ '_StrikeThrough" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'format_strike.gif" unselectable="on"></button>'
//+    '<button title="Superscript" id="_' +objname+ '_SuperScript" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'format_sup.gif" unselectable="on">\n'
//+    '<button title="Subscript" id="_' +objname+ '_SubScript" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'format_sub.gif" unselectable="on"></button>'
  +    '<button title="Justify Left" id="_' +objname+ '_JustifyLeft" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'align_left.gif" unselectable="on"></button>'
  +    '<button title="Justify Center" id="_' +objname+ '_JustifyCenter" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'align_center.gif" unselectable="on"></button>'
  +    '<button title="Justify Right" id="_' +objname+ '_JustifyRight" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'align_right.gif" unselectable="on">\n'
  +    '<button title="Ordered List" id="_' +objname+ '_InsertOrderedList" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'list_num.gif" unselectable="on"></button>'
  +    '<button title="Bulleted List" id="_' +objname+ '_InsertUnorderedList" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'list_bullet.gif" unselectable="on">\n'
//+    '<button title="Decrease Indent" id="_' +objname+ '_Outdent" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'indent_less.gif" unselectable="on"></button>'
//+    '<button title="Increase Indent" id="_' +objname+ '_Indent" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'indent_more.gif" unselectable="on">\n'
  +    '<button title="Insert Table" id="_' +objname+ '_InsertTable" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'insert_table.gif" unselectable="on">\n'
  +    '<button title="Insert Web Link" id="_' +objname+ '_CreateLink" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'link.gif" unselectable="on"></button>'
  +    '<button title="Insert Image" id="_' +objname+ '_InsertImage" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'image.gif" unselectable="on">\n'
  +    '<button title="Font Color" id="_' +objname+ '_ForeColor" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'color_fg.gif" unselectable="on"></button>'
  +    '<button title="Background Color" id="_' +objname+ '_BackColor" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'color_bg.gif" unselectable="on">\n'
//+    '<button title="Horizontal Rule" id="_' +objname+ '_InsertHorizontalRule" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'hr.gif" unselectable="on"></button>'
  +    '<button title="Show Keyboard" id="_' +objname+ '_Keyboard" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'keyboard.gif" unselectable="on">\n'
  +    '<button title="View HTML Source" id="_' +objname+ '_HtmlMode" class="btn" onClick="editor_setmode(this.id)" unselectable="on"><img src="' +imgURL+ 'html.gif" unselectable="on"></button>'
//+    '<button title="About this editor" id="_' +objname+ '_about" class="btn" onClick="editor_action(this.id)" unselectable="on"><img src="' +imgURL+ 'about.gif" unselectable="on"></button>'
  + '</td></tr></table>\n'




  + '<textarea ID="_' +objname + '_editor" style="width:' +width+ '; height:' +height+ '; margin-top: -1px; margin-bottom: -1px;"></textarea>'

  + '<input type="hidden" name="' +objname+ '" value="">'
  ;

  var contents = document.all[objname].value;             
  document.all[objname].outerHTML = editor;               
  document.all['_'+objname+'_editor'].value = contents;   

  editor_setmode('_' +objname+ '_HtmlMode', 'init');      

}


function editor_action(button_id) {

  var BtnParts = Array();
  BtnParts = button_id.split("_");
  var objname    = button_id.replace(/^_(.*)_[^_]*$/, '$1');
  var cmdID      = BtnParts[ BtnParts.length-1 ];
  var button_obj = document.all[button_id];
  var editor_obj = document.all["_" +objname + "_editor"];

  if (editor_obj.tagName.toLowerCase() == 'textarea') { return; }

  var editdoc = editor_obj.contentWindow.document;
  _editor_focus(editor_obj);

  var idx = button_obj.selectedIndex;
  if (idx != null) {
    var val = button_obj[ idx ].value;
    editdoc.execCommand(cmdID,0,val);
  }

  else if (cmdID == 'ForeColor' || cmdID == 'BackColor') {

    var posX    = event.screenX;
    var posY    = event.screenY + 20;
    var screenW = screen.width;                                
    var screenH = screen.height - 20;                           
    if (posX + 232 > screenW) { posX = posX - 232 - 40; }       
    if (posY + 164 > screenH) { posY = posY - 164 - 80; }       
    var wPosition   = "dialogLeft:" +posX+ "; dialogTop:" +posY;

    var oldcolor = _dec_to_rgb(editdoc.queryCommandValue(cmdID));
    var newcolor = showModalDialog(_editor_url + "select_color.html", oldcolor,
                                   "dialogWidth:238px; dialogHeight: 187px; "
                                   + "resizable: no; help: no; status: no; scroll: no; "
                                   + wPosition);
    if (newcolor != null) { editdoc.execCommand(cmdID, false, "#"+newcolor); }
  }

  else {
    if (cmdID.toLowerCase() == 'subscript' && editdoc.queryCommandState('superscript')) { editdoc.execCommand('superscript'); }
    if (cmdID.toLowerCase() == 'superscript' && editdoc.queryCommandState('subscript')) { editdoc.execCommand('subscript'); }

    if (cmdID.toLowerCase() == 'createlink'){
      editdoc.execCommand(cmdID,1);
    }

    else if (cmdID.toLowerCase() == 'insertimage'){
      showModalDialog(_editor_url + "insert_image.html", editdoc, "resizable: no; help: no; status: no; scroll: no; ");
    }

else if (cmdID.toLowerCase() == 'inserttable') 
{ 
showModalDialog(_editor_url + "insert_table.html?"+objname, 
window, 
"resizable: yes; help: no; status: no; scroll: no; "); 
} 

else if (cmdID.toLowerCase() == 'keyboard') 
{ 
showModalDialog(_editor_url + "show_keyboard.html?"+objname, 
window, 
"resizable: yes; help: no; status: no; scroll: no; "); 
} 

    else if (cmdID.toLowerCase() == 'about'){
      var html = '<HTML><HEAD><TITLE>About</TITLE>\n'
               + '<style>\n'
               + '  html,body,textarea { font-family: verdana,arial; font-size: 9pt; };\n'
               + '</style></HEAD>\n'
               + '<BODY style="background: threedface; color: #000000"  topmargin=5 leftmargin=12>\n\n'
               + '<span style="font-family: arial black, arial; font-size: 28px; letter-spacing: -2px;">Unicode Editor</span> by Kurdland.net<br>\n'
               + 'WYSIWYG (What You See Is What You Get) editor.<br>\n\n'
               + '<p>For more information visit:<br>\n'
               + '<a href="http://www.kurdland.net/sorani/editor.asp" target="_blank">http://www.kurdland.net</a><br><br>\n'
               + '</body></html>\n\n';

      var popup = window.open('', 'ColorPicker',
                  "location=no,menubar=no,toolbar=no,directories=no,status=no," +
                  "height=120,width=350,resizable=no,scrollbars=no");
      popup.document.write(html);
    }

    else {
      editdoc.execCommand(cmdID);
    }
  }

  editor_updateUI(objname);
}

function editor_updateUI(objname,runDelay) {
  var editor_obj  = document.all["_" +objname+  "_editor"];       
  if (runDelay == null) { runDelay = 0; }
  var editdoc, editEvent;
  
  if (runDelay > 0) { return setTimeout(function(){ editor_updateUI(objname); }, runDelay); }

  if (this.tooSoon == 1 && runDelay >= 0) { this.queue = 1; return; } 
  this.tooSoon = 1;
  setTimeout(function(){
    this.tooSoon = 0;
    if (this.queue) { editor_updateUI(objname,-1); };
    this.queue = 0;
    }, 333);  

  if (editor_obj.tagName.toLowerCase() == 'textarea') {                       
    document.all[objname].value = editor_obj.value;                        
    return;
  } else {                                                               
    editdoc = editor_obj.contentWindow.document;                          
    editEvent = editor_obj.contentWindow.event;
    _fix_placeholder_urls(editdoc);
    document.all[objname].value = editdoc.body.innerHTML;                   
  }

  var IDList = Array('Bold','Italic','Underline','JustifyLeft','JustifyCenter','JustifyRight','InsertOrderedList','InsertUnorderedList');
  for (i=0; i<IDList.length; i++) {                                
    var button_obj = document.all["_" +objname+ "_" +IDList[i]];    
    if (button_obj == null) { continue; }                          
    var cmdActive = editdoc.queryCommandState( IDList[i] );

    if (!cmdActive)  {                                  
      if (button_obj.className != 'btn') { button_obj.className = 'btn'; }
      if (button_obj.disabled  != false) { button_obj.disabled = false; }
    } else if (cmdActive)  {                           
      if (button_obj.className != 'btnDN') { button_obj.className = 'btnDN'; }
      if (button_obj.disabled  != false)   { button_obj.disabled = false; }
    }

  }

  var IDList = Array('FontName','FontSize');
  for (i=0; i<IDList.length; i++) {
    var cmdActive = editdoc.queryCommandState( IDList[i] );
    var button_obj = document.all["_" +objname+ "_" +IDList[i]];   
    button_obj.disabled = false;
  }

  var fontname = editdoc.queryCommandValue('FontName');
  var fontsize = editdoc.queryCommandValue('FontSize');
  if (fontname != null) { fontname = fontname.toLowerCase(); }

  var fontname_obj = document.all["_" +objname+ "_FontName"];
  if (fontname == null) { fontname_obj.value = fontname; }
  else {
    var foundfont;
    var fonts = fontname_obj.length;
    for (i=0; i<fonts; i++) {
      var thisfont = fontname_obj[i].text.toLowerCase();
      if (thisfont == fontname) {
        fontname_obj.selectedIndex = i;
        foundfont = 1;
      }
    }
    if (foundfont != 1) { fontname_obj.value = fontname; }     
  }

  var fontsize_obj = document.all["_" +objname+ "_FontSize"];
  if (fontsize == null) { fontsize_obj.value = fontsize;}
  else {
    for (i=0; i<7; i++) {
      var thissize = fontsize_obj[i].text;
      if (thissize == fontsize) { fontsize_obj.selectedIndex = i; }
    }
  }
}


function editor_setmode(button_id, mode) {

  var BtnParts = Array();
  BtnParts = button_id.split("_");
  var objname     = button_id.replace(/^_(.*)_[^_]*$/, '$1');
  var cmdID       = BtnParts[ BtnParts.length-1 ];
  var editor_obj = document.all["_" +objname + "_editor"];
  var editdoc;    


  var TextEdit   = '<textarea ID="_' +objname + '_editor" style="width:' +editor_obj.style.width+ '; height:' +editor_obj.style.height+ '; margin-top: -1px; margin-bottom: -1px;"></textarea>';
  var RichEdit   = '<iframe ID="_' +objname+ '_editor"    style="width:' +editor_obj.style.width+ '; height:' +editor_obj.style.height+ ';"></iframe>';


  if (mode == "textedit" || editor_obj.tagName.toLowerCase() == 'iframe') {
    editdoc = editor_obj.contentWindow.document;
    var contents = editdoc.body.createTextRange().htmlText;
    editor_obj.outerHTML = TextEdit;
    editor_obj = document.all["_" +objname + "_editor"];
    editor_obj.value = contents;
    editor_updateUI(objname);

    var IDList = Array('Bold','Italic','Underline','StrikeThrough','SubScript','SuperScript','JustifyLeft','JustifyCenter','JustifyRight','InsertOrderedList','InsertUnorderedList','Outdent','Indent','ForeColor','BackColor','InsertHorizontalRule','CreateLink','InsertImage','InsertTable','Keyboard');
    for (i=0; i<IDList.length; i++) {                                
      var button_obj = document.all["_" +objname+ "_" +IDList[i]];   
      if (button_obj == null) { continue; }                          
      button_obj.className = 'btnNA';
      button_obj.disabled = true;
    }

    var IDList = Array('FontName','FontSize');
    for (i=0; i<IDList.length; i++) {
      var button_obj = document.all["_" +objname+ "_" +IDList[i]];   
      if (button_obj == null) { continue; }                         
      button_obj.disabled = true;
    }

    editor_obj.onkeypress  = function() { editor_updateUI(objname); }
    editor_obj.onkeyup     = function() { editor_updateUI(objname); }
    editor_obj.onmouseup   = function() { editor_updateUI(objname); }
    editor_obj.ondrop      = function() { editor_updateUI(objname, 100); }     
    editor_obj.oncut       = function() { editor_updateUI(objname, 100); }
    editor_obj.onpaste     = function() { editor_updateUI(objname, 100); }
    editor_obj.onblur      = function() { editor_updateUI(objname, -1); }

    document.all[objname].value = editor_obj.value;

    _editor_focus(editor_obj);
  }


  else {
    var contents = editor_obj.value;

    editor_obj.outerHTML = RichEdit;
    editor_obj = document.all["_" +objname + "_editor"];

    editdoc    = editor_obj.contentWindow.document;

    editdoc.open();
    editdoc.write(''
      + '<html dir="rtl"><head>\n'
      + '<style>\n'
      + 'body { background-color: #FFFFFF; font-family: "Unikurd Web, Tahoma"; font-size: x-small; } \n'
      + '</style>\n'
      + '</head>\n'
      + '<SCRIPT language=javascript src="kurdi.js"></SCRIPT>'
      + '<body contenteditable="true" topmargin=1 leftmargin=1 onkeypress=FKeyPress() id=kurdland dir=rtl onkeydown=FKeyDown() style="FONT-SIZE: 13px; FONT-FAMILY: Unikurd Web, Tahoma">'
      + contents
      + '</body>\n'
      + '</html>\n'
      );
    editdoc.close();

    var IDList = Array('Bold','Italic','Underline','StrikeThrough','SubScript','SuperScript','JustifyLeft','JustifyCenter','JustifyRight','InsertOrderedList','InsertUnorderedList','Outdent','Indent','ForeColor','BackColor','InsertHorizontalRule','CreateLink','InsertImage','InsertTable','Keyboard');
    for (i=0; i<IDList.length; i++) {
      var button_obj = document.all["_" +objname+ "_" +IDList[i]];
      if (button_obj == null) { continue; }
      button_obj.className = 'btn';
      button_obj.disabled = false;
    }

    editdoc.onkeypress     = function() { editor_updateUI(objname); }
    editdoc.onkeyup        = function() { editor_updateUI(objname); }
    editdoc.onmouseup      = function() { editor_updateUI(objname); }
    editdoc.body.ondrop    = function() { editor_updateUI(objname, 100); }    
    editdoc.body.oncut     = function() { editor_updateUI(objname, 100); }
    editdoc.body.onpaste   = function() { editor_updateUI(objname, 100); }
    editdoc.body.onblur    = function() { editor_updateUI(objname, -1); }

    editor_obj.onload      = function() { editdoc.body.innerHTML = document.all[objname].value; }

    _fix_placeholder_urls(editdoc);
    document.all[objname].value = editdoc.body.innerHTML;                     
    if (mode != 'init') {             
      _editor_focus(editor_obj);
    }

  }

  if (mode != 'init') {             
    editor_updateUI(objname);
  }

}

function _editor_focus(editor_obj) {

  if (editor_obj.tagName.toLowerCase() == 'textarea') {         
    var myfunc = function() { editor_obj.focus(); };
    setTimeout(myfunc,100);                                    
  }

  else {                                                      
    var editdoc = editor_obj.contentWindow.document;           
    var editorRange = editdoc.body.createTextRange();          
    var curRange    = editdoc.selection.createRange();         

    if (curRange.length == null &&                              
        !editorRange.inRange(curRange)) {                       
      editorRange.collapse();                                   
      editorRange.select();                                    
      curRange = editorRange;
    }
  }

}

function _dec_to_rgb(value) {
  var hex_string = "";
  for (var hexpair = 0; hexpair < 3; hexpair++) {
    var byte = value & 0xFF;            
    value >>= 8;                       
    var nybble2 = byte & 0x0F;          
    var nybble1 = (byte >> 4) & 0x0F;   
    hex_string += nybble1.toString(16); 
    hex_string += nybble2.toString(16); 
  }
  return hex_string.toUpperCase();
}

function _fix_placeholder_urls(editdoc) {
  var i;

  for (i=0; i < editdoc.links.length; i++) {
    editdoc.links[i].href = editdoc.links[i].href.replace(/^[^*]*(\*\*\*)/, "$1");
  }

  for (i=0; i < editdoc.images.length; i++) {
    editdoc.images[i].src = editdoc.images[i].src.replace(/^[^*]*(\*\*\*)/, "$1");
  }

}

function editor_insertHTML(objname, str1,str2, reqSel) {
  var editor_obj = document.all["_" +objname + "_editor"];    
  if (str1 == null) { str1 = ''; }
  if (str2 == null) { str2 = ''; }

  if (document.all[objname] && editor_obj == null) {
    document.all[objname].focus();
    document.all[objname].value = document.all[objname].value + str1 + str2;
    return;
  }

  if (editor_obj == null) { return alert("Unable to insert HTML.  Invalid object name '" +objname+ "'."); }

  _editor_focus(editor_obj);

  var tagname = editor_obj.tagName.toLowerCase();
  var sRange;

  if (tagname == 'iframe') {
    var editdoc = editor_obj.contentWindow.document;
    sRange  = editdoc.selection.createRange();
    var sHtml   = sRange.htmlText;

    if (sRange.length) { return alert("Unable to insert HTML.  Try highlighting content instead of selecting it."); }

    var oldHandler = window.onerror;
    window.onerror = function() { alert("Unable to insert HTML for current selection."); return true; } 
    if (sHtml.length) {                                 
      if (str2) { sRange.pasteHTML(str1 +sHtml+ str2) } 
      else      { sRange.pasteHTML(str1); }             
    } else {                                            
      if (reqSel) { return alert("Unable to insert HTML.  You must select something first."); }
      sRange.pasteHTML(str1 + str2);                    
    }
    window.onerror = oldHandler;
  }

  else if (tagname == 'textarea') {
    editor_obj.focus();
    sRange  = document.selection.createRange();
    var sText   = sRange.text;

    if (sText.length) {                                 
      if (str2) { sRange.text = str1 +sText+ str2; }  
      else      { sRange.text = str1; }               
    } else {                                            
      if (reqSel) { return alert("Unable to insert HTML.  You must select something first."); }
      sRange.text = str1 + str2;                        
    }
  }
  else { alert("Unable to insert HTML.  Unknown object tag type '" +tagname+ "'."); }

  sRange.collapse(false); 
  sRange.select();        

}
