function validateFields(form, fieldInfo)
{

  for (var i=0; i<fieldInfo.length; i++)
  {
    var row = fieldInfo[i].split(",");
    var satisfiesIfConditions = true;
    while (row[0].match("^if:"))
    {
      var condition = row[0];
      condition = condition.replace("if:", "");
      var parts = condition.split("=");
      var fieldToCheck = parts[0];
      var valueToCheck = parts[1];
      var fieldnameValue = "";      
      if (form[fieldToCheck].type == undefined) 
      {
        for (var j=0; j<form[fieldToCheck].length; j++)
        {
          if (form[fieldToCheck][j].checked)
            fieldnameValue = form[fieldToCheck][j].value;
        }
      }
      else                    
        fieldnameValue = form[parts[0]].value;

      if (fieldnameValue != valueToCheck)
      {
        satisfiesIfConditions = false;
        break;
      }
      else 
        row.shift();    
    }

    if (!satisfiesIfConditions)
      continue;


    var requirement = row[0];
    var fieldName   = row[1];

    if (row.length == 6)
    {
      var fieldName2   = row[2];
      var fieldName3   = row[3];
      var date_flag    = row[4];
      var errorMessage = row[5];
    }
    else if (row.length == 4)
    {
      fieldName2   = row[2];
      errorMessage = row[3];
    }
    else
      errorMessage = row[2];

    switch (requirement)
    {
      case "required":
        // if radio buttons, do separate check:
        if (form[fieldName].type == undefined)
        {
          var oneIsChecked = false;
          for (j=0; j<form[fieldName].length; j++)
          {
            if (form[fieldName][j].checked)
              oneIsChecked = true;
          }
          if (!oneIsChecked)
          {
            alertMessage(form[fieldName], errorMessage);
            return false;           
          }
        }
        // otherwise, just perform ordinary "required" check.
        else if (!form[fieldName].value)
        {         
          alertMessage(form[fieldName], errorMessage);
          return false;
        }
        break;

      case "valid_email":
        if (form[fieldName].value && !isValidEmail(form[fieldName].value))
        {
          alertMessage(form[fieldName], errorMessage);
          return false;         
        }
        break;

      default:
        alert("Unknown requirement flag in validateFields(): " + requirement);
        return false;
    }
  }
  
  return true;
}

function alertMessage(obj, message)
{ 
  var backgroundColor = "#ffffee";

  alert(message);

  // if "obj" is an array: it's a radio button. Focus on the first element.
  if (obj.type == undefined)
    obj[0].focus();
  else
  {
    obj.style.background = backgroundColor;
    obj.focus();
  }
  return false;
}


function isValidEmail(str)
{
  // trim starting / ending whitespace
  str = str.replace(/^\s*/, "");
  str = str.replace(/\s*$/, "");

  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)

  if (str.indexOf(at)==-1)
    return false
  
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
    return false
  
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    return false

  if (str.indexOf(at,(lat+1))!=-1)
    return false

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    return false

  if (str.indexOf(dot,(lat+2))==-1)
    return false

  if (str.indexOf(" ")!=-1)
    return false

  return true;
}

// add JS validation
var rules = new Array();

rules.push("required,first_name,Please enter your first name.");
rules.push("required,last_name,Please enter your last name.");
rules.push("required,email,Please enter your email address.");
rules.push("valid_email,email,Please enter a valid email address.");
