function isNumericCode(str)
{
  var i = 0;
  var flag = 0;
    if (str.length > 0)
    {
         for (j=0;j < str.length; j++) {
             if (str.charAt(j) == '0') flag = 1;
             if (str.charAt(j) == '1') flag = 1;
             if (str.charAt(j) == '2') flag = 1;
             if (str.charAt(j) == '3') flag = 1;
             if (str.charAt(j) == '4') flag = 1;
             if (str.charAt(j) == '5') flag = 1;
             if (str.charAt(j) == '6') flag = 1;
             if (str.charAt(j) == '7') flag = 1;
             if (str.charAt(j) == '8') flag = 1;
             if (str.charAt(j) == '9') flag = 1;
         };
    };
    if (flag == 1) return 1;
    return 0;
}

function isZipCode(str) {
  var leftFive = "";
  var midHyphen = "";
  var rightFour = "";
    if (str.length > 0)
    {
       if (str.length == 5) {
          for (i=0; i < str.length; i++) {
                 if ( isNumericCode( str.charAt(i) ) == 0 ) return 0;
          };
       };
       if (str.length == 10) {
          leftFive = new String(str).substring(0,5);
          midHyphen = new String(str).substring(5,6);
          rightFour = new String(str).substring(6,10);
          for (i=0; i < leftFive.length; i++) {
                 if ( isNumericCode( leftFive.charAt(i) ) == 0 ) return 0;
          };
          for (i=0; i < rightFour.length; i++) {
                 if ( isNumericCode( rightFour.charAt(i) ) == 0 ) return 0;
          };
          if (!('-' == midHyphen)) return 0;
       };
    };
       if ( str.length == 0)  return 0;
       if ( (str.length !=5) && (str.length!=10) ) return 0;
    return 1;
}

function checkFormValues(formValues) { 
CRLF = "\n\r";
var strErrMsg = "";
var boolErr = false;
var boolEmail = false;
var strFName = formValues.fname.value;
var strLName = formValues.lname.value;
var strAddress1 = formValues.address1.value;
var strAddress2 = formValues.address2.value;
var strCity = formValues.city.value;
var strState = formValues.state.value;
var strZipCode = formValues.zipcode.value;
var strEmail = formValues.email.value;
 if( strFName.length == 0 )
 {
  strErrMsg = strErrMsg + CRLF + "Your first name is missing (required)";
  boolErr = true;
 };

 if( strLName.length == 0 )
 {
  strErrMsg = strErrMsg + CRLF + "Your last name is missing (required)";
  boolErr = true;
 };

 if( strAddress1.length == 0 )
 {
  strErrMsg = strErrMsg + CRLF + "Your street address is missing (required)";
  boolErr = true;
 };

 if( strCity.length == 0 )
 {
  strErrMsg = strErrMsg + CRLF + "Your city is missing (required)";
  boolErr = true;
 };

if ( strState.length != 2  )
 {
  strErrMsg = strErrMsg + CRLF + "Your state is missing (required)  ";
  boolErr = true;
 };

 if( isZipCode(strZipCode) == 0 )
 {
  strErrMsg = strErrMsg + CRLF + "Your zip code is missing (required)";
  strErrMsg = strErrMsg + CRLF + "  (zip code format must be 99999 or 99999-9999 only)";
  boolErr = true;
 };

 if( strEmail.length == 0 )
 {
  strErrMsg = strErrMsg + CRLF + "Your email is missing (required for confirmation)";
  boolErr = true;
  } else {
    for (i=0; i<strEmail.length;i++)
    {
      if (strEmail.charAt(i) == "@")
      {
        boolEmail = true;
      };
    };
    if (!boolEmail)
    {
      strErrMsg = strErrMsg + CRLF + "Your email format is incorrect (missing @___.com) (required for confirmation)";
      boolErr = true;
    };

 };

if(boolErr)
 {
  alert(strErrMsg);
 };

if(!boolErr)
 {
  formValues.action = "processComicRequest.php";
 } else {
  formValues.action ="requestComicBook.php"
  };

}

