 /* Copyright (C) 2006 by Joseph McVerry - American Coders, Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

function Page(node)
{

this.sections=new Array();

if (node == null) return
var sectionNodes=node.getElementsByTagName("section");

for (var i = 0; i < sectionNodes.length; i++)
  this.sections[i]=new Section(sectionNodes.item(i));
}



new Page(null)

Page.prototype.display = page_display;
Page.prototype.clear = page_clear;
Page.prototype.get = page_get;




function page_display(node, dataNode)
{

  var i;
  var alength = this.sections.length
  
  var inp = document.createElement('input');
  inp.setAttribute("type", "hidden");
  inp.setAttribute("value", "");
  
  node.appendChild(inp)
  
  var dataSubNode;
    
  if (dataNode == undefined)
        dataSubNode = undefined;
  else  
        dataSubNode = dataNode.childNodes;
        
  if (dataSubNode == undefined) {
       dataSubNode = new Array()
       for (i=0; i<alength; i++)
         dataSubNode[i] = undefined
       }
      
  
  for (i=0; i<alength; i++)
    {
      this.sections[i].display(node, dataSubNode[i]);
    }

  
}
function page_clear()
{
  var i;
  var alength = this.sections.length
  for (i=0; i<alength; i++)
    {
       this.sections[i].clear();
    }

}
function page_get(doPrev)
{
  var i;
  
  document.forms[0].nextButton.disabled=true
  document.forms[0].prevButton.disabled=true
  var alength = this.sections.length
  var tsave= "";
  var allSave = "";
 // alert('start page_get');
  var enteredFirst=false;
  for (i=0; i<alength; i++)
    {
      if (i==0)
        enteredFirst = this.sections[i].didUserEnterData();
      if (this.sections[i].didUserEnterData() == false && this.sections[i].required != 'M') 
        continue;
      if (i>0 && enteredFirst == false &&   this.sections[i].didUserEnterData() == true)
        {
          alert("Enter data into primary section fields if data is present in secondary sections")
        document.forms[0].nextButton.disabled=false
        document.forms[0].prevButton.disabled=false
         return;
        }
      tsave = this.sections[i].get();
      
      if (tsave == false)
        {
        document.forms[0].nextButton.disabled=false
        document.forms[0].prevButton.disabled=false
        return;
         }
      else
      if (tsave != "") {
          allSave += tsave;   
          }
    }

 
  var sending = "&nextState=?"

  if (doPrev == true)
	  sending+="&doPrev=1"
   else   sending+="&doPrev=0"
	  

  sending=sending+"\n"+allSave


 //alert(sending);
/*
try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   } catch (e) {
    alert("Permission UniversalBrowserRead denied."+e);
   }
   */
 // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandler(req, incoming);
  req.onreadystatechange = handlerFunction;

  // Open an HTTP  connection to the servlet
  // Third parameter specifies request is asynchronous.
  try{
  //alert(prefix+theActionToTake+"/?"+sending)
  req.open("POST", prefix+theActionToTake+"/?"+sending, true);
  }
  catch (e)
    {
      alert(e)
    }

  // Specify that the body of the request contains form data
  req.setRequestHeader("Content-Type",
                       "application/x-www-form-urlencoded");

  // Send form encoded data stating that I want to add the
  // specified item to the cart.

  req.send(null);
  
}


