//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 == "") {
		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.CONCLAVESHIRT.value != 0 && form.CONCLAVESHIRTSIZE.value == "Null") {
			errors = errors+"- Select a Conclave T-shirt size\n";
		}
		if (form.SECTIONSHIRT.value != 0 && form.SECTIONSHIRTSIZE.value == "Null") {
			errors = errors+"- Select a Section T-Shirt 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 == "") {
		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.CONCLAVESHIRT.value != 0 && form.CONCLAVESHIRTSIZE.value == "Null") {
			errors = errors+"- Select a Conclave T-shirt size\n";
		}
		if (form.SECTIONSHIRT.value != 0 && form.SECTIONSHIRTSIZE.value == "Null") {
			errors = errors+"- Select a Section T-Shirt 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(){
	
	if (document.getElementById('tpshow').value == "1") {
		//Zero-fill any blank trading post qty
		if(document.getElementById('CONCLAVESHIRT').value == "0"){ document.getElementById('CONCLAVESHIRTSIZE').value = "Null"; }
		if(document.getElementById('SECTIONSHIRT').value == "0"){ document.getElementById('SECTIONSHIRTSIZE').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('COLLECTORSET').value*22).toFixed(2);
		var tp2 = parseFloat(document.getElementById('CONCLAVEPATCH').value*5).toFixed(2);
		var tp3 = parseFloat(document.getElementById('SECTIONPATCH').value*5).toFixed(2);
		var tp4 = parseFloat(document.getElementById('CONJACKETPATCH').value*14).toFixed(2);
		var tp5 = parseFloat(document.getElementById('CONCHENILLEPATCH').value*22).toFixed(2);
		var tp6 = parseFloat(document.getElementById('SECJACKETPATCH').value*12).toFixed(2);
		var tp7 = parseFloat(document.getElementById('SECCHENILLEPATCH').value*22).toFixed(2);
		var tp8 = parseFloat(document.getElementById('NECKERCHIEF').value*22).toFixed(2);
		var tp9;
		var tp9size = document.getElementById('CONCLAVESHIRTSIZE').value;
		if (tp9size == "S" || tp9size == "M" || tp9size == "L" || tp9size == "XL"){
			tp9 = parseFloat(document.getElementById('CONCLAVESHIRT').value*17).toFixed(2); 
		} else if (tp9size == "2XL" || tp9size == "3XL" || tp9size == "4XL"){
			tp9 = parseFloat(document.getElementById('CONCLAVESHIRT').value*18).toFixed(2); 
		} else if (tp9size == "Null") {
			tp9 = parseFloat(0).toFixed(2);
		}
		var tp10;
		var tp10size = document.getElementById('SECTIONSHIRTSIZE').value;
		if (tp10size == "S" || tp10size == "M" || tp10size == "L" || tp10size == "XL"){
			tp10 = parseFloat(document.getElementById('SECTIONSHIRT').value*17).toFixed(2); 
		} else if (tp10size == "2XL" || tp10size == "3XL" || tp10size == "4XL"){
			tp10= parseFloat(document.getElementById('SECTIONSHIRT').value*18).toFixed(2); 
		} else if (tp10size == "Null") {
			tp10 = parseFloat(0).toFixed(2);
		}
		var tp11 = parseFloat(document.getElementById('BELTBUCKLE').value*15).toFixed(2);	
		var tp12 = parseFloat(document.getElementById('CONBOLO').value*10).toFixed(2);
		var tp13 = parseFloat(document.getElementById('SECBOLO').value*10).toFixed(2);	
		var tp14 = parseFloat(document.getElementById('CONSHIELD').value*7).toFixed(2);
		var tp15 = parseFloat(document.getElementById('SECSHIELD').value*7).toFixed(2);	
		var tp16 = parseFloat(document.getElementById('HAT').value*20).toFixed(2);	
		var tp17 = parseFloat(document.getElementById('KNIFE').value*35).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)).toFixed(2);
	} else {
		var tpost = parseFloat(0).toFixed(0);
	}
	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;
	}
}
