function validate(formID)
{
	if(validateRB(formID) == true){
		formID.submit();
	}
}

function validateNewRegUsr(formID)
{
	if(validateRB(formID) == true){
		if(validateUsrNameNotInPassword(formID) == true){
			formID.submit();
		}
	}
}

function validateUsrNameNotInPassword(formID)
{
	var len = formID.elements.length;
	carryon = true;
	var fieldtype ="";
	var strUN = "";
	var strPW = "";

	for(var i=0;(carryon && (i<len)); i++)
	{
		var fieldname = formID.elements[i].name
		if(fieldname.charAt(3)=="_" && formID.elements[i].type != "hidden")
		{
			fieldtype="";
			for(x=0;x<3;x++)
			{
				fieldtype += fieldname.charAt(x);
			}
		}else{fieldtype = "";};

		 if(fieldtype == "usn")
		{
			strUN = formID.elements[i].value;
		}
		 if(fieldtype == "psw")
		{
			strPW = formID.elements[i].value;
		}
	};
	
	if(strUN!="" && strPW!=""){
		if(strPW.indexOf(strUN)!=-1){
			alert("Please choose a password that does not contain your username.");
			carryon = false;
		}
	};
	
	return carryon;	
}

function validateRB(formID)
{
	var len = formID.elements.length
	var icbcCount = 0;
	var i;
	var cbcName = "";
	carryon = true;
	var fieldtype ="";

	var arrRad = new Array(
				 new Array("","","","","","","","","",""),
				 new Array(false,false,false,false,false,false,false,false,false,false));

	for(var i=0;(carryon && (i<len)); i++)
	{
		var fieldname = formID.elements[i].name
		if(fieldname.charAt(3)=="_" && formID.elements[i].type != "hidden")
		{
			fieldtype="";
			for(x=0;x<3;x++)
			{
				fieldtype += fieldname.charAt(x);
			}
		}else{fieldtype = "";};
		
		if(fieldtype == "txt")
		{
			validTxt(formID,i)
		}
		 if(fieldtype == "eml")
		{
			validEml(formID,i)
		}
		 if(fieldtype == "sel")
		{
			validSel(formID,i)
		}
		 if(fieldtype == "seo")
		{
			validSeo(formID,i)
		}
		 if(fieldtype == "dat")
		{
			validDat(formID,i)
		}
		 if(fieldtype == "dop")
		{
			validDop(formID,i)
		}
		 if(fieldtype == "pcd" || fieldname == "opt_postcode")
		{
			if (fieldtype=="pcd")
				validPcd(formID,i);
			else
				{
				if(formID.elements[i].value!="")
					validPcd(formID,i);
				}
		}
		 if(fieldtype == "usn")
		{
			validUsn(formID,i)
		}
		 if(fieldtype == "psw")
		{
			validPsw(formID,i)
		}
		if(fieldtype == "mip")
		{
			validMIPsw(formID,i)
		}		
		 if(fieldtype == "sib")
		{
			validSIB(formID,i)
		}
		 if(fieldtype == "crd")
		{
			validLuhn(formID,i)
		}
		 if(fieldtype == "rad")
		{
			readRad(formID,i, arrRad)
		}
		 if(fieldtype == "yer")
		{
			validYear(formID,i)
		}
		if(fieldtype == "cbc")
		{
			cbcName = formID.elements[i].name;
			if(formID.elements[i].checked == true)
			{
				icbcCount++;
			}
		}
	};
	
	validCheckBoxCount(cbcName, icbcCount);
	validRad(arrRad);
	return carryon;
}

// ---------------------------------------------------------------------------
// validate check box choice
// element of form: cbc_x_name
// cbc = 'check box choice'
// x = max number of choices
// name = name of field
// validates that the number of choices = x
// ---------------------------------------------------------------------------
function validCheckBoxCount(name, num)
{
	var icbcMax = 0;
	
	if(carryon == true && name!=""){
		//max number of choices is 99
		icbcMax = name.charAt(4);
		if(name.charAt(5) != "_"){
			icbcMax += name.charAt(5);
		};
		if(icbcMax > num){
			if(icbcMax == 1)
			{
				alert("Please tick at least one choice.");
			}else{
				alert("Please tick at least " + icbcMax + " choices.");
			};
			carryon = false;
		}else{
		if(icbcMax < num){
			if(icbcMax == 1)
			{
				alert("Please restrict your selection to one choice only.");
			}else{
				alert("Please restrict your selection to " + icbcMax + " choices.).");
			};
			carryon = false;
		}}
	}
}

//SIB VALIDATION
function validSIB(formID,i)
{	
	var SIB_CHAR_LIMIT=7
	var sSIB = formID.elements[i].value
	var iLength = formID.elements[i].value.length
	

	if((sSIB == "" || sSIB.charAt(0) == " ") )
	{
		alert("Please enter a valid SIB number.")
		carryon = false
		formID.elements[i].focus()
		return
	}
	
	for (var loop=0; loop < iLength ; loop++)
	{
		if (sSIB.charAt(loop) == " ")
		{
			alert("The SIB number may not contain spaces.")
			carryon = false
			formID.elements[i].focus()
			return
		}
	}
	if(iLength > SIB_CHAR_LIMIT )
	{
		alert("The SIB number must not exceed " + SIB_CHAR_LIMIT + " characters.")
		carryon = false;
		formID.elements[i].focus()
	}
	else
	{
		carryon = true;
	}
	
}


// at least one element from the set of characters 'text' must belong to the
// set of characters 'includeCharacters'
function containsChars(text, includeCharacters)
{
	var textLength = text.length;
	
	for(var index = 0; index < textLength; index++)
	{
		if (includeCharacters.indexOf(text.charAt(index)) >= 0)
		{
			return true;
		}
	}
	return false;
}

// a function that takes a string and a string of allowed characters
// and returns true if any of the characters in the text are not in the list
function containsIllegalCharacters(text, allowedCharacters)
{
	var textLength = text.length;
	
	for(var index = 0; index < textLength; index++)
	{
		if (allowedCharacters.indexOf(text.charAt(index)) < 0)
		{
			return true;
		}
	}
	return false;
}

//PASSWORD VALIDATION
function validPsw(formID,i)
{
	var txtLength = formID.elements[i].value;
	var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
	var strLength = formID.elements[i].value.length;

	// Test string for existence of regular expression.
	if (containsIllegalCharacters(txtLength, chars))
	{
		alert("Sorry, the password may not contain characters apart from letters, numbers and spaces.");
		carryon = false;
		formID.elements[i].focus();
		return;
	}

	// Check for leading and trailing spaces
	if (txtLength.charAt(0) == " " || txtLength.charAt(strLength-1) == " ")
	{   
		alert("Please choose a password that does not begin or end with a space.")
		carryon = false;
		formID.elements[i].focus()
	}
	else 
	{
		if(formID.elements[i].value.length < 10 || formID.elements[i].value.length >128) //Check password length
		{
			alert("Please choose a password between 10 and 128 characters.");
			carryon = false;
			formID.elements[i].focus();
		}
		else
		{
			if(formID.elements[i].value != formID.elements[i+1].value) //Check passwords match
			{
				alert("Please enter matching passwords.");
				carryon = false;
				formID.elements[i+1].focus();
			}
			else //Ensure there is at least one space in the password
			{
				var spaceFound = false;
				for (var count=1;count<(strLength-1);count++)
				{
					if (txtLength.charAt(count) == " ")
					{
						spaceFound = true;
						break;
					}
				}
				if (spaceFound != true)
				{
					alert("You must have at least one space in your password");
					carryon = false;
					formID.elements[i].focus();
				}
			}
		}
	};
	return txtLength;
}

//PASSWORD VALIDATION
function validMIPsw(formID,i)
{
	var txtLength = formID.elements[i].value;
	var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
	var nums = "0123456789";
	var upperchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var lowerchars = "abcdefghijklmnopqrstuvwxyz";
	var strLength = formID.elements[i].value.length;

	// Test string for existence of regular expression.
	if (containsIllegalCharacters(txtLength, chars))
	{
		alert("Sorry, the password may not contain characters apart from letters, numbers and spaces.");
		carryon = false;
		formID.elements[i].focus();
		return;
	}

	// Check for leading and trailing spaces
	if (txtLength.charAt(0) == " " || txtLength.charAt(strLength-1) == " ")
	{   
		alert("Please choose a password that does not begin or end with a space.")
		carryon = false;
		formID.elements[i].focus()
	}
	else 
	{
		if(formID.elements[i].value.length < 10 || formID.elements[i].value.length >128) //Check password length
		{
			alert("Please choose a password between 10 and 128 characters.");
			carryon = false;
			formID.elements[i].focus();
		}
		else
		{
			if(formID.elements[i].value != formID.elements[i+1].value) //Check passwords match
			{
				alert("Please enter matching passwords.");
				carryon = false;
				formID.elements[i+1].focus();
			}
			else //Ensure there is at least one number in the password
			{
				
				if (!containsChars(txtLength, nums))
				{
					alert("You must have at least one numeric character in your password");
					carryon = false;
					formID.elements[i].focus();				
				}
				else
				{
					if ((containsChars(txtLength, upperchars) && !containsChars(txtLength, lowerchars)) || (!containsChars(txtLength, upperchars) && containsChars(txtLength, lowerchars)))
					{	
						alert("You must have a mixture of upper and lower case characters in your password");
						carryon = false;
						formID.elements[i].focus();			
					}
				}
					
			}
		}
	};
	return txtLength;
}

//VALID USER ID
function validUsn(formID,i)
{
	var sibNo="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var strlength = formID.elements[i].value.length
	var txtLength = formID.elements[i].value
	if(strlength > 14 || txtLength.charAt(0) == " " || formID.elements[i].value == "")
	{
		formID.elements[i].focus()
		alert("Your User Name should contain only letters and numbers")
		carryon = false;
		formID.elements[i].focus()
	}
	else
	{
		for(var mee=0;carryon && (mee < strlength); ++ mee)
		{
			var temp = formID.elements[i].value.substring(mee, mee+1)
			if(sibNo.indexOf(temp) == -1)
            {
	  			alert("Your User Name should contain only letters and numbers")
	  			carryon = false;
				formID.elements[i].focus()
            }
		}
	}
	return txtLength;
}

		
// RADIO BUTTONS VALIDATION SUB SCRIPT
function readRad(formID,i, arrRad)
{
	var sRadName = formID.elements[i].name;
	var c;
	
	for(c=0;c<arrRad[0].length;c++)
	{
		if(arrRad[0][c] == formID.elements[i].name){
			arrRad[1][c] = arrRad[1][c] || formID.elements[i].checked;
			return;
		}else
		if(arrRad[0][c] == ""){
			arrRad[0][c] = formID.elements[i].name;
			arrRad[1][c] = formID.elements[i].checked;
			return
		}
	}
		formID.rad_Nin.focus();
}

function validRad(arrRad)
{

	//check all the radio buttons have been selected
	bResult = true;
	
	for(i=0;i<arrRad[0].length;i++){
	
		sRadName = arrRad[0][i];
		bRadChecked = arrRad[1][i];
		
		if(sRadName != ""){
		
			bResult = bResult && bRadChecked;
		};
	};
			 
	if(bResult == false && carryon == true){
		alert("For all mandatory groups of items please ensure that an option has been selected");
		carryon = false;
	};
	
}

//SELECT BOX VALIDATION SUB SCRIPT
function validSel(formID,i)
{
	if(formID.elements[i][formID.elements[i].selectedIndex].value == "")
	{
		alert("Please select a value")
		carryon = false;
		formID.elements[i].focus()	
	}
	else
	{	
		carryon = true;
	}
}

// SELECT OTHER VALIDATION SUB SCRIPT
function validSeo(formID,i)
{
	if(formID.elements[i - 1][formID.elements[i - 1].selectedIndex].value == "other")
	{
		carryon = false;

		if(formID.elements[i].value == "")
		{
			alert("Please enter a value")
			carryon = false;
			formID.elements[i].focus()
		}
		else
		{
			carryon = true;
		}
	}
}

//NORMAL TEXT FIELD VALIDATION SUB SCRIPT
function validTxt(formID,i)
{
	var currTxt = formID.elements[i].value
	
	//remove all blank characters and check that remaining text has more than 0 characters
	re = /\s/;
	var newTxt = currTxt.replace(re, "");
	var noSpaceTxt
	while(newTxt != noSpaceTxt)
	{
		noSpaceTxt=newTxt
		newTxt = noSpaceTxt.replace(re, "");
		
	}

	if( noSpaceTxt.length < 1)
	{
		alert("Please fill out this field")
		carryon = false;
		//clear the field as it only has spaces
		formID.elements[i].value=""
		formID.elements[i].focus()
	}
	else
	{
		carryon = true;
	}
}

//CHECK TO SEE IF OPTIONAL DATE FIELD IS FILLED
function validDop(formID,i)
{
	if(formID.elements[i].value == "")
	{
		carryon = true;
	}
	else
	{
		validDat(formID,i);
	}
}

//DATE VALIDATION
function validDat(formID,i)
{
	var digits="1234567890"
    var fldLen = formID.elements[i].value.length
    var thisismydate =""
    var thisismydatee =""
	var	unused = ""

    for(var me=0; me < fldLen; ++ me)
    {
		var temp = formID.elements[i].value.substring(me, me+1)
        if(digits.indexOf(temp) == -1)
        {
	  		var unused = me
	  	}			
		else
		{
			var thisismydate = formID.elements[i].value.charAt(me)
			var thisismydatee = thisismydatee + thisismydate ;                     
		}
	}

	isDate(formID,thisismydatee,i)
}
	
function isDate(formID,thisismydatee,i)
{
	var myDateValue = thisismydatee
	if(myDateValue.length < 7)
	{
		alert("please enter a correct date length")
		formID.elements[i].select()
		carryon = false;
	}
	else
	{
		var myDaya = myDateValue.charAt(0)
		var myDayb = myDateValue.charAt(1)
		var myFullDay = myDaya + myDayb
		var myMontha = myDateValue.charAt(2)
		var myMonthb = myDateValue.charAt(3)
		var myFullMonth = myMontha + myMonthb

		var myYeara = myDateValue.charAt(4)
		var myYearb = myDateValue.charAt(5)
		var myYearc = myDateValue.charAt(6)
		var myYeard = myDateValue.charAt(7)
		var myFullYear = myYeara + myYearb + myYearc + myYeard

		if(myFullDay >31)
		{
			alert("please pick a proper day")
			formID.elements[i].select()
			carryon = false;
		}
		
		if(myFullMonth >12)
		{
			alert("please pick a proper month")
			formID.elements[i].focus()
			carryon = false;
		}

		if(myFullYear <2000 )
		{
			alert("please pick a proper year")
			formID.elements[i].focus()
			carryon = false;
		}
	}
}

//POSTCODE VALIDATION
function validPcd(formID,i)
{
	var digits=" 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    var fldLen = formID.elements[i].value.length
    var thisispcd =""
    var thisispcde =""
	var	unused = ""
	unused = formID.elements[i].value.length;
	if(unused!="")
		{
		for(var me=0; me < fldLen; ++ me)
			{
			var temp = formID.elements[i].value.substring(me, me+1)
			if(digits.indexOf(temp) == -1)
			    {
			  		var unused = me
			    }
				else
				{
					var thisispcd = formID.elements[i].value.charAt(me)
					var thisispcde = thisispcde + thisispcd ;		
				}
			}
			isPcd(formID,thisispcde,i)
		}
	else
		{
		carryon=true;
		}
}
 
function isPcd(formID,thisispcde,i)
{
	var myPostCode = thisispcde
	var pcLength = myPostCode.length
	var pc =""
	var valid = false;
	var identity=""
	
	digits ="1234567890"
	letters =" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	for(pc=myPostCode.length-1;pc>=0;--pc)
		{
		blank = myPostCode.charAt(pc);
		if(blank==' ')
			identity +=" ";
		else if(letters.indexOf(blank)!=-1)
			identity += "A";
		else if(digits.indexOf(blank)!=-1)
			identity += "9";
		}
		
	if(identity=="AA9 9A")
		valid=true;
	else if (identity=="AA9 99A")
		valid=true;
	else if (identity=="AA9 A9A")
		valid=true;
	else if (identity=="AA9 9AA")
		valid=true;
	else if (identity=="AA9 99AA")
		valid=true;
	else if (identity=="AA9 A9AA")
		valid=true;

	if (valid == false)
		{
		alert('Please enter a valid Postcode');
		formID.elements[i].focus();
		carryon=false;
		}
}


//EMAIL VALIDATION SUB SCRIPT
function validEml(formID,i)
{
	var sEmail = new String(formID.elements[i].value);
	
	if(testEmail(sEmail) == true) 
	{
		carryon = true;
	}
	else
	{
		formID.elements[i].focus();
		alert("Email Address is invalid.\n\nPlease correct it.");
		carryon = false;
	}
}

// LUHN CARD NUMBER SUB SCRIPT
function validLuhn(formID,i)
{
	var sLuhn = new String(formID.elements[i].value);
	
	if(sLuhn.length != 0)
	{
		if(IsLuhnCheckValid(sLuhn) == true)
		{
			carryon = true;
		}
		else
		{
			formID.elements[i].focus();
			alert("Card number is invalid.\n\nPlease correct it.");
			carryon = false;
		}
	}
	else
	{
		formID.elements[i].focus();
		alert("Please fill out this field");
		carryon = false;
	}
}

// Check for a 4 digit number
function validYear(formID,i)
{
	var num, bPass = false;
	
	num = formID.elements[i].value;
	
	if(num >= 1000 && num <= 2000){
		bPass = true;
	};
	
	if(bPass == true){
		carryon = true;
	}else{
		formID.elements[i].focus();
		alert("Please enter a valid 4 digit year");
		carryon = false;
	}
}

function testEmail(src) 
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(src);
}

//---------------------------------------------------------------------------------------------------
// Name:		IsLuhnCheckValid
// Notes:		Function to check whether the given number passes the Luhn check algorithm.
// Params:		[in]	cardnumber		string that holds the card number.
//				[out]   Return Value	1 = pass, 0 = fail
// Author:		JML
// Date:		8/11/2001
//---------------------------------------------------------------------------------------------------function IsLuhnCheckValid(cardnumber){
	var i, j, bdouble, ch, chval, result, retval;
	bdouble = false;	result = 0;	retval = false;
		if(parseInt(cardnumber) != "NaN" || parseInt(cardnumber) != "0"){	
		for(i=cardnumber.length-1;i>=0;i--){			ch = cardnumber.substring(i,i+1);
			chval = parseInt(ch);
			if(bdouble == true){			
				chval = chval * 2;				chval += '';								if(chval.length > 1){
					for(j=0;j<chval.length;j++){	
						result += parseInt(chval.substring(j,j+1));
					}
				}else{
					result += parseInt(chval);
				}				
			}else{				result += chval;
			};						//toggle bdouble			if(bdouble == true){				bdouble = false;			}else{				bdouble = true;			}		};	
		// convert result to a string		result += '';			// check the last digit to see if it is '0'
		if(result.substring(result.length - 1, result.length) == '0'){			retval = true;		}
	};	
		return retval;
};//---------------------------------------------------------------------------------------------------
// Name:		SubmitFormButtonClicked
// Notes:		Function to sumit form and set a hidden field called "button clicked"
// Params:		[in]	- value that button clicked should be set to
// Author:		GTB
// Date:		28/11/2001
//---------------------------------------------------------------------------------------------------
function SubmitFormButtonClicked(Source)
{
	document.form1.buttonclicked.value=Source;
	document.form1.submit();
};

//---------------------------------------------------------------------------------------------------
// Name:		Trim
// Notes:		Function to trim leading and trailing whitespace from the given text element.
// Params:		[in] text - the text element to trim
// Author:		JML
// Date:		03/12/2001
//---------------------------------------------------------------------------------------------------
function Trim(text){
	while(''+text.value.charAt(0)==' ')
		text.value=text.value.substring(1,text.value.length);
	while(''+text.value.charAt(text.value.length-1)==' ')
		text.value=text.value.substring(0,text.value.length-1);
}

