// email validation script

String.prototype.isEmail = function() {
	if (this.length<5) {
		return false;
	}
	
	var iChars = "*|,\":<>[]{}`';()&$#%";
	var eLength = this.length;
	for (var i = 0; i<eLength; i++) {
		if (iChars.indexOf(this.charAt(i)) != -1) {
			return false;
		}
	}
	
	var atIndex = this.lastIndexOf("@");
	if (atIndex<1 || (atIndex == eLength-1)) {
		return false;
	}
	
	var pIndex = this.lastIndexOf(".");
	if (pIndex<4 || (pIndex == eLength-1)) {
		return false;
	}
	
	if (atIndex>pIndex) {
		return false;
	}
	return true;
};

/* THIS VALIDATES THE SEARCH BOX */

function validate_searchbox(theForm)
{
	if (theForm.searchquery.value == "")
	  	{
			alert("Please input a search query.");
			theForm.searchquery.focus();
			return (false);
		}
}

/* THIS VALIDATES THE ARCHIVE PULLDOWN */

function validate_archive_pulldown(theForm)
{
	if (theForm.pr_archive_year_select.value == "default")
	  	{
			alert("Please select a year.");
			theForm.pr_archive_year_select.focus();
			return (false);
		}
}

/* THIS VALIDATES THE PRESS RELESASE REGISTRAION PAGE */

function validate_pr_reg(theForm)
{

	// title
	if (theForm.title.value == "default")
  	{
		alert("Please select your title.");
		theForm.title.focus();
		return (false);
	}
	
	// forename
	if (theForm.forename.value == "")
  	{
		alert("Please input your forename.");
		theForm.forename.focus();
		return (false);
	}
	
	// surname
	if (theForm.surname.value == "")
  	{
		alert("Please input your surname.");
		theForm.surname.focus();
		return (false);
	}
	
	// is it a solid email structure
	if (!theForm.email.value.isEmail())
	{
		alert("Please enter a valid email address.\ne.g. user@domain.com");
		theForm.email.focus();
		return (false);
	}
	
	// agree
	if (!theForm.agree.checked)
  	{
		alert("Please make sure agree with the Terms and Conditions.");
		return (false);
	}
	
	// return if OK
	return (true);
	
}

/* THIS VALIDATES THE GENERIC REGISTRAION PAGE */

function validate_gen_reg(theForm)
{

	// title
	if (theForm.title.value == "default")
  	{
		alert("Please select your title.");
		theForm.title.focus();
		return (false);
	}
	
	// forename
	if (theForm.forename.value == "")
  	{
		alert("Please input your forename.");
		theForm.forename.focus();
		return (false);
	}
	
	// surname
	if (theForm.surname.value == "")
  	{
		alert("Please input your surname.");
		theForm.surname.focus();
		return (false);
	}
	
	// is it a solid email structure
	if (!theForm.email.value.isEmail())
	{
		alert("Please enter a valid email address.\ne.g. user@domain.com");
		theForm.email.focus();
		return (false);
	}
	
	// agree
	if (!theForm.agree.checked)
  	{
		alert("Please make sure agree with the Terms and Conditions.");
		return (false);
	}
	
	// return if OK
	return (true);
	
}

/* THIS VALIDATES THE GENERIC REGISTRAION PAGE */

function validate_contact_form(theForm)
{

	// title
	if (theForm.title.value == "default")
  	{
		alert("Please select your title.");
		theForm.title.focus();
		return (false);
	}
	
	// forename
	if (theForm.forename.value == "")
  	{
		alert("Please input your forename.");
		theForm.forename.focus();
		return (false);
	}
	
	// surname
	if (theForm.surname.value == "")
  	{
		alert("Please input your surname.");
		theForm.surname.focus();
		return (false);
	}
	
	// is it a solid email structure
	if (!theForm.email.value.isEmail())
	{
		alert("Please enter a valid email address.\ne.g. user@domain.com");
		theForm.email.focus();
		return (false);
	}
	
	// comments
	if (theForm.comments.value == "")
  	{
		alert("Please input your comments.");
		theForm.comments.focus();
		return (false);
	}
	
	// agree
	if (!theForm.agree.checked)
  	{
		alert("Please make sure agree with the Terms and Conditions.");
		return (false);
	}
	
	// return if OK
	return (true);
	
}
