 /* Copyright (C) 2006-2007 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 makePositions(flds)
{
  var t = flds.split(",");
  var n = new Array()
  for (var i = 0; i < t.length; i++)
     n[i] = t[i]*10/10;
 return n
 }
 
 
function rules_test(section)
  {
    var sb = "";
    //alert('rules test' + this.fields)
    var positions = makePositions(this.fields)
    var positionCount = positions.length
    if (this.typerule == "oneOrMoreMustExist")
    {
        var pos;
        for (i = 0; i < positionCount; i++)
        {
          /*for (var j = 0; j < section.fieldCrossRef.length; j++) {
  
            alert(j+' field is ' +positions[j]+' field is ' +  section.fieldCrossRef[j])
            }*/
          pos = section.fieldCrossRef[positions[i]]
          //alert(positions[i]+' '+pos+' = '+section.fieldCrossRef[positions[i]])
          if (pos > section.elements.length) continue;
          pos = section.fieldCrossRef[pos]
          if (pos > section.elements.length) continue;
          if (section.elements[pos]==null) continue;
          de = section.elements[pos];
          if (de != null)
          if (de.get() != null && de.get().length > 0) return true; // one does exist
          sb += de.title + "\n";

        }
        return (section.name +" rule failure: at least one of these data element fields must be used: " + sb);
    }
    else
    if (this.typerule == "ifFirstExistsThenAllMustExist")
    {
        var pos;
        pos = section.fieldCrossRef[positions[0]];
        //alert('ifetame 1st='+pos)
        if (pos > section.elements.length) return true; // first doesn't exists
        if (section.elements[pos]==null) return true;

        de = section.elements[pos];
        if (de == null) return true;  // don't care
        if (de.get() == null || de.get().length == 0) return true;  // don't care

        var name = de.title;

        for (i = 1; i < positionCount; i++)
        {
          pos = section.fieldCrossRef[positions[i]];
          //alert('ifetame '+i+'='+pos)
          if (pos > section.elements.length)
              return ("if " + name + " is used then other fields are required");
          de = section.elements[pos];
          if (de != null)
          if (de.get() == null || de.get().length == 0)
              return ("if " + name + " is used then you must use " + de.title);
        }
        return true;
    }
    else
    if (this.typerule == "oneAndOnlyOneMayExist")
    {
        var pos;
	    var cnt = 0;
	    var  sb = "Enter data into only ONE of these fields: ";
        var pos;
        for (i = 0; i < positionCount; i++)
	        {
	          pos = section.fieldCrossRef[positions[i]];
	          if (pos > section.elements.length) continue;
	          if (section.elements[pos]==null) continue;
	          de =  section.elements[pos];
	          if (de == null)
	             continue;
	          sb += "\n"+de.title;
	          if (de.get() == null || de.get().length == 0)
	              continue;
	          cnt++;
	        }
	        if (cnt > 1)
    	        return sb;

       return true;
    }
    else
    if (this.typerule == "ifFirstExistsThenAtLeastOneMoreMustExist")
    {
        var pos;
	    var cnt = 0;
	    var sb = "";
        if (section.elements.length == 0) return true; // first doesn't exists
        pos = section.fieldCrossRef[positions[0]];
        if (section.elements[pos]==null) return true;
        de = section.elements[pos];
        if (de == null) return null;  // don't care
        if (de.get() == null || de.get().length == 0) return true;  // don't care
        var name = de.title
        for (i = 1; i < positionCount; i++)
        {
          pos = section.fieldCrossRef[positions[i]];
          if (pos > section.elements.length)
              continue;
          if (section.elements[pos]==null) continue;
          de = section.elements[pos];
          sb += "\n"+de.title;
          if (de == null)  continue;
          if (de.get() == null || de.get().length == 0) continue;
          cnt++;
        }
        if (cnt == 0)
          return ("if " + name + "\nis used then use least one of the following " + sb);
        return true;
    }
    else
    if (this.typerule == "allOrNoneMayExist")
    {
 	  if (section.elements.length == 0) return true; // first doesn't exists
      var pos;
      var cnt = 0;
      sb = "Enter data into none or all of the following fields: ";
      for (i = 0; i < positionCount; i++)
        {
          pos = section.fieldCrossRef[positions[i]];
          if (section.elements[pos]==null) continue;
          if (pos > section.elements.length) continue;
          de = section.elements[pos-1];
          if (de == null) continue;
          sb += "\n" + de.title;
          if (de.get() == null || de.get().length == 0) continue;
          cnt++;
       }
      if (cnt == 0 || cnt == positionCount)
          return true;
      return sb;
     }
    else
    if (this.typerule == "ifFirstExistsThenNoOthersMayExist")
    {
        if (section.elements.length == 0) return true; // first doesn't exists
        var pos;
        var cnt = 0;
        pos = section.fieldCrossRef[positions[0]];
        if (section.elements[pos]==null) return true;
        de = section.elements[pos];
        if (de == null) return null;  // don't care
        if (de.get() == null || de.get().length == 0) return true;  // don't care
        var sb = "if " + de.title + " is used then don't use the following: ";
        for (i = 1; i < positionCount; i++)
        {
          pos = section.fieldCrossRef[positions[i]];
          if (pos > section.elements.length)
             continue;
          if (section.elements[pos]==null)
             continue;
          de = section.elements[pos];
          if (de == null) continue;
          if (de.get() == null || de.get().length == 0) continue;
          sb+="\n"+de.title;
          cnt++;
        }
        if (cnt > 0)
          return sb;
        return true;
    }
    else
    alert("Don't know rule " + this.typerule)
}
function DESRules(node)
{
  if (node == null)
    return
    
  var attr=node.attributes
	//<rule type='oneOrMoreMustExist' fields='2,3'>
	
  for (var i = 0; i < attr.length; i++) {	
    if (attr[i].name == 'type')	
       this.typerule = attr[i].value
    if (attr[i].name == 'fields')
      this.fields = attr[i].value;
  }
}

new DESRules(null);
DESRules.prototype.test = rules_test;


