// javascript - feedback form validation

/***********************************************************************************************************
	Check Not Spaces Only functions ie + ns4 + ns6 compatible
	Written by Rajesh Sohoni on 09/01/2003
  **********************************************************************************************************
*/

var SpacesOnlyError;

/*----------------------------------------------------------------------------------------------------------
	checkNotSpacesOnly    : checks if the value entered has only spaces or not

	value is the entered string

	return value : true if value is not spaces only
  ----------------------------------------------------------------------------------------------------------
*/
function checkNotSpacesOnly(value)
{
	for(var i=0, spaceCount=0; i< value.length; i++)
	{
		if(" ".indexOf(value.charAt(i)) == 0)
			spaceCount++;
	}
	if(spaceCount == value.length)
	{
		SpacesOnlyError = "You have entered only spaces in the field";
		return false;
	}

	return true;
}

/*----------------------------------------------------------------------------------------------------------
	checkNotSpacesOnlyField : checks if the value entered in the form field has only spaces or not and
			     shows an alert if it has only spaces.

	field is the form field in which user enters value
	fieldName is the Caption of the form field use to display in the alert

	return value : true if value is not spaces only
  ----------------------------------------------------------------------------------------------------------
*/
function checkNotSpacesOnlyField(field, fieldName)
{
	if(checkNotSpacesOnly(field.value) == false)
	{
		alert(SpacesOnlyError + " " + fieldName + " .");
		field.value = "";
		field.focus();
		return false;
	}
}
/***********************************************************************************************************
	Check Email functions ie + ns4 + ns6 compatible
	Written by Rajesh Sohoni on 09/03/2003
  **********************************************************************************************************
*/

var EmailError;

/*----------------------------------------------------------------------------------------------------------
	checkEmail : checks if the emailid is enetered in valid format
	Valid format  : 1. Special characters such as <!-- ',%,$,#,&,*,(,)] -->, etc are invalid.

	value is the emailid string

	return value : true if emailid is in valid format else false
  ----------------------------------------------------------------------------------------------------------
*/
function checkEmail(value)
{
	var bFlag1 = false;
	var bFlag2 = false;

	if(value.length == 0)
	{
		EmailError = "Please enter your email id.";
		return false;
	}

	if(value.length < 7)
	{
		EmailError = "Please enter a valid email id.";
		return false;
	}

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(!filter.test(value))
	{
		EmailError = "Please enter a valid email id";
		return false;
	}

	for(var i=0; i< value.length; i++)
	{
		if("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@_-. ".indexOf(value.charAt(i)) == -1)
		{
			EmailError = "Any of the characters [',\",%,$,#,&,*,(,)] including blank spaces are not allowed in your emailid.";
			return false;
		}

		if("@".indexOf(value.charAt(i)) != -1)
			bFlag1=true;

		if(".".indexOf(value.charAt(i)) != -1)
			bFlag2=true;
	}

	if((bFlag1 == false) ||  (bFlag2 == false))
	{
		EmailError = "Please make sure your emailid has both the characters '@' and '.'(dot).";
		return false;
	}

	return true;
}

/*----------------------------------------------------------------------------------------------------------
	checkEmailField : checks if the email entered in the form field is in valid format and
			     shows an alert if it is invalid.

	field is the form field in which user enters emailid

	return value : true if emailid is in valid format else false
  ----------------------------------------------------------------------------------------------------------
*/
function checkEmailField(field)
{
	if(checkEmail(field.value) == false)
	{
		alert(EmailError);
		field.focus();
		return false;
	}
}

/***********************************************************************************************************
	Check Text Area functions ie + ns4 + ns6 compatible
	Written by Rajesh Sohoni on 09/05/2003
  **********************************************************************************************************
*/

var TextAreaError;

/*----------------------------------------------------------------------------------------------------------
	checkTextArea    : checks if the value entered is within maxLength limit

	value is the entered string
	maxLength is the max number of characters allowed. default is 300 characters

	return value : true if value is value contains more number of characters than maxLength
  ----------------------------------------------------------------------------------------------------------
*/
function checkTextArea(value, maxLength)
{
	if(maxLength == '')
		maxLength=900;

	var textLength = value.length;

	if(textLength >= maxLength)
	{
		TextAreaError = "The text you have entered contains " + textLength + " characters including blank spaces and line breaks."
		TextAreaError += " Please limit the text to " + maxLength + " characters";
		return false;
	}

	return true;
}

/*----------------------------------------------------------------------------------------------------------
	checkTextAreaField : checks the max length for value entered in the form field (text area) and
			     shows an alert if it is contains more than the maxLength characters.

	field is the form field in which user enters value
	fieldName is the Caption of the form field use to display in the alert
	maxLength is the max number of characters allowed in the text area

	return value : true if value is not empty
  ----------------------------------------------------------------------------------------------------------
*/
function checkTextAreaField(field, fieldName, maxLength)
{
	if(checkTextArea(field.value, maxLength) == false)
	{
		alert(TextAreaError + " in the field " + fieldName + " .");
		field.focus();
		return false;
	}
}

/* --------------------------------------------------------------------------------------------------------
	function submitform() to check the form fields
	
	it is client side validation
   --------------------------------------------------------------------------------------------------------
*/
function submitForm(form)
{
	var strComments = form.txtcomments.value;
	var strEmail	= form.txtEmail.value;
	var strCaptchaAns = form.txtCaptchaAns.value;

	if(checkNotSpacesOnly(strComments)==false || Math.abs(strComments.length)== 0 )
	{
		alert("Please Enter Your Comments !!");
		form.txtcomments.focus();
		return false;
	}
	
	if(checkTextArea(strComments)==false)
	{
		return false;
	}
	
	if(checkEmailField(form.txtEmail)==false)
	{
		form.txtEmail.focus();
		return false;
	}
	if(checkNotSpacesOnly(strCaptchaAns)==false || Math.abs(strCaptchaAns.length)== 0 )
	{
		alert("Please Enter Captcha Code !!");
		form.txtCaptchaAns.focus();
		return false;
	}
	
	return true;
}
