
/* - workingpaper_edit.js - */
function workingpaper_edit()
{
   var currenturl = window.location.toString();
   if (
         currenturl.indexOf("/working-papers/") != -1
         && currenturl.indexOf("/edit") != -1
         && currenturl.indexOf("/working-papers/einfuehrung/") == -1
      )
   {
      workingpaper_edit_display_additional_fields("none");
      workingpaper_edit_show_getInfoButton();
      workingpaper_edit_show_unhideButton()
   }
   return false;
}

function workingpaper_edit_display_additional_fields(modus)
{
   document.getElementById("archetypes-fieldname-title").style.display = modus;
   document.getElementById("archetypes-fieldname-description").style.display = modus;
   document.getElementById("archetypes-fieldname-author").style.display = modus;
   document.getElementById("archetypes-fieldname-year").style.display = modus;
   document.getElementById("archetypes-fieldname-fulltext").style.display = modus;
   document.getElementById("archetypes-fieldname-autoren").style.display = modus;
}

function workingpaper_edit_show_getInfoButton()
{
   var button = document.createElement('input');
   button.setAttribute('type', 'button');
   button.setAttribute('id', 'button-getRemoteInfo');
   button.setAttribute('value', 'Daten holen');
   button.setAttribute('onclick', 'javascript:workingpaper_edit_getRemoteInfo();');
   button.setAttribute('style', 'margin-left: 20px;');
   button.setAttribute('class', 'context');

   var urldiv = document.getElementById("archetypes-fieldname-remoteUrl");
   var ulroot = urldiv.getElementsByTagName("input")[0];
   urldiv.insertBefore(button, ulroot.nextSibling);
}


function workingpaper_edit_show_unhideButton()
{
   var button = document.createElement('input');
   button.setAttribute('type', 'button');
   button.setAttribute('id', 'button-unhide');
   button.setAttribute('value', 'Felder selbst ausfüllen');
   button.setAttribute('onclick', 'javascript:workingpaper_edit_display_additional_fields("block");');
   button.setAttribute('style', 'margin-left: 20px;');
   button.setAttribute('class', 'context');

   var urldiv = document.getElementById("archetypes-fieldname-remoteUrl");
   var ulroot = urldiv.getElementsByTagName("input")[0];
   urldiv.insertBefore(button, ulroot.nextSibling.nextSibling);
}

function workingpaper_edit_getRemoteInfo()
{
  var scripturl = "http://www.sfb-repraesentationen.de/getMetadata_external?EDOC_URL=";
  var wpurl = document.getElementById("remoteUrl").value;
  var url = scripturl + escape(wpurl);

  var daten;
  // getJSON geht irgendwie nicht (einfach weil das Format noch nicht stimmt?)
  jQuery.get(url, function(data){
                     workingpaper_edit_extractRemoteInfo(data);
                  });
}

function workingpaper_edit_extractRemoteInfo(daten_stream)
{
   /*
   daten['title'] = "Hallo Welt";
   daten['abstract'] = "Es geht um nichts.";
   daten['autor'] = "Paule B.";
   daten['jahr'] = "1968";
   */

   //  daten = [irgendwie so] json.eval(daten_stream);
   var abstract = daten_stream;
   var titel = "Test Titel"; // daten['title'];
   var autor = "Test Autor"; // daten['author'];
   var jahr = "Test Jahr";


   document.getElementById("title").value = titel;
   document.getElementById("description").innerHTML = abstract;
   document.getElementById("author").value = autor;
   document.getElementById("year").value = jahr;

   workingpaper_edit_display_additional_fields("block");
}

registerPloneFunction(workingpaper_edit); 

