// Javascript

function pickLockers(){
	document.getElementById('halflocker').disabled=false;
	document.getElementById('fulllocker').disabled=false;
}

function checkForm(){
	
	// set variable quick references
	memCY = document.getElementById('memCY');
	memNY = document.getElementById('memNY');
	waiver = document.getElementById('waiver');
	firstName = document.getElementById('billTo_firstName');
	lastName = document.getElementById('billTo_lastName');
	pid = document.getElementById('billTo_customerID');
	street1 = document.getElementById('billTo_street1');
	city = document.getElementById('billTo_city');
	state = document.getElementById('billTo_state');
	zip = document.getElementById('billTo_postalCode');
	homePhone = document.getElementById('billTo_phoneNumber');
	email = document.getElementById('billTo_email');
	lottery = document.getElementById('lottery');
	maleSelection = document.getElementById('male');
	femaleSelection = document.getElementById('female');
	
	if(chooseMems(memCY, memNY, "You must choose either a current-year or next-year membership or both. Please revise your selections and try again.", "If you are a current member, you must choose a next-year membership to continue. Please revise your selections and try again.")){
		if (notEmpty(firstName, "Please enter your first name.")){
			if (notEmpty(lastName, "Please enter your last name.")){
				if (isPennID(pid, "Please enter your Penn ID number.")){
					if (isNumeric(pid, "Please enter your numeric Penn ID number.")){
						if (isGender(maleSelection, femaleSelection, "Please select your gender.")){
							if (notEmpty(street1, "Please enter your street address.")){
								if (notEmpty(city, "Please enter your city.")){
									if (isState(state, "Please enter your state as a two-letter abbreviation")){
										if (isZip(zip, "Please enter your five-digit zip code.")){
											if (isNumeric(zip, "Please enter your five-digit numeric zip code.")){
												if (isNumeric(homePhone, "Please enter your 10-digit phone number without spaces or punctuation.")){
													if (isPhone(homePhone, "Please enter your 10-digit phone number without spaces or punctuation.")){
														if (notEmpty(email, "Please enter your email address.")){
															if (isEmail(email, "Please enter your valid email address.")){
																if (waiverChecked(waiver, "You cannot register unless you agree to the User Agreement/Waiver, Instructional Program Agreement, and Locker Agreement.")){
																	if (isEligible(memCY, memNY, lottery, "You must choose a 2009-2010 membership to enter the lottery. Please correct your membership selections and try again.", "You must either choose a summer 2009 membership or already be a member to enter the locker lottery. Please correct your selections and try again.")){
																		return true;
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}	
					}
				}
			}
		}
	}
	return false;
}
							
											

function chooseMems(elem1, elem2, helperMsg1, helperMsg2){
	if (elem1.value == "none" && elem2.value == "none") {
		alert(helperMsg1);
		elem1.focus();
		return false;
	}
	else if (elem1.value == "mem" && elem2.value == "none") {
		alert(helperMsg2);
		elem1.focus();
		return false;	
	}
	else {
		return true;
	}
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isPhone(elem, helperMsg){
	if(elem.value.length != 10){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function isPennID(elem, helperMsg){
	if(elem.value.length != 8){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isState(elem, helperMsg){
	if(elem.value.length != 2){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isZip(elem, helperMsg){
	if(elem.value.length != 5){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function waiverChecked(elem, helperMsg){
	if(elem.checked){
		return true;
	}
	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isEmail(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if (elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isEligible(elem1, elem2, elem3, helperMsg1, helperMsg2){
	if (elem3.checked) {
		if (elem2.value == "none"){
			alert(helperMsg1);
			elem2.focus();
			return false;
		} else if (elem1.value == "none"){
			alert(helperMsg2);
			elem1.focus();
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function isGender(elem1, elem2, helperMsg){
	if (!elem1.checked && !elem2.checked){
		alert(helperMsg);
		return false;
	} else {
		return true;
	}
}