
/* 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 Element(node)
{

     if (node == null)
        return
    this.type = "AN"
    this.help = null
    this.value=""
    this.multiple = "N"
    this.buttons = "N"
    this.list = null
    this.repeatCnt = 0
  
    var attr=node.attributes
    
    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 == 'type')   
	   this.type = attr[i].value
	if (attr[i].name == 'value')   
	   this.value = attr[i].value
	if (attr[i].name == 'required')   
          this.required = attr[i].value
	if (attr[i].name == 'min')   
         this.min = attr[i].value
	if (attr[i].name == 'max')   
         this.max = attr[i].value
	if (attr[i].name == 'help')   
         this.help = attr[i].value
    
    }
     
	//<element id='something' name='Entity Identifier Code' required='M' min='2' max='3' type='AN'>

   
   nodes=node.childNodes
   for (var k=0; k<nodes.length; k++){
      nodl = nodes[k]
      if (nodl.nodeType == 3) {
          var t = trim(nodl.nodeValue)
          if (t.length > 0) {
              this.value = t
              //alert(t+t.length)
              }
      }
   }


  l = node.getElementsByTagName("valueList")

  if (l == null) {
    return
    }


  for (var i = 0; i < nodes.length; i++)
     { 
        if (nodes.item(i).tagName == 'valueList')
          {
             var attr=nodes.item(i).attributes
      	     for (var i = 0; i < attr.length; i++) 
      	       { 
		 if (attr[i].name == 'multiple')
		       this.multiple = attr[i].value
		 if (attr[i].name == 'buttons')
		       this.buttons = attr[i].value
     	        }
   	  }
      }


   

   this.list = new List(l.item(0))
   if (this.list.size() == 0 && this.type == 'ID')
     this.type = 'AN'

}

function element_didUserEnterData()
{
   if (this.type == "DisplayOnly")
     return false
   if (this.type == "NotUsed")
     return false
     
    if (this.repeatCnt == 0)
      return (this.get() > '&'+this.id+"=")
    
    for (var i = 1; i <= this.repeatCnt; i++)  
      {
         
         if (this.get(this.id+"_"+i) > '&'+this.id+"_"+i+'=')
           return true
      }
    
    return false

}

   new Element(null)

   Element.prototype.display = element_display
   Element.prototype.clear = element_clear
   Element.prototype.verify = element_verify
   Element.prototype.verifyAll = element_verifyAll
   Element.prototype.get = element_get
   Element.prototype.getAll = element_getAll
   Element.prototype.didUserEnterData = element_didUserEnterData



function date_display() { return "no date display"; }

var goWithCheckBoxes = 8


function element_clear()
{
  
  this.value = ''
  
  this.defaultValue = ''
   displayID = this.id
   if (this.repeatCnt > 0)
     displayID += "_1";  // just need to clear the first field, the rest should be removed
  
  
   if (this.type == "ID")
    {
    if (this.multiple == 'Y') 
    {
      if (this.buttons == 'Y')
       {
         var cbname = 'cb'+displayID
       	 for (k = 0; k < document.forms[0].elements[cbname].length; k++)
            document.forms[0].elements[cbname][k].checked = false

       }
      else
      {
          var inx = document.forms[0].elements[displayID].selectedIndex
          if (inx > -1)
              document.forms[0].elements[displayID].options[inx].selected=false
  
      }
    }
    else {
    if (this.list.size() == 1)
     {
       document.forms[0].elements['cb'+displayID].checked=false
             
       }
      else
      if (this.list.size() < goWithCheckBoxes  || this.buttons == 'Y')
       {
         var rbname = 'rb'+displayID
       	 for (k = 0; k < document.forms[0].elements[rbname].length; k++)
            document.forms[0].elements[rbname][k].checked = false

       }
      else
      {
          var inx = document.forms[0].elements[displayID].selectedIndex
          if (inx > -1)
              document.forms[0].elements[displayID].options[inx].selected=false
  
      }
      }
    }  
   else {  
  	document.forms[0].elements[displayID].value=''
  	}
}


function element_display(node, repeatCnt, dataNode)
{
   
    if (repeatCnt == undefined)
       repeatCnt = 0
   
    this.repeatCnt = repeatCnt
    
    if (dataNode == undefined)
      this.value = ""
    else {
          this.value = ""
          var attr=dataNode.attributes
      	  for (var i = 0; i < attr.length; i++) {
            if (attr[i].name == 'value')   
              this.value = attr[i].value        
      } // for
}
    
    
    displayID = this.id
    if (this.repeatCnt > 0)
       displayID+="_"+this.repeatCnt
    
    if (this.type == "NotUsed")
     {
       var inp = document.createElement("input")
       inp.setAttribute('type', 'hidden')
       inp.setAttribute('name' , displayID)
       inp.setAttribute('value' ,this.value)
       node.appendChild(inp)
       return 
      }
      

   var div1 = document.createElement("div")
   node.appendChild(div1)
   if (this.help != null)      
         div1.setAttribute("title", 'header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]')

      
  if (this.type == "TA")
  {
   maxcol =80
   rowcnt =10
   colcnt=0
   for (var ii=0; ii<this.value.length;ii++)
    {
      
       if (this.value.charAt(ii) == '\n') {
          rowcnt++
          if (colcnt > maxcol) maxcol=colcnt
          colcnt=0
          continue
       }
       colcnt++
    }
   var ta = document.createElement("textarea")
   ta.setAttribute('name', displayID)
   ta.setAttribute('rows', rowcnt)
   ta.setAttribute('cols', maxcol)
   ta.appendChild(document.createTextNode(addNewLines(this.value)))
   div1.appendChild(ta)   	
   return
  }
  
 if (this.type == "ID")
  {
  
    var divTable  = document.createElement('table')
    divTable.setAttribute("width", "100%")
    divTable.setAttribute("border", "1")
    divTable.setAttribute("bgcolor", "#FAEBD0")
    div1.appendChild(divTable)
    
    var divTableRow  = document.createElement('tr')
    divTable.appendChild(divTableRow)
    var divTableCol  = document.createElement('td')
    divTableRow.appendChild(divTableCol)
    divTableCol.setAttribute("width", "20%")
    divTableCol.setAttribute("align", "right")
    divTableCol.appendChild(document.createTextNode(this.name))
    
    
  
    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)
      }

  
  divTableCol = document.createElement('td')
  divTableRow.appendChild(divTableCol)
  divTableCol.setAttribute("width" , "80%")
  divTableCol.setAttribute("align" , "left")
  
  if (this.multiple == 'Y')
    {

    if (this.buttons == 'Y')
     {
       var i
       var inp = document.createElement("input")
       inp.setAttribute('type', 'hidden')
       inp.setAttribute('name' ,displayID)
       inp.setAttribute('value' ,this.value)
       divTableCol.appendChild(inp)
       
       
       var lineLength = 0
       var selectArray = this.value.split(';')
       
       for (var i = 0; i < this.list.size(); i++)
         {
            inp = document.createElement("input")
	    inp.setAttribute('type', 'checkbox')
	    inp.setAttribute('name' ,'cb'+displayID)
	    inp.setAttribute('value' ,this.list.values[i])
	    divTableCol.appendChild(inp)

            
            for (j = 0; j < selectArray.length; j++) 
              {
            	if (selectArray[j] == this.list.values[i])  {
            	  inp.checked = 'true'
            	  break
            	  }
               }	  
            
            divTableCol.appendChild(document.createTextNode(this.list.meanings[i]))
            
            lineLength += this.list.meanings[i].length
            
            if (lineLength > 50)
              {   
                divTableCol.appendChild(document.createElement('br'))
                lineLength = 0
              }
         }
     }
    else
     {
      var inp = document.createElement("select")
      inp.multiple='true'; 
      inp.setAttribute('name', displayID)
      inp.setAttribute('size' , 3)
      divTableCol.appendChild(inp)
      
      this.list.display(this.value, inp)
      //append the id lists here docmnt+=this.list.display(this.value)
   
      }

    }
    else
    {
        
      if (this.list.size() == 1)
       {
  
         var inp = document.createElement("input")
         inp.setAttribute('type', 'hidden')
         inp.setAttribute('name' ,'cb'+displayID)
         inp.setAttribute('value' ,this.value)
         divTableCol.appendChild(inp)
  
         inp = document.createElement("input")
         inp.setAttribute('type', 'checkbox')
         inp.setAttribute('name' ,displayID)
         inp.setAttribute('value' ,this.list.values[0])
         divTableCol.appendChild(inp)
  
         
         if (this.value > "") 
             inp.clicked = 'true'
             
         divTableCol.appendChild(document.createTextNode(this.list.meanings[0]))
     
       }
       
      else
      
      if (this.list.size() < goWithCheckBoxes  || this.buttons == 'Y')
       {
         var i
         var inp = document.createElement("input")
         inp.setAttribute('type', 'hidden')
         inp.setAttribute('name' ,displayID)
         inp.setAttribute('value' ,this.value)
         divTableCol.appendChild(inp)
         
         var lineLength = 0
         
         for (var i = 0; i < this.list.size(); i++)
           {
           inp = document.createElement("input")
           inp.setAttribute('type', 'radio')
           inp.setAttribute('name' , 'rb'+displayID)
           inp.setAttribute('value' ,this.list.values[i])
           divTableCol.appendChild(inp)
         
           if (this.value == this.list.values[i]) 
               inp.checked = 'true'
              
           divTableCol.appendChild(document.createTextNode(this.list.meanings[i]))
              
           lineLength += this.list.meanings[i].length
              
           if (lineLength > 50)
             {
               divTableCol.appendChild(document.createElement('br'))
               lineLength = 0
             }
        }
  
    }
    else
    {
    
      var inp = document.createElement("select")
      inp.setAttribute('name', displayID)
      inp.setAttribute('size', 3)
      
      this.list.display(this.value, inp)
    
    
      divTableCol.appendChild(inp)     
      }
    }
    
    
  
    return

 }  // end of id work



 if (this.type == "DT")
  {
  
  
    var divTable  = document.createElement('table')
    divTable.setAttribute("width", "100%")
    divTable.setAttribute("border", 1)
    div1.appendChild(divTable)
    
    var divTableRow  = document.createElement('tr')
    divTable.appendChild(divTableRow)
    var divTableCol  = document.createElement('td')
    divTableRow.appendChild(divTableCol)
    divTableCol.appendChild(document.createTextNode(this.name)); 
    divTableCol.setAttribute('width', '20%')
    divTableCol.setAttribute('align', 'right')
    
    var divTableCol  = document.createElement('td')
    divTableRow.appendChild(divTableCol)

    var inp = document.createElement("input")
    inp.setAttribute('type', 'text')
    inp.setAttribute('name', displayID)
    inp.setAttribute('id', displayID)
    inp.setAttribute('value', this.value)
    inp.setAttribute('size', 18)
    divTableCol.appendChild(inp)




    inp = document.createElement("a")
    inp.setAttribute('href', '#')
    inp.setAttribute('onclick', "cal1.select(document.forms[0]."+displayID+",'anchor1"+displayID+"','M/d/yyyy'); return false;")
    inp.setAttribute('name', "anchor1"+displayID)
    inp.setAttribute('id', "anchor1"+displayID)
    divTableCol.appendChild(inp)

    var img = document.createElement("IMG")
    img.setAttribute('src', "images/calendar.gif")
    img.setAttribute('alt', "?")
    img.setAttribute('border', 0)
    inp.appendChild(img)
    
    return; 

 }


  if (this.type == "DisplayOnly")
   {

    var divTable  = document.createElement('table')
    divTable.setAttribute("width", "100%")
    divTable.setAttribute("border", 1)
    div1.appendChild(divTable)
    
    var divTableRow  = document.createElement('tr')
    divTable.appendChild(divTableRow)
    var divTableCol  = document.createElement('td')
    divTableRow.appendChild(divTableCol)
    divTableCol.appendChild(document.createTextNode(this.name)); 
    divTableCol.setAttribute('width', '20%')
    
    divTableCol  = document.createElement('td')
    divTableRow.appendChild(divTableCol)
    divTableCol.appendChild(document.createTextNode(this.value)); 
    divTableCol.setAttribute('name', displayID)
    
    // removed hidden field; seemed redundant
    
    return
    
    }
    
    
  else
    {
      if (this.max > 30)
        dismax=30
      else
        dismax=this.max
        
        
        
    var divTable  = document.createElement('table')
    divTable.setAttribute("width", "100%")
    divTable.setAttribute("border", 1)
    div1.appendChild(divTable)
    
    var divTableRow  = document.createElement('tr')
    divTable.appendChild(divTableRow)
    var divTableCol  = document.createElement('td')
    divTableRow.appendChild(divTableCol)
    divTableCol.appendChild(document.createTextNode(this.name)); 
    divTableCol.setAttribute('align', 'right')
    divTableCol.setAttribute('width', '20%')
        
    var divTableCol  = document.createElement('td')
    divTableRow.appendChild(divTableCol)
    
    divTableCol.setAttribute('align', 'left')

    var inp = document.createElement('input')
    inp.setAttribute('type', 'text')
    inp.setAttribute('size', dismax)
    inp.setAttribute('maxlength', this.max)
    inp.setAttribute('name', displayID)
    inp.setAttribute('value', this.value)
    divTableCol.appendChild(inp)
        
      
    return
   }

    

}


function element_verifyAll()
{
  
  if (this.repeatCnt == 0)
     return this.verify(this.id)

   var retString = true
   
   for (var i = 1; i <= this.repeatCnt; i++)
      retString &= this.verify(this.id+"_"+i)
    
   return retString
   
   
}



function element_verify(displayID)
{

  if (this.type == "NotUsed")
     return true
  if (this.type == "DisplayOnly")
      return true
  var i
  var len
  var val
  
  if (displayID == undefined)
    displayID = this.id
  
  
  if (this.type == 'DT') {
         val = getDate(displayID)
       }
  else
    val = document.forms[0].elements[displayID].value
  if (this.type == "ID")
   {
    if (this.list.size() < goWithCheckBoxes  || this.buttons == 'Y')
     len = val.length
    else
     len = this.list.get(displayID).length

    }
  else
     len = trim(val).length

  if (len == 0)
    {
     if (this.required == "M")
       {
        alert('Field '+this.name+" is a required field when the section is mandatory")
        return "Missing data"
       }
    }
  else if (len < this.min)
         {
           alert(this.name+" value is too short. "+len+" "+trim(val))
           return "Too short"
         }
  else if (this.type == "DT")
         {

          ret = true
          //ret = checkDate(val)

          if (ret != true)
            {
             alert(this.name+" "+ret)
             return ret
            }
         }
  else if (this.type == "TM")
         {
          ret = checkTime(val)
          if (ret != true)
            {
             alert(this.name+" "+ret)
              return ret
            }
         }
  else if (this.type.substring(0,1) == "N")
        {
        //alert(this.get(i))
         ret = checkInteger(val)
         if (ret != true)
           {
             alert(this.name+" "+ret)
              return ret
           }
         }
  else if (this.type == "R")
         {
          ret = checkReal(val)
          if (ret != true)
            {
             alert(this.name+" "+ret)
             return ret
            }
         }
  return true
}



function checkDate(inString)
{


   var len = inString.length
   if (len == 0)
     return true
   if (len != 6 && len != 8)
         return("Date length "+len+" incorrect")

   var ret = checkInteger(inString)
   if (ret != true)
      return ret

   var year
   var mo
   var da
   if (len == 8)
    {
      year = inString.substring(0,4)
      if (year < 1900 || year > 2100)
        return "Invalid year "+year
      mo = inString.substring(4,6)
      da = inString.substring(6,8)
    }
   else {
      // len == 6 ; already numeric
      year = inString.substring(0,2)
      mo = inString.substring(2,4)
      da = inString.substring(4,6)
    }
   if (mo < 1 || mo > 12)
      return "Invalid month"
   if (da < 1)
      return "Invalid day"
   mo = mo - 1
   var monthday = new Array(30,28,30,31,30,31,31,30,31,30,31,30)
   if ((year % 4) == 0)
     monthday[1] = 29
   if (da > monthday[mo])
      return "Invalid day for month"



   return true
   
   
}
function checkTime(inString)
{
   var len = inString.length
   if (len == 0)
     return true
   if (len != 4)
         return("Time length "+len+" incorrect, must length of 4, use 24 hour clock")

   var ret = checkInteger(inString)
   if (ret != true)
      return ret

   hour = inString.substring(0,2)
   if (hour < 0)
      return("Time value for hour too low, must be 00-23")
   if (hour > 23)
      return("Time value for hour too high, must be 00-23")

   min = inString.substring(2,4)
   if (min < 0)
      return("Time value for minute too low, must be 00-59")
   if (min > 59)
      return("Time value for minute too high, must be 00-59")

   return true
}
function checkInteger(inString)
{
   var len = inString.length
   if (len == 0)
     return true
   var i
   for (i = 0; i < len; i++)
     if (inString.charAt(i) < '0' || inString.charAt(i) > '9')
        return "Invalid numeric character at position "+i+" with a value of "+inString.charAt(i) +" in string "+ inString

   return true
}
function checkReal(inString)
{
   var len = inString.length
   if (len == 0)
     return true
     
   inString = trim(inString)
   var dec = 0
   var i
   var neg= 0
   for (i = 0; i < len; i++)
    {
     if ((inString.charAt(i) < '0' || inString.charAt(i) > '9') && inString.charAt(i) != '.' && inString.charAt(i) != '-')
        return "Invalid numeric character and not a decimal"
     if (inString.charAt(i) == '.') {
        if (dec > 0)
             return "Too many decimal point characters"
       dec++
       }
     if (inString.charAt(i) == '-') {
       if (i !=0  && i != inString.length-1)
           return "Invalid position for negative indicator";    
       if (neg > 0)
           return "Too many negative indicators";    
        neg++
       } 
     }


   return true

}

function element_getAll()
{
  
  if (this.repeatCnt == 0)
     return this.get(this.id)

   var retString = ""
   for (var i = 1; i <= this.repeatCnt; i++) {
      b = this.get(this.id+"_"+i)
      if (b > "")
        retString+=b
      }
    
   return retString
   
   
}

function element_get(displayID)
{
    var i

    if (this.type == "NotUsed")
           return ""
    if (this.type == "DisplayOnly")
           return '&'+this.id+"="+this.value
           
           
    if (displayID == undefined)
      displayID = this.id
      
    if (this.type == "ID")
      {
      
      if (this.multiple == 'Y')
      {


       if (this.buttons == 'Y')
        {
    	 var cbname = 'cb'+displayID
    	 //alert('getting '+rbname)
    	 var got=""
    	 for (k = 0; k < document.forms[0].elements[cbname].length; k++)
    	   {
    	   if (document.forms[0].elements[cbname][k].checked == true)
    	     {
    	       if (got == "")
    	          got = '&'+displayID+"="+escape(document.forms[0].elements[cbname][k].value)
    	       else
    	          got += ";"+escape(document.forms[0].elements[cbname][k].value)
    	     }
    	   }
    	 return got
      	}
      else
        {
          return trim('&'+displayID+"="+this.list.get(displayID))
        }
      
      }
      
      
      
      else {
      if (this.list.size() == 1)
       {
	var cbname = 'cb'+displayID
	//alert(cbname)
      	if (document.forms[0].elements[cbname].checked == true)
      	  {
      	   document.forms[0].elements[displayID].value = document.forms[0].elements[cbname].value
      	   return '&'+displayID+"="+document.forms[0].elements[cbname].value
      	  }
      	return ""
        }
      else if (this.list.size() < goWithCheckBoxes  || this.buttons == 'Y')
        {
    	 var rbname = 'rb'+displayID
    	 //alert('getting '+rbname)
    	 for (k = 0; k < document.forms[0].elements[rbname].length; k++)
    	   {
    	   if (document.forms[0].elements[rbname][k].checked == true)
    	     {
    	       document.forms[0].elements[displayID].value = document.forms[0].elements[rbname][k].value
    	       return '&'+displayID+"="+escape(document.forms[0].elements[rbname][k].value)
    	     }
    	   }
    	 return ""
      	}
      else
        {
          return trim('&'+displayID+"="+this.list.get(displayID))
        }
       } 
      }
    else if (this.type == "DT")
            {
            
          //alert("getting "+displayID)
     
            return '&'+displayID+"="+getDate(displayID)

	}
    else { //alert(displayID+ " "+ this.type+ document.forms[0].elements[displayID])
      //if (trim(document.forms[0].elements[displayID].value).length > 0)
      //  {
          return '&'+displayID+"="+escape(trim(document.forms[0].elements[displayID].value))
      //   }
         }

   return ""
  }


function getDate(t)
{

return  trim(document.forms[0].elements[t].value)

	
}



function addNewLines(ins)
{
  outxt = ""
  var i
  for (i=0; i<ins.length; i++)
    {
      if (ins.charAt(i) == '~')
        outxt += '\n'
      else
        outxt += ins.charAt(i)
    }
  return outxt
}

