
  // 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;
  }


