/*
	the trim function removes all white space from the beginning and/or the end
	Possible values for func:
	- left		removes all leading white space
	- right		remove all trailing white space
	- both		removes all leading and trailing white space
	- <empty>	same as both
*/

function trim(arg,func)
{
	var trimvalue = "";
	arglen = arg.length;
	if (arglen < 1)
		return trimvalue;
	if (func==null || func=="")
		func = "both";
		
	if (func == "left" || func== "both")
	{
		i = 0;
		pos = -1;
		while (i < arglen)
		{
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i)))
			{
				pos = i;
				break;
			}
			i++;
		}
	}

	if (func == "right" || func== "both")
	{
		var lastpos = -1;
		i = arglen;
		while (i >= 0)
		{
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i)))
			{
				lastpos = i;
				break;
			}
			i--;
		}
	}

	if (func == "left")
	{
		trimvalue = arg.substring(pos,arglen-1);
	}

	if (func == "right")
	{
		trimvalue = arg.substring(0,lastpos+1);
	}

	if (func == "both")
	{
		trimvalue = arg.substring(pos,lastpos + 1);
	}

	return trimvalue;
}


/*
	The chckContent function checks the content of a field.
	It return a empty string if the field is correct otherwise it return an error description
*/

function checkContent(field,name,minLength,maxLength)
{
	var value = trim(field.value);
	if (value=="")
	{
		return "A value for the '" + name + "' field.\n"
	}
	else if (value.length<minLength)
	{
		return "At least " + minLength + " characters for the '" + name + "' field.\n"
	}
	else if (value.length>maxLength)
	{
		return "At most " + maxLength + " characters for the '" + name + "' field.\n"
	}
	return ""
}


/*
	The checkEmail function checks if the field contains a calid email address.
	It return a empty string if the field is correct otherwise it return an error description
*/

function checkEmail(field,name)
{
	var emailAddress = trim(field.value);
	if (emailAddress=="")
	{
		return "A value for the '" + name + "' field.\n"
	}
	else
	{
		var atSymbolAt = emailAddress.indexOf('@');
		var lastDotAt = emailAddress.lastIndexOf('.');
		var spaceAt = emailAddress.indexOf(' ');
		var length = emailAddress.length;

		// at least one @ must be present and not before position 2
		// @yellow.com : NOT valid
		// x@yellow.com : VALID
		
		if (atSymbolAt < 0 )
			return "There must be a '@' in the email address field '" + name + "'.";
		if (atSymbolAt < 1 )
			return "There must be at least one character before the '@' in the email address field '" + name + "'.";


		// at least one . (dot) afer the @ is required
		// x@yellow : NOT valid
		// x.y@yellow : NOT valid
		// x@yellow.org : VALID
		
		if (lastDotAt < atSymbolAt)
			return "There must be at least one '.' after the '@' in the email address field '" + name + "'.";

		// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
		// x.y@yellow. : NOT valid
		// x.y@yellow.a : NOT valid
		// x.y@yellow.ca : VALID
		
		if (length - lastDotAt <= 2)
			return "There must be at least two chracters after the last '.' in the email address field '" + name + "'.";


		// no empty space " " is permitted (one may trim the email)
		// x.y@yell ow.com : NOT valid
		
		if (spaceAt != -1)
			return "There no spaces allowed in the email address field '" + name + "'.";

		return "";
	}
}
