/**
* @file formvalidation.js
*
* JavaScript Form Validation
* 
* JavaScript can be used to validate input data in HTML forms before sending off the content to a server.
*
* Form data that typically are checked by a JavaScript could be:
*
*  - has the user left required fields empty?
*  - has the user entered a valid e-mail address?
*  - has the user entered a valid date?
*  - has the user entered text in a numeric field?
*/

/**
 * Required Fields
 *
 * The function below checks if a required field has been left empty. 
 * If the required field is blank, an alert box alerts a message and the 
 * function returns false. If a value is entered, the function returns true (means that data is OK)
 * @param field - form field name
 * @param alerttxt - Message to be shown if something goes wrong
 * @see http://www.w3schools.com/js/js_form_validation.asp
 */
function validateRequired(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		{
			alert(alerttxt);
			return false;
		}
		else
		{
			return true;
		}
	}
}

/**
 * E-mail Validation
 * The function below checks if the content has the general syntax of an email.
 * This means that the input data must contain at least an @ sign and a dot (.). 
 * Also, the @ must not be the first character of the email address, 
 * and the last dot must at least be one character after the @ sign
 * @param - field form field name
 * @param alerttxt - Message to be shown if something goes wrong 
 * @see http://www.w3schools.com/js/js_form_validation.asp 
 */
function validateEmail(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
	  	{
	  		alert(alerttxt);
	  		return false;
	  	}
		else 
		{
			return true;
		}
	}
}

/**
* Length Greater Than Validation
* @param field - form field name
* @param fLength - form field length
* @param alerttxt - Message to be shown if something goes wrong
*/
function validateLengthGreaterThan(field, fLength, alerttxt)
{
	with (field)
	{
		size=value.length;
		
		if (size>fLength) 
	  	{
	  		return true;
	  	}
		else 
		{
	  		alert(alerttxt);
	  		return false;			
		}
	}
}


/**
* Javascript numeric validation. Supports a decimal point as well as negative numbers.
* @see http://snippets.dzone.com/posts/show/3381
*/
function isNumeric(value) 
{
	if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/))
		return false;

	return true;
}

