// Insert Textarea
function myTextareaInsert(editor){
  var element;
  var id = "__myTempId__"; // temporary id for new element
  if(editor.InsertHTML("<textarea id='"+id+"'></textarea>")){// insert our new element into Editor's content (Editor's content is saved automaticaly on success)
     element = editor.getDocument().getElementById(id);// get just inserted element
     element.id=null;// remove temporary id
     element.removeAttribute("id");
     myTextareaPopup(editor,element,false);}}// work with custom popup window
function myTextareaEdit(editor,element){// on custom menu item ("Textarea properties") click
  editor.SaveContent();// save Editor's content before edititing this element properties
  myTextareaPopup(editor,element,true);}// work with custom popup window
function myRemoveTextarea(editor,element){// on custom menu item ("Remove Textarea") click
  editor.DeleteNode(element);} // it saves Editor content before deleting, delete it
// custom menu items are available always
function myTextareaEditCheck  (editor,element) {return true;}
function myRemoveTextareaCheck(editor,element) {return true;}
// working with Textarea popup
function myTextareaPopup(editor, element, isEdit){
  function postback(doc,popup_iframe){ //on OK and Cancell buttons click
    if(doc != null){ // OK clicked
      // get fields values from popup
      var rows     = doc.getElementById("rowsField"    ).value;
      var cols     = doc.getElementById("colsField"    ).value;
      var disabled = doc.getElementById("disabledField").checked;
      var readonly = doc.getElementById("readonlyField").checked;
      var id       = doc.getElementById("idField"      ).value;
      var name     = doc.getElementById("nameField"    ).value;
      // check input fields and highlight them on error
      if((isNaN(rows) && rows.length > 0) || rows < 0) {setSelectionRange(doc.getElementById("rowsField" )); /* don't close popup */ return false;}
      if((isNaN(cols) && cols.length > 0) || cols < 0) {setSelectionRange(doc.getElementById("colsField" )); /* don't close popup */ return false;}
      if(id.length > 0 && editor.getDocument().getElementById(id) != null && editor.getDocument().getElementById(id) != element)// duplicate 'id'
        {setSelectionRange(doc.getElementById("idField"   )); /* don't close popup */ return false;}
      // update properties of the element
      element.rows = (rows.length > 0)?rows: 2;
      element.cols = (cols.length > 0)?cols:20;
      if(id.length > 0)element.id = id;
      else{element.id=null;element.removeAttribute("id");}
      if(name.length > 0)element.name = name;
      else{element.name="";element.removeAttribute("name");}
      element.disabled = disabled;
      element.readOnly = readonly;
      if(document.all && !window.opera)element.value = doc.getElementById("valueField").value;
      else{element.innerHTML = doc.getElementById("valueField").value;}
      if(document.all && !window.opera && isEdit){
        try{
          var rng = editor.getDocument().body.createControlRange();
          rng.add(element);
          rng.select();}
        catch(e){}
      }
    }else{ // Cancel Clicked
      editor.RestoreContent();// restore saved Editor's content (after success InsertHtml)
    }
    return true;} // close popup
  function init(doc,popup_iframe){  // on Popup window content init
    if(doc != null){
      // init popup's fields
      doc.getElementById("idField"      ).value   = element.id;
      doc.getElementById("nameField"    ).value   = element.name;
      doc.getElementById("valueField"   ).value   = element.innerHTML;
      doc.getElementById("rowsField"    ).value   = (element.rows <= 0)? 2:element.rows;
      doc.getElementById("colsField"    ).value   = (element.cols <= 0)?20:element.cols;
      doc.getElementById("disabledField").checked = element.disabled;
      doc.getElementById("readonlyField").checked = element.readOnly;
    }}
    editor.customPopup("popup_textarea","textarea","__vb_textarea.aspx",postback,init, true, isEdit);}// open custom popup window
  function setSelectionRange(input){// selecting <input> value 
    input.focus();
    if (input.setSelectionRange){input.setSelectionRange(0, input.value.length);}
    else if (input.createTextRange) {
      var range = input.createTextRange();
      range.collapse(true);
      range.moveEnd  ('character', input.value.length);
      range.moveStart('character', 0);
      range.select();
  }}// End Textarea
  
  
  
//Insert fieldset
// on custom button click
function myFieldsetInsert(editor){
  var element;
  var id = "__myTempId__"; // temporary id for new element
  // insert our new element into Editor's content (Editor's content is saved automaticaly on success)
  if(editor.InsertHTML("<fieldset id='"+id+"' style='width:300px; height:50px;'><legend>title</legend>text</fieldset>")){
     element = editor.getDocument().getElementById(id);// get just inserted element
     element.id=null;// remove temporary id
     element.removeAttribute("id");
     myFieldsetPopup(editor,element,false);}}// work with custom popup window
function myFieldsetEdit(editor,element){// on custom menu item ("Fieldset properties") click
  editor.SaveContent();// save Editor's content before edititing this element properties
  myFieldsetPopup(editor,element,true);}// work with custom popup window
function myRemoveFieldset(editor,element){// on custom menu item ("Remove Fieldset") click
  // delete it
  editor.DeleteNode(element);}// it saves Editor content before deleting
// custom menu items are available always
function myFieldsetEditCheck  (editor,element) {return true;}
function myRemoveFieldsetCheck(editor,element) {return true;}
function myFieldsetPopup(editor, element, isEdit){// working with Fieldset popup
  function postback(doc,popup_iframe){ //on OK and Cancell buttons click
    if(doc != null){ // OK clicked
      // get fields values from popup
      var width   = doc.getElementById("widthField"  ).value;
      var height  = doc.getElementById("heightField" ).value;
      var padding = doc.getElementById("paddingField").value;
      var margin  = doc.getElementById("marginField" ).value;

      // check input fields and highlight them on error
      if((isNaN(width  ) && width  .length > 0) || width   < 0) {setSelectionRange(doc.getElementById("widthField"  )); /* don't close popup */ return false;}
      if((isNaN(height ) && height .length > 0) || height  < 0) {setSelectionRange(doc.getElementById("heightField" )); /* don't close popup */ return false;}
      if((isNaN(padding) && padding.length > 0) || padding < 0) {setSelectionRange(doc.getElementById("paddingField")); /* don't close popup */ return false;}
      if((isNaN(margin ) && margin .length > 0) || margin  < 0) {setSelectionRange(doc.getElementById("marginField" )); /* don't close popup */ return false;}
       
      // update properties of the element
      element.style.width   = (!isNaN(width  ) && width  .length > 0)?(width  +"px"):"";
      element.style.height  = (!isNaN(height ) && height .length > 0)?(height +"px"):"";
      element.style.padding = (!isNaN(padding) && padding.length > 0)?(padding+"px"):"";
      element.style.margin  = (!isNaN(margin ) && margin .length > 0)?(margin +"px"):"";
    }
    else{ // Cancel Clicked
      // restore saved Editor's content (after success InsertHtml)
      editor.RestoreContent();}
    return true;} // close popup
  function init(doc,popup_iframe){  // on Popup window content init
    if(doc != null){
      // init popup's fields
      doc.getElementById("widthField"  ).value = (element.style.width  )?parseInt(element.style.width  ):"";
      doc.getElementById("heightField" ).value = (element.style.height )?parseInt(element.style.height ):"";
      doc.getElementById("paddingField").value = (element.style.padding)?parseInt(element.style.padding):"";
      doc.getElementById("marginField" ).value = (element.style.margin )?parseInt(element.style.margin ):"";}}
  // open custom popup window
  editor.customPopup("popup_fieldset","fieldset","__vb_fieldset.aspx",postback,init, true, isEdit);}
// selecting <input> value 
function setSelectionRange(input){
  input.focus();
  if (input.setSelectionRange){input.setSelectionRange(0, input.value.length);}
  else if (input.createTextRange){
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd  ('character', input.value.length);
    range.moveStart('character', 0);
    range.select();}}//End fieldset
 