/*

   Uses articletype_validate from articletype.js
   
 */
 
/* 
   insert_image saves the selected node from editorid into hiddenid,
   and then submits formid to the given url.
 */


function insert_image(field, value, formid, editorid) {
  // tag selected node
  var editor = tinyMCE.get(editorid);
  var startNode = editor.selection.getStart();

  // check that startNode is okay
  // ie6: check that toString method is OK
  if ( startNode == null )
    tiny_mce_insert(editor, '<span class="awaiting_asset"></span>');

  else if ( !startNode.toString )
    tiny_mce_insert(editor, '<span class="awaiting_asset"></span>');

  else if ( !startNode.toString().startsWith("http://") )
    tiny_mce_insert(editor, '<span class="awaiting_asset"></span>');
  
  else 
    $("asset").value = startNode.toString();

  // remove any confirmation dialog that might be set
  window.onbeforeunload = '';

  set_and_submit(field, value, formid);
}


function exit_msg() {
  // empty placeholder
  return null;
}

function redirect(url) {
    document.location.href=url;
}

//
// Submit form with formid after changing its action to the specified url
//
function submit_to(formid, url) {
   var form = document.getElementById(formid);
   form.action = url;
   form.submit();
}

//
// Submit form with formid after setting given field to given value
// Also call last_chance_actions().
//
function set_and_submit(field_id, value, formid) {
  field = document.getElementById(field_id);
  field.value = value

  last_chance_actions();

  var form = document.getElementById(formid);
  form.submit();
}

function last_chance_actions() {
  // tinyMCE must save its content (copy back to hidden fields)
  tinyMCE.triggerSave();

  // Copy the contents of all the nicEdit fields
  // into hidden submit-fields
  var niceditors = editors.split(",");
  for (i=0; i<niceditors.length; i++) {
    // first you need the editor object,
    // then you need the editors 'instance object'
    // (whatever that is)
    editor = nicEditors.findEditor(niceditors[i]);
    if (editor) {
      editor = editor.instanceById(niceditors[i]);
      submit_id = niceditors[i] + "_submit";
      submit_el = $(submit_id);

      if (submit_el && editor.getContent) {
        var content = editor.getContent();
        submit_el.value = content;
      }
    }
  }
}

function remove_element(element) {
//  element = document.getElementById(id);
//  if (element)
    element.parentNode.removeChild(element);
}
//
// Used by image_dialog.tt to start a file transfer right after
// the user has chosen a file
//
function submitform(formid) {
   var form = document.getElementById(formid);
   form.submit();
}

function ac_validate_input(input, li) { // ac_input, input
   // set the value of the real (hidden) input field to the id of the selected
   // element
   var real_id = input.id + "_real"
   document.getElementById(real_id).value = li.id;

   validate_field(input.id);
}

function invalidate_field(input_id) {
  input = document.getElementById(input_id);

  if (input && !input.className.match("invalid_input"))
     input.className += " invalid_input";  
}

/*
function validate_field(input_id) {
  input = document.getElementById(input_id);

  if ( input && input.className.match(" invalid_input") )
//     input.className.replace(" invalid_input", "");
     input.className = "aha";
  
}*/

function ac_on_hide(input, ac_input) {
  //validate_field(input.id);
  
  // and hide
  ac_prototype_on_hide(input, ac_input);
}

// cut & paste from vendor/rails/actionpack/lib/action_view/helpers/javascripts/controls.js
// This is the original method that is run when auto_complete is set to hide.
// We need to append this method on to our own to get the desired functionality.
function ac_prototype_on_hide(element, update) { 
  new Effect.Fade(update,{duration:0.15}) 
};

// Set default value for auto_complete field.
// Also set the value of the hidden real field to this auto_complete field
// to the id of auto_complete_field value.
function ac_set_default_value(input) {
  var default_element = document.getElementById(input.id + "_default");

  if ( default_element ) {
    var defaultvalue = default_element.value;
    
    // id,label
    var regexp = /^([^,]+),/;
    var id_value = defaultvalue.split(regexp);
    
    var id    = id_value[1];
    var value = id_value[2];
    
    if ( id && value ) {
      var real_id = input.id + "_real"
      document.getElementById(real_id).value = id

      input.value = value
    }
  }
}

