// JavaScript Document

function formValidator(){
	// Make quick references to our fields
		var mFname = document.getElementById('mFname');
	var cName = document.getElementById('cName');
	var email = document.getElementById('email');
	var rEmail = document.getElementById('rEmail');
    var rButton = document.form1.membership;
// Check each input in the order that it appears in the form!
	if(isAlphabet(mFname, "Please enter only letters for your name")){
		if(isAlphanumeric(cName, "Numbers and Letters Only for your company address")){
			if(emailValidator(email, "Please enter a valid email address")){
				if(remailValidator(email,rEmail, "The email are not the same")){
					if(rButtonChecker(rButton,"Please select one of the membership options.")){
							return true;
	        		  }
				}
			}
		}
	}
	
	
	return false;
	
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function remailValidator(elem,elem2, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		if (elem.value==elem2.value){return true;
		}else {
			alert(helperMsg);
		     elem.focus();
			return false;}
		
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function rButtonChecker(elem,helpermsg)
{
// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < elem.length; counter++)
 {
 // If a radio button has been selected it will return true
 // (If not it will return false)
 if (elem[counter].checked)
 radio_choice = true; 
 }

 if (!radio_choice)
 {
 // If there were no selections made display an alert box 
 alert(helpermsg)
 return false;
 }
 return true;
}







