/*
	 Validate comment form on LLP website
*/

function hasText(field) {
	// Verify that there is some kind of text in the field
	var str, myRegExp;
	str = field.value;
	myRegExp = /\S/;
	return myRegExp.test(str);
}

function isEmail(field) {
	// Verify that the input is a valid email address in its form
	var str, myRegExp;
	str = field.value;
	myRegExp = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
	return myRegExp.test(str);
}


function checkForm(form) {

	// Declare variables
	var nameLabel, emailLabel, commentsLabel, nameText, emailText, commentsText, errors, blue, red;
	
	// Set cleanFlag to assume there are no errors
	errors = 0;
	
	// Set LLP text colors
	blue = '#395BAE';
	red  = '#FF0000';
	
	// Get references to text field objects
	nameLabel = document.getElementById('myName');
	nameText = document.getElementById('Name');
	emailLabel = document.getElementById('myEmail');
	emailText = document.getElementById('Email');
	commentsLabel = document.getElementById('myComments');
	commentsText = document.getElementById('Comments');
	
	// Validate each field and provide feedback if required
	
	// Name
	if (hasText(nameText)) {
		// There is a name
		nameLabel.innerHTML = 'Name';
		nameLabel.style.color = blue;
	}
	else {
		// There is not a name
		errors+=1;
		nameLabel.innerHTML = '* Missing Name';
		nameLabel.style.color = red;
	}
	
	// Email
	if (isEmail(emailText)) {
		// Email address has proper form
		emailLabel.innerHTML = 'Email';
		emailLabel.style.color = blue;
	}
	else {
		// Email address does not have proper form
		errors+=1;
		emailLabel.innerHTML = '* Invalid Email Address';
		emailLabel.style.color = red;
	}
	
	// Comments
	if (hasText(commentsText)) {
		// There is a comment
		commentsLabel.innerHTML = 'Comment/Question';
		commentsLabel.style.color = blue;
	}
	else {
		// There is not a comment
		errors+=1;
		commentsLabel.innerHTML = '* Missing Comment';
		commentsLabel.style.color = red;
	}
	
	if (!errors) {
		document.getElementById('submit').setAttribute('value','Thanks!');
		document.getElementById('submit').setAttribute('disabled',true);
		return true;
	}
	else {
		return false;
	}
}

