/* 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 date_display()
{
return "no date display";
}


function list_display(val)
{
  var i;

  var docmnt="";

  
  if (val > "")
    docmnt+="<option value=''>Not Selected";
  else
    docmnt+="<option value='' SELECTED>Not Selected";

  for (i=0; i<this.values.length; i++)
    {
	if (val == this.values[i]) 
		 slct=" SELECTED"
	else slct = " "	  
    

	docmnt+="<option value='"+this.values[i]+"'"+slct+">"+this.meanings[i];
    }
    

   return docmnt;

}
function list_size()
{

  return this.values.length;
}
function list_get(lpos)
{

  e = document.OBOEForm.elements[lpos]
  ndx = e.selectedIndex;
  if (ndx < 1)
    return "";

  return trim(this.values[ndx-1])
}
function List(node)
{
   this.values = new Array();
   this.meanings = new Array();
   
  if (node== null)
    return;

   ids = node.getElementsByTagName("id");


   for (var i = 0; i < ids.length; i++) {
	id =  ids.item(i)
	var attr = id.attributes
	for (var ii = 0; ii < attr.length; ii++) {
	if (attr[ii].name == 'code')
	  this.values[i]=attr[ii].value
	if    (attr[ii].name == 'value')
	 this.meanings[i]=attr[ii].value
	 }
   }


}

new List(null);
List.prototype.display = list_display;
List.prototype.size = list_size;
List.prototype.get = list_get;

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;
}


function de_clear()
{
  
  this.value = '';
  
  this.defaultValue = '';
  
  
  
   if (this.type == "ID")
    {
    if (this.list.size() == 1)
     {
	document.OBOEForm.elements['cb'+this.id].checked=false;
		
	}
	else
	if (this.list.size() < 7)
	{
	  var rbname = 'rb'+this.id
		 for (k = 0; k < document.OBOEForm.elements[rbname].length; k++)
	     document.OBOEForm.elements[rbname][k].checked = false;

	}
	else
	{
	   var inx = document.OBOEForm.elements[this.id].selectedIndex
	   if (inx > -1)
		document.OBOEForm.elements[this.id].options[inx].selected=false
  
	}
	}
   else {  
  		document.OBOEForm.elements[this.id].value='';
  	}
}


function de_display()
{
 var docmnt="";

 if (this.type == "NotUsed")
  {
   docmnt+="<input type='hidden' name='"+this.id+"' value='"+this.value+"'>\n";
  //alert(docmnt)
   return docmnt;
   }
 if (this.type == "TA")
  {
   maxcol =50
   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++;
    }
     if (this.help != null)	
		docmnt+='<div title="header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]">'
	docmnt+="<textarea  name='"+this.id+"' rows="+rowcnt+" cols="+maxcol+" >"+addNewLines(this.value)+"</textarea>";
	if (this.help != null)	
		docmnt+='</div>'
   return docmnt;
  }
 else
 if (this.type == "ID")
  {
    docmnt+="<table width=100% border=1><tr>";
    docmnt+="<td width=20%  align=right>"+this.name;
    
    if (this.required == "M")
	docmnt+="<br><font size=-2>* - Required</font>";
    docmnt+="</td><td width=80%  align=left>\n";
    //alert(this.id+" " + this.type)
    if (this.list.size() == 1)
     {
	docmnt+="<input type='hidden' name='"+this.id+"' value=''>";
	if (this.help != null)	
	  	docmnt+='<div title="header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]">'

	docmnt+="<input type='checkbox' name='cb"+this.id+"' value='"+this.list.values[0]+"'"
	if (this.value > "") 
	    docmnt+= " CLICKED "
	docmnt+=">"+this.list.meanings[0]+"\n";
	if (this.help != null)	
			docmnt+='</div>'

	    
     }
    else
    if (this.list.size() < 7)
     {
	var i;
	docmnt+="<input type='hidden' name='"+this.id+"' value=''>";
	//alert('setting rb'+this.id+ ' list size ' + this.list.size());
		if (this.help != null)	
			docmnt+='<div title="header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]">'

	for (i = 0; i < this.list.size(); i++)
	  {
	     docmnt+="<input type=radio name='rb"+this.id+"' value='"+this.list.values[i]+"'"
	     if (this.value == this.list.values[i]) 
		docmnt+= ' CHECKED '
	     
	     
	     docmnt+=">"+this.list.meanings[i]+"<br>\n";
	  }
		 if (this.help != null)	
			docmnt+='</div>'

     }
    else
    {
	   if (this.help != null)	
	   	docmnt+='<div title="header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]">'

    docmnt+="<select name='"+this.id+"' size=3";
    docmnt+=">\n";
    docmnt+=this.list.display(this.value);
    docmnt+="</select>\n";
	   if (this.help != null)	
	   	docmnt+='</div>'

    }

    docmnt+="</td>";
    docmnt+="<td width=20%>&nbsp;";
    docmnt+="</table>\n";
  //alert(docmnt)
    return docmnt;

    }
 else
 if (this.type == "DT")
  {


    docmnt+="<table width=100% border=1><tr>";
    docmnt+="<td width=20%  align=right>"+this.name;
	 if (this.help != null)	
	 	docmnt+='<div title="header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]">'

  docmnt+="<td align=left><INPUT TYPE=\"text\" NAME=\""+this.id+"\" VALUE=\""+this.value+"\" SIZE=10>"
  docmnt+="<A HREF=\"#\" "
  docmnt+="   onClick=\"cal1.select(document.OBOEForm."+this.id+",'anchor1"+this.id+"','yyyyMMdd'); return false;\""
  docmnt+=" NAME=\"anchor1"+this.id+"\" ID=\"anchor1"+this.id+"\"><IMG SRC=\"images/calendar.gif\" ALT=\"?\" BORDER=0></A>"
	 if (this.help != null)	
	 	docmnt+='</div>'

  
  docmnt+="</table>"
  return docmnt;
  
 }

  if (this.type == "DisplayOnly")
   {
    docmnt+="<table width=100% border=1><tr>";
    docmnt+="<td width=20%  align=right>"+this.name+": <td align=left>";
    docmnt+="<input type='hidden' name='"+this.id+"' value='"+this.value+"'>";
    docmnt+=this.value;
    docmnt+="</table>";
    
    }
  else
    {
	if (this.max > 30)
	 dismax=30
	else
	 dismax=this.max
	docmnt+="<table width=100% border=1><tr>";
	docmnt+="<td width=20% align=right>"+this.name+": <td align=left>";

	if (this.help != null)	
		docmnt+='<div title="header=['+this.name+'] body=['+this.help+'] delay=[450] singleclickstop=[on] offsetx=[-200]">'
  
	docmnt+="<input type='text' size=" + dismax + " MAXLENGTH='" + this.max;
	docmnt+= " name='"+this.id+"' id='"+this.id+"' value='"+this.value+"'"
	docmnt+= " onkeyup=upperCase('"+this.id+"') "
	docmnt+= ">\n";
	
	if (this.help != null)	
		docmnt+="</div>"
	docmnt+="</table>" 
	
     }
  docmnt+="\n";
  //alert(docmnt)
  return docmnt;
}

function upperCase(x)
{
var y=document.getElementById(x).value;
document.getElementById(x).value=y.toUpperCase();
}

function de_verify()
{

  if (this.type == "NotUsed")
     return true;
  if (this.type == "DisplayOnly")
	return true;
  var i;
  var len;
  var val;
  if (this.type == 'DT') {
	 val = getDate(this);
	}
  else
    val = document.OBOEForm.elements[this.id].value
  if (this.type == "ID")
   {
    if (this.list.size() < 7)
     len = val.length;
    else
     len = this.list.get(this.id).length;

    }
  else
     len = trim(val).length;

  if (len == 0)
    {
     if (this.required == "M")
	{
	 alert('Field '+this.name+" is a required field when the segment 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 = 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 de_get()
{
    var i;

    if (this.type == "NotUsed")
	    return "";
    if (this.type == "DisplayOnly")
	    return '&'+this.id+"="+this.value;
    if (this.type == "ID")
	{
	if (this.list.size() == 1)
	{
	var cbname = 'cb'+this.id
	//alert(cbname)
		if (document.OBOEForm.elements[cbname].checked == true)
		  {
		   document.OBOEForm.elements[this.id].value = document.OBOEForm.elements[cbname].value;
		   return '&'+this.id+"="+document.OBOEForm.elements[cbname].value;
		  }
		return "";
	 }
	else if (this.list.size() < 7)
	 {
    	 var rbname = 'rb'+this.id
    	 //alert('getting '+rbname)
    	 for (k = 0; k < document.OBOEForm.elements[rbname].length; k++)
    	   {
    	   if (document.OBOEForm.elements[rbname][k].checked == true)
    	     {
    		document.OBOEForm.elements[this.id].value = document.OBOEForm.elements[rbname][k].value;
    		return '&'+this.id+"="+escape(document.OBOEForm.elements[rbname][k].value);
    	     }
    	   }
    	 return "";
		}
	else
	 {
	   return trim('&'+this.id+"="+this.list.get(this.id));
	 }
	}
    else if (this.type == "DT")
	     {
	     
     
	     return '&'+this.id+"="+getDate(this);

	}
    else { //alert(this.id);
	if (trim(document.OBOEForm.elements[this.id].value).length > 0)
	 {

	   return '&'+this.id+"="+escape(trim(document.OBOEForm.elements[this.id].value));
	  }
	  }

   return ""
  }


function getDate(t)
{

return  trim(document.OBOEForm.elements[t.id].value)

	
}

function DataElement(node)
{

  if (node == null)
     return;
    
    this.help = null;
  
    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 == 'pos')   
	   this.pos = attr[i].value
	if (attr[i].name == 'id')   
	   this.id = attr[i].value
	if (attr[i].name == 'name')   
	   this.name = 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
    
    }
     
	//<de pos='1' id='something' name='Entity Identifier Code' required='M' min='2' max='3' type='AN'>

   
/*
   var a = node.getAttribute('value')

   if (a != null && a != 'null' && a != '')
		this.value = attr['value'].value
   else this.value = ''
*/
   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);
		}
	}
   }


  this.list = null;
  l = node.getElementsByTagName("valueList");

  if (l == null) {
    return;
    }

   this.list = new List(l.item(0));
   if (this.list.size() == 0 && this.type == 'ID')
     this.type = 'AN'

}

function de_didUserEnterData()
{
   if (this.type == "DisplayOnly")
     return false;
   if (this.type == "NotUsed")
     return false;
     
    
   
   return (this.get() > '&'+this.id+"=");

}

   new DataElement(null);

   DataElement.prototype.display = de_display;
   DataElement.prototype.clear = de_clear;
   DataElement.prototype.verify = de_verify;
   DataElement.prototype.get = de_get;
   DataElement.prototype.didUserEnterData = de_didUserEnterData;

