function checkForm() {

        var isGood = true;

	var name = document.getElementById("textfield").value;
	var email = document.getElementById("textfield3").value;
	var phone = document.getElementById("textfield4").value;

	if (name == "" || email == "" || phone == "") {
		alert("All form should be completed in order to submit.");
		return false;
	}

	
	if (!IsEmail(email)) {
		alert("Invalid email.  Email address should be in this format: user@domain.com");
		return false;
	}

	if (!IsAlpha(name)) {
		alert("Invalid name.  Name should contain only letters.");
		return false;
	}

	if (!IsNumeric(phone)) {
		alert("Invalid phone number.  This field should contain only numbers.");
		return false;
	}
    
	
	if (echeck(email)==false){
		 
        return false ;
	}
        return isGood;

}

function IsAlpha(name) {

return(name.match(/^[a-zA-Z\s+]+$/));

}


function IsNumeric(sText) {

   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }

return IsNumber;
   
}



function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid email.  Email address should be in this format: user@domain.com")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email.  Email address should be in this format: user@domain.com")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid email.  Email address should be in this format: user@domain.com")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid email.  Email address should be in this format: user@domain.com")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid email.  Email address should be in this format: user@domain.com")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid email.  Email address should be in this format: user@domain.com")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid email.  Email address should be in this format: user@domain.com")
		    return false
		 }

 		 return true					
	}



function IsEmail(emailString) {

return(emailString.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/));

}

function validate_and_submit() {


	if (checkForm()) {
		document.fm.submit();
	}

}

