/* AP - 30/11/2006 - code adapted from login.js */

/* Error Messages */
var allFieldsEmpty				= "Please complete the form to send us comments.",
	emptyName					= "Please enter your name.",
	emptyEmailaddress			= "Please enter your e-mail address.",
	emptyComments				= "Please enter your comments.",
	invalidName					= "The name field can only accept alphabetic, hyphen and space characters.",
	invalidEmailaddress			= "The email address must be valid.",
	tooManyComments				= "Please enter less than 1000 characters.";

function setFocus(fieldName)
{
	if (fieldName == "name")
	{
		document.form1.name.select();
		document.form1.name.focus();
	}
	else if (fieldName == "name")
	{
		document.form1.emailaddress.select();
		document.form1.emailaddress.focus();
	}
	else
	{
		document.form1.comments.select();
		document.form1.comments.focus();
	}
}

function validateInput()
{

	document.form1.name.value = document.form1.name.value.trim();
	document.form1.emailaddress.value = document.form1.emailaddress.value.trim();
	document.form1.comments.value = document.form1.comments.value.trim();
		
	var name = document.form1.name.value;
	var emailaddress = document.form1.emailaddress.value;
	var comments = document.form1.comments.value;

	/*if(name.value == "" && emailaddress.value == "" && comments.value == "")
	{
		alert(allFieldsEmpty);
		setFocus("name");
		//document.getElementById('btnSignIn').disabled = false;
		return false;
	}*/
	
	if (name == null || name == "")
	{
		alert(emptyName);
		setFocus("name");
		//document.getElementById('btnSignIn').disabled = false;
		return false;
	}
	
	if (checkForRegExp(name)){
		alert(invalidName);
		setFocus("name");
		//document.getElementById('btnSignIn').disabled = false;
		return false;
	}
	
	if (emailaddress == null || emailaddress == "")
	{
		alert(emptyEmailaddress);
		setFocus("emailaddress");
		//document.getElementById('btnSignIn').disabled = false;
		return false;
	}
	
	if (!validate_email(emailaddress))
	{
		alert(invalidEmailaddress);
		setFocus("emailaddress");
		//document.getElementById('btnSignIn').disabled = false;
		return false;
	}
	
	if (comments == null || comments == "")
	{
		alert(emptyComments);
		setFocus("comments");
		//document.getElementById('btnSignIn').disabled = false;
		return false;
	}
	
	if (comments.length > 1000)
	{
		alert(tooManyComments);
		setFocus("comments");
		//document.getElementById('btnSignIn').disabled = false;
		return false;
	}

	var ready=confirm("Click OK to send comments or Cancel to return to the form.");
	if (ready)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

/* E-mail validation code adapted from W3Schools.com
http://www.w3schools.com/js/js_form_validation.asp */
function validate_email(field)
{
	apos=field.indexOf("@")
	dotpos=field.lastIndexOf(".")
	if (apos < 1||dotpos-apos < 2) 
	{
		return false;
	}
	else
	{
		return true;
	}
}

// A trim function for 
String.prototype.trim = function()
{
	// skip leading and trailing whitespace
	// and return everything in between
	var x=this;
//		x=x.replace(/^\s*(.*)/, "$1");
//		x=x.replace(/(.*?)\s*$/, "$1");
//		return x;

	var SPACE = 32;
	var trimedString = "";	
	var exactVal = this;
	var start = 0;
	var end = 0;

	// index of first character other than space
	for (i=0; i<exactVal.length; i++)
	if(!(exactVal.charCodeAt(i) == SPACE))
	{
		start = i;
		break;
	}

	// index of last character other than space
	for (i=exactVal.length-1; i>0; i--)
		if (!(exactVal.charCodeAt(i) == SPACE))
		{
			end = i;
			break;
		}

	// Extract The string free from starting spaces and ending spaces but not space in between.
	for (i=start; i<=end; i++)
		trimedString = trimedString+ exactVal.charAt(i);

	// now exact value becomes the trimed string
	exactVal  = trimedString;
	return exactVal;
}

function checkForRegExp(fieldValue)
{
	var myRegExp = /[^a-z-\s]/i;
	
	if(myRegExp.test(fieldValue))
	{
		return true;
	}
	return false;
}