function isValidPhone( element, label, isRequired )
{
  testValue = element.value.replace(/[^\-\d]+/g, '' );

  if( element.value.match(/[^\-\d]/) )
  {
    alert( label + " only accepts Numbers and Dashes.  Please enter a value." );
    element.value = testValue;
    element.focus();
    return false;
  }
  element.value = testValue;
  testValue = testValue.replace( /\D+/g, '' );

  /* If element is a required field, check if nothing was entered.  If it isn't required, then return true; */
  if( testValue.length == 0 )
  {
    if( isRequired == true )
    {
      alert( label + " is a required field.  Please Retry." );
      element.focus();
      return false;
    }

    return true;
  }

  /* Test for String Less Than 10 Digits */
  if( testValue.length < 10 )
  {
    alert( label + " must be at least 10 Digits Long.  Please Re-Enter." );
    element.focus();
    return false;
  }

  /* Test for String Greater Than 20 Digits */
  if( testValue.length > 20 )
  {
    alert( label + " cannot be more than 20 Digits.  Please Re-Enter." );
    element.focus();
    return false;
  }

  /* If Processing Reached here, then the field is Valid so return TRUE */
  return true;
}

function hasBannedPatterns( form )
{
  var re_banned_patterns = /(http:\/\/www|http:\/\/|http|www\.|\/\/)/gi;

  // List of Elements to exclude from Validating
  var excludedFields = {
    'referer_url':'referer_url', 'redirect':'redirect'
  };

  // Check to see if other Excluded Elements were passed into the function
  if( arguments.length > 1 )
  {
    excluded_elements = arguments[1];
    type = typeof(excluded_elements);
    if( type.toLowerCase() == 'string' )
      excludedFields[ excludedFields ] = excluded_elements;
    else
    {
      for( exc_index=0; exc_index < excluded_elements.length; exc_index++ )
      {
        excludedFields[ excludedFields ] = excluded_elements[exc_index];
      }
    }
  }

  for( index = 0; index < form.elements.length; index++ )
  {
    element = form.elements[index];  

    if( excludedFields[element.name] ) continue;

    if( element.value.match( re_banned_patterns ) )
    {
      alert(element.name + " Contains Banned Patterns.\n\nPlease don't include URL components (eg: http://www.example.com).\n\n" + element.name + " will now be Reset.");
      element.value = '';
      element.focus();
      return true;
      break;
    }
  }

  return false;
}

function M2ReevesValidate(theForm)
{
  if( hasBannedPatterns(theForm) ) return false;
 
  if (theForm.FirstName.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }

  if (theForm.FirstName.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }

  if (theForm.EmailAddress.value == "")
  {
    alert("Please enter your email address.");
    theForm.EmailAddress.focus();
    return (false);
  }

  if (theForm.EmailAddress.value.length > 90)
  {
    alert("Please enter at most 90 characters in the \"Email Address\" field.");
    theForm.EmailAddress.focus();
    return (false);
  }

  boolValidPhone = isValidPhone( theForm.DaytimePhone, "Daytime Phone", true );
  if( !boolValidPhone ) return false;
  
  if (theForm.PUCity.value == "")
  {
    alert("Please enter a value for the \"Pickup City\" field.");
    theForm.PUCity.focus();
    return (false);
  }

  if (theForm.PUCity.value.length > 30)
  {
    alert("Please enter at most 30 characters in the \"Pickup City\" field.");
    theForm.PUCity.focus();
    return (false);
  }

  if (theForm.PUState.value == "")
  {
    alert("The first \"Pickup State\" option is not a valid selection.  Please choose one of the 50 states.");
    theForm.PUState.focus();
    return (false);
  }

  if (theForm.DTCity.value == "")
  {
    alert("Please enter a value for the \"Delivery City\" field.");
    theForm.DTCity.focus();
    return (false);
  }

  if (theForm.DTCity.value.length > 30)
  {
    alert("Please enter at most 30 characters in the \"Delivery City\" field.");
    theForm.DTCity.focus();
    return (false);
  }

  if (theForm.DTState.value == "")
  {
    alert("The first \"Delivery State\" option is not a valid selection.  Please choose one of the 50 states.");
    theForm.DTState.focus();
    return (false);
  }

  if (theForm.VehicleType1.value == "")
  {
    alert("You MUST Choose a Vehicle Type for the first Vehicle Entry.\n\nPlease choose a Vehicle Type from the options available.");
    theForm.VehicleType1.focus();
    return (false);
  }

  return (true);
}
