// this function sets an example form in the contenet field
function setup_form() {
   $('page_content').value = $('page_content').value +
      "<nt>\n<%= start_form_tag({:action => \"process_response\"}, {:multipart => true}) %>\n\n" +      
      "<!-- id of page -->\n" +
      "<%= hidden_field_tag \"page_id\", \"#{@page.id}\" %>\n\n\n" +
      "<!-- IMPORTANT: label the field headers -->\n" +      
      "<%= hidden_field_tag \"response[field_headers]\", \"field1:first_name,field2:last_name,field3:address\" %>\n\n" +
      "<p>\n" +
      "Email:<br /><%= text_field_tag \"response[email]\" %>\n" +
      "</p>\n\n" +
      "<p>\n" +
      "first name:<br /><%= text_field_tag \"response[field1]\" %>\n" +
      "</p>\n\n" +
      "<p>\n" + 
      "last name:<br /><%= text_field_tag \"response[field2]\" %>\n" +
      "</p>\n\n" +
      "<p>\n" + 
      "address:<br /><%= text_field_tag \"response[field3]\" %>\n" +
      "</p>\n\n" +
      "<p>\n" + 
      "upload:<br /><%= file_field_tag \"upload[upload]\" %>\n" +
      "<%= hidden_field_tag \"upload[description]\", \"#form upload#\" %>\n" +
      "</p>\n\n" +
      "<div class=\"buttons\">\n<button type=\"submit\">Submit</button>\n</div>\n\n" +
      "<%= end_form_tag %>\n</nt>";
   return true;
}


// this is for IE, remember the cursor position if
// somebody types a text in the main content
var current_cursor_range = null;


// This is for knowing which textarea is currently used
var which_text_area = null;


// this function remembers the position of the cursor
// in the main_content text area
function set_cursor_position(text_area_id) {
   //
   which_text_area = $(text_area_id);
   //
   if (document.selection && document.selection.createRange()) {
      current_cursor_range = document.selection.createRange();
   }
   return true;
}


// inserts myValue in myField at the current cursor position
function insertAtCursor(myValue) {
   myField = null;
    if (which_text_area != null) {
      myField = which_text_area;
    } else {
       myField = $('page_content');
    }
   if (document.selection) {
      // IE support
      if (current_cursor_range != null) {
         current_cursor_range.text = myValue;
         current_cursor_range.collapse();
         current_cursor_range.select();
      } else {
         myField.focus();
         sel = document.selection.createRange();   
         sel.text = myValue;         
      }
      current_cursor_range = null;
   } else if (myField.selectionStart || myField.selectionStart == '0') {
      // MOZILLA/NETSCAPE support
      scrollPos = myField.scrollTop;
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      myField.value = myField.value.substring(0, startPos)
         + myValue + myField.value.substring(endPos, myField.value.length);
      myField.scrollTop = scrollPos;
      myField.focus();
      var cursorPos = endPos + myValue.length;
      myField.setSelectionRange(cursorPos , cursorPos);
   } else {
      myField.value += myValue;
   }
}

function encloseSelection(prefix, suffix) {
   textarea = null;
    if (which_text_area != null) {
      textarea = which_text_area;
    } else {
       textarea = $('page_content');
    }
   textarea.focus();
   var start, end, sel, scrollPos, subst;
   if (typeof(document["selection"]) != "undefined") {
      sel = document.selection.createRange().text;
   } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
      start = textarea.selectionStart;
      end = textarea.selectionEnd;
      scrollPos = textarea.scrollTop;
      sel = textarea.value.substring(start, end);
   }
   if (sel.match(/ $/)) { // exclude ending space char, if any
      sel = sel.substring(0, sel.length - 1);
      suffix = suffix + " ";
   }
   subst = prefix + sel + suffix;
   if (typeof(document["selection"]) != "undefined") {
      var range = document.selection.createRange().text = subst;
      textarea.caretPos -= suffix.length;
   } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
      textarea.value = textarea.value.substring(0, start) + subst +
         textarea.value.substring(end);
      if (sel) {
         textarea.setSelectionRange(start + subst.length, start + subst.length);
      } else {
         textarea.setSelectionRange(start + prefix.length, start + prefix.length);
      }
      textarea.scrollTop = scrollPos;
   }
}

// this is for adding the textile codes
function add_textile(filepath, type, align, embed_or_link) {
   result = "";
   switch(embed_or_link) {
      case "embed":
         if (type.match(/image/)) {
            switch(align) {
               case "left":
                  result = "!<";
                  break;
               case "center":
                  result = "!=";
                  break;
               case "right":
                  result = "!>";
                  break;
               default :
                  result = "!";
            }
            result += filepath + "!";
         } else if (type.match(/video/)) {
            result = "<notextile>\n" +
               "<script language=\"JavaScript\">embed_video('" + filepath + "', '320', '250');</script>\n" +
               "</notextile>\n";
         } else if (type.match(/audio/) || type.match(/application/)) {
            if (type.match(/application/) && filepath.match(/\.swf$/)) {
               result = "<notextile>\n" +
                  "<div id=\"flash\"></div>\n" +
                  "<script language=\"JavaScript\">embed_flash('" + filepath + "', 'flash');</script>\n" +
                  "</notextile>\n";
            } else {
               
               result = "\"link_text\":" + filepath;   
            }      
         }
         break;
         
      case "link":
         result = "\"link_text\":" + filepath;
         break;
         
      default :
         result = "\"link_text\":" + filepath;
   }
   return result;
}
