//######################## MAIN FORM VALIDATION FUNCTION ######################
function validate() {

	var msg = '';

	if ( _proxy_jslib_handle(document.frmComp.txtFirstName, 'value', '', 0, 0) == "") {
		msg+="\nFirst Name";
	} 

	if ( _proxy_jslib_handle(document.frmComp.txtSurname, 'value', '', 0, 0) == "") {
		msg+="\nLast Name";
	}				

	if (document.frmComp.slctMonth.selectedIndex == 0 || document.frmComp.slctDay.selectedIndex == 0 || document.frmComp.slctYear.selectedIndex == 0){
		msg+="\nDate of Birth";
	}	

	if ( _proxy_jslib_handle(document.frmComp.slctCountry, 'value', '', 0, 0) == 0) {
		msg+="\nCountry";
	}								

	if ( _proxy_jslib_handle(document.frmComp.txtEmail, 'value', '', 0, 0) == "") {
		msg+="\nEmail Address";
	}

	if ( _proxy_jslib_handle(document.frmComp.txtEmail2, 'value', '', 0, 0) == "") {
		msg+="\nConfirm Email";
	}
	
	if (  _proxy_jslib_handle(document.frmComp.txtEmail, 'value', '', 0, 0) !=  _proxy_jslib_handle(document.frmComp.txtEmail2, 'value', '', 0, 0) ) {
		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( _proxy_jslib_handle(document.frmComp.txtEmail, 'value', '', 0, 0))) {
		alert("Please enter a valid email address");
		return false;	
	}
	
	if (!isDOB( _proxy_jslib_handle( _proxy_jslib_handle(document.frmComp.slctDay, (document.frmComp.slctDay.selectedIndex), 0, 0), 'value', '', 0, 0),
			 _proxy_jslib_handle( _proxy_jslib_handle(document.frmComp.slctMonth, (document.frmComp.slctMonth.selectedIndex), 0, 0), 'value', '', 0, 0),
			 _proxy_jslib_handle( _proxy_jslib_handle(document.frmComp.slctYear, (document.frmComp.slctYear.selectedIndex), 0, 0), 'value', '', 0, 0))) {
		return false;	
	}
		
	if ( _proxy_jslib_handle(document.frmComp.txtMobile, 'value', '', 0, 0) != ""){
		if (!isMobile( _proxy_jslib_handle(frmComp.txtMobile, 'value', '', 0, 0))){
			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( _proxy_jslib_handle(frmComp.slctCountry, 'value', '', 0, 0) == "GBR"){
			 _proxy_jslib_assign('', frmComp.txtzipcode, 'value', '=', ( ""));
			frmComp.txtzipcode.disabled = true;
			frmComp.txtPostcode1.disabled = false;
			frmComp.txtPostcode3.disabled = false;
		} else {
			 _proxy_jslib_assign('', frmComp.txtPostcode1, 'value', '=', ( ""));
			 _proxy_jslib_assign('', 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 >  _proxy_jslib_handle(monthdays, (mm - 1), 0, 0)) || (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));
} ;
_proxy_jslib_flush_write_buffers() ;