//Right align amount text box
//document.theform.amount.options[document.theform.amount.selectedIndex].value


//Levels
function level() {
other=document.theform.amount.value;
if (is_ie) {
	if (other!="" && other <50) {
		if (document.theform.renewal[0].checked) {
			//document.getElementById('level').innerHTML = '<span style="color: 000000">Level:</span> Introductory';
			document.getElementById('level').innerHTML = '';
		} else if (document.theform.renewal[1].checked) {
			document.getElementById('level').innerHTML = '';
		}
	} else if (other!="" && other <=99 && other>=50) {
		if (document.theform.renewal[0].checked) {
			document.getElementById('level').innerHTML = '<span style="color: 000000">Level:</span> Introductory';
		} else if (document.theform.renewal[1].checked) {
			document.getElementById('level').innerHTML = '';
		}
	} else if (other!="" && other <=499 && other>=100) {
	document.getElementById('level').innerHTML = '<span style="color: 000000">Level:</span> Basic';
	} else if (other!="" && other <=999 && other>=500) {
	document.getElementById('level').innerHTML = '<span style="color: 000000">Level:</span> Sustaining';
	} else if (other!="" && other <=4999 && other>=1000) {
	document.getElementById('level').innerHTML = '<span style="color: 000000">Level:</span> Patron';
	} else if (other!="" && other <=24999 && other>=5000) {
	document.getElementById('level').innerHTML = '<span style="color: 000000">Level:</span> Benefactor';
	} else if (other!="" && other>=25000) {
	document.getElementById('level').innerHTML = '<span style="color: 000000">Level:</span> Cato Club 200';
	}
}
}

//Validation
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.ac_phone,theform.phone)) msg+= "* Area Code & Phone Number\n";
	if (!validemail(theform.email)) msg+= "* Email\n";
	if (document.theform.donation[0].checked) {
		if ((!cardtype(theform.creditcard)) || (!validtext(theform.accountno)) || (!expired(theform.expdate_mon,theform.expdate_yr)) || (!validtext(theform.cardname)) || (!money(theform.amount))) { 		
			msg+= "* Payment Information\n";
			if (!money(theform.amount)) msg+= "   * Amount\n";
			if (!cardtype(theform.creditcard)) msg+= "   * Method of Payment\n";
			if (!validtext(theform.accountno)) msg+= "   * Account #\n";
			if (!expired(theform.expdate_mon,theform.expdate_yr)) msg+= "   * Expiration Date\n";
			if (!validtext(theform.cardname)) msg+= "   * Name on Card\n";
		} else {
			msg+="";
		}
	}
	if (document.theform.gift[1].checked) {
		if ((!validtext(theform.gift_name)) || (!validtext(theform.gift_address)) || (!validtext(theform.gift_city)) || (!validtext(theform.gift_state)) || (!validtext(theform.gift_zip)) || (!validtext(theform.gift_country))) {
			msg+= "* Gift Information\n";
			if (!validtext(theform.gift_name)) msg+= "   * Name\n";
			if (!validtext(theform.gift_address)) msg+= "   * Address\n";
			if (!validtext(theform.gift_city)) msg+= "   * City\n";
			if (!validtext(theform.gift_state)) msg+= "   * State\n";
			if (!validtext(theform.gift_zip)) msg+= "   * Zip\n";
			if (!validtext(theform.gift_country)) msg+= "   * Country\n";
		} else {
			msg+="";
		}
	}

    if (!msg == "") {
		alert("Please fill in or correct the following field(s) before continuing:\n" + msg);
		return false;
	} else {
		document.theform.submit.value="Processing...";
		return true;
	}
}

	function validtext(textfield) {
		if (textfield.value=="") return false; 
		return true;
	}

	function validemail(email) {
		if (email.value.indexOf('@')==-1) return false;	
		return true;
	}

function money() {
amount=document.theform.amount.value;
	if (amount=="" || amount==0 || isNaN(amount)) {
		return false;
	} else {
		return true;	
	}
}

//Validates phone numbers
function countrytest(country,ac_phone,phone) {
	var where=country.value.toUpperCase();
	var area=ac_phone.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(ac_phone,phone) {
	var area=document.theform.ac_phone.value;
		if ((area.length !=3) || (isNaN(area))) {
			//alert(area + " is not a valid area code.");
			return(ac_phone,phone);
		} else {
			phonetest();
			return true;
		}
}
		
function phonetest(ac_phone,phone) {
	var area=document.theform.ac_phone.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(ac_phone,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 cardtype() {
	if (!document.theform.creditcard[0].checked && !document.theform.creditcard[1].checked && !document.theform.creditcard[2].checked) {
		return false;
	} else {
		return true;
	}
}

function validcred(creditcard,acct) {
	var cc=document.theform.creditcard
	var acct=document.theform.acct.value
	if (acct=="" || isNaN(acct)) {
		return false;
	} else {
		for (count=0; count<2; ++count) {
			if (cc.selectedIndex == 0) {
				if (acct.length==13 || acct.length==16) {
					return true;
				} else {
					return false;
				}
			}
			if (cc.selectedIndex == 1) {
				if (acct.length==16) {
					return true;
				} else {
					return false;
				}
			}
			if (cc.selectedIndex == 2) {
				if (acct.length==15) {
					return true;
				} else {
					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;
	}
}



