// Validation

function validate() {
	
	var txtForenames = document.getElementById("txtForenames");
	var txtSurname = document.getElementById("txtSurname");
	var txtHometelno = document.getElementById("txtHometelno");
	var txtMobiletelno = document.getElementById("txtMobiletelno");
	var txtEmail = document.getElementById("txtEmail");
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");

	
	if (chkPrivacyPolicy.checked==true && vrule_name(txtForenames.value) && vrule_name(txtSurname.value) && (vrule_telephone(txtHometelno.value) || vrule_telephone(txtMobiletelno.value)) && vrule_email(txtEmail.value)) {
		// tidy up the field content before submitting
		document.getElementById("txtHometelno").value = vtidy_telephone( txtHometelno.value );
		document.getElementById("txtMobiletelno").value = vtidy_telephone( txtMobiletelno.value );
		return true;	
	} else {
		var msg = "The following fields must be completed correctly before we can process your call back request\n\n";
		msg += (!vrule_name(txtForenames.value))?" - First name\n":"";
		msg += (!vrule_name(txtSurname.value))?" - Surname\n":"";
		msg += (!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value))?" - Telephone or Mobile number\n":"";
		msg += (!vrule_email(txtEmail.value))?" - A valid email address\n":"";
		msg += (!chkPrivacyPolicy.checked==true)?" - Agree to our Privacy Policy\n":"";
		msg += "\nPlease supply these missing details and then click 'Get free help now'";
		alert(msg);	
		return false;
	}
}

function vrule_telephone( value ) {
	
	value = vtidy_telephone( value );
	
	telNoPattern = /^\d{11}$/;
	return telNoPattern.test(value);
}

function vtidy_telephone( value ) {
	// strip out any spaces and other junk
	return value.replace( /[^0-9]/g, "");
}

function vrule_name( value ) {
	
	if (value.length == 0) return false; // name is required
	
	telNoPattern = /^[a-zA-Z\-\' ]*$/;
	
	return telNoPattern.test(value);
}

function vrule_email( value ) {
	
	if (value.length == 0) return true; // email address is optional
	
	telNoPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	return telNoPattern.test(value);
}



// Show details

function showDetails() {
	changeDisplay( "details", "block" );
}

function hideDetails() {
	changeDisplay( "details", "none" );
}

function changeDisplay( id, type ) {
	var sel = document.getElementById( id );
	sel.style.display = type;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



// Pop up window

function openpopup(popurl){
var winpops=window.open(popurl,"","width=400,height=400,scrollbars,resizable")
}



