function FormatCanadianCcy(checkStr)
// The value must be valid
{
var amt = parseFloat(checkStr)

      return "$" + amt.toFixed(2)
}

var AlertText; 

//==============================================================
function readCookie(name)
{
	// Create an array for each section of the cookie
	var cs = document.cookie.split(';')
	
	// Set the search string
	var nameEQ = name + "="
	
	// Look through each section
	for(var i=0; i < cs.length; i++)
	{
		var c = cs[i]

		// Delete spaces
		while (c.charAt(0)==' ') c = c.substring(1, c.length)
    
		// See if the cookie is in this section
		var ci = c.indexOf(nameEQ)

		if (c.indexOf(nameEQ) != -1)
		{
			// Cookie found - get the string with the cookie value
			var cr = c.substring(ci + nameEQ.length, c.length)
			
			// It might be part of a cookie dictionary...
			ci = cr.indexOf('&')

			if (ci == -1) return cr
			
			// It is in a dictionary, so strip the unwanted chars
			return cr.substr(0, ci)
		}  
	}
	
	return null
}

//=======================================================================
function ShowAlerts()
{
	if (AlertText != "")
	{
		alert(AlertText);
	}
}

//=============================================================
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 either ddmmyy or dd/mm/yy format";

	if (checkStr.length != 6 && 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 ValidateMultiEmail(inputStr)
{
var iStart;
var iLen;
var checkStr;

	iStart = 0;
	while (iStart < inputStr.length)
	{
		iLen = inputStr.indexOf(';', iStart);
		if (iLen == -1)
		{
			iLen = inputStr.length - iStart;
		}
		else
		{
			iLen -= iStart;
		}
		checkStr = Trim(inputStr.substr(iStart, iLen));
		if (ValidateEmail(checkStr) == false)
		{
			return false;
		}
		iStart += iLen + 1;
	}

	return true;
}

//=======================================================================
function ValidateEmail(checkStr)
{
var ch;
var CountAts = 0;
var CountDots = 0;
var Service = 0;
var i;
var ispCount = 10;
var ispFound = new Array('@aol','@blueyonder','@btinternet','@btopenworld','@freeserve','@hotmail','@ntlworld','@tesco','@tiscali','@virgin');
var ispLook = new Array('@aol.com','@blueyonder.co.uk','@btinternet.com','@btopenworld.com','+','@hotmail.com','@ntlworld.com','@tesco.net','@tiscali.co.uk','@virgin.net');
var ispText = new Array('AOL is a .com','blueyonder is a .co.uk','btinternet is a .com','btopenworld is a .com','freeserve is a something@something.freeserve.co.uk','hotmail is a .com','ntlworld is a .com','tesco is a .net','tiscali is a .co.uk','virgin is a .net');

	if (checkStr.length < 5)
	{
		alert("Email address invalid:\n" + checkStr + "\n\nInsufficient characters to be an 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("Email address invalid:\n" + checkStr + "\n\nAn email address cannot start or end with the @ symbol");
				return false;
				}
				if (checkStr.charCodeAt(i - 1) == 46)
				{
					alert("Email address invalid:\n" + checkStr + "\n\nImproper use of dots and @ symbol");
				return false;
				}
			}
			if (ch == 46)
			{
				CountDots++;
				if (i == 0 || i == (checkStr.length - 1))
				{
					alert("Email address invalid:\n" + checkStr + "\n\nAn email address cannot start or end with a dot");
				return false;
				}
				if (checkStr.charCodeAt(i - 1) == 46 || checkStr.charCodeAt(i - 1) == 64)
				{
					alert("Email address invalid:\n" + checkStr + "\n\nImproper use of dots and/or @ symbol");
				return false;
				}
			}
		}
		else
		{
			alert("Email address invalid:\n" + checkStr + "\n\nDisallowed characters: only letters, numbers and @._- characters are permitted");
			return false;
		}
	}

	if (CountAts != 1)
	{
		alert("Email address invalid:\n" + checkStr + "\n\nAn 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;
	}

	for (i = 0; i < ispCount; i++)
	{
		if (checkStr.indexOf(ispFound[i]) != -1)
		{
			if (checkStr.indexOf(ispLook[i]) == -1)
			{
				Service = ispText[i];
			}
		}
	}

	if (Service != 0)
	{
		if (confirm("Email address may be invalid:\n" + checkStr + "\n\nI believe " + Service + " service -\n\nIf you are sure the email address has been entered correctly, click OK, otherwise click Cancel and try again.") == true)
		{
			return true;
		}
		return false;
	}
		
	if (CountDots == 0)
	{
		alert("Email address invalid:\n" + checkStr + "\n\nAn email address must contain at least one dot")
		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 ValidateNumber(checkStr)
// The value must be entirely numeric, ie each digit is 0 to 9
{
var ch;

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charCodeAt(i)
		// 48-57 0-9
		if (ch >= 48 && ch <= 57)
		{
		}
		else
		{
			alert('Please enter a number using only digits 0 to 9')
			return false
		}
	}
      return true
}

