function addMember(mode) {

	if( memberCount == 20 ) {
		document.getElementById('addMemberLink').style.display = 'none';
		return;
	}

	++memberCount;

	var titles = [
		"Mr",
		"Mrs",
		"Miss",
		"Ms",
		"Mstr"
	];

	var tableRow = document.createElement("tr");
	tableRow.setAttribute( 'id', 'memberRow' + memberCount );
	
	var fnCell =  document.createElement("td");
	var aaselect = document.createElement("select");
	aaselect.setAttribute("name","memberTitle"+memberCount);
	aaselect.setAttribute("id","memberTitle"+memberCount);

	for (var i = 0; i < titles.length; i++ ) {
		var option = document.createElement("option");
		var txt = document.createTextNode(titles[i]);
		option.appendChild(txt);
		aaselect.appendChild(option);
	}

	fnCell.appendChild(aaselect);
	tableRow.appendChild(fnCell);
	
	var fnCell =  document.createElement("td");
	var fninput = document.createElement("input");
	fninput.setAttribute("name","memberFirstName"+memberCount);
	fninput.setAttribute("id","memberFirstName"+memberCount);
	fninput.setAttribute("title","Member " + memberCount + " First Name");
	fninput.setAttribute("size","13");
	fninput.setAttribute("class","required");
	fninput.className='required';
	fnCell.appendChild(fninput);
	tableRow.appendChild(fnCell);
	
	var fnCell =  document.createElement("td");
	var fninput = document.createElement("input");
	fninput.setAttribute("name","memberSurname"+memberCount);
	fninput.setAttribute("id","memberSurname"+memberCount);
	fninput.setAttribute("title","Member " + memberCount + " Surname");
	fninput.setAttribute("size","13");
	fninput.setAttribute("class","required");
	fninput.className='required';
	fnCell.appendChild(fninput);
	tableRow.appendChild(fnCell);
	
	var fnCell =  document.createElement("td");
	var fninput = document.createElement("input");
	fninput.setAttribute("name","memberDOB"+memberCount);
	fninput.setAttribute("id","memberDOB"+memberCount);
	fninput.setAttribute("title","Member " + memberCount + " Date of Birth");
	fninput.setAttribute("size","10");
	fninput.setAttribute("class","required date");
	fninput.className='required date';
	fninput.setAttribute("value","dd/mm/yyyy");
	fninput.onfocus = function() {
		if( this.value == 'dd/mm/yyyy' ) this.value = '';
	}
	fninput.onblur = function() {
		if( this.value == '' ) this.value = 'dd/mm/yyyy';
	}
	fnCell.appendChild(fninput);
	tableRow.appendChild(fnCell);

	// Blank cell where student id would go
	var fnCell =  document.createElement("td");
	tableRow.appendChild(fnCell);
	
	document.getElementById('membersTable').appendChild(tableRow);
	
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function isFilled(field) {
	if (field.value.length < 1 || field.value == field.defaultvalue) {
		return false;
	} else {
		return true;
	}
}

function isDate(field) {
		var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])\/(0?[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|[12]\d|30)\/(0?[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|1\d|2[0-8])\/0?2\/((1[6-9]|[2-9]\d)\d{2}))|(29\/0?2\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
   if ((field.value.match(RegExPattern)) && (field.value!='')) {
       return false;
   } else {
       return true;
   }
}

function isFourDigits(field) {
	var RegExPattern = /^[0-9]{4}$/;
	return field.value.match(RegExPattern);
}

function validateForm(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.className.indexOf("required") != -1) {
			if (!isFilled(element)) {
				if( element.title != '' )
					alert("Please fill in the \""+element.title+"\" field.");
				else
					alert("Please fill in the \""+element.name+"\" field.");
				element.focus();
				return false;
			}
			if (element.className.indexOf("email") != -1) {
				if (!isEmail(element)) {
					if( element.title != '' )
						alert("The \""+element.title+"\" field must be a valid email address.");
					else
						alert("The \""+element.name+"\" field must be a valid email address.");
					element.focus();
					return false;
				}
			}
			if (element.className.indexOf("date") != -1) {
				if (isDate(element)) {
					if( element.title != '' )
						alert("The \""+element.title+"\" field must be a valid date.");
					else
						alert("The \""+element.name+"\" field must be a valid date.");
					element.focus();
					return false;
				}
			}
			if (element.className.indexOf("fourdigits") != -1) {
				if (!isFourDigits(element)) {
					if( element.title != '' )
						alert("The \""+element.title+"\" field must contain four digits.");
					else
						alert("The \""+element.name+"\" field must contain four digits.");
					element.focus();
					return false;
				}
			}
		}
	}
	return true;
}

function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
		if (thisform.className.indexOf("novalidation") == -1) {
			thisform.onsubmit = function() {
			return validateForm(this);
			}
		}
	}
}

function toggleStudentIDField(whichfield) {
	var thisStudentID = document.getElementById(whichfield);
	if (thisStudentID.disabled != true) {
		thisStudentID.value = "";
		thisStudentID.disabled = true;
		thisStudentID.setAttribute("class","");
	} else {
		thisStudentID.disabled = false;
		thisStudentID.setAttribute("class","required");
	}
	
}

addLoadEvent(prepareForms);
