var emailexp = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]{2,3}$/i
var pcexp = /^[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d/

function isDigit (c)
{   return ((c >= "0") && (c <=	"9"))
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z"))	|| ((c >= "A") && (c <=	"Z")) )
}

function isSpecial (c)
// It might be easier to define	what is	NOT allowed
{  return ( (c != "\"")	)
}

function isEmpty(s)
{   return ((s == null)	|| (s.length ==	0))
}

function isInteger (s)

{   var	i;

    if (isEmpty(s)) return false;

    // Search through string's characters one by one
    // until we	find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for	(i = 0;	i < s.length; i++)
    {
	// Check that current character	is number.
	var c =	s.charAt(i);

	if (!isDigit(c)) return	false;
    }

    // All characters are numbers.
    return true;
}

// Returns true	if string s is empty or	contains whitespace characters only.
function isWhitespace (s) {

    var	i;
    var	whitespace = " \t\n\r";
    var	quote =	"\"";

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we	find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for	(i = 0;	i < s.length; i++)
    {
	// Check that current character	isn't whitespace.
	var c =	s.charAt(i);
	if (whitespace.indexOf(c) == -1) return	false;
    }

    // All characters are whitespace.
    return true;
}


// Returns true	if string s is English letters (A .. Z,	a..z), numbers
// or any of the defined special characters.
function isAlphanumeric	(s)

{   var	i;

    if (isEmpty(s)) return false;

    // Search through string's characters one by one
    // until we	find a non-alphanumeric	character.
    // When we do, return false; if we don't, return true.

    for	(i = 0;	i < s.length; i++)
    {
	// Check that current character	is number or letter.
	var c =	s.charAt(i);

	if (! (isLetter(c) || isDigit(c) || isSpecial(c)) )
	return false;
    }

    // All characters are numbers, letters, or special.
    return true;
}

function isValid(pattern, str) {
    return pattern.test(str)
}

function checkForm(AForm){

    if (isWhitespace(AForm.NAME.value)) {
	alert("Please enter a value in the Name field.");
	document.pfo.NAME.select();
	document.pfo.NAME.focus();
	return false;
     } else {
	if (!isAlphanumeric(AForm.NAME.value)) {
	    alert("Invalid character found in the Name field.");
	    document.pfo.NAME.select();
	    document.pfo.NAME.focus();
	    return false;
	}
     }

     if (isWhitespace(AForm.ADDRESS.value)) {
       alert("Please enter a value in the Address field.");
       document.pfo.ADDRESS.select();
       document.pfo.ADDRESS.focus();
       return false;
     } else {
	if (!isAlphanumeric(AForm.ADDRESS.value)) {
	    alert("Invalid character found in the Address field.");
	    document.pfo.ADDRESS.select();
	    document.pfo.ADDRESS.focus();
	    return false;
	}
     }

     if (isWhitespace(AForm.CITY.value))	{
       alert("Please enter a value in the City field.");
       document.pfo.CITY.select();
       document.pfo.CITY.focus();
       return false;
     } else {
	 if (!isAlphanumeric(AForm.CITY.value))	{
	     alert("Invalid character found in the City	field.");
	     document.pfo.CITY.select();
	     document.pfo.CITY.focus();
	     return false;
	 }
     }

     if (isWhitespace(document.pfo.COUNTRY.options[document.pfo.COUNTRY.selectedIndex].value)) {
       alert("Please select a Country.");
	return false;
     }

     if (isWhitespace(AForm.ZIP.value)) {
       alert("Please enter a value in the Postal Code field.");
       document.pfo.ZIP.select();
       document.pfo.ZIP.focus();
       return false;
     } else {
       if (document.pfo.COUNTRY.options[document.pfo.COUNTRY.selectedIndex].value == "Canada") {
         if (!isValid(pcexp, document.pfo.ZIP.value)) {
              alert("The Postal Code field must contain a valid postal cod (like T5J 0N5).");
	      document.pfo.ZIP.select();
	      document.pfo.ZIP.focus();
	      return false;
	 }
       }
     }

     if (isWhitespace(AForm.STATE.value)) {
       alert("Please enter a value in the Province field.");
       document.pfo.STATE.select();
       document.pfo.STATE.focus();
       return false;
     } else {
       if (!isAlphanumeric(AForm.STATE.value)) {
         alert("Invalid character found in the Province field.");
	 document.pfo.STATE.select();
	 document.pfo.STATE.focus();
	 return false;
       }
     }

     if (isWhitespace(AForm.PHONE.value)) {
       alert("Please enter a value in the Phone field.");
       document.pfo.PHONE.select();
       document.pfo.PHONE.focus();
       return false;
     } else {
       if (!isAlphanumeric(AForm.PHONE.value)) {
         alert("Invalid character found in the Phone field.");
	 document.pfo.PHONE.select();
	 document.pfo.PHONE.focus();
	 return false;
	 }
     }

     if(isWhitespace(AForm.EMAIL.value)) {
       alert("Please enter a value in the Email field.");
       document.pfo.EMAIL.select();
       document.pfo.EMAIL.focus();
       return false;
     } else {
       if (!isValid(emailexp, document.pfo.EMAIL.value)) {
         alert("The email field must contain a valid email address (like foo@bar.com).");
	 document.pfo.EMAIL.select();
	 document.pfo.EMAIL.focus();
	 return false;
      }
    }

    return true;
}

function submitForm(formName) {

  var AForm = document.forms[formName];

  if(!isInteger(AForm.Qty1.value)) {
    if (!isInteger(AForm.Qty2.value)) {
      alert("Please enter a valid quantity.");
      return;
    }
  }
	
  AForm.submit();
}


