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 = {
    'post_back':'post_back'
  };

  // 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 PatriotValidate(theForm)
{
  if( hasBannedPatterns(theForm) ) return false;
 
  if (theForm.first_name.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.first_name.focus();
    return (false);
  }

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

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

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

  boolValidPhone = isValidPhone( theForm.phone, "Daytime Phone", true );
  if( !boolValidPhone ) return false;

  if (theForm.year1.value == "")
  {
    alert("Please enter a value for the \"Vehicle 1 year\" field.");
    theForm.year1.focus();
    return (false);
  }
  
  if (theForm.pickup_city.value == "")
  {
    alert("Please enter a value for the \"Pickup City\" field.");
    theForm.pickup_city.focus();
    return (false);
  }

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

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

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

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

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