// Javascript Program to Check the "realname" and "email" of a form
// CheckForm test the "realname" variable for entry and the "email" variable for format
function checkform(f) { // f is the form (passed using the this keyword)

	f.realname.value = f.firstname.value + " " + f.lastname.value; 
	
	if(f.firstname.value.length < 2){
		alert("Please Enter Your First Name.");
		f.firstname.style.background = "yellow"; // change the color of text field
		f.firstname.focus(); // put the prompt in the name field 
		return false; // make sure the form is not submitted
	}
	
	if(f.lastname.value.length < 2){
		alert("Please Enter Your Last Name.");
		f.lastname.style.background = "yellow"; // change the color of text field
		f.lastname.focus(); // put the prompt in the name field 
		return false; // make sure the form is not submitted
	}
	
//	if(f.Phone.value.length < 7){
//		alert("Please Enter Phone Number.");
//		f.Phone.style.background = "yellow"; // change the color of text field
//		f.Phone.focus(); // put the prompt in the name field 
//		return false; // make sure the form is not submitted
//	}

	error = check_Phone(f.Phone);
	if(error != ""){
		alert(error);
		f.Phone.style.background = "yellow"; // change the color of text field
		f.Phone.focus(); // put the prompt in the name field 
		return false; // make sure the form is not submitted
	}	
	
	// check the first email address ( the exclamation means "not" )
	if(!check_email(f.email.value)){
		alert("Invalid E-mail Address. Please Enter Your E-mail Address");
		f.email.style.background = "yellow"; // change the color of text field
		f.email.focus(); // put the prompt in the name field
		return false; // make sure the form is not submitted
	}
}

// Email Validation. Written by PerlScriptsJavaScripts.com
function check_email(e) {
	ok = " 1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
			return (false);
		}	
	} 
	
	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}

function check_Phone(fld) {
	var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

	if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
    }
    return error;

}

