var htmlForm = null; // Used to store the template copy of the form


// Re-parents the form for use in editing an existing blog entry, and fills
// the controls appropriately
//
// Params:
//  curr - Current edit index

function ShowEdit(curr) {
  if (!document.getElementById('initentryform'))
    return;

  // If we haven't saved the template copy of the form yet, do it now
  if (!htmlForm)
    htmlForm = document.getElementById('initentryform');

  // If the form has a parent, detach it
  if (htmlForm.parentNode)
    htmlForm.parentNode.removeChild(htmlForm);

  // Re-parent the form to it's new position
  var dest = document.getElementById('editentry' + curr);
  dest.appendChild(htmlForm);

  // Force the wysiwyg control to re-initialise, otherwise it won't work
  wysRecreateControl('blog_content');

  // Show the edit headers in the file section on the forms
  for (i = 1; i <= 4; ++i) {
    if (document.getElementById('div_new_file'  + i)) {
      document.getElementById('div_new_file'  + i).style.display = 'none';
      document.getElementById('div_edit_file' + i).style.display = '';
      if (document.getElementById('initfile_id' + curr + '_' + i)) { 
        document.getElementById('div_del_file'  + i).style.display = '';
        var blogid = document.getElementById('initblogid' + curr).innerHTML;
        var filename = document.getElementById('initfile_name' + curr + '_' + i).innerHTML;
        var icon = document.getElementById('initfile_icon' + curr + '_' + i).innerHTML;
        if (document.getElementById('div_edit_icon' + i)) {
          document.getElementById('div_edit_icon' + i).innerHTML = icon;
        }
        var title = document.getElementById('initfile_title' + curr + '_' + i).innerHTML;
        if (document.getElementById('title' + i))
          document.getElementById('title' + i).value = title;
      } else {
        document.getElementById('div_del_file'  + i).style.display = 'none';
        document.getElementById('div_edit_file' + i).innerHTML = ''
      }
      if (i != curr && document.getElementById('ENTRY' + i))
        document.getElementById('ENTRY' + i).style.display = '';
    }
  }
  document.getElementById('ENTRY' + curr).style.display = 'none';

  // Invoke 'FillForm' to put in the correct value.
  window.setTimeout("FillForm(" + curr + ")", 50);
}


// Re-parents the form for adding a new blog entry
//
// Params:
//  curr - Current edit index

function ShowNewEdit(blogid) {
  if (!document.getElementById('initentryform'))
    return;

  // If we haven't saved the template copy of the form yet, do it now
  if (!htmlForm)
    htmlForm = document.getElementById('initentryform');

  // If the form has a parent, detach it
  if (htmlForm.parentNode)
    htmlForm.parentNode.removeChild(htmlForm);

  // Re-parent the form to it's new position
  var dest = document.getElementById('showaddentry');
  dest.appendChild(htmlForm);

  // Force the wysiwyg control to re-initialise, otherwise it won't work
  wysRecreateControl('blog_content');

  // Show the add headers in the file section on the forms
  for (i = 1; i <= 4; i++) {
    if (document.getElementById('div_edit_file' + i)) {
      document.getElementById('div_edit_file' + i).style.display = 'none';
      document.getElementById('div_del_file'  + i).style.display = 'none';
      document.getElementById('div_new_file'  + i).style.display = '';
      document.getElementById('file_id' + i).value = '';
      document.getElementById('file_old' + i).value = '';
      document.getElementById('showall' + i).selectedIndex = 2;
    }
  }

  // Clear out the controls
  document.getElementById('id')    .value = '';
  document.getElementById('blogid').value = blogid;
  document.getElementById('title') .value = '';
  var now = new Date();
  document.getElementById('date_day') .value = now.getDate();
  document.getElementById('date_mon') .value = now.getMonth() + 1;
  document.getElementById('date_year').value = now.getFullYear();
  document.getElementById('date_hour').value = now.getHours();
  document.getElementById('date_min') .value = now.getMinutes();
  document.getElementById('date_sec') .value = now.getSeconds();
}


// Use the data in the hidden controls for the specified entry to populate
// the controls
//
// Params:
//  curr - Current edit index

function FillForm(curr) {
  if (!document.getElementById('initentryform'))
    return;

  var id        = document.getElementById('initid'        + curr).innerHTML;
  var title     = document.getElementById('inittitle'     + curr).innerHTML;
  var content   = document.getElementById('initcontent'   + curr).innerHTML;
  var blogid    = document.getElementById('initblogid'    + curr).innerHTML;
  var date_day  = document.getElementById('initdate_day'  + curr).innerHTML;
  var date_mon  = document.getElementById('initdate_mon'  + curr).innerHTML;
  var date_year = document.getElementById('initdate_year' + curr).innerHTML;
  var date_hour = document.getElementById('initdate_hour' + curr).innerHTML;
  var date_min  = document.getElementById('initdate_min'  + curr).innerHTML;
  var date_sec  = document.getElementById('initdate_sec'  + curr).innerHTML;
  for (i = 1; i <= 4; ++i) {
    if (document.getElementById('initfile_id' + curr + '_' + i)) {
      var file_id  = document.getElementById('initfile_id' + curr + '_' + i).innerHTML;
      document.getElementById('file_id' + i).value = file_id;
      var file_name = document.getElementById('initfile_name' + curr + '_' + i).innerHTML;
      document.getElementById('file_old' + i).value = file_name;
      var access = parseInt(document.getElementById('initfile_access' + curr + '_' + i).innerHTML);
      document.getElementById('showall' + i).selectedIndex = access - 1;
      // alert(access);
      // alert(document.getElementById('showall' + i).selectedIndex);
      var file_title = document.getElementById('initfile_title' + curr + '_' + i).innerHTML;
      document.getElementById('title' + i).value = file_title;
    } else {
      if (document.getElementById('file_id' + i)) {
        document.getElementById('file_id' + i).value = '';
        document.getElementById('file_old' + i).value = '';
        document.getElementById('showall' + i).selectedIndex = 2;
        document.getElementById('title' + i).value = '';
      }
    }
  }
  document.getElementById('id')       .value = id;
  document.getElementById('title')    .value = title;
  document.getElementById('blogid')   .value = blogid;
  document.getElementById('date_day') .value = date_day;
  document.getElementById('date_mon') .value = date_mon;
  document.getElementById('date_year').value = date_year;
  document.getElementById('date_hour').value = date_hour;
  document.getElementById('date_min') .value = date_min;
  document.getElementById('date_sec') .value = date_sec;
  wysSetContent('blog_content', content);
}


var onLoadFunc = window.onload; // Storage for remembering the previous onLoad
window.onload = Blog_OnLoad;    // Set a new onLoad handler

function Blog_OnLoad() {
  // Call the original onLoad handler first
  if (onLoadFunc)
    onLoadFunc();

  // Now hide the template form
  if (document.getElementById('initentry'))
    document.getElementById('initentry').style.display='none';  
}

