// http://www.sandersweb.net/comments/valid.js
// Copyright (C) 2011 David Sanders
// Updated 2011-07-08
/* Function submitIt() validates the user input for the feedback form located at
 * http://www.sandersweb.net/comments/
 * Returns false if there is a problem and true otherwise.
 * Email must match a regex for structure and be between 7 and 40 characters.
 * Name must be between 2 and 40 characters.
 * Comments must be between 5 and 480 characters.
 * Rating must be between 1 and 10.
 * Website max length 80 and match regex.
 */
function submitIt(myForm) {
    /* email address regex */
	var re = /^\w[\-.%\w]*\@[\-a-z0-9]+(\.[\-a-z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|cat|jobs|mobi|pro|travel|arpa|asia|tel|[a-z][a-z])$/i;
    /* website URL regex */
    var urlre = /^(https?|ftp)\:\/\/[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+(\/([a-z0-9+\$_-]\.?)+)*\/?/i;
	var val = myForm.email.value;
	if (val.indexOf("@") >= 1) {
		var name = val.substring(0,val.indexOf("@"));
		var domain = val.substring(val.indexOf("@")+1, val.length);
		if ((!re.test(val))||(name.length > 40)||(name.length < 1)||
					(domain.length > 40)||(domain.length < 5)) {
			alert("Invalid email address.  It must be like e.g. name@example.com "+
			"(max name length 40, min 1; max domain length 40, min 5)");
			myForm.email.focus();
			myForm.email.select();
			return false;
		}
		var val2 = myForm.name.value;
		if ((val2.length > 40)||(val2.length < 2)) {
			alert("Invalid name field (max length 40, min 2 chars)");
			myForm.name.focus();
			myForm.name.select();
			return false;
		}
		var val3 = myForm.comments.value;
		if ((val3.length > 480)|| (val3.length < 5)) {
			alert("Invalid comments field (max length 480, min 5 chars)");
			myForm.comments.focus();
			myForm.comments.select();
			return false;
		}
        var val4 = myForm.web.value;
		if (val4.length > 80) {
			alert("Invalid website field (max length 80)");
			myForm.web.focus();
			myForm.web.select();
			return false;
		}
        var val5 = myForm.rating.value;
        if ((val5.length > 0) && (val5 < 1 || val5 > 10)) {
            alert("Rating must be between 1 and 10");
            myForm.rating.focus();
			myForm.rating.select();
			return false;
        }
        var val6 = myForm.web.value;
        if (val6.length > 0 && !urlre.test(val6)) {
            alert("The website URL appears incorrect, please verify." + 
                    "It must be like http://www.example.com/");
            myForm.web.focus();
			myForm.web.select();
			return false;
		}
	} else {
			alert("The email address is not formatted correctly.  Please verify.");
			return false;
	}
	return true;
}


