///////////////////////////////////////////////////////////////////////////////
// Function:	checkAlphaSpaceChar()
// Description:	checks to see if the value of the element only contains a 
//				alphanumeric,whitespace or valid characters
// Inputs:		pElement - pointer to a form element
//				strOutput - an optional output string
// Returns:		true - if the value is a number
//				false - if the value is not a number
///////////////////////////////////////////////////////////////////////////////
function checkAlphaSpaceChar(pElement, strOutput)
{
	if(isAlphaSpaceChar(pElement.value) == false)
	{
		if(strOutput == "")
		{
			alert("Invalid Input.\n\nYou must enter only characters in this field.");
		}
		else
		{
			alert("Invalid Input.\n\n" + strOutput);
		}
		pElement.focus();
		return false;
	}
	return true;
}
// End of checkAlphaSpaceChar()
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Function:	checkAlphaNumeric()
// Description:	checks to see if the value of the element only contains a 
//				alphabetical,whitespace or valid characters
// Inputs:		pElement - pointer to a form element
//				strOutput - an optional output string
// Returns:		true - if the value is a number
//				false - if the value is not a number
///////////////////////////////////////////////////////////////////////////////
function checkAlphaNumeric(pElement, strOutput)
{
	if(isAlphaNumeric(pElement.value) == false)
	{
		if(strOutput == "")
		{
			alert("Invalid Input.\n\nYou must enter only characters or numbers in this field.");
		}
		else
		{
			alert("Invalid Input.\n\n" + strOutput);
		}
		pElement.focus();
		return false;
	}
	return true;
}
// End of checkAlphaNumeric()

///////////////////////////////////////////////////////////////////////////////
// Function:	checkAlphaNumSpaceChar()
// Description:	checks to see if the value of the element only contains a 
//				alphabetical,whitespace or valid characters
// Inputs:		pElement - pointer to a form element
//				strOutput - an optional output string
// Returns:		true - if the value is a number
//				false - if the value is not a number
///////////////////////////////////////////////////////////////////////////////
function checkAlphaNumSpaceChar(pElement, strOutput)
{
	if(isAlphaNumSpaceChar(pElement.value) == false)
	{
		if(strOutput == "")
		{
			alert("Invalid Input.\n\nYou must enter only characters or numbers in this field.");
		}
		else
		{
			alert("Invalid Input.\n\n" + strOutput);
		}
		pElement.focus();
		return false;
	}
	return true;
}
// End of checkAlphaNumSpaceChar()
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Function:	checkNumSpaceChar()
// Description:	checks to see if the value of the element only contains numeric
//				or whitespace characters
// Inputs:		pElement - pointer to a form element
//				strOutput - an optional output string
// Returns:		true - if the value is a number
//				false - if the value is not a number
///////////////////////////////////////////////////////////////////////////////
function checkNumSpaceChar(pElement, strOutput)
{
	if(isNumberSpace(pElement.value) == false)
	{
		if(strOutput == "")
		{
			alert("Invalid Input.\n\nYou must enter only numbers or spaces in this field.");
		}
		else
		{
			alert("Invalid Input.\n\n" + strOutput);
		}
		pElement.focus();
		return false;
	}
	return true;
}
// End of checkNumSpaceChar()
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Function:	checkNumeric()
// Description:	checks to see if the value of the element only contains a 
//				number
// Inputs:		pElement - pointer to a form element
//				strOutput - an optional output string
// Returns:		true - if the value is a number
//				false - if the value is not a number
///////////////////////////////////////////////////////////////////////////////
function checkNumeric(pElement, strOutput)
{
	if(isInteger(pElement.value) == false)
	{
		if(strOutput == "")
		{
			alert("Invalid Input.\n\nYou must enter only numbers in this field.");
		}
		else
		{
			alert("Invalid Input.\n\n" + strOutput);
		}
		pElement.focus();
		return false;
	}
	return true;
}
// End of checkNumeric()

///////////////////////////////////////////////////////////////////////////////
// Function:	checkDate()
// Description:	checks to see if the date entered is valid
// Inputs:		pElement - pointer to a form element
// Returns:		true - if the date is valid
//				false - if the date is invalid
///////////////////////////////////////////////////////////////////////////////

function checkDate(pElement)
{
	pValue = pElement.value;

	var strDay = pValue.substr(0, 2);
	var strMonth = pValue.substr(3, 2);
	var strYear = pValue.substr(6, 4);

	if(pValue.length != 10 || isDate(strYear, strMonth, strDay) == false)
	{
		alert("Invalid Date.\n\nYou have entered an invalid date. The date must be entered in the format dd/mm/yyyy.");
		pElement.focus();
		return false;
	}

	if(pValue.substr(2,1) != "-" || pValue.substr(5,1) != "-")
	{
		pElement.value = strDay + "-" + strMonth + "-" + strYear;
	}
	return true;
}
// End of checkDate()
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// Function:	checkDateOfBirth()
// Description:	Check the customers date of birth to ensure the dd/mm/yyyy
//				format has been followed. Also check the customer is over 
//				18
// Inputs:		none
// Returns:		true - if the date format is correct and the customer is 
//				over 18
//				false - if the date is incorrect or the customer is under 
//				18
///////////////////////////////////////////////////////////////////////////
function checkDateOfBirth(pElement)
{
	// check the supplied date
	if(checkDate(pElement) == false)
		return false;

	// get the current date
	var objCurrentDate = new Date();

	var nDOBDay = parseInt(pElement.value.substr(0,2), 10);
	var nDOBMonth = parseInt(pElement.value.substr(3,2), 10);
	var nDOBYear = parseInt(pElement.value.substr(6,4), 10);

	// check the customer is old enough
	var objDOB = new Date(nDOBYear, (nDOBMonth - 1), nDOBDay);

	var nTime = objCurrentDate - objDOB;
	var nAge = nTime / (1000 * 3600 * 24 * 365.25);

	if(nAge < 18)
	{
		alert("Invalid Age\n\nYou must be over 18 years of age to use this service.");
		pElement.focus();
		return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////
// Function:	checkPasswords()
// Description:	Check that the passwords contain valid characters, are longer
//				than 8 characters and are both the same.
// Inputs:		pElement1, pElement2 - pointers to both password fields
// Returns:		true - if valid and equal.
//				false - if either is invalid or if not equal.
///////////////////////////////////////////////////////////////////////////
function checkPasswords( pElement1, pElement2 )
{
	if( checkPassword( pElement1 ) == false )
		return false;
	if( checkPassword( pElement2 ) == false )
		return false;
	if( pElement1.value != pElement2.value ){
		alert("Inconsistent Passwords.\n\nPlease enter the same value for each password.")
		pElement2.focus();
		return false;
	}
	return true;
}

function checkPassword(pElement)
{
	if (checkAlphaNumeric(pElement, 'Your password must only consist of characters and numbers.') == false)
	{
		return false;
	}
	else
	{
		if (pElement.value.length < 8)
		{
			alert("Invalid Password.\n\nYour password must be greater than 8 characters and consist only of alphabetic and numeric characters.")
			pElement.focus();
			return false;
		}
	}
	return true;
		
}


function checkPostCode(pElement1, strOutput)
{

	if(isPostCode(pElement1.value) == false)
	{
		if (strOutput == "")
		{
			alert("Invalid Input.\n\nPlease check you have correctly completed the post code field.");
		}
		else
		{
			alert("Invalid Input.\n\n" + strOutput);
		}
//		pElement1.focus();
		return false;
	}
	pElement1.value = pElement1.value.toUpperCase();
	return true;
}

function isPostCode (pString)
{   

var s = "";

if (isEmpty(pString)) return false;

for (var i = 0; i < pString.length; i++)
{   
	if(pString.charAt(i) >= '0' && pString.charAt(i) <= '9')
	{
		s+="N";
	}
	else
	{
		if(pString.charAt(i) != ' ')
			s+="A";
	}
}

if(s == "AANNAA")
	return true;
if(s == "AANANAA")
	return true;
if(s == "ANNAA")
	return true;
if(s == "ANANAA")
	return true;
if(s == "AANNNAA")
	return true;
if(s == "ANNNAA")
	return true;
if(s == "AAANAA")
	return true;
 
return false;
}

///////////////////////////////////////////////////////////////////////////////
// Function:	checkEmail()
// Description:	checks the users email address
// Inputs:		pElement - the form element containing the email address
//				strOutput - an optional output string
// Returns:		true - if email address okay
//				false - if bad email address
///////////////////////////////////////////////////////////////////////////////
function checkEmail(pElement)
{
	if(isEmail(pElement.value) == false)
	{
		alert("Invalid Email Address.\n\nPlease check you have correctly completed the email address field. You're email address must be of the form of forename.surname@company.domain or person@company.domain.");
		pElement.focus();
		return false;
	}
	return true;
}
// End of checkEmail()
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Function:	checkSortCode()
// Description:	checks to see if the format of the sort code is valid
// Inputs:		pElement - pointer to a form element
// Returns:		true - for valid sort code formats
//				false - for invalid sort code formats
///////////////////////////////////////////////////////////////////////////////
function checkSortCode(pElement, pElementFocus)
{
	pValue = pElement.value;

	var strFirst = pValue.substr(0,2);
	var strSecond = pValue.substr(3,2);
	var strThird = pValue.substr(6,2);

	if((pValue.length != 8) || (isIntegerInRange(parseInt(strFirst), 0, 99) == false) || (isIntegerInRange(parseInt(strSecond), 0, 99) == false) || (isIntegerInRange(parseInt(strThird), 0, 99) == false))
	{
		alert("Invalid Sort Code.\n\nYour sort code must be in the format nn-nn-nn where n is an integer.");
		pElementFocus.focus();
		return false;
	}
	if( strFirst != "20" )
	{
		alert("Invalid Sort Code.\n\nYour Barclays sort code should begin with the number 20.");
		pElementFocus.focus();
		return false;
	}

	if(pValue.substr(2,1) != "-" || pValue.substr(5,1) != "-")
	{
		pElement.value = strFirst + "-" + strSecond + "-" + strThird;
	}
	return true;
}
// End of checkSortCode()


