//
//		 PAFAppResources: Initialize client side of Portlet Application Framework
//

Portal.Portlet.PAFAppResources = Portal.Portlet.extend({

   // In PAF, the form always posts to itself. It should update and interpret data, and
   // then *redirect* to another page if necessary.
   
   // This code fixes up the EntrezForm in PAF templates to automatically post
   // to the current window pathname.
   
   // If the programmer specifies a hidden input with id="entrez-action",
   // that action (instead of POST) is used for the form.
   
   // If the programmer specifies a hidden input with id="entrez-method",
   // that method is used for the form. 
       
   // FIXME: This is a hack. EntrezForm should default to posting to same page,
   // and there should be some way of indicating how to post (or get) elsewhere.
   // The problem here is that we've mixed Entrez into PAF. This should be refactored.
   
   init: function(path, name, notifier) {
      var oThis = this;  // Use <oThis> instead of <this> for registering callbacks
      this.base(path, name, notifier);  // Call superclass constructor
      
      if (!document.forms[0]) return; // Do nothing if no form.
      
      var getNodeValue = function(id, dfl) {
         var node = $(id);
         return node ? node.value : dfl;
      };
      
      var entrezAction = getNodeValue('entrez-action', window.location.pathname);
      var entrezMethod = getNodeValue('entrez-method', null);
      var entrezSubmit = getNodeValue('entrez-submit', false);
      
      document.forms[0].action = entrezAction;
      
      // FIXME: We need to include query string here, too, I think... At least for POST...      
      if (entrezMethod) {
         document.forms[0].method = entrezMethod;
      }

      // By default, entrez form ignores submits. Setting "entrezSubmit" to "true" means let Entrez form
      // actually process the submit. This probably will only work correctly for action="get"       
      if (entrezSubmit) {
         document.forms[0].onsubmit="return true;"
      }
   }
});
