/* Script by hscripts.com */
function alphanumeric(alphane)
{
	var numaric = alphane;
    /* alert("alphane is " + alphane + "."); */
	
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
			 return false;
		  }
		}
 return true;
}
/* Script by hscripts.com */



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

  if( element.value.match(/[^\-\d]/) )
  {
    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 FormValidate(theForm)
{
  if( hasBannedPatterns(theForm) ) return false;
  
  if( !alphanumeric(theForm.sendername.value) ) return false;
  
 
  if (theForm.sendername.value == "")
  {
    alert("Please enter a value for the \"Name\" field.");
    theForm.sendername.focus();
    return (false);
  }

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

  if (theForm.sendername.value.length < 3)
  {
    alert("Please enter at least 3 characters in the \"Name\" field.");
    theForm.sendername.focus();
    return (false);
  }

  if (theForm.senderemail.value == "")
  {
    alert("Please enter a value for the email field.");
    theForm.senderemail.focus();
    return (false);
  }

  if (theForm.location.value == "")
  {
    alert("Please enter a value for your general location field.");
    theForm.location.focus();
    return (false);
  }
  
  if (theForm.message.value == "")
  {
    alert("Please enter your Message in the area below.");
    theForm.message.focus();
    return (false);
  }
  
  boolValidPhone = isValidPhone( theForm.phone, "txtPhone", true );
  if( !boolValidPhone ) return false;
  
  return (true);
}
