//utility function

function hasValidValue(form_name, field_name)
{
  var field_value;
  var has_value = false;
  for( var i = 0; i < field_name.length; i++)
  {
    var nField = field_name[i];
    var field_type = form_name.elements[nField].type;
    switch(field_type)
    {
      case 'text': //single text field
        field_value = form_name.elements[nField].value;
        if (field_value != "")
        {
          has_value = true;
        }
        break;
        
      case 'textarea': // text area
        field_value = form_name.elements[nField].value;
        if (field_value != "")
        {
          has_value = true;
        }      
        break;
        
      case 'select-one': // selection list whereby only one option can be selected
        field_value = form_name.elements[nField].selectedIndex;
        if (field_value != 0)
        {
          has_value = true;
        } 
        break;
        
      case 'checkbox':
        field_value = form_name.elements[nField].checked;
        if (field_value == true)
        {
          has_value = true;
        }
        break;      
        
      default:
        has_value = false;
        alert("No case found to handle the HTML element. Contact the Administrator");
        break;  
    }
    if (has_value == true)
    {
      break;
    }
  }  
  return has_value;
}


// define the FormField() constructor 
// eg. field2 = new FormField("Monogram", "vw-initial1", "vw-initial2", "vw-initial3");
function FormField()
{ 
  this.display_name = arguments[0];
  this.name = new Array(arguments.length-1); 
  for(i=1; i < arguments.length; i++)
  { 
    this.name[i-1] = arguments[i]; 
  }
}


// define the ReqdField() constructor 
function ReqdField()
{ 
  // get the form name, it should be the first argument
  if (arguments[0] != "")
  {
    this.form_name = arguments[0];
  } 
   
  // get FormField Objects
  this.reqd_fields = new Array(arguments.length-1); 
  for(i=1; i < arguments.length; i++)
  { 
    this.reqd_fields[i-1] = arguments[i]; 
  }
  
  this.validate = function(valid)
  {
    if (valid == true)
    {
      var field_name;
      var hasValue;
      for(i=0; i<this.reqd_fields.length; i++)
      {
        field_name = this.reqd_fields[i].name;
        hasValue = hasValidValue(this.form_name, field_name);
        if (hasValue == false)
        {
          alert(this.reqd_fields[i].display_name + " is required");
          valid = false;
          break;
        }
      }
    }
    return valid;
  }  
}

// define the MuExField() constructor 
function MuExField()
{ 
  // get the form name, it should be the first argument
  if (arguments[0] != "")
  {
    this.form_name = arguments[0];
  } 
  
  // get FormField Objects
  this.mu_ex_fields = new Array(arguments.length-1); 
  for(i=1; i<arguments.length; i++)
  { 
    this.mu_ex_fields[i-1] = arguments[i]; 
  }
  
  this.validate = function(valid)
  {
    if (valid == true)
    {
      var field_name;
      var hasValue;
      var flag = 0;
      for(i=0; i<this.mu_ex_fields.length; i++)
      {
        field_name = this.mu_ex_fields[i].name;
        hasValue = hasValidValue(this.form_name, field_name);
        if (hasValue == true && flag == 1)
        {
          alert(this.generateError(this.mu_ex_fields));
          valid = false;
          break;
        }
        else if (hasValue == true)
        {
          flag = 1;
        }
      }
    }
    return valid;
  }
 
  // function to generate the error string
  this.generateError = function(field_array)
  {
    var err_str = "You can either enter ";
    for(j=0; j<field_array.length; j++)
    {
      err_str = err_str + field_array[j].display_name;
      if (j < field_array.length-1)
      {
        err_str = err_str + " or ";
      }
    }
    return err_str;
  }
  
}


// define the ReqdFieldIf() constructor 
function ReqdFieldIf()
{ 
  // get the form name, it should be the first argument
  if (arguments[0] != "")
  {
    this.form_name = arguments[0];
  } 
  
  // get the base field
  if (arguments[1] != "")
  {
    this.base_field = arguments[1];
  }  
  // get FormField Objects
  this.reqd_fields_if = new Array(arguments.length-2); 
  for(i=2; i<arguments.length; i++)
  { 
    this.reqd_fields_if[i-2] = arguments[i]; 
  }
  
  this.validate = function(valid)
  {
    // if one of the base field has value then the others can't be blank
    if (valid == true)
    {
      var field_name;
      var hasValue;
      field_name = this.base_field.name;
      
      hasValue = hasValidValue(this.form_name, field_name);

      if (hasValue == true)
      {
        for(i=0; i<this.reqd_fields_if.length; i++)
        {
          field_name = this.reqd_fields_if[i].name;
          hasValue = hasValidValue(this.form_name, field_name);
          if (hasValue == false)
          {
            alert(this.generateError(this.reqd_fields_if));
            valid = false;
            break;
          }
        }       
      }
    }
    
    // if the others have value then the one of the base field should have a value.
    if (valid == true)
    {
      var field_name;
      var hasValue;
      for(i=0; i<this.reqd_fields_if.length; i++)
      {
        field_name = this.reqd_fields_if[i].name;
        hasValue = hasValidValue(this.form_name, field_name);
        if (hasValue == true)
        {
          field_name = this.base_field.name;
          hasValue = hasValidValue(this.form_name, field_name); 
          if (hasValue == false)
          {
            alert("Enter value for " + this.base_field.display_name);
            valid = false;
            break;
          }                   
        }
      }      
    }    
      
    return valid;
  }
  
  // function to generate the error string
  this.generateError = function(others_f)
  {
    var err_str = "Enter value for " ;
    for(j=0; j<others_f.length; j++)
    {
      err_str = err_str + others_f[j].display_name;
      if (j < others_f.length-1)
      {
        err_str = err_str + " , ";
      }
    }
    return err_str;
  } 
}









