//=============================================================
function Trim(text)
{
var trimmed;

	trimmed = text;
	if (trimmed.length == 0)
	{
		return trimmed;
	}

	while (trimmed.charAt(trimmed.length - 1) == ' ')
	{
		trimmed = trimmed.substr(0, trimmed.length - 1);
	}

	while (trimmed.charAt(0) == ' ')
	{
		trimmed = trimmed.substr(1);
	}

	return trimmed;
}

//=======================================================================
function ValidateDate(checkStr)
{
var ch;
var ErrAlert = "Please enter the date in dd/mm/yy format";

	if (checkStr.length != 8)
	{
		alert(ErrAlert);
		return false;
	}
	
	if (checkStr.length == 6)
	{
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charCodeAt(i);
			// 48-57 0-9
			if (ch >= 48 && ch <= 57)
			{
			}
			else
			{
				alert(ErrAlert);
				return false;
			}
		}
	}
	else
	{
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charCodeAt(i);
			// 48-57 0-9 47/
			if (ch >= 48 && ch <= 57 && i != 2 && i != 5)
			{
			}
			else
			{
				if (ch == 47 && (i == 2 || i == 5))
				{
				}
				else
				{
					alert(ErrAlert);
					return false;
				}
			}
		}
	}

      return true;
}

//=======================================================================
function ValidateEmail(checkStr)
{
var ch;
var CountAts = 0;
var CountDots = 0;
var Service = 0;

	if (checkStr.length < 5)
	{
		alert ("Please enter a valid email address");
		return false;
	}
      	
	checkStr = checkStr.toLowerCase();

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charCodeAt(i);
		// 97-112 a-z  48-57 0-9 45_ 46. 64@ 95-
		if ((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch == 45 || ch == 46 || ch == 64 || ch == 95)
		{
			if (ch == 64)
			{
				CountAts++;
				if (i == 0 || i == (checkStr.length - 1))
				{
					alert("Your email address cannot start or end with the @ symbol");
				return false;
				}
			}
			if (ch == 46)
			{
				CountDots++;
				if (i == 0 || i == (checkStr.length - 1))
				{
					alert("Your email address cannot start or end with a dot");
				return false;
				}
			}
		}
		else
		{
			alert("Your email address contains disallowed characters - only letters, numbers and @._- characters are permitted");
			return false;
		}
	}

	if (CountAts != 1)
	{
		alert("Your email address must contain exactly one @ symbol");
		return false;
	}

	if (checkStr.substring(0, 4) == "www.")
	{
		alert("Your appear to be confusing your email address with a web address, by starting it with www.\n\nPlease remove the www and try again.");
		return false;
	}

	if (checkStr.indexOf("@aol") != -1)
	{
		if (checkStr.indexOf("@aol.com") == -1)
		{
			Service = "AOL is a .com";
		}
	}

	if (checkStr.indexOf("@blueyonder") != -1)
	{
		if (checkStr.indexOf("@blueyonder.co.uk") == -1)
		{
			Service = "blueyonder is a .co.uk";
		}
	}

	if (checkStr.indexOf("@btinternet") != -1)
	{
		if (checkStr.indexOf("@btinternet.com") == -1)
		{
			Service = "btinternet is a .com";
		}
	}

 	if (checkStr.indexOf("@btopenworld") != -1)
	{
		if (checkStr.indexOf("@btopenworld.com") == -1)
		{
			Service = "btopenworld is a .com";
		}
	}

 	if (checkStr.indexOf("@freeserve") != -1)
	{
		Service = "freeserve is a something@something.freeserve.co.uk";
	}

 	if (checkStr.indexOf("@hotmail") != -1)
	{
		if (checkStr.indexOf("@hotmail.com") == -1)
		{
			Service = "hotmail is a .com";
		}
	}

 	if (checkStr.indexOf("@ntlworld") != -1)
	{
		if (checkStr.indexOf("@ntlworld.com") == -1)
		{
			Service = "ntlworld is a .com";
		}
	}

	if (checkStr.indexOf("@tesco") != -1)
	{
		if (checkStr.indexOf("@tesco.net") == -1)
		{
			Service = "tesco is a .net";
		}
	}

 	if (checkStr.indexOf("@tiscali") != -1)
	{
		if (checkStr.indexOf("@tiscali.co.uk") == -1)
		{
			Service = "tiscali is a .co.uk";
		}
	}

 	if (checkStr.indexOf("@virgin") != -1)
	{
		if (checkStr.indexOf("@virgin.net") == -1)
		{
			Service = "virgin is a .net";
		}
	}

	if (Service != 0)
	{
		if (confirm("I believe " + Service + " service -\n\nIf you are sure your email address has been entered correctly, click OK, otherwise click Cancel and try again.") == true)
		{
			return true;
		}
		return false;
	}
		
	if (CountDots == 0)
	{
		if (confirm("You have not entered any dots in your email address, for example as in 'aol.com'\n\nIf you are sure your email address has been entered correctly, click OK, otherwise click Cancel and try again.") == true)
		{
			return true;
		}
		return false;
	}

      return true;

}

//=======================================================================
function ValidatePlainText(checkStr, bAllowSpaces, sField)
{
var ch;
var msg;

	checkStr = checkStr.toLowerCase();

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charCodeAt(i);
		// 97-112 a-z  48-57 0-9
		if ((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || (ch == 32 && bAllowSpaces == true))
		{
		}
		else
		{
			msg = sField + " can only contain letters and numbers";
			if (bAllowSpaces == true)
			{
				msg = msg + " and spaces";
			}
			alert(msg);
			return false;
		}
	}

	return true;
}

//=======================================================================
function ValidateURL(checkStr)
{
var ch;

	if (checkStr.length < 7)
	{
		alert ("Please enter a valid domain, e.g. mycompany.co.uk");
		return false;
	}
      	
	checkStr = checkStr.toLowerCase();

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charCodeAt(i);
		// 97-112 a-z  48-57 0-9 45_ 46. 47/ 58: 95-
		if ((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch == 45 || ch == 46 || ch == 47 || ch == 58 || ch == 95)
		{
		}
		else
		{
			alert("Your www entry contains illegal characters - please retry");
			return false;
		}
	}

	if (checkStr.substring(0, 4) != "www." && checkStr.substring(0, 7) != "http://")
	{
		return (confirm("Your web address does not begin with 'www.' nor 'http://'.\n\nIf you are sure your entry is correct, click Ok otherwise click Cancel and try again."));
	}
		
      return true;
}
