// Contact Us web form data validation

function isValidEmail(strEmail){
	// This function returns true if the email is valid and false if not
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

   // Search email text for regular expression matches
    if (strEmail.search(validRegExp) == -1) {return false;} 
    return true; 
}

function validate_required(field,alerttxt){
	with (field){
		if (value==null||value==""){
			alert(alerttxt);return false;
		}
		else {return true;}
	}
}

function validate_ifa_submission(thisform){
	with (thisform){

		task.value = 'SubmitIFAReg';
		
		if (validate_required(forename,"Please provide your forename.")==false)
			{forename.focus();return false;}

		if (validate_required(surname,"Please provide your surname.")==false)
			{surname.focus();return false;}

		//Validate e-mail
		if (validate_required(submittedemail,"Please provide your e-mail address.")==false)
			{submittedemail.focus();return false;}

		if (!isValidEmail(submittedemail.value))
			{alert("Sorry but the e-mail address provided isn't valid.\n\nPlease check it and try again.");
			submittedemail.focus();return false;}

		if (submittedemail.value!=email2.value)
			{alert("Sorry but the e-mail confirmation doesn't match the first one given.\n\nPlease check it and try again.");
			email2.focus();return false;}

		if (validate_required(username,"Please choose a username to access the IFA area. This can be changed once you've looged in.")==false)
			{username.focus();return false;}

		if (validate_required(password,"Please choose a password to access the IFA area. This can be changed once you've looged in.")==false)
			{password.focus();return false;}

		//Validate phone Nos if telephone is the preferred contact method
		if (Contact_method[1].checked){
				
				if ((hometel.value==null||hometel.value=="")&&(worktel.value==null||worktel.value=="")&&(mobiletel.value==null||mobiletel.value=="")){
					alert("You have indicated that you prefer to be contacted by phone. Please provide at least one phone number (home, work or mobile) so that we can contact you at a convenient time.");
					hometel.focus();return false
				}
				else{
					if (validate_required(calltime,"Please gives us the time when it would be most convenient for us to call you.")==false)
						{calltime.focus();return false;}
				}
		}

		// Prompt if no phone or e-mail recorded
		if ((hometel.value==null||hometel.value=="")&&(worktel.value==null||worktel.value=="")&&(mobiletel.value==null||mobiletel.value=="")&&(submittedemail.value=="")){
				alert("Please provide either a phone number or an e-mail address so that we can get back to you.");
				hometel.focus();return false
		}

	}
}

function clearForm(){
  var r=confirm("Are you sure you want to remove all information from this form?\n\nClick 'OK' for yes or 'Cancel' for no.")
  if (r==true){
	document.forms.ifareg.reset()
  }
  else{
    alert("You've chosen not to clear the form so please continue\nand click 'Submit enquiry' when ready.")
  }
}

// IFA-specific checks
function checkPassword(myControl){

	// Remove leading and trailing space
	myControl.value = myControl.value.replace(/^\s+|\s+$/g,'');

	if (myControl.value.length<6){
		alert("Please ensure your password is at least 6 characters long.\n\nYou can give a temporary password now to register if you wish as you can change it at any time in the future after you log in to your IFA area of the Sheffield Mutual web site.");
		myControl.focus();
	}
}