 /* 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 Group(node)
{


  if (node == null)
     return

  this.repeats = 1
  this.hasRepeated = 0


  var attr=node.attributes
  

  for (var i = 0; i < attr.length; i++) {
              
          if (attr[i].name == 'id')
             this.id = attr[i].value
          if (attr[i].name == 'name')
             this.name = attr[i].value
          if (attr[i].name == 'required')
             this.required = attr[i].value
          if (attr[i].name == 'help')
             this.help = attr[i].value
          if (attr[i].name == 'repeats')
             this.repeats = attr[i].value
    }


  var cd=node.childNodes
  var decnt=0
  this.elements = new Array()
   for (var i = 0; i < cd.length; i++)
        {
               if (cd.item(i).tagName == 'group') {
                         c = new Group(cd.item(i))
	 		 this.elements[decnt]=c
	 		 decnt++
	 		 
	 		 }
	       else		 
               if (cd.item(i).tagName == 'element' ) {
                         de = new Element(cd.item(i))
	 		 this.elements[decnt]=de
	 		 //alert(decnt +' is in array at ' + i)
	 		 decnt++
	 		 }
        }
}

  new Group(null)
  Group.prototype.display = group_display
  Group.prototype.clear = group_clear
  Group.prototype.verify = group_verify
  Group.prototype.get = group_get
  Group.prototype.didUserEnterData = group_didUserEnterData
  Group.prototype.doRepeat = group_doRepeat

  this.appendNewDisplay = undefined

function group_display(displayNode, repeatCnt, dataNode)
{
  /** method can be called with 5 situations
      1. by parent with no data (first time in for the page)
         this.appendNewDisplay is undefined
         hasRepeated = 0
         repeats = 0
      2. by parent with data (the whole page has been shown at least once)
         this.appendNewDisplay is undefined
         hasRepeated = 0 
         repeats = 0
      3, by itself with the repeating data from #2 (recursion?)
         this.appendNewDisplay is defined, should be the incoming node argument
         hasRepeated = 0
         repeats > 0
      4. by the add button 
         this.appendNewDisplay is defined, should be the incoming node argument
         hasRepeated = 0
         repeats > 0
      5. by the clear screen button
  **/
  
  
  var divHelp
  if (this.appendNewDisplay == undefined) {
     var divCtr = document.createElement('center')
     displayNode.appendChild(divCtr)
     var divHelp = document.createElement('div')
     divCtr.appendChild(divHelp)
     if (this.help != null)
       divHelp.setAttribute('title', 'header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]')
       this.appendNewDisplay = divHelp // this changes the display node
       
      var attr=dataNode.attributes
      for (var i = 0; i < attr.length; i++) {
        if (attr[i].name == 'repeated')   
          this.hasRepeated = attr[i].value        
      } // for
  }     
  else 
       divHelp = this.appendNewDisplay
       
  var divTable  = document.createElement('table')
  divTable.setAttribute("width", "100%")
  divTable.setAttribute("bgcolor", "#FAEBD0")
  divTable.setAttribute("border", 1)
  divHelp.appendChild(divTable)
  
  var divTableRow  = document.createElement('tr')
  divTable.appendChild(divTableRow)
  var divTableCol  = document.createElement('td')
  divTableRow.appendChild(divTableCol)

  var dispName = this.name  
  
 
  if (this.repeats > 1) {
  
    if (this.hasRepeated > 0) {
  
    if (repeatCnt != undefined) 
     // it's undefined when the "add ..." button is pressed
     {
     if (repeatCnt == 0) {
     // 0 indicates that method was called by parent node (section or another group)
     // look at data node's first attribute to find out how many times this has repeated
        for (var i = 0; i < this.hasRepeated; i++) {
          // display the individual repeating group object
          
	  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<this.hasRepeated; i++)
	    {
	       this.display(this.appendNewDisplay, (i+1), dataSubNode[i])
	    }
	    
        }
        return // after displaying all the groups nothing else needs to be done
        
     }    // end of if (repeatCnt == 0...
     else {
    
      // method is calling itself to get to the repeating data subnodes 
      
       dispName += "_" + repeatCnt
    
     } 
     }  // end of if (repeatCnt != undefined ...
    else  // do some work for the add button
    {
	  dispName += "_" + this.hasRepeated
	  repeatCnt = this.hasRepeated     

     } 
     } // end of (this hasrepeated > 0
    else {
      this.hasRepeated = 1
      dispName += "_" + this.hasRepeated
      repeatCnt = this.hasRepeated     
      }  
    } // end of  if (this.repeats > 1...
    
    
    
    
  divTableCol.appendChild(document.createTextNode(dispName)); 

  
  if (this.required == 'M') {
     divTableCol.appendChild(document.createElement("br"))
     var divFont = document.createElement("font")
     divFont.setAttribute("size", "-2")
     divFont.appendChild(document.createTextNode("- Required"))
     divTableCol.appendChild(divFont)
    }
  
    

  divTableRow  = document.createElement('tr')
  divTable.appendChild(divTableRow)
  divTableCol  = document.createElement('td')
  divTableRow.appendChild(divTableCol)

  var i
  var alength = this.elements.length
  
  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.elements[i].display(divTableCol, repeatCnt, dataSubNode[i])
    }
    
    
  if (this.repeats > 1)
   {
    if (this.hasRepeated-1 < this.repeats-1)
    {
      divTableRow  = document.createElement('tr')
      divTable.appendChild(divTableRow)
      divTableCol  = document.createElement('td')
      divTableCol.setAttribute("align", 'right')
      divTableRow.appendChild(divTableCol)
       
      if (repeatCnt == this.hasRepeated) {
      	var inp = document.createElement("input")
      	inp.setAttribute('type', 'button')
      	inp.setAttribute('value', 'Add Another '+this.name)
      	inp.who = this
      	inp.onclick = function(){this.who.doRepeat(); divTableCol.removeChild(this);}
      	divTableCol.appendChild(inp)
      	}
    }
   }

  
}


function group_doRepeat()
{
   
   this.hasRepeated++
   this.display(this.appendNewDisplay); 
}

function group_clear()
{
  var alength = this.elements.length
  var i
  
  if (this.repeats > 1)
     {
      this.hasRepeated = 1;
      }

  for (i=0; i<alength; i++)
    {
        this.elements[i].clear()
    }
    
  x=this.appendNewDisplay.childNodes;
  for (i=x.length-1; i > -1;i--)
  {
      this.appendNewDisplay.removeChild(x[i])
  }
  
  //this.appendNewDisplay
    
  this.display(this.appendNewDisplay, 1)  
}



function group_verify()
{
  var ok = true
  var i
  var alength = this.elements.length
  for (i=0; i<alength; i++)
    {
       if (this.elements[i].verifyAll == undefined)
         testvalue = this.elements[i].verify()
       else
         testvalue = this.elements[i].verifyAll()
       if (testvalue != true)
         return false
    }
  return ok
}


function group_get()
{
  var a = "&"+this.id+"_repeat="+this.hasRepeated
  var alength = this.elements.length
  var i
  for (i=0; i<alength; i++)
    {
       if (this.elements[i].getAll == undefined) {
         b =  this.elements[i].get()
         a+=b
         }
       else {
         b = this.elements[i].getAll(); 
         a+=b 
         }
    }
  return a
}


function group_didUserEnterData()
{
  var alength = this.elements.length
  var i
  for (i=0; i<alength; i++)
    {
      if (this.elements[i].didUserEnterData()  == true)
        return true
    }

  return false
}

