$(document).ready(function(){
	$("#add_another_player").click(function() {
		strFieldTitle = 'player_fields';
		$.get("registration_tts_player_fields.php", function(data){
			$("#player_fields tr:last").after(data);
		});
	});
});

function ValidateForm(theform) {
	var lastNameElements    = document.getElementsByName("lastName[]");
	var firstNameElements   = document.getElementsByName("firstName[]");
	var finNumberElements   = document.getElementsByName("finNumber[]");
	var genderElements      = document.getElementsByName("gender[]");
	var yearOfBirthElements = document.getElementsByName("yearOfBirth[]");
	var grade_idElements    = document.getElementsByName("grade_id[]");
	var school_idElements   = document.getElementsByName("school_id[]");
	var otherSchoolElements = document.getElementsByName("otherSchool[]");

	var numberOfPlayers = lastNameElements.length;

	for (var count = 0; count < numberOfPlayers; count++){
		if (lastNameElements[count].value=='') {
			alert('Please provide last name for player')
			lastNameElements[count].focus()
			return false
		}

		if (firstNameElements[count].value=='') {
			alert('Please provide first name for player')
			firstNameElements[count].focus()
			return false
		}

		/*
		if (finNumberElements[count].value=='') {
			alert('Please provide FIN number for player')
			finNumberElements[count].focus()
			return false
		}
		*/

		if (checkSelect(genderElements[count])==false) {
			alert('Please select gender of player')
			genderElements[count].focus()
			return false
		}

		if (checkSelect(yearOfBirthElements[count])==false) {
			alert('Please select year of birth of player')
			yearOfBirthElements[count].focus()
			return false
		}

		if (checkSelect(grade_idElements[count])==false) {
			alert('Please select grade of player')
			grade_idElements[count].focus()
			return false
		}

		if (checkSelect(school_idElements[count])==false) {
			alert('Please select school of player')
			school_idElements[count].focus()
			return false
		}

		var numberOfSchoolOptions = school_idElements[count].length;
		if (school_idElements[count].options[numberOfSchoolOptions-1].selected) {
			if (otherSchoolElements[count].value=='') {
				alert('Please provide name of school of player')
				otherSchoolElements[count].focus()
				return false
			}
		}
	}

	if ((theform.fLastName.value=='') && (theform.fFirstName.value=='') && (theform.mLastName.value=='') && (theform.mFirstName.value=='')) {
		alert('Please provide at least one parents name')
		theform.fLastName.focus()
		return false
	}
	
	if ( ((theform.fLastName.value!='') && (theform.fFirstName.value=='')) || ((theform.fLastName.value=='') && (theform.fFirstName.value!=''))) {
		alert('Please provide firstname and lastname for father')
		if (theform.fLastName.value=='') theform.fLastName.focus(); else theform.fFirstName.focus();
		return false
	}

	if ( ((theform.mLastName.value!='') && (theform.mFirstName.value=='')) || ((theform.mLastName.value=='') && (theform.mFirstName.value!='')) ) {
		alert('Please provide firstname and lastname for mother')
		if (theform.mLastName.value=='') theform.fLastName.focus(); else theform.mFirstName.focus();
		return false
	}

	if ((theform.homePhone.value=='') && (theform.fMobile.value=='') && (theform.mMobile.value=='')) {
		alert('Please provide at least one contact number')
		theform.homePhone.focus()
		return false
	}
	
	if ( (theform.homePhone.value!='') && (theform.homePhone.value.search && theform.homePhone.value.search(new RegExp("^[0-9 \-]*$","g"))<0) ) {
		alert('Only numerics, space and dash allowed for home phone')
		theform.homePhone.focus()
		return false
	}

	if ( (theform.fMobile.value!='') && (theform.fMobile.value.search && theform.fMobile.value.search(new RegExp("^[0-9 \-]*$","g"))<0) ) {
		alert('Only numerics, space and dash allowed for father mobile')
		theform.fMobile.focus()
		return false
	}

	if ( (theform.mMobile.value!='') && (theform.mMobile.value.search && theform.mMobile.value.search(new RegExp("^[0-9 \-]*$","g"))<0) ) {
		alert('Only numerics, space and dash allowed for mother mobile')
		theform.mMobile.focus()
		return false
	}

	if ((theform.email1.value=='') && (theform.email2.value=='')) {
		alert('Please provide at least one email')
		theform.email1.focus()
		return false
	}

	if ((theform.email1.value!='') && (checkForValidEmailAddress(theform.email1)==false)) {
		alert('Please enter a valid e-mail address')
		theform.email1.focus()
		return false
	}

	if ((theform.email2.value!='') && (checkForValidEmailAddress(theform.email2)==false)) {
		alert('Please enter a valid e-mail address')
		theform.email2.focus()
		return false
	}

	if ( (theform.pMobile.value!='') && (theform.pMobile.value.search && theform.pMobile.value.search(new RegExp("^[0-9 \-]*$","g"))<0) ) {
		alert('Only numerics, space and dash allowed for player mobile')
		theform.pMobile.focus()
		return false
	}

	if ((theform.pEmail.value!='') && (checkForValidEmailAddress(theform.pEmail)==false)) {
		alert('Please enter a valid player e-mail address')
		theform.pEmail.focus()
		return false
	}

	/*
	if(checkSelect(theform.carBrandId_carMakeId)==false) {
		alert('Please choose car brand and model')
		theform.carBrandId_carMakeId.focus()
		return false
	}
	*/

	return true
}

function checkSelect(theSelect)
{
	if (theSelect.options[0].selected == true) {
		return false;
	}
	return true;
}

function checkForValidEmailAddress(theinput)
{
	s=theinput.value

	if(s.search) {
		return (s.search(new RegExp("^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$","gi"))>=0)
	}

	if(s.indexOf) {
		at_character=s.indexOf('@')
		if(at_character<=0 || at_character+4>s.length)
			return false
	}

	if(s.length<6)
		return false
	else
		return true
}
