var bright = '#EFF5FB';

var AlertText; 
var IE;

//==============================================================
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 IeCheck()
{
//	Make sure IE version is >= 5.5

var n = navigator.appName
var v  = navigator.appVersion

	if (n == "Microsoft Internet Explorer" && parseInt(v) >= 4)
	{
		if (v.indexOf('MSIE 5.5') > 0 || (v.indexOf('MSIE 4') == -1 && v.indexOf('MSIE 5') == -1))
		{
			return true
		}
	}
	return false
}

IE = IeCheck()

//=======================================================================
function showImg(sImage)
{
// Isolate the name after the final /
sImage = sImage.substr(sImage.lastIndexOf('/') + 1)

	win1 = window.open('showImage.asp?i=images/large/' + sImage, 'win1', 'top=10,left=10,scrollbars=0,status=0,toolbar=0,resizable=1,menubar=0')
}

//==============================================================
function prettyDate(y, m, d)
{
var theDay = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
var theDate = new Date(y, m-1, d)
var theMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
var letters = "th"

if (d.charAt(0) == '0') d = d.charAt(1)

if (d == 1 || d == 21 || d == 31) letters = "st"
if (d == 2 || d == 22) letters = "nd"
if (d == 3 || d == 23) letters = "rd"

return theDay[theDate.getDay()] + ", " + d + letters + " " + theMonth[m-1] + " " + y 
}

//==============================================================
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 FormatCcy(checkStr)
// The value must be valid
{
var amt = parseFloat(checkStr)

      return "£" + amt.toFixed(2)
}

//=======================================================================
function ValidateDate(checkStr)
{
var d, m, y
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
				}
			}
		}
	}

	d = parseInt(checkStr.substring(0, 2), 10)
	m = parseInt(checkStr.substring(3, 5), 10)

	if (d < 1 || d > 31)
	{
		alert("The day is invalid: " + d)
		return false
	}

	if (d > 29 && m == 2)
	{
		alert("The day is invalid in February: " + d)
		return false
	}
	
	if (m < 1 || m > 12)
	{
		alert("The month is invalid: " + m)
		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 i;

	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 38& 45_ 46. 64@ 95-
		if ((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch == 38 || 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;
	}

	if (CountDots == 0)
	{
		alert("Email address invalid:\n" + checkStr + "\n\nAn email address must contain at least one dot")
		return false;
	}

      return true;

}

//=======================================================================
function ValidateAmount(checkStr)
// The value must conform to an amount mask
{
var ch
var decimal_point_count

decimal_point_count = 0

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charCodeAt(i)
		// 48-57 0-9 46.
		if (ch >= 48 && ch <= 57)
		{
		}
		else
		{
			if (ch == 46)
			{
				decimal_point_count++
				if (checkStr.length - i == 1)
				{
					alert('The amount should not end in a decimal point')
					return false
				}
			}
			else
			{
				alert('Please enter a valid amount, eg 4.75')
				return false
			}
		}
	}

	if (decimal_point_count > 1)
	{
		alert('Please enter a valid amount, eg 2.15')
		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
}

//=======================================================================
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;
}
