 /* Copyright (C) 2006-2009 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 seg_display()
{
  docmnt ="";
  var alength = this.dataelements.length;

  docmnt+="<center>\n";
  if (this.help != null)      
      	docmnt+='<div title="header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]">'

  //alert(this.name)
  docmnt+="<font size=+1>"+this.name+"</font>\n";
  if (this.required == "M")
     docmnt+="<br><font size=-2>Mandatory Segment</font>\n";
  docmnt+="<table width=100% bgcolor=\"#FAEBD0\">";
  docmnt+="<tr><td width=10%>&nbsp;</td>\n";
  docmnt+="<td width=80% rowspan="+alength+">";
  var i;
  for (i=0; i<alength; i++)
    {
      //alert(i)
      docmnt+= this.dataelements[i].display();
    }
  docmnt+="</td><td width=10%>&nbsp;</td></tr>";
  docmnt+="</table>";
  docmnt+="</center>\n";
  if (this.help != null)      
	docmnt+="</div>\n";
  return docmnt;

}
function seg_clear()
{
  var alength = this.dataelements.length;

  var i;
  for (i=0; i<alength; i++)
    {
      //alert(i)
      this.dataelements[i].clear();
    }

}
function seg_verify()
{
  var ok = true;
  var dataEntered = false;
  var i;
  var alength = this.dataelements.length;

  if (this.required != "M") /* check to see if any data entered */
    {
      if (this.didUserEnterData() == false)
        return true;
    }

  //alert(this.name+" req is "+this.required)
  for (i=0; i<alength; i++)
    {
       testvalue = this.dataelements[i].verify();

       if (testvalue != true)
         ok=false;
    }
  if (ok == false)
     return false;
  if (this.rules == null)
    return ok;
  for (i = 0; i < this.rules.length; i++)
    {
       
       testvalue = this.rules[i].test(this);
       if (testvalue != true)
         {
            alert(testvalue);
            ok = false;
         }
    }
  return ok;
  
}

function seg_didUserEnterData()
{
      var dataEntered = false;
     
    for (i = 0; i < this.dataelements.length; i++)
        {
          if (this.dataelements[i].didUserEnterData() == true)
                dataEntered = true;

        }
      return dataEntered;
}

function seg_get()
{
  if (this.verify() != true)
    {
      return false;
    }
    
   
  
  var a = "";
  var alength = this.dataelements.length;
  for (i=0; i<alength; i++)
    {
       a+=this.dataelements[i].get();
    }
  return a
}
function Segment(node)
{
    if (node == null)
       return;
       
     
    var attr=node.attributes
    
    	//<segment id="N1" name='Payer Identification' required='O'>

    
    for (var i = 0; i < attr.length; i++) {
     if (attr[i].name == 'name')   
        this.name = attr[i].value
     if (attr[i].name == 'id')   
        this.id = attr[i].value
     if (attr[i].name == 'required')   
        this.required = attr[i].value
     if (attr[i].name == 'help')   
        this.help = attr[i].value
        }
     
     this.dataelements = new Array()
     this.name = this.name
     

     var cd=node.childNodes;
     var decnt=0
     this.fieldCrossRef = Array()

     for (var i = 0; i < cd.length; i++)
        {
               //alert(cd.item(i).tagName)
               if (cd.item(i).tagName == 'COMP'  ||  cd.item(i).tagName == 'comp' ) {
                         c = new Composite(cd.item(i));
	 		 this.dataelements[decnt]=c;
	 		 decnt++;
	 		 
	 		 }
	       else		 
               if (cd.item(i).tagName == 'DE' || cd.item(i).tagName == 'de' ) {
                         de = new DataElement(cd.item(i));
	 		 this.dataelements[decnt]=de;
	 		 //alert(decnt +' is in array at ' + de.pos)
	 		 this.fieldCrossRef[de.pos] = decnt;
	 		 decnt++;
	 		 }
	      //else alert(cd.item(i).tagName+' not used')
        }
        
        
     ruls=node.getElementsByTagName("rule");

     this.rules = new Array()

	 for (var i = 0; i < ruls.length; i++)
        {
 		 this.rules[i]=new DESRules(ruls.item(i));
             
        }


}
  new Segment(null);
  Segment.prototype.clear = seg_clear;
  Segment.prototype.display = seg_display;
  Segment.prototype.verify = seg_verify;
  Segment.prototype.get = seg_get;
  Segment.prototype.didUserEnterData   =seg_didUserEnterData;

