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;
  
  for( index = 0; index < form.elements.length; index++ )
  {
    element = form.elements[index];  

    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 FormValidate(theForm)
{
	
var x= theForm.email.value;
/*alert('The value of x is' + x + '.');
return false;
*/
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  theForm.email.focus();
  return false;
  }
  
  
  if( hasBannedPatterns(theForm) ) return false;
 
  if (theForm.name.value == "")
  {
    alert("Please tell us your \"Name\"!");
    theForm.name.focus();
    return (false);
  }
  if (theForm.zip.value == "")
  {
    alert("Please tell us your \"Zip Code\"!");
    theForm.zip.focus();
    return (false);
  }  
  if (theForm.size.value == "")
  {
    alert("Please tell us the size of dumpster you need!");
    theForm.size.focus();
    return (false);
  }   
  if (theForm.email.value.length < 1)
  {
    alert("Please tell us your \"Email Address\"!");
    theForm.email.focus();
    return (false);
  }
  if (theForm.email.value.length > 90)
  {
    alert("Please enter at most 90 characters for your email address.");
    theForm.email.focus();
    return (false);
  }
  boolValidPhone = isValidPhone( theForm.phone, "Telephone Number", true );
  if( !boolValidPhone ) return false;
  
  if (theForm.phone.value == "")
  {
    alert("Please provide your phone number so we can cann you in an instant.");
    theForm.phone.focus();
    return (false);
  }
  

  
  return (true);
}
