//######################## MAIN FORM VALIDATION FUNCTION ######################
function validate() {

	var msg = '';

	if (document.frmComp.txtFirstName.value == "") {
		msg+="\nFirst Name";
	} 

	if (document.frmComp.txtSurname.value == "") {
		msg+="\nLast Name";
	}				

	if (document.frmComp.slctMonth.selectedIndex == 0 || document.frmComp.slctDay.selectedIndex == 0 || document.frmComp.slctYear.selectedIndex == 0){
		msg+="\nDate of Birth";
	}	

	if (document.frmComp.slctCountry.value == 0) {
		msg+="\nCountry";
	}								

	if (document.frmComp.txtEmail.value == "") {
		msg+="\nEmail Address";
	}

	if (document.frmComp.txtEmail2.value == "") {
		msg+="\nConfirm Email";
	}
	
	if ( document.frmComp.txtEmail.value != document.frmComp.txtEmail2.value ) {
		msg+="\nEmail addresses do not match.";
	}
	
	if (!document.frmComp.agree.checked) {
		msg+='\nYou must agree to the Privacy Policy and/or Terms and Conditions.';
	}
	
	if (msg != "") {
		alert("Please complete the following field(s)\n" + msg);
		return false;
	}
	
	if (!isEmail(document.frmComp.txtEmail.value)) {
		alert("Please enter a valid email address");
		return false;	
	}
	
	if (!isDOB(document.frmComp.slctDay[document.frmComp.slctDay.selectedIndex].value,
			document.frmComp.slctMonth[document.frmComp.slctMonth.selectedIndex].value,
			document.frmComp.slctYear[document.frmComp.slctYear.selectedIndex].value)) {
		return false;	
	}
		
	if (document.frmComp.txtMobile.value != ""){
		if (!isMobile(frmComp.txtMobile.value)){
			alert("Please enter a valid mobile number.");
			return false;
		}
	}
}

//###################################
function isMobile( str ) {
	var r1 = new RegExp("[a-z]|[A-Z]");
	return (!r1.test(str));
}

//#############################
function checkCountry() {
	if (document.all){
		if(frmComp.slctCountry.value == "GBR"){
			frmComp.txtzipcode.value = "";
			frmComp.txtzipcode.disabled = true;
			frmComp.txtPostcode1.disabled = false;
			frmComp.txtPostcode3.disabled = false;
		} else {
			frmComp.txtPostcode1.value = "";
			frmComp.txtPostcode3.value = "";
			frmComp.txtzipcode.disabled = false;
			frmComp.txtPostcode1.disabled = true;
			frmComp.txtPostcode3.disabled = true;
		}
	}	
}

//#######################################################
function isDOB(dd,mm,yyyy) {
	
	monthdays = new Array(12);
	monthdays[0]=31;
	monthdays[1]=28;
	monthdays[2]=31;
	monthdays[3]=30;
	monthdays[4]=31;
	monthdays[5]=30;
	monthdays[6]=31;
	monthdays[7]=31;
	monthdays[8]=30;
	monthdays[9]=31;
	monthdays[10]=30;
	monthdays[11]=31;
	
	todayDate=new Date();
	thisyear=todayDate.getFullYear();

	if (isNaN(dd) || isNaN(mm) || isNaN(yyyy)) {
		alert('Please ensure you have entered numbers for the Date of Birth');
		return false;
	}

	if (((yyyy % 4 == 0) && !(yyyy % 100 == 0)) ||(yyyy % 400 == 0)) monthdays[1]++;
	
	if ((yyyy < 1900) || (yyyy >= (thisyear+1900))) {
		alert('Please check the year for the Date of Birth');
		return false;
	}
	
	if ((mm > 12) || (mm < 1)) {
		alert('Please check the month for the Date of Birth');
		return false;
	}
	
	if ((dd > monthdays[mm - 1]) || (dd < 1)) {
		alert('Please check the day for the Date of Birth');
		return false;
	}
	
	var testYear = parseInt(yyyy)+14;
	if( testYear > thisyear ) {
		alert( "Sorry, you must be 14 or over to register.");
		return false;
	}
	
	if( testYear == thisyear ) {
		var testMonth = parseInt(todayDate.getMonth()) + 1;
		
		if( testMonth < parseInt(mm) ) {
			alert( "Sorry, you must be 14 or over to register." );
			return false;
		}
		
		if( testMonth == parseInt(mm) ) {
			var testDay = parseInt(todayDate.getDate());
			
			if( testDay < parseInt(dd) ) {
				alert( "Sorry, you must be 14 or over to register." );
				return false;
			}	
		}
	}
	
	return true;

}

//#################################
function isEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}