/*
-------------------------------------------------------
Series of functions to validate HTML form input
Be careful about form field names!
------------------------------------------------------- */


/* Series of generic validation functions
------------------------------------------------------- */



// Check if the email field is entered and/or valid
function checkEmail (strng) {
	var error="";
	if (strng == "") {
   	error = "You must provide your email address.\n";
	}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "You must provide a valid email address.\n";
    }
    else {
	//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
       if (strng.match(illegalChars)) {
          error = "Your email address contains illegal characters.\n";
       }
    }
	return error;    
}


// Check if the name field is entered
function checkName (strng) {
	var error = "";
	if (strng == "") {
		error = "You must provide your name.\n";
	}
	return error;
}






// Check the $notify text field:
// this field can't be validated easily, but
// we can check whether text has been entered
function checkNotify (strng) {
	var error = "";
	if (strng == "") {
		error = "You must provide at least one recipient email address.\n";
	}
	return error;
}

// Check the $question text field
function checkQuestion (strng) {
	var error = "";
	if (strng == "") {
		error = "You must provide a question or a comment.\n";
	}
	return error;
}



/* Series of functions tailored to specific forms
on the site. These use the generic validation
functions listed above. Called from a javascript
link attached to an <input type="button">. E.g:
<input type="button" onClick="checkForm(this.form);">
------------------------------------------------------- */

// Validate the "Share Your Decision" form
// Required fields are: from_name, from_email, notify
function checkShareForm(theform) {
	var why = "";
	why += checkEmail(theform.from_email.value);
	why += checkName(theform.from_name.value);
	why += checkNotify(theform.notify.value);
	if (why != "") {
		alert(why);
		return false;
	}
	else theform.submit();
	return true;
}

// Validate the "Questions & Feedback" form
// Required fields are: from_name, from_email, question
function checkQuestionForm(theform) {
	var why = "";
	why += checkEmail(theform.from_email.value);
	why += checkName(theform.from_name.value);
	why += checkQuestion(theform.question.value);
	if (why != "") {
		alert(why);
		return false;
	}
	else theform.submit();
	return true;
}