  //Created by Tammy Connelly for the Cato Institute.  Last updated 9/27/01.  The best forum script.
  
  function formCheck(theform) { 
	var msg="";

	if (!validtext(theform.firstname)) msg+= "* First Name\n";
	if (!validtext(theform.lastname)) msg+= "* Last Name\n";
	if (!validtext(theform.affiliation)) msg+= "* Affiliation\n";	
	if (!validtext(theform.address)) msg+= "* Address\n";
	if (!validtext(theform.city)) msg+= "* City\n";
	if (!validtext(theform.state)) msg+= "* State\n";
	if (!ctest(theform.zip)) msg+= "* Zip\n";
	if (!validtext(theform.country)) msg+= "* Country\n";
	if (!countrytest(theform.country,theform.areacode,theform.phone)) msg+= "* Area Code & Phone Number\n";
	if (!validemail(theform.email)) msg+= "* Email\n";

    if (!msg == "") {
		alert("Please fill in or correct the following field(s) before continuing:\n" + msg);
			return false;
		} else {
		//alert("Thank you for registering for this event!")
			return true;
		}
	}

	function validtext(textfield) {
		if (textfield.value=="") return false; 
		return true;
	}

	function validemail(email) {
		if (email.value.indexOf('@')==-1) return false;	
		return true;
	}

//Validates phone numbers
function countrytest(country,areacode,phone) {
	var where=country.value.toUpperCase();
	var area=areacode.value;
	var phone=phone.value;
	if (where=="USA" || where=="US" || where=="UNITED STATES" || where=="U.S." || where=="U.S.A.") {
		areatest();
		if (areatest()==true && phonetest()==true) {
			return true;
		} else {
			return false
		}
	} else {
		if (area=="" || phone=="") {
			return false;
		} else {
		//alert (area + "-" + phone + " is not a USA phone number");
		return true;
		}
	}
}

function areatest(areacode,phone) {
	var area=document.theform.areacode.value;
		if ((area.length !=3) || (isNaN(area))) {
			//alert(area + " is not a valid area code.");
			return(areacode,phone);
		} else {
			phonetest();
			return true;
		}
}
		
function phonetest(areacode,phone) {
	var area=document.theform.areacode.value;
	var phone=document.theform.phone.value;
		if ((phone.length !=7) || (isNaN(phone))) {
			if (hyphen()==true) {
			return true; 
			} else {		
			//alert(area + "-" + phone + " is not a valid phone number.");
			return(areacode,phone);
			}
		} else {
			//alert(area + "-" + phone + " is a valid phone number.");
			return true;
		}
}

function hyphen(phone) {
  var str = document.theform.phone.value;        
  var re = /^[0-9]{3}[\-\W+][0-9]{4}$/;

  if (re.test(str)) {
    return true;
  } else {
  return false;
  }
}

//Validates zip code
function ctest(country,zip) {
	var where=document.theform.country.value.toUpperCase();
	var code=document.theform.zip.value;
	if (where=="USA" || where=="US" || where=="UNITED STATES" || where=="U.S." || where=="U.S.A.") {
		ziptest();
		if (ziptest()==true) {
			return true;
		} else {
			return false
		}
	} else {
		if (code=="") {
			return false;
		} else {
		//alert (code + " is not a USA zip code");
		return true;
		}
	}
}

function ziptest(zip) {
	var code=document.theform.zip.value;
		if ((code.length !=5) || (isNaN(code))) {
			if ((code.length !=9) || (isNaN(code))) {
				if (hyphenz()==true) {
					return true; 
				} else {
					//alert(code + " is not a valid zip code.");
					return false;
				}
				//alert(code + " is not a valid zip code.");
				return false;
			} else {
				//alert ("Without RegEx!")
				return true;
			}
		}
	//alert(code + " is a valid zip code.");
	return true;
}

function hyphenz(zip) {
  var str = document.theform.zip.value;        
  var re = /^[0-9]{5}[\-\W+][0-9]{4}$/;

  if (re.test(str)) {
    return true;
  } else {
	return false;
  }
}
		
/*function ziptest(zip) {
	var code=document.theform.zip.value;
	for (count=0; count<=code.length; ++count) {
		var number=code.charAt(count);
		if ((code.length !=5) || (isNaN(number))) {
			if (hyphenz()==true) {
			return true; 
			} else {
			//alert(code + " is not a valid zip code.");
			return false;
			}
		}
	}
	//alert(code + " is a valid zip code.");
	return true;
}

function hyphenz(zip) {
  var str = document.theform.zip.value;        
  var re = /^[0-9]{5}\-[0-9]{4}|[0-9]{5}\W+[0-9]{4}$/;
  var nine = /^[0-9]{9}$/;

  if (re.test(str)) {
    return true;
  } else {
	if (nine.test(str)) {
		return true;
	} else {
		return false;
	}
  return false;
  }
}*/
