
  // useful form checking functions
  // javascript 1.0 compliant

  /*******************
   * email functions *
   *******************/

  // check the email address for proper formatting
  function isEmail(em) {

	if (em.length == 0) {
		return "You have not submitted an email address.";
	}

	var n = em.indexOf("@");
	if (n == -1) {
		return "Missing \"@\" in email address.";
	}
	var user = em.substr(0,n);
	var host = em.substring(n + 1, em.length)
	if (user.length == 0) {
		return "Invalid email: 0 chars in user name.";
	}
	if (user.indexOf("@") >= 0) {
		return "Invalid email: \"@\" used in user name.";
	}
	if (user.indexOf(" ") >= 0) {
		return "Invalid email: whitespace in user name.";
	}
	if (!isHostName(host)) {
		return "Invalid email: bad host name.";
	}
	return 1;
  }

  // true if the hostname is properly formatted.
  // only a-z, 0-9, "-" and "." are permitted in a hostname.
  // do a couple easy checks to begin with, then look more generally
  function isHostName(str) {

	if (str.length == 0) {
		return 0;
	}
	if (str.indexOf(" ") >= 0) {
		return 0;
	}
	if (str.indexOf("@") >= 0) {
		return 0;
	}
	if (str.indexOf("..") >= 0) {
		return 0;
	}
	if (str.charAt(0) == ".") {
		return 0;
	}
	if (str.indexOf(".") == -1) {
		return 0;
	}
	var ch = "";
	for (var i = 0; i < str.length; i++) {
		ch = str.charAt(i);
		if ((!aToZ(ch)) && (!isNumberJ10(ch)) && (ch != "-") && (ch != ".")) {
			return 0;
		}
	}
	return 1;
  }

  /***********************
   * telephone functions *
   ***********************/

  // check telephone number
  function isTelephone(tel) {

	if (tel.length == 0) {
		return "You must include your telephone number.";
	}

	for (var i = 0; i < tel.length; i++) {
		ch = tel.charAt(i);
		if (!isNumberJ10(ch)) {
			if (ch != " " && ch != "(" && ch != ")" && ch!="-") {
				return "Invalid char in telephone number.";
			}
		}
	}

	tel = stripNonNumbers(tel);
	if (tel.length < 6) {
		return "Invalid telephone number: too short.";
	}
	if (tel.length > 13) {
		return "Invalid telephone number: too long.";
	}
	return 1;
  }

  // hack job of formatting telephone numbers
  // twinned with function doPhoneFormat
  function formatTelephone(tel) {

	tel = stripNonNumbers(tel);
	if (tel.length < 8) {
		tel = doPhoneFormat(3,1,tel);
		return tel;
	}
	if (tel.length == 8) {
		tel = doPhoneFormat(4,1,tel);
		return tel;
	}
	if (tel.length < 11) {
		tel = doPhoneFormat(3,2,tel);
		return tel;
	}
	if (tel.length == 11) {
		tel = doPhoneFormat(4,2,tel);
		return tel;
	}
	if (tel.length < 14) {
		tel = doPhoneFormat(3,3,tel);
		return tel;
	}
  }
  function doPhoneFormat(div,no,tel) {

	var tel2 = "";
	var n = 0;
	for (var i = 0; i < tel.length; i++) {
		tel2 = tel2 + tel.charAt(i);
		if ((!((i + 1) % div)) && n < no) {
			n++;
			tel2 = tel2 + " ";
		}	
	}
	return tel2;
  }

  /*************************
   * credit card functions *
   *************************/

  // check credit card number
  // assume credit card numbers are between 12 and 24 numeric chars..?
  function isCreditCard(ccn) {
	if (ccn.length == 0) {
		return "You have not included your credit card number.";
	}
	for (var i = 0; i < ccn.length; i++) {
		ch = ccn.charAt(i);
		if (!isNumberJ10(ch)) {
			if (ch != " " && ch != "(" && ch != ")" && ch!="-") {
				return "Invalid char in credit card number.";
			}
		}
	}
	ccn = stripNonNumbers(ccn);
	if (ccn.length < 12) {
		return "Invalid credit card number: too short.";
	}
	if (ccn.length > 19) {
		return "Invalid credit card number: too long.";
	}
    sum = 0; mul = 1; l = ccn.length;
    for (i = 0; i < l; i++) {
      digit = ccn.substring(l-i-1,l-i);
      tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) == 0)
    return 1;
  else
    return "The credit card number is not valid";
  }

  function formatCreditCard(ccn) {

    var ccn2 = "";
	var div = 4;
	for (var i = 0; i < ccn.length; i++) {
		ccn2 = ccn2 + ccn.charAt(i);
		if (!((i + 1) % div)) {
			ccn2 = ccn2 + " ";
		}	
	}
	return ccn2;
  }

  /*********************
   * string functions  *
   *********************/

  // remove all non-numerals from the string
  function stripNonNumbers(str) {

	var ch = "";
	var str2 = "";
	for (var i = 0; i < str.length; i++) {
		ch = str.charAt(i);
		if (isNumberJ10(ch)) {
			str2 = str2 + ch;
		}
	}
	return str2;
  }

  // return string 'str' with all occurances of char 'ch' stripped out
  // if the char isn't in the string, return the string unchanged
  function stripChar(ch,str) {
	
	var str2 = "";
	for (var i = 0; i < str.length; i++) {
		if (str.charAt(i) != ch) {
			str2 = str2 + str.charAt(i);
		}
	}
	return str2;
  }

  // function true if char 'ch' is an alphabetical letter, upper or lower case
  function aToZ(ch) {

	c = ch.toLowerCase();
    if (c >= "a" && c <= "z") { return 1; }

	return 0;
  }

  // function true if char 'ch' is numeric.
  // strange title in case there's already a javascript function 'isNumber'
  // in a later version - after 1.0
  function isNumberJ10(ch) {

	for (var i = 0; i < 10; i++) {
		if (ch == "" + i) {
			return 1;
		}
	}
	return 0;
  }

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return 1;
  return false;
}  // END FUNCTION isVisa()

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return 1;
  return false;

} // END FUNCTION isMasterCard()

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return 1;
  return false;

} // END FUNCTION isAmericanExpress()


