// Product Check Form, Copyright © 2011 Brimrose Corporation of America, All Rights Reserved
//
var szFullNameError = "The Full Name field is required.\n\nPlease enter it now.";
var szCompanyNameError = "The Company Name field is required.\n\nPlease enter it now.";
var szAddress1Error = "The Address field is required.\n\nPlease enter it now.";
var szCityError = "The City field is required.\n\nPlease enter it now or enter N/A to supress error.";
var szStateError = "The State field is required.\n\nPlease enter it now or enter N/A to supress error.";
var szZipCodeError = "The Zip Code field is required.\n\nPlease enter it now.";
var szCountryError = "The Country field is required.\n\nPlease enter it now.";
var szPhoneNumberError = "A valid Phone Number is a required.\n\nPlease enter it now.";
var szEmailAddrError = "A valid Email Address is a required.\n\nPlease enter it now.";
var szCommentError = "Please enter your comments, questions or suggestion now.";
var whitespace = " \t\r\n";


function checkform(f)
{
  if (f.Full_Name) {
    if (!checkString(f.Full_Name)) {
      alert(szFullNameError);
      return false;
    }
  }

  if (f.Company) {
    if (!checkString(f.Company)) {
      alert(szCompanyNameError);
      return false;
    }
  }


  if (f.Address1) {
    if (!checkString(f.Address1)) {
      alert(szAddress1Error);
      return false;
    }
  }

  if (f.City) {
    if (!checkString(f.City)) {
      alert(szCityError);
      return false;
    }
  }

  if (f.State) {
    if (!checkString(f.State)) {
      alert(szStateError);
      return false;
    }
  }

  if (f.Zip_Code) {
    if (!checkString(f.Zip_Code)) {
      alert(szZipCodeError);
      return false;
    }
  }

  if (f.Country) {
    if (!checkString(f.Country)) {
      alert(szCountryError);
      return false;
    }
  }

  if (f.Phone_Num) {
    if (!checkString(f.Phone_Num)) {
      alert(szPhoneNumberError);
      return false;
    }
  }

  if (f.Email_Addr) {
    if (!checkEmail(f.Email_Addr)) {
      alert(szEmailAddrError);
      return false;
    }
  }
  
  if (f.Comment) {
    if (!checkString(f.Comment)) {
      alert(szCommentError);
      return false;
    }
  }
  
  return true;
}

function checkSelect(theField)
{
  if (theField.selectedIndex < 0) {
    theField.focus();
    return false;
  }

  return true;
}

function checkString(theField) {
  if (isWhiteSpace(theField.value)) {
    theField.focus();
    return false;
  }

  return true;
}

function checkEmail(theField)
{
  if (isWhiteSpace(theField.value)) {
    alert(szEmailAddrError);
    theField.focus();
    return false;
  }
  if (!isEmail(theField.value)) {
    alert(szEmailAddrError);
    theField.focus();
    return false;
  }

  return true;
}

function isEmail(s)
{
  var i = 1;
  var Len = s.length;

  while( (i < Len) && (s.charAt(i) != "@") ) {
    i++;
  }

  if ((i == Len) || (s.charAt(i) != "@")) {
    return false;
  }
  else {
    i += 2;
  }

  while( (i < Len) && (s.charAt(i) != ".") ) {
    i++;
  }

  if ((i == Len) || (s.charAt(i) != ".")) {
    return false;
  }

  return true;
}

function isWhiteSpace(s)
{
  var i;
  var c;

  if ((s != null) && (s.length > 0)) {
    for (i = 0; i < s.length; i++) {
      c = s.charAt(i);
      if (whitespace.indexOf(c) == -1) {
        return false;
      }
    }
  }

  return true;
}

