
//********************************************************************
// beginning of javascript code that might be moved into a web api call
 
var somethingChanged = null;

//...
// removed this May 2010 when incorporating CKEditor
function enableSaveButton()
{
  /*
  if ( document.EditorForm.TopSaveButton.disabled == true )
  {
    document.EditorForm.TopSaveButton.disabled = false;
  }
  if ( document.EditorForm.BottomSaveButton.disabled == true )
  {
    document.EditorForm.BottomSaveButton.disabled = false;
  }
  */

  //addNewRepeatingEntry( 'Calendar' );
}

//...
function cancelEdit() // profiler
{
  /*
  // an attempt to prevent changes from being lost, in particular due to someone exiting the page
  var result = "";
  if ( somethingChanged )
  {
    result = confirm( 'The text has changed, do you want to discard the changes?' );
    if ( !result )
    {
      return;
    }
  }
  */

  hideEditor();
}

//...
function editText( elementIdToEdit ) // profiler
{
  var topMenuLogin = document.getElementById( 'TopMenuLogin' );
  if ( topMenuLogin.innerHTML == "Logout" )
  {
    showEditor();
    var element = document.getElementById( elementIdToEdit );
    document.EditorForm.ElementIdToEdit.value = elementIdToEdit;
    //editBox.focus();
  }
}

//! JSON
function packageEditorSubmission( editorStructure, submission ) // profiler
{
  if( editorStructure.hasOwnProperty('editor') )
  {
    if(!submission)
      submission = { "editor": "Submission" };
    packageEditorSubmission( editorStructure.editor, submission );
  }

  //if ( !submission )
  //{
  //  submission = {"Submision":"Submission"};
  //}

  if( editorStructure.hasOwnProperty('@attributes') )
  {
    //writetext( 'module: ' + editorStructure['@attributes'].name );
    submission['@attributes'] = {};
    for( var propertyName in editorStructure['@attributes'] )
    {
      submission['@attributes'][propertyName] = editorStructure['@attributes'][propertyName];
    }
  }

  // make sure the editor structure contains something we need to act upon...  (empty structure elements should be ignored)
  if( editorStructure.hasOwnProperty('element') )
  {
    // if the structure has multiple elements, we need to pull them out individually to work with them
    if( !!editorStructure.element.size )
    {
      //writetext( 'element.length='+editorStructure.element.length );
      var editorStructureSize = editorStructure.element.size()
      for( var i = 0; i < editorStructureSize; i++ )
      {
        var elementName = editorStructure.element[i];
        if (elementName.hasOwnProperty('@attributes'))
        {
          elementName = editorStructure.element[i]['@attributes']['name'];
          submission[elementName] = {};
          submission[elementName]['@attributes'] = {};
          for( var propertyName in editorStructure.element[i]['@attributes'] )
          {
            submission[elementName]['@attributes'][propertyName] = editorStructure.element[i]['@attributes'][propertyName];
          }
        }
        //writetext( 'element['+i+']: ' + elementName );
        var editorElement = document.EditorForm[elementName];
        // capture the element value
        captureEditorElementValue( submission, elementName, editorElement );
      }
    }
    else
    {
      //writetext( 'element: ' + editorStructure.element );
      var editorElement = document.EditorForm[editorStructure.element];
      // capture the element value...
      captureEditorElementValue( submission, editorStructure.element, editorElement );
    }
  }

  if( editorStructure.hasOwnProperty('submodule') )
  {
    submission.submodules = [];
    if( !!editorStructure.submodule.length )
    {
      for( var i = 0; i < editorStructure.submodule.length; i += 1 )
      {
        submission.submodules [i] = {};
        packageEditorSubmission( editorStructure.submodule[i], submission.submodules[i] );
      }
    }
    else
    {
      submission.submodules [0] = {};
      packageEditorSubmission( editorStructure.submodule, submission.submodules[0] );
    }
  }

  if( editorStructure.hasOwnProperty('module') )
  {
    submission.modules = [];
    if( !!editorStructure.module.length )
    {
      for( var i = 0; i < editorStructure.module.length; i += 1 )
      {
        submission.modules[i] = {};
        packageEditorSubmission( editorStructure.module[i], submission.modules[i] );
      }
    }
    else
    {
      submission.modules[0] = {};
      packageEditorSubmission( editorStructure.module, submission.modules[0] );
    }
  }

  return submission;
}

function captureEditorElementValue( submission, elementName, editorElement ) // profiler
{
  if (!submission[elementName])
  {
    submission[elementName] = {};
  }

  if( !!editorElement.length )
  {
    for( var j = 0; j < editorElement.length; j++ )
    {
      var editorElementChoice = editorElement[j];
      if (editorElementChoice.checked)
      {
        submission[elementName]['value'] = myBase64Encode(editorElementChoice.value);
        //submission[elementName] = 'editorElementChoice.'+editorElementChoice.value;
        break;
      }
    }
  }
  else
  {
    if (editorElement.type == "checkbox")
    {
      if (editorElement.checked)
        submission[elementName]['value'] = myBase64Encode('1');
      else
        submission[elementName]['value'] = myBase64Encode('0');
      //submission[elementName] = 'editorElement.checked='+editorElement.checked;
    }
    else
    {
      submission[elementName]['value'] = myBase64Encode(editorElement.value);
      //submission[elementName] = 'editorElement.'+editorElement.value;
    }
  }
}

// helper function for debugging purposes
function writetext( text )
{
  var divElement = document.EditorForm.EditorDebug;
  if (divElement)
  {
    divElement.innerHTML += text;
  }
}

/*
// don't yet need this code, and it will need to change for JSON
function addNewRepeatingEntry( module )
{
  var newEntryPrototypeElement = document.getElementById( 'New'+module );
  if ( newEntryPrototypeElement )
  {
    var newEntry = newEntryPrototypeElement.value;
    var body =   document.getElementsByTagName("body")[0];
    var bodyContent = body.innerHTML;
    bodyContent = bodyContent.replace( '<!--New'+module+'-->', newEntry+'<!--New'+module+'-->' );
    body.innerHTML = bodyContent;
  }
}
*/

//... converted to JSON
function updateText() // profiler
{
  var elementIdToEdit = document.EditorForm.ElementIdToEdit.value;
  var element = document.getElementById( elementIdToEdit );

  var result = "";
  //if ( somethingChanged )
  {
    //result = confirm( 'The text has changed, do you want to save the changes?' );
	result = confirm( 'Do you want to save the changes?' );
    if ( result )
    {
      //... this form of XML creation seems to be working - setting it aside while i explore how to 
      //var editorStructure = new XML( document.EditorForm.EditorXML.value );
      var editorStructureJSON = '' + document.EditorForm.EditorJSON.value;
      var editorStructure = editorStructureJSON.evalJSON();
      var submission = packageEditorSubmission( editorStructure );

      /* ... testing...
	  //alert( submission );
      //document.getElementById( 'Contents' ).innerHTML = submission;
      //alert( submission );
      document.getElementById( 'Contents' ).value = postContent( '/_/webapi/Content.php', 'Method=UpdateContent&Id='+document.EditorForm.Id.value+'&XmlSubmission='+submission );
	  return;
      //*/
      
      // update the databse and refresh the page content
      //postContent( '/_/webapi/Content.php', 'Method=UpdateContent&Submission='+Object.toJSON(submission) );
      new Ajax.Request('/_/webapi/Content.php', { parameters: {'Method':'UpdateContent','Submission':Object.toJSON(submission)},
        asynchronous: false } );

      if (element)
      {
        //element.innerHTML = postContent( '/_/webapi/Content.php', 'Method=RenderContent&Id='+document.EditorForm.Id.value );
        new Ajax.Request('/_/webapi/Content.php', { parameters: {'Method':'RenderContent','Id':document.EditorForm.Id.value},
          onSuccess: function(transport) {element.innerHTML = transport.responseText;},
          asynchronous: false } );
      }
    }
  }

  //document.EditorForm.TopSaveButton.disabled = true;
  //document.EditorForm.BottomSaveButton.disabled = true;

  hideEditor();
}

//... no JSON (but might be able to use Prototype?)
// was used for the old approach to editing, mouse over caused editor box to appear.
// abandoned because that didn't allow for page navigation, only editing
function hookEditorEvents( elementId )
{
  var element = document.getElementById( elementId );
  if ( element )
  {
    hookEvent( element, 'onMouseOver', 'showBorder( this )' );
    hookEvent( element, 'onMouseOut', 'hideBorder( this )' );
    hookEvent( element, 'onClick', 'editText( "'+elementId+'" )' );
  }
}
// end of javascript code that might be placed into a web api call 
//****************************************************************/

