  //This script was created by Tammy Connelly.  Last updated 9/27/01.
  
  function formCheck(theform) { 
    var theform=document.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_1)) 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 (!validcred(theform.acct)) msg+= "* Acct. No.\n";
	if (!expired(theform.expdate_mon,theform.expdate_yr)) msg+= "* Expired\n";
	if (!validtext(theform.cardname)) msg+= "* Name on Card\n"; 
	//if (!regtype(theform.num_regtype1)) msg+= "* Number of Individual(s) Attending\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 (document.theform.email.value.indexOf('@')==-1) return false;	
		return true;
	}

	function regtype(num_regtype1) {
		if (document.theform.num_regtype1.value=="" || document.theform.num_regtype1.value=="0"  || isNaN(document.theform.num_regtype1.value)) return false;	
		return true;
	}

//Validates phone numbers
function countrytest(country,areacode,phone) {
	var where=document.theform.country.value.toUpperCase();
	var area=document.theform.areacode.value;
	var phone=document.theform.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;
  }
}
		
//Validates credit card information
function validcred(creditcard,acct) {
	var cc=document.theform.creditcard;
	var acct=document.theform.acct.value;
	if (acct=="" || isNaN(acct)) {
		if (cardnum()==true) {
			return true;
		} else {
			return false;
		}
		return false;
	} else {
	for (count=0; count<2; ++count) {
		if (cc.selectedIndex == 0) {
			if (acct.length==13 || acct.length==16) {
				//alert ("Correct without Regular Expressions!");
				return true;
			} else {
				//alert ("Wrong without Regular Expressions!");
				return false;
			}
		}
		if (cc.selectedIndex == 1) {
			if (acct.length==16) {
				//alert ("Correct without Regular Expressions!");
				return true;
			} else {
				return false;
			}
		}
		if (cc.selectedIndex == 2) {
			if (acct.length==15) {
				//alert ("Correct without Regular Expressions!");
				return true;
			} else {
				return false;
			}
		}
	}
		return true;
	}
}

function cardnum(creditcard,acct) {
  var cc = document.theform.creditcard;
  var str = document.theform.acct.value;        
  var visa = /^[0-9]{4}[\-\W+]([0-9]{3}[\-\W+]){2}[0-9]{3}$/;
  var mc = /^([0-9]{4}[\-\W+]){3}[0-9]{4}$/;
  var amex = /^[0-9]{4}[\-\W+][0-9]{6}[\-\W+][0-9]{5}$/;

  if ((visa.test(str)) && (cc.selectedIndex == 0)) {
	//alert ("Correct with Regular Expressions!");
    return true; 
  } else {
	if ((mc.test(str)) && (cc.selectedIndex == 0 | cc.selectedIndex == 1)) {
		return true;
	} else {
		if ((amex.test(str)) && (cc.selectedIndex == 2)) {
			return true;
		} else {
			return false;
		}
		return false;
	}
  	return false;
  }
}

function expired(expdate_mon,expdate_yr) {
var month = document.theform.expdate_mon;
var year = document.theform.expdate_yr.options[document.theform.expdate_yr.selectedIndex].text;
var d = new Date();
  	if (month.selectedIndex < d.getMonth() && year <= d.getFullYear()) {
		if (month.selectedIndex < d.getMonth() && year > d.getFullYear()) {
			//alert ("Valid Month and Year (Month is less and Year is more)");
			return true;
		} else {
			//alert ("Invalid Month and Year (Month is more and Year is less)");
			return false;
		}
		//alert ("Invalid expiration date")
		return false;
	} else {
  		if (month.selectedIndex >= d.getMonth() && year < d.getFullYear()) {
			//alert ("Invalid Month and Year (Month is more/equal to and Year is less)");
			return false;
		} else {
			//alert ("Valid Month and Year (Month is less/equal to and Year is more)");
			return true;
		}
		//alert ("Valid expiration date")
		return false;
	}
}
