// Function to check input elements with the specified class in the specified
// container that they are not null.  If no container is specified, use the
// document.  If the alertFlag is not specified or is false, the function will
// popup an alert if it finds a null input.
//
function checkInputsNotNull(container, inpClass, alertFlag)
{
  var cont_e;
  if (container) {
    cont_e = document.getElementById(container);
  } else {
    cont_e = document;
  }

  // Make sure the container is valid.
  if (cont_e) {
    var inp_array = getElementsByClass(inpClass, cont_e, 'input');
    for (var i = 0; i < inp_array.length; i ++) {
      if (checkNotNull(inp_array[i], alertFlag) == false) {
        return(false);
      }
    }

    var inp_array = getElementsByClass(inpClass, cont_e, 'textarea');
    for (var i = 0; i < inp_array.length; i ++) {
      if (checkNotNull(inp_array[i], alertFlag) == false) {
        return(false);
      }
    }

    return(true);
  }

  else {
    alert("checkInputsNotNull: Invalid container");
    return(false);
  }
}

// Validation function - value must not be null or blank.
// If the alertFlag is not specified or is true, the function will
// popup an alert if it finds a null input.
//
function checkNotNull(e, alertFlag)
{
  // Warn the user if the value is empty.
  if (trim(e.value) == '') {
    if (! alertFlag) {
      alert('This field cannot be blank.');
      e.focus();
      e.select();
    }

    return(false);
  }
  else {
    return(true);
  }
}

// Function to make sure the parameter is numeric.
//
function validateNumber(e)
{
  // Warn the user if we don't have a number.
  var val = e.value;
  if (! isNumeric(val)) {
    alert('Please enter a number in this field.');
    e.focus();
    e.select();
    return(false);
  }

  return(true);
}  

// Function to make sure parameters are valid numbers.
// The wholeFlag tells me the number of decimal points to round to.
//
function validateAmount(e, wholeFlag)
{
  // Strip dollar and percent signs and commas.
  var val = e.value;
  var ind = val.indexOf('$');
  val = (ind > -1) ? val.substring(1) : val;

  ind = val.indexOf('%');
  val = (ind > -1) ? val.substring(0, ind) : val;

  val = stripCommas(val);

  // Warn the user if we don't have a number.
  if (! isNumeric(val)) {
    alert('Please enter a number in this field.');
    e.focus();
    e.select();
    return(false);
  }

  // Round to a the specified number of decimal places.
  if (wholeFlag == 0) {
    Math.round(val);
  }
  else if (wholeFlag > 0) {
    val = (val/1).toFixed(wholeFlag);
  }

  // Format the number with commas.
  val = addCommas(val);

  e.value = val;
  return(true);
}

// Function to validate a date as in input value.
//
var MONTH_DAYS = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
function validateDate(e)
{
  var ret = true;

  // Split the date into components.
  // I should have 3 parts, and all must be numbers.
  var parts = e.value.split('/');

  if (parts.length != 3) {
    ret = false;
  }
  else {
    // Test the month part.
    if ((! isNumeric(parts[0])) || (parts[0] < 1) || (parts[0] > 12)) {
      ret = false;
    }
    // Test the year part.
    else if ((! isNumeric(parts[2])) || (parts[2] < 1)) {
      ret = false;
    }
    // Test the day part.
    else if ((! isNumeric(parts[1])) || (parts[1] < 1) ||
             ((parts[1] > MONTH_DAYS[parts[0]-1]) && (parts[2]%4 != 0)) ||
             (parts[1] > MONTH_DAYS[parts[0]-1]+1)) {
      ret = false;
    }
  }

  if (! ret) {
    alert('Please enter a valid date in m/d/yyyy form.');
    e.focus();
    e.select();
    return(false);
  }

  return(true);
}

// Function to validate a year as an input value.
//
function validateYear(e)
{
  var ret_val = true;

  // First, the year must be a number.
  if (ret_val = validateNumber(e, -1)) {
    // Strip the commas added by validateNumber.
    e.value = stripCommas(e.value);

    // Second, compare the value to the current year.
    var today = new Date();
    var this_year = today.toLocaleFormat("%Y");
    if (e.value > this_year) {
      alert('The year you enter must be the current year or a prior year.');
      ret_val = false;
    }
  }

  if (! ret_val) {
    e.focus();
    e.select();
  }

  return(ret_val);
}

//
// This function checks if the input value is a valid time.
//
function validateTime(e)
{
  // If the value does not have a colon, put them in the appropriate place.
  if (e.value) {
    if (e.value.indexOf(':') == -1) {
      var old = e.value;
      e.value = old.substring(0, old.length-2) + ':' + old.substr(old.length-2, 2);
    }
    
    if (! isTime(e.value)) {
      alert('Please enter a valid time in hh:mm form.');

      e.focus();
      e.select();
      return(false);
    }
  }

  return(true);
}

// This function validates if the input value is a valid phone number.
//
function validatePhone(e)
{
  if (e.value) {
    // Strip the spaces, dashes, and parentheses from the input.
    var inp = e.value;
    var ind;
    while ((ind = inp.indexOf(' ')) > -1) {
      inp = inp.substring(0, ind) + inp.substring(ind+1);
    }
    while ((ind = inp.indexOf('-')) > -1) {
      inp = inp.substring(0, ind) + inp.substring(ind+1);
    }
    while ((ind = inp.indexOf(')')) > -1) {
      inp = inp.substring(0, ind) + inp.substring(ind+1);
    }
    while ((ind = inp.indexOf('(')) > -1) {
      inp = inp.substring(0, ind) + inp.substring(ind+1);
    }

    // Make sure what's left is all numeric.
    if ((isNumeric(inp.substring(0, 10))) && (inp.length >= 10)) {
      e.value = '(' + inp.substring(0, 3) + ') ' + inp.substring(3, 6) + '-' + inp.substring(6, 10);
      if (inp.length > 10) {
        e.value += ' ' + inp.substring(10).trim();
      }

      ChangedFlag = true;
      return(true);
    }
    else {
      alert('Please enter a valid phone number with area code.');

      e.focus();
      e.select();
      return(false);
    }
  }
  else {
    return(true);
  }
}

// Function to validate the image type based on its file extension.
//
function validateImage(e)
{
  var val = e.value;

  // Find the extension of the file.
  var ind = val.lastIndexOf('.');
  var ext = '';
  if (ind != -1) {
    ext = val.substring(ind+1);
  }

  // Set up a regular expression to test the extension.
  // The extension has to be one of the accepted types.
  var regexp = /^(jpg|jpeg|gif|png|tif|tiff)$/i;
  if (regexp.test(ext)) {
    return (true);
  }
  else {
    alert("The image must be a file with one of the following extensions:\n  .jpg or .jpeg\n  .gif\n  .png\n  .tif or .tiff");

    e.value = '';
    e.focus();
    return(false);
  }
}

// Function to validate a url.
//
function validateURL(e)
{
  var val = e.value;

  // Set up a regular expression to test the value.
  var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
  if (regexp.test(val)) {
    return(true);
  }
  else {
    alert('Please enter a valid URL.');

    e.focus();
    e.select();
    return(false);
  }
}

// Function to validate an email address.
//
function validateEmail(e, override)
{
  var val = e.value;

  // Set up a regular expression to test the value.
  var regexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (((override) && (val == '')) ||
      (regexp.test(val))) {
    return(true);
  }
  else {
    alert('Please enter a valid email address.');

    e.focus();
    e.select();
    return(false);
  }
}
