//Login Form Validation
function loginValid(form){
	errors = "";
	if (form.email.value == "") {
		errors = errors+"- E-mail Address is required\n"
	}
	if (form.password.value == "") {
		errors = errors+"- Password is required\n";
	}
	if (errors == ""){
		return true;
	} else {
		errors = "The system could not log you in because of the following reasons.\nPlease make corrections and try again.\n\n"+errors;
		window.alert(errors);	
		return false;
	}
}
//Password Change Form Validation
function changePassValid(form){
	errors = "";
	if (!/^[A-Za-z\S]{6,12}$/.test(form.password.value)) {
		errors = errors+"You password must use 6-12 letters, numbers, or special characters.\nPlease try again.\n";
		window.alert(errors);
		return false;	
	} else {
		if (form.password.value != form.password2.value) {
			errors = "Passwords do not match.\nPlease try again.\n"
			window.alert(errors);
			return false;
		} else {
			if(form.password.value == "") {
				errors = errors+" - Password is required\n";
			}
			if(form.password2.value == "") {
				errors = errors+" - Confirmation Password is required\n";
			}
			if (errors == "") {
				return true;
			} else {
				errors = "Your request could not be processed for the following reasons.\nPlease make corrections and try again.\n\n"+errors;
				window.alert(errors);	
				return false;
			}	
		}
	}
}
//Forgotten Password Form Validation
function forgotPassValid(form){
	errors = "";
	if (form.username.value == "") {
		errors = "Username is required to process your request.\nPlease try again.\n"
		window.alert(errors);
		return false;
	} else {
		return true;
	}
}
//New Account Registration Form Validation
function accountRegValid(form){
	errors = "";
	if (form.FIRSTNAME.value == "") {
		errors = errors+"- First Name is required\n";
	}
	if (form.LASTNAME.value == "") {
		errors = errors+"- Last Name is required\n";
	}
	if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.EMAIL.value)) {
		errors = errors+"- Please enter a valid Email Address\n";
	}
	if (form.EMAIL.value != form.EMAIL2.value) {
		errors = errors+"- Email Addresses do not match\n";	
	}
	if (form.AGE.value == "") {
		errors = errors+"- Birthday is required\n";
	}
	if (form.COUNCIL.value == "") {
		errors = errors+"- Council is required\n";
	}
	if (!/^[A-Za-z0-9S]{1,25}$/.test(form.USERNAME.value)) {
		errors = errors+"- Username does not meet requirements\n";
	}
	if (!/^[A-Za-z0-9S]{6,12}$/.test(form.PASSWORD.value)) {
		errors = errors+"- Password does not meet requirements\n";
	}
	if (form.PASSWORD.value != form.PASSWORD2.value ) {
		errors = errors+"- Passwords do not match\n";
	}
	if (errors == ""){
		return true;
	} else {
		errors = "Your account could not be created for the following reasons.\nPlease make corrections and try again.\n\n"+errors;
		window.alert(errors);	
		return false;
	}
}
//Triggers age calculation
function getage (form) {
	var userage=0;
	var m = form.MONTH.value;
	var d = form.DAY.value;
	var y = form.YEAR.value;
	if (m != "" ) {
		if (d != "") {
			if (y != ""){
				userage = calcage(m,d,y);
				form.AGE.value = userage;
			}
		}
	}
		
}
//Calculates age based on supplied bithdate
function calcage(m,d,y) {
	var currentTime = new Date();
	var userage = currentTime.getFullYear() - y;
	if ((currentTime.getMonth()+1) <= m) {
		if(currentTime.getDate() < d) {
			userage = userage - 1;	
		}
	}
	return userage;
}
function displaymessage(msg){
	var message = "";
	if (msg == "notavail") {
		message = "This feature is not available at this time.\n\nPlease try back later.";
		window.alert(message);
	} else if (msg == "leavesite") {
		message = "You are about to navigate away from this site.\n\nAre you sure you want to do this?";
		window.confirm(message);
	} else if (msg == "closed") {
		message = "Online Conclave registration has ended.\n\nIf you still wish to attend, you must register as a walk-on at the event.";
		window.alert(message);
	} else if (msg == "tradingpost") {
		message = "Select this option only if you do not plan on attending Conclave.\n\nIf you plan on attending, return to your Account Dashboard and use the regular registration form.";
		window.alert(message);
	}
}

function eventValidation(form){
	errors = "";
	
	if (form.FIRSTNAME.value == "") {
		errors = errors+"- First Name is required\n";
	}
	if (form.LASTNAME.value == "") {
		errors = errors+"- Last Name is required\n";
	}
	if (form.GENDER.value == "Null") {
		errors = errors+"- Gender is required\n";
	}
	if (!/^(\d{4})[.-](\d{1,2})[.-](\d{1,2})$/.test(form.BIRTHDATE.value)) {
		errors = errors+"- Enter a valid Birthdate\n";
	}
	if (form.ADDRESS.value == "Null") {
		errors = errors+"- Address is required\n";
	}
	if (form.CITY.value == "") {
		errors = errors+"- City is required\n";
	}
	if (form.STATE.value == "Null" || form.STATE.value == "" || form.STATE.value == "  " || form.STATE.value == " ") {
		errors = errors+"- State is required\n";
	}
	if (!/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(form.ZIP.value)) {
		errors = errors+"- Enter a valid Zip Code\n";
	}
	if (!/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(form.PHONE.value)) {
		errors = errors+"- Enter a valid Phone Number\n"
	}
	if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.EMAIL.value)) {
		errors = errors+"- Enter a valid Email Address\n";
	}
	if (form.COUNCIL.value == "") {
		errors = errors+"- Council is required\n";
	}
	if (form.OAHONOR.value == "Null") {
		errors = errors+"- OA Honor is required\n";
	}
	if (form.ATTENDANCE.value == "Null") {
		errors = errors+"- Attendance is required\n";
	}
	if (form.FIRSTTIME.value == "Null") {
		errors = errors+"- First time attendee is required\n";
	}
	if (form.tpshow.value == "1") {
		if (form.TP10.value != 0 && form.TP10SIZE.value == "Null") {
			errors = errors+"- Select a Conclave T-shirt size\n";
		}
		if (form.TP11.value != 0 && form.TP11SIZE.value == "Null") {
			errors = errors+"- Select a Section T-Shirt size\n";
		}
		if (form.TP12.value != 0 && form.TP12SIZE.value == "Null") {
			errors = errors+"- Select a Fleece Jacket size\n";
		}
		if (form.TP13.value != 0 && form.TP13SIZE.value == "Null") {
			errors = errors+"- Select a Windbreaker Jacket size\n";
		}
		if (form.TP14.value != 0 && form.TP14SIZE.value == "Null") {
			errors = errors+"- Select a Hoodie size\n";
		}
	}
	if (errors == ""){
		calcTotal();
		return true;
	} else {
		errors = "Your registration could not be completed for the following reasons.\nPlease make corrections and try again.\n\n"+errors;
		window.alert(errors);	
		return false;
	}
}


function eventBulkValidation(form){
	errors = "";
	
	if (form.FIRSTNAME.value == "") {
		errors = errors+"- First Name is required\n";
	}
	if (form.LASTNAME.value == "") {
		errors = errors+"- Last Name is required\n";
	}
	if (form.GENDER.value == "Null") {
		errors = errors+"- Gender is required\n";
	}
	if (!/^(\d{4})[.-](\d{1,2})[.-](\d{1,2})$/.test(form.BIRTHDATE.value)) {
		errors = errors+"- Enter a valid Birthdate\n";
	}
	if (form.ADDRESS.value == "Null") {
		errors = errors+"- Address is required\n";
	}
	if (form.CITY.value == "") {
		errors = errors+"- City is required\n";
	}
	if (form.STATE.value == "Null" || form.STATE.value == "" || form.STATE.value == "  " || form.STATE.value == " ") {
		errors = errors+"- State is required\n";
	}
	if (!/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(form.ZIP.value)) {
		errors = errors+"- Enter a valid Zip Code\n";
	}
	if (form.COUNCIL.value == "") {
		errors = errors+"- Council is required\n";
	}
	if (form.OAHONOR.value == "Null") {
		errors = errors+"- OA Honor is required\n";
	}
	if (form.ATTENDANCE.value == "Null") {
		errors = errors+"- Attendance is required\n";
	}
	if (form.tpshow.value == "1") {
		if (form.TP10.value != 0 && form.TP10SIZE.value == "Null") {
			errors = errors+"- Select a Conclave T-shirt size\n";
		}
		if (form.TP11.value != 0 && form.TP11SIZE.value == "Null") {
			errors = errors+"- Select a Section T-Shirt size\n";
		}
		if (form.TP12.value != 0 && form.TP12SIZE.value == "Null") {
			errors = errors+"- Select a Fleece Jacket size\n";
		}
		if (form.TP13.value != 0 && form.TP13SIZE.value == "Null") {
			errors = errors+"- Select a Windbreaker Jacket size\n";
		}
		if (form.TP14.value != 0 && form.TP14SIZE.value == "Null") {
			errors = errors+"- Select a Hoodie size\n";
		}
	}
	if (errors == ""){
		calcTotal();
		return true;
	} else {
		errors = "Your registration could not be completed for the following reasons.\nPlease make corrections and try again.\n\n"+errors;
		window.alert(errors);	
		return false;
	}
}


function calcTotal(){
	
	//Zero-fill any blank trading post qty
	if (document.getElementById('tpshow').value == "1") {
		if(document.getElementById('TP10').value == "0"){ document.getElementById('TP10SIZE').value = "Null"; }
		if(document.getElementById('TP11').value == "0"){ document.getElementById('TP11SIZE').value = "Null"; }
		if(document.getElementById('TP12').value == "0"){ document.getElementById('TP12SIZE').value = "Null"; }
		if(document.getElementById('TP13').value == "0"){ document.getElementById('TP13SIZE').value = "Null"; }
		if(document.getElementById('TP14').value == "0"){ document.getElementById('TP14SIZE').value = "Null"; }
	}
	//Perform Calculations
	var attend;
	var attype = document.getElementById('ATTENDANCE').value;
	var trans = parseFloat(0).toFixed(2);
	if (attype == "TP"){
		trans = parseFloat(0).toFixed(2);
		attend = parseFloat(0).toFixed(2);
	} else if (attype == "Full") {
		trans = parseFloat(document.getElementById('LFEE').value).toFixed(2);
		attend = parseFloat(document.getElementById('FEE_STD').value).toFixed(2);
	} else if (attype == "Visit") {
		trans = document.getElementById('LFEE').value;
		attend = parseFloat(document.getElementById('FEE_VST').value).toFixed(2);
	} else if (attype == "Null") {
		trans = parseFloat(0).toFixed(2);
		attend = parseFloat(0).toFixed(2);
	}
	if (document.getElementById('tpshow').value == "1") {
		var tp1 = parseFloat(document.getElementById('TP1').value*125).toFixed(2);
		var tp2 = parseFloat(document.getElementById('TP2').value*20).toFixed(2);
		var tp3 = parseFloat(document.getElementById('TP3').value*4).toFixed(2);
		var tp4 = parseFloat(document.getElementById('TP4').value*4).toFixed(2);
		var tp5 = parseFloat(document.getElementById('TP5').value*4).toFixed(2);
		var tp6 = parseFloat(document.getElementById('TP6').value*4).toFixed(2);
		var tp7 = parseFloat(document.getElementById('TP7').value*45).toFixed(2);
		var tp8 = parseFloat(document.getElementById('TP8').value*12).toFixed(2);
		var tp9 = parseFloat(document.getElementById('TP9').value*12).toFixed(2);
		var tp10;
		var tp10size = document.getElementById('TP10SIZE').value;
		if (tp10size == "S" || tp10size == "M" || tp10size == "L" || tp10size == "XL"){
			tp10 = parseFloat(document.getElementById('TP10').value*18).toFixed(2); 
		} else if (tp10size == "2XL"){
			tp10 = parseFloat(document.getElementById('TP10').value*20).toFixed(2); 
		} else if (tp10size == "3XL"){
			tp10 = parseFloat(document.getElementById('TP10').value*23).toFixed(2); 
		} else if (tp10size == "4XL" || tp10size == "5XL" || tp10size == "6XL"){
			tp10 = parseFloat(document.getElementById('TP10').value*25).toFixed(2); 
		} else if (tp10size == "Null") {
			tp10 = parseFloat(0).toFixed(2);
		}
		var tp11;
		var tp11size = document.getElementById('TP11SIZE').value;
		if (tp11size == "S" || tp11size == "M" || tp11size == "L" || tp11size == "XL"){
			tp11 = parseFloat(document.getElementById('TP11').value*18).toFixed(2); 
		} else if (tp11size == "2XL"){
			tp11 = parseFloat(document.getElementById('TP11').value*20).toFixed(2); 
		} else if (tp11size == "3XL"){
			tp11 = parseFloat(document.getElementById('TP11').value*23).toFixed(2); 
		} else if (tp11size == "4XL" || tp11size == "5XL" || tp11size == "6XL"){
			tp11 = parseFloat(document.getElementById('TP11').value*25).toFixed(2); 
		} else if (tp11size == "Null") {
			tp11 = parseFloat(0).toFixed(2);
		}
		var tp12;
		var tp12size = document.getElementById('TP12SIZE').value;
		if (tp12size == "S" || tp12size == "M" || tp12size == "L"){
			tp12 = parseFloat(document.getElementById('TP12').value*48).toFixed(2); 
		} else if (tp12size == "XL" || tp12size == "2XL"){
			tp12 = parseFloat(document.getElementById('TP12').value*51).toFixed(2); 
		} else if (tp12size == "3XL"){
			tp12 = parseFloat(document.getElementById('TP12').value*52).toFixed(2); 
		} else if (tp12size == "4XL" || tp12size == "5XL" || tp12size == "6XL"){
			tp12 = parseFloat(document.getElementById('TP12').value*53).toFixed(2); 
		} else if (tp12size == "Null") {
			tp12 = parseFloat(0).toFixed(2);
		}
		var tp13;
		var tp13size = document.getElementById('TP13SIZE').value;
		if (tp13size == "S" || tp13size == "M" || tp13size == "L"){
			tp13 = parseFloat(document.getElementById('TP13').value*30).toFixed(2); 
		} else if (tp13size == "XL" || tp13size == "2XL"){
			tp13 = parseFloat(document.getElementById('TP13').value*33).toFixed(2); 
		} else if (tp13size == "3XL"){
			tp13 = parseFloat(document.getElementById('TP13').value*34).toFixed(2); 
		} else if (tp13size == "4XL" || tp13size == "5XL" || tp13size == "6XL"){
			tp13 = parseFloat(document.getElementById('TP13').value*35).toFixed(2); 
		} else if (tp13size == "Null") {
			tp13 = parseFloat(0).toFixed(2);
		}
		var tp14;
		var tp14size = document.getElementById('TP14SIZE').value;
		if (tp14size == "S" || tp14size == "M" || tp14size == "L"){
			tp14 = parseFloat(document.getElementById('TP14').value*28).toFixed(2); 
		} else if (tp14size == "XL" || tp14size == "2XL"){
			tp14 = parseFloat(document.getElementById('TP14').value*31).toFixed(2); 
		} else if (tp14size == "3XL"){
			tp14 = parseFloat(document.getElementById('TP14').value*32).toFixed(2); 
		} else if (tp14size == "4XL" || tp14size == "5XL" || tp14size == "6XL"){
			tp14 = parseFloat(document.getElementById('TP14').value*33).toFixed(2); 
		} else if (tp14size == "Null") {
			tp14 = parseFloat(0).toFixed(2);
		}
		var tp15 = parseFloat(document.getElementById('TP15').value*35).toFixed(2);
		var tp16 = parseFloat(document.getElementById('TP16').value*8.75).toFixed(2);
		var tp17 = parseFloat(document.getElementById('TP17').value*5.75).toFixed(2);
		var tp18 = parseFloat(document.getElementById('TP18').value*10).toFixed(2);
		var tp19 = parseFloat(document.getElementById('TP19').value*5.75).toFixed(2);
		var tp20 = parseFloat(document.getElementById('TP20').value*10).toFixed(2);
		var tpost = parseFloat(parseFloat(tp1)+parseFloat(tp2)+parseFloat(tp3)+parseFloat(tp4)+parseFloat(tp5)+parseFloat(tp6)+parseFloat(tp7)+parseFloat(tp8)+parseFloat(tp9)+parseFloat(tp10)+parseFloat(tp11)+parseFloat(tp12)+parseFloat(tp13)+parseFloat(tp14)+parseFloat(tp15)+parseFloat(tp16)+parseFloat(tp17)+parseFloat(tp18)+parseFloat(tp19)+parseFloat(tp20)).toFixed(2);
	} else
	var tpost = parseFloat(0).toFixed(2);
	document.getElementById('FEEATT').value = attend;
	document.getElementById('FEETRAN').value = trans;
	document.getElementById('FEETP').value = tpost;
	document.getElementById('FEETOTAL').value = parseFloat(parseFloat(attend)+parseFloat(trans)+parseFloat(tpost)).toFixed(2);
}


//Recieve Confirmation before Cancelling Registration
function verifyCancel(){
	confirmString = "Are your sure you want to Cancel your Conclave Registration?";
	var answer = window.confirm(confirmString);
	if (answer == true)	{
		return true;
	} else {
		return false;
	}
}
