//	Javascript included in Register.xslt
//	Created by James Strath, 07 May 2002 (copied from xslt)
//  Amended:	26 Feb 2002	Colin Turner	Rewritten for rm.com self registration
//				04 Jun 2003 Pablo Humphreys		Make customer number mandatory field
//				09 Jun 2003 Pablo Humphreys		Use isNumeric to validate numerical fields
//				19 Jun 2003	Pablo Humphreys		Improve client side validation of order number
// 21/10/03 Steve Pearce  Add NonEducational functions
//				21 May 2003 Ajai V.T.			Copied from Register_v6.js.
//												Added Validation for Confirm Email Address field.
//				28 June	2003 Ajai V.T.			Copied from Register_v7.js
//												Added Location check for Password Reminder field
//				22 Oct 2004 Pablo Humphreys Added extra validation for enhancements and created Register_v9.js
//				26 Oct 2004 Pablo Humphryes Tweaked validation code due to change validation fields
//				03 Nov 2004 Pablo Humphreys Fixed validation issues and added to submitAction function
//				15 Nov 2004 Pablo Humphreys Make email comparison case-insensitive
//				23 Nov 2004 Pablo Humphreys	Enforce selection of Title and handle contact selection.
//				30 Nov 2004 Pablo Humphreys Clear Postcode & CustomerNo when CustomerNo Unknown selected from inital page
//				04 Jan 2006 Steve Richens	Protect from final submit button being pressed multiple times, created V10

function copyOwnLogin()
{
	var iOptionCount = document.frmRegistration.elements['inpLogin'].length;
	
	document.frmRegistration.elements['inpLogin'][iOptionCount-1].value = document.frmRegistration.elements['inpOwnLogin'].value;
}

function submitInitialPage(pbNonEd,pstrClass)
{
	if (validateInitialPage(pbNonEd,pstrClass))
	{
		document.frmRegistration.submit();
	}
}

function submitManualRegistration()
{
	if (validateManualRegistrationPage())
	{
		document.frmRegistration.submit();
	}
}

function submitContactSelection()
{
	if (validateContactSelect())
	{
		document.frmRegistration.submit();
	}
}

function submitContactData(pbNonEd,pstrClass)
{
	if (validateContactData(pbNonEd,pstrClass))
	{
		document.frmRegistration.submit();
	}
}

function submitLoginData(pbNonEd,pstrClass)
{
	if (validateLoginData(pbNonEd,pstrClass))
	{
		if (document.frmRegistration.elements['inpRegistrationDisabled']) {
			document.frmRegistration.elements['inpRegistrationDisabled'].value = 'True';
		}
		document.frmRegistration.submit();
	}
}

function submitDepartment()
{
	if (validateDepartment(false))
	{
		document.frmRegistration.submit();
	}
}

function validateDepartment(pbNonEd)
{
	var bResult = true;
	var strMessage = '';
	var bGetElementById = (document.getElementById?true:false);
	
	var selectedValue = document.frmRegistration.inpDepartment[document.frmRegistration.inpDepartment.selectedIndex].value	
	
	if (selectedValue > 0) // An existing department has been seleceted.
	{
		document.frmRegistration.inpDepartmentSelect.value = 'ExistingHECustomer';
		document.frmRegistration.inpCustomerNo.value = selectedValue;
	}
	else if (selectedValue == 'MainHECustomer') // Create account under the main uni id
	{		
		document.frmRegistration.inpDepartmentSelect.value = 'MainHECustomer';
	}
	else if (selectedValue == 'NewHECustomer') // Create a new dept
	{
		document.frmRegistration.inpDepartmentSelect.value = 'NewHECustomer';
		document.frmRegistration.inpAction.value = 'DisplayManualRegistrationData';
	}
	else // validation error msg
	{
		strMessage = strMessage + 'You must select a Department from the list\n';
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdDepartment').className = 'dataerror';
		}
	}
	
	if(!bResult)
	{
		alert('The following errors have been found. Please correct and continue.\n\n' + strMessage );
	}	
	return bResult;
}

function validateInitialPage(pbNonEd,pstrClass)
{
	var bPhoneError, bSTDError;
	var bResult = true;
	var strMessage = '';
	var bGetElementById = (document.getElementById?true:false);
	
	var strClass;
	if (pbNonEd) { strClass=(pstrClass!=null?pstrClass:'smallgrey')}
	else {strClass='';}
	
	if (document.frmRegistration.elements['inpTitle'])
	{
		if (document.frmRegistration.elements['inpTitle'].value.length == 0)
		{
		strMessage = strMessage + 'Please choose your title\n';
		bResult = false;
		if (bGetElementById)
			{
			document.getElementById('tdTitle').className = 'dataerror';
			}
		}
		else
		{
		if (bGetElementById)
			{
			document.getElementById('tdTitle').className = strClass;
			}
		}	
	}
	
	if (pbNonEd && document.frmRegistration.elements['inpJobTitle']) 
	{
		if (document.frmRegistration.elements['inpJobTitle'].value.length == 0)
		{
		strMessage = strMessage + 'Job title is mandatory\n';
		bResult = false;
		if (bGetElementById)
			{
			document.getElementById('tdJobTitle').className = 'dataerror';
			}
		}
		else
		{
		if (bGetElementById)
			{
			document.getElementById('tdJobTitle').className = strClass;
			}
		}
	}			

	
	if (pbNonEd && document.frmRegistration.elements['inpCompany']) 
	{
		if (document.frmRegistration.elements['inpCompany'].value.length == 0)
		{
		strMessage = strMessage + 'Company Name is mandatory\n';
		bResult = false;
		if (bGetElementById)
			{
			document.getElementById('tdCompany').className = 'dataerror';
			}
		}
		else
		{
		if (bGetElementById)
			{
			document.getElementById('tdCompany').className = strClass;
			}
		}
	}
			
	if (document.frmRegistration.elements['inpForename'].value.length == 0)
	{
		strMessage = strMessage + 'Forename is mandatory\n';
		if(bGetElementById)
		{
			document.getElementById('tdForename').className = 'dataerror';
		}
		bResult = false;
	}
	else
	{
		if (ValidateText(document.frmRegistration.elements['inpForename'].value, 'true', 'false'))
		{
			if(bGetElementById)
			{
				document.getElementById('tdForename').className = strClass;
			}
		}
		else
		{
			strMessage = strMessage + 'Forename must only contain letters\n';
			bResult = false;
			if(bGetElementById)
			{
				document.getElementById('tdForename').className = 'dataerror';
			}
		}
	}
	
	if (document.frmRegistration.elements['inpSurname'].value.length == 0)
	{
		strMessage = strMessage + 'Surname is mandatory\n';
		bResult = false;
		if(bGetElementById)
		{
			document.getElementById('tdSurname').className = 'dataerror';
		}
	}
	else
	{
		if (ValidateText(document.frmRegistration.elements['inpSurname'].value, 'true', 'false'))
		{
			if(bGetElementById)
			{
				document.getElementById('tdSurname').className = strClass;
			}
		}
		else
		{
			strMessage = strMessage + 'Surname must only contain letters\n';
			bResult = false;
			if(bGetElementById)
			{
				document.getElementById('tdSurname').className = 'dataerror';
			}
		}
	}
	
	
	if (document.frmRegistration.elements['inpEmail'].value.length == 0)
	{
		strMessage = strMessage + 'Email address is mandatory\n';
		bResult = false;
		if(bGetElementById)
		{
			document.getElementById('tdEmail').className = 'dataerror';
		}
	}
	else
	{
		if(browserVersion >= 4.0)
		{
			// call Javascript1.2 validation
			if(checkEmail(document.frmRegistration.inpEmail.value) == false)
			{
				strMessage = strMessage + 'Email address format is incorrect\n';
				bResult = false;
				if(bGetElementById)
				{
					document.getElementById('tdEmail').className = 'dataerror';
				}
			}
			else
			{
				if(bGetElementById)
				{
					document.getElementById('tdEmail').className = strClass;
				}
			}			
			
				
		}
		else
		{	//call Javascript1.1 validation
			if(ValidateEMail1(document.frmRegistration.inpEmail.value) == false)
			{
				strMessage = strMessage + 'Email address format is incorrect\n';
				bResult = false;
				if(bGetElementById)
				{
					document.getElementById('tdEmail').className = 'dataerror';
				}
			}				
			else
			{
				if(bGetElementById)
				{
					document.getElementById('tdEmail').className = strClass;
				}
			}	
			
						
		}	
	}
	
	if (document.frmRegistration.elements['inpConfirmEmail'])
	{
	if (document.frmRegistration.elements['inpConfirmEmail'].value.length == 0)
	{
		strMessage = strMessage + 'Confirm Email Address field is mandatory\n';
		bResult = false;
		if(bGetElementById)
		{
			document.getElementById('tdConfirmEmail').className = 'dataerror';
		}
	}
	else
	{
		if(browserVersion >= 4.0)
		{
			// call Javascript1.2 validation
			if(checkEmail(document.frmRegistration.inpConfirmEmail.value) == false)
			{
				strMessage = strMessage + 'Confirm Email Address format is incorrect\n';
				bResult = false;
				if(bGetElementById)
				{
					document.getElementById('tdConfirmEmail').className = 'dataerror';
				}
			}
			else
			{
				if(bGetElementById)
				{
					document.getElementById('tdConfirmEmail').className = strClass;
				}
			}							
		}
		else
		{	//call Javascript1.1 validation
			if(ValidateEMail1(document.frmRegistration.inpConfirmEmail.value) == false)
			{
				strMessage = strMessage + 'Confirm Email Address format is incorrect\n';
				bResult = false;
				if(bGetElementById)
				{
					document.getElementById('tdConfirmEmail').className = 'dataerror';
				}
			}				
			else
			{
				if(bGetElementById)
				{
					document.getElementById('tdConfirmEmail').className = strClass;
				}
			}	
		}	
	}
		
	if (document.frmRegistration.elements['inpEmail'].value.toLowerCase() != document.frmRegistration.elements['inpConfirmEmail'].value.toLowerCase())
	{
		strMessage = strMessage + 'Email Address and Confirm Email Address fields should be the same\n';
		bResult = false;
		if(bGetElementById)
		{
			document.getElementById('tdConfirmEmail').className = 'dataerror';
		}
	}	
	}
		
	if (!pbNonEd)
	{
		if (document.frmRegistration.elements['inpCustomerNo'].value.length == 0)
		{
			strMessage = strMessage + 'Customer Number is mandatory\n';
			if(bGetElementById && document.getElementById('tdCustomerNo'))
			{
				document.getElementById('tdCustomerNo').className = 'dataerror';
			}
			bResult = false;
		}
		else
		{
			if (isNumeric(document.frmRegistration.elements['inpCustomerNo'].value))
			{
				if(bGetElementById && document.getElementById('tdCustomerNo'))
				{
					document.getElementById('tdCustomerNo').className = strClass;
				}
			}
			else 
			{
				strMessage = strMessage + 'Customer Number must only contain numbers\n';
				bResult = false;
				if(bGetElementById && document.getElementById('tdCustomerNo'))
				{
					document.getElementById('tdCustomerNo').className = 'dataerror';
				}		
			}
		}
		if (document.frmRegistration.elements['inpPostcode'].value.length == 0) {
			strMessage = strMessage + 'Post Code is mandatory\n';
			if(bGetElementById && document.getElementById('tdPostcode'))
			{
				document.getElementById('tdPostcode').className = 'dataerror';
			}
			bResult = false;
		}
		else
		{
			if(bGetElementById && document.getElementById('tdPostcode'))
			{
				document.getElementById('tdPostcode').className = strClass;
			}
		}
	}
	else
	{
		if(bGetElementById && document.getElementById('tdCustomerNo') && document.getElementById('tdPostcode'))
		{
			document.frmRegistration.inpPostcode.value = '';
			document.frmRegistration.inpCustomerNo.value ='';			
			document.getElementById('tdPostcode').className = strClass;
			document.getElementById('tdCustomerNo').className = strClass;
		}		
	}
		
	if (document.frmRegistration.elements['inpSTDCode'] && document.frmRegistration.elements['inpSTDCode'].value.length > 0 && !isNumeric(document.frmRegistration.elements['inpSTDCode'].value))
	{
		strMessage = strMessage + 'The STD Code must be numeric\n';
		bSTDError = true;
		bResult = false;
	}
	else
	{
		bSTDError = false;
	}
	
	if (document.frmRegistration.elements['inpTelephoneNo'] && document.frmRegistration.elements['inpTelephoneNo'].value.length > 0 && !isNumeric(document.frmRegistration.elements['inpTelephoneNo'].value))
	{
		strMessage = strMessage + 'The Telephone Number must be numeric\n';
		bPhoneError = true;
		bResult = false;
	}
	else
	{
		bPhoneError = false;
	}
	
	if(bSTDError || bPhoneError)
	{
		if(document.getElementById && document.getElementById('tdPhone'))
		{
			document.getElementById('tdPhone').className = 'dataerror';
		}
	}
	else
	{
		if(document.getElementById && document.getElementById('tdPhone'))
		{
			document.getElementById('tdPhone').className = strClass;
		}
	}
	
	
	if (pbNonEd)
		{
		// Check address details entered
		//
		// Address line 1	
		if (document.frmRegistration.elements['inpAddress1'] && document.frmRegistration.elements['inpAddress1'].value.length == 0)
			{
			strMessage = strMessage + 'Address line 1 is mandatory\n';
			bResult = false;
			if(bGetElementById)
				{
				document.getElementById('tdAdd1').className = 'dataerror';
				}
			}
		
		// City
		if (document.frmRegistration.elements['inpCity'] && document.frmRegistration.elements['inpCity'].value.length == 0)
			{
			strMessage = strMessage + 'City is mandatory\n';
			bResult = false;
			if(bGetElementById)
				{
				document.getElementById('tdCity').className = 'dataerror';
				}
			}		


		// County
		if (document.frmRegistration.elements['inpCounty'] && document.frmRegistration.elements['inpCounty'].value.length == 0)
			{
			strMessage = strMessage + 'County is mandatory\n';
			bResult = false;
			if(bGetElementById)
				{
				document.getElementById('tdCounty').className = 'dataerror';
				}
			}

		// Postcode
		if (document.frmRegistration.elements['inpPostCode'] && document.frmRegistration.elements['inpPostCode'].value.length == 0)
			{
			strMessage = strMessage + 'Postcode is mandatory\n';
			bResult = false;
			if(bGetElementById)
				{
				document.getElementById('tdPostcode').className = 'dataerror';
				}
			}		
		}

	
	if(!bResult)
	{
		alert('The following errors have been found. Please correct and continue.\n\n' + strMessage );
	}

	return bResult;
}

function validateManualRegistrationPage()
{
	var bPhoneNoProvided, bPhonePreferred, bPhoneSTDError, bPhoneError;
	var bFaxNoProvided, bFaxNoPreferred, bFaxSTDError, bFaxError;
	var bResult = true;
	var strMessage = '';
	var bGetElementById = (document.getElementById?true:false);
	
	// Job Title
	if (document.frmRegistration.elements['inpJobTitle'].value.length == 0)
	{
		strMessage = strMessage + 'Job Title is mandatory\n';
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdJobTitle').className = 'dataerror';
		}
	}
	else
	{
		if (bGetElementById)
		{
			document.getElementById('tdJobTitle').className = '';
		}
	}

	//Area of interest
	if (document.frmRegistration.inpAreaOfInterest.options[document.frmRegistration.inpAreaOfInterest.selectedIndex].value == '0')
	{
		strMessage = strMessage + 'Area of interest is mandatory\n';
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdInterest').className = 'dataerror';
		}
	}
	else
	{
		if (bGetElementById)
		{
			document.getElementById('tdInterest').className = '';
		}
	}
	
	// Phone number not mandatory, but if provided or selected should be valid
	bPhoneNoProvided = document.frmRegistration.elements['inpPhoneSTDCode'].value.length > 0 ||
					   document.frmRegistration.elements['inpPhoneNo'].value.length > 0;
    bPhonePreferred = document.frmRegistration.elements['inpPreferred'][1].checked;
    					   
	bPhoneSTDError = false;
	bPhoneError = false;
	
	if (bPhoneNoProvided || bPhonePreferred)
	{
		if (document.frmRegistration.elements['inpPhoneSTDCode'].value.length == 0)
		{
			strMessage = strMessage + 'You must provide the telephone STD code\n';
			bSTDError = true;
		}
		
		if (document.frmRegistration.elements['inpPhoneNo'].value.length == 0)
		{
			strMessage = strMessage + 'You must provide the telephone number\n';
			bPhoneError = true;
		}
	}
	
	if(document.frmRegistration.elements['inpPhoneSTDCode'].value.length > 0 && 
	   !isNumeric(document.frmRegistration.elements['inpPhoneSTDCode'].value))
	{
		strMessage = strMessage + 'The telephone STD code must be numeric\n';
		bSTDError = true;
	}
	
	if (document.frmRegistration.elements['inpPhoneNo'].value.length > 0 && 
	    !isNumeric(document.frmRegistration.elements['inpPhoneNo'].value))
	{
		strMessage = strMessage + 'The telephone number must be numeric\n';
		bPhoneError = true;
	}
	
	if(bPhoneSTDError || bPhoneError)
	{
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdPhone').className = 'dataerror';
		}
	}
	else
	{
		if (bGetElementById)
		{
			document.getElementById('tdPhone').className = '';
		}
	}

	// FAX number not mandatory, but if provided or selected it should be valid
	bFaxNoProvided = document.frmRegistration.elements['inpFaxSTDCode'].value.length > 0 ||
					 document.frmRegistration.elements['inpFaxNo'].value.length > 0;
    bFaxNoPreferred = document.frmRegistration.elements['inpPreferred'][2].checked;
    					   
	bFaxSTDError = false;
	bFaxError = false;
	
	if (bFaxNoProvided || bFaxNoPreferred)
	{
		if (document.frmRegistration.elements['inpFaxSTDCode'].value.length == 0)
		{
			strMessage = strMessage + 'You must provide the fax STD code\n';
			bFaxSTDError = true;
		}
		
		if (document.frmRegistration.elements['inpFaxNo'].value.length == 0)
		{
			strMessage = strMessage + 'You must provide the fax number\n';
			bFaxError = true;
		}
	}
	
	if(document.frmRegistration.elements['inpFaxSTDCode'].value.length > 0 && 
	   !isNumeric(document.frmRegistration.elements['inpFaxSTDCode'].value))
	{
		strMessage = strMessage + 'The fax STD code must be numeric\n';
		bFaxSTDError = true;
	}
	
	if (document.frmRegistration.elements['inpFaxNo'].value.length > 0 && 
	    !isNumeric(document.frmRegistration.elements['inpFaxNo'].value))
	{
		strMessage = strMessage + 'The fax number must be numeric\n';
		bFaxError = true;
	}
	
	if(bFaxSTDError || bFaxError)
	{
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdFax').className = 'dataerror';
		}
	}
	else
	{
		if (bGetElementById)
		{
			document.getElementById('tdFax').className = '';
		}
	}

	if (document.frmRegistration.inpEstablishment)
	{
		if (document.frmRegistration.inpEstablishment.value.length == 0)
		{
			strMessage = strMessage + 'Establishment name is mandatory\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdEstablishment').className = 'dataerror';
			}
		}
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdEstablishment').className = '';
			}
		}
	}		

	if (document.frmRegistration.inpAddress1)
	{
		if (document.frmRegistration.inpAddress1.value.length == 0)
		{
			strMessage = strMessage + 'Address is mandatory\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdAddress').className = 'dataerror';
			}
		}
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdAddress').className = '';
			}
		}
	}	

	if (document.frmRegistration.inpCity)
	{
		if (document.frmRegistration.inpCity.value.length == 0)
		{
			strMessage = strMessage + 'City name is mandatory\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdCity').className = 'dataerror';
			}
		}
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdCity').className = '';
			}
		}
	}
	
	if (document.frmRegistration.inpDepartment)
	{
		if (document.frmRegistration.inpDepartment.value.length == 0)
		{
			strMessage = strMessage + 'Department name is mandatory\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdDepartment').className = 'dataerror';
			}
		}
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdDepartment').className = '';
			}
		}
	}
	
	if (document.frmRegistration.elements['inpPostcode'].value.length == 0)
	{
		strMessage = strMessage + 'Postcode is mandatory\n';
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdPostcode').className = 'dataerror';
		}
	}
	else
	{
		if (bGetElementById)
		{
			document.getElementById('tdPostcode').className = '';
		}
	}

	// Terms and conditions
	if (document.frmRegistration.elements['inpTerms'].checked)
	{
		if (bGetElementById)
		{
			document.getElementById('tdTerms').className = '';
		}
	}
	else
	{
		strMessage = strMessage + 'Please indicate you accept the terms and conditions and the privacy statement\n';
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdTerms').className = 'dataerror';
		}
	}
	

	if(!bResult)
	{
		alert('The following errors have been found. Please correct and continue.\n\n' + strMessage );
	}
	
	return bResult

}

function validateContactSelect()
{	
	var bGetElementById = (document.getElementById?true:false);
	// Check which option is selected	
	for (var iLoop = 0, iLength = document.frmRegistration.inpRegistrationContactMatch.length; iLoop < iLength ; iLoop++)
	{
		if (document.frmRegistration.inpRegistrationContactMatch[iLoop].checked)
		{
			if (document.frmRegistration.inpRegistrationContactMatch[iLoop].value != -1)
			{
				var strContactCustomerValue = document.frmRegistration.inpRegistrationContactMatch[iLoop].value
				document.frmRegistration.inpContact.value = strContactCustomerValue.substring(0,strContactCustomerValue.indexOf("-"))
				document.frmRegistration.inpCustomerNo.value = strContactCustomerValue.substring(strContactCustomerValue.indexOf("-")+1)
			}
			else
			{
				document.frmRegistration.inpAction.value = 'CreateNewContact';
			}
			return true;
		}
	}
	if (bGetElementById)
	{
		alert('Please select one of the contacts from the selected list');
		return false;
	}	
}

function validateContactData(pbNonEd, pstrClass)
{
	var bResult = true;
	var strMessage = '';
	var bGetElementById = (document.getElementById?true:false);	
	
	var strClass;
	if (pbNonEd) { strClass=(pstrClass!=null?pstrClass:'smallgrey')}
	else {strClass='';}
	
	//Job Title + Area of Interest only required in Educational establishment.
	if (!pbNonEd)
	{
		//Job Title
		if (document.frmRegistration.elements['inpJobTitle'].value.length == 0)
		{
			strMessage = strMessage + 'Job Title is mandatory\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdJobTitle').className = 'dataerror';
			}
		}
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdJobTitle').className = strClass;
			}
		}
		
		if (document.frmRegistration.inpAreaOfInterest.options[document.frmRegistration.inpAreaOfInterest.selectedIndex].value == '0')
		{
			strMessage = strMessage + 'Area of interest is mandatory\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdInterest').className = 'dataerror';
			}
		}
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdInterest').className = strClass;
			}
		}
	}

	if(!bResult)
	{
		alert('The following errors have been found. Please correct and continue.\n\n' + strMessage );
	}
	
	return bResult;
}

function validateLoginData(pbNonEd,pstrClass) 
{
	var bResult = true;
	var strMessage = '';
	var bGetElementById = (document.getElementById?true:false);	
	
	var strClass;
	if (pbNonEd) { strClass=(pstrClass!=null?pstrClass:'smallgrey')}
	else {strClass='';}
	
	var iOptionCount = document.frmRegistration.elements['inpLogin'].length;
	var iLoop;
	
	for (iLoop=0;iLoop < iOptionCount;iLoop++)
	{
		if(document.frmRegistration.elements['inpLogin'][iLoop].checked)
		{
			if (document.frmRegistration.elements['inpLogin'][iLoop].value.length < 6)
			{
				strMessage = strMessage + 'Login is mandatory and must be at least 6 characters long\n';
				bResult = false;
				if (bGetElementById)
				{
					document.getElementById('tdLogin').className = 'dataerror';
				}
			}
			else
			{
				if (validateLogin(document.frmRegistration.elements['inpLogin'][iLoop].value))
				{
					if (bGetElementById)
					{
						document.getElementById('tdLogin').className = strClass;
					}
				}
				else
				{
					strMessage = strMessage + 'Login must only contain alpha-numeric characters without spaces\n';
					bResult = false;
					if (bGetElementById)
					{
						document.getElementById('tdLogin').className = 'dataerror';
					}
				}
			}
		}
	}

	if (document.frmRegistration.elements['inpPassword'].value.length < 6)
	{
		strMessage = strMessage + 'Password is mandatory and must be at least 6 characters long\n';
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdPassword').className = 'dataerror';
			document.getElementById('tdConfirmPassword').className = 'dataerror';
		}
	}
	else
	{
		if(document.frmRegistration.elements['inpPassword'].value != document.frmRegistration.elements['inpPasswordCheck'].value)
		{
			strMessage = strMessage + 'Password and Confirm Password differ\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdPassword').className = 'dataerror';
				document.getElementById('tdConfirmPassword').className = 'dataerror';
			}
		}
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdPassword').className = strClass;
				document.getElementById('tdConfirmPassword').className = strClass;
			}
		}
	}
	
	if (document.frmRegistration.inpOrganisation.value=="RMInternal")
	{
		if (document.frmRegistration.inpPasswordReminder.value.length == 0)
		{
			strMessage = strMessage + 'Password Reminder is mandatory\n';
			bResult = false;
			if (bGetElementById)
			{
				document.getElementById('tdReminder').className = 'dataerror';
			}
		}	
		else
		{
			if (bGetElementById)
			{
				document.getElementById('tdReminder').className = strClass;
			}
		}
	}

	// Terms and conditions accepted
	if (document.frmRegistration.elements['inpTerms'].checked)
	{
		if (bGetElementById)
		{
			document.getElementById('tdTerms').className = strClass;
		}
	}
	else
	{
		strMessage = strMessage + 'Please indicate you accept the terms and conditions and the privacy statement\n';
		bResult = false;
		if (bGetElementById)
		{
			document.getElementById('tdTerms').className = 'dataerror';
		}
	}
	
	if(!bResult)
	{
		alert('The following errors have been found. Please correct and continue.\n\n' + strMessage );
	}
	else	
	{
		 document.getElementById('SubmitOn').style['display'] = 'none';
		 document.getElementById('SubmitOff').style['display'] = 'block';
	}
	
	return bResult;
}

function validateLogin(pstrLogin)
{
	var theChar;
	
	for(var iLoop=0; iLoop < pstrLogin.length; iLoop++)
	{
		theChar =  pstrLogin.charAt(iLoop);
		
		if (!isAlphaNumeric(theChar) || theChar==' ')
		{
			return false;
		}
	}
	return true;
}


function ValidOrderNo(pstrOrderNo)
{	
	if (isNumeric(pstrOrderNo)) // check if pstrOrderNo contains any alpha characters
	{
		if (pstrOrderNo.length > 8) // all numbers now check length
			return false; // too many numbers for a valid order
	}
	else // contains alpha characters somewhere	
	{
		if ((pstrOrderNo.substr(0,2)).toUpperCase() !== 'RM') // check first 2 chars
		{
			return false; // characters are not what we want	
		}
		else // check that we dont have any other alpha chars
		{
			var strTemp = pstrOrderNo.substr(2,pstrOrderNo.length)
			if (!isNumeric(strTemp))
				return false;
		}		
	}
	return true;
}


function submitAction(pstrAction)
	{
	//
	// User has selected an Action to preceed with
	//
	var bOK = true;
	var bGetElementById = (document.getElementById?true:false);
	switch (pstrAction)
		{
		case 'LOGINDETAILS':
			//
			// Check user entered required data items
			//
			
			bOK = validateInitialPage(true);
				
			break;

		case 'SubmitSelfRegistration':
		
			bOK = validateLoginData(true);
			
			if (bOK) {
				if (document.frmRegistration.elements['inpRegistrationDisabled']) {
					document.frmRegistration.elements['inpRegistrationDisabled'].value = 'True';
				}
			}

			//if (bOK == true)
			//	{
			//	//
			//	// Has user given permission for us to send them emails in future..
			//	//
			//	if (document.frmRegistration.elements['inpReceiveEmail'].checked == false)
			//		{
			//		if (confirm('Please confirm if you do NOT wish to receive emails from RM\n\nClick OK to grant permission to receive emails.')==true)
			//			{
			//			document.frmRegistration.elements['inpReceiveEmail'].checked = true;
			//			}
			//		}
			//	}
		
			break;
		
		case 'NONEDUCATIONALORG':
			document.frmRegistration.action = 'RegistrationNonEd.asp';
			document.frmRegistration.inpRegType.value = 'PRIVATECOMPANY';	
			break;
			
		case 'NONEDUCATIONALPERSON':
			document.frmRegistration.action = 'RegistrationNonEd.asp';
			document.frmRegistration.inpRegType.value = 'PRIVATEPERSON';
			break;
			
		case 'CONTINUESHOPPING':
			document.frmRegistration.action = '../Products/CreateOrder.asp';
			document.frmRegistration.inpAction.value = 'FROMSELFREG';
			break

		case 'PWREMINDER':
			document.frmRegistration.action = '/Security/Login/RequestPasswordReminder.asp';
			document.frmRegistration.inpAction.value = 'FROMSELFREG';
			break

		case 'LEAFUNDED':
			document.frmRegistration.action = 'Register.asp';
			break

		case 'EMAILORDER':
			
			var strVN1 = document.frmRegistration.inpVN1.value;
			var strVN2 = document.frmRegistration.inpVN2.value;
			var strVN3 = document.frmRegistration.inpVN3.value;
			var strVN4 = document.frmRegistration.inpVN4.value;
		
			if (strVN1.length > 0 || strVN2.length > 0 || strVN3.length > 0 || strVN4.length > 0 ) 
				{
				// user has entered Voucher no
				var strVN = strVN1 + '-' + strVN2 + '-' + strVN3 + '-' + strVN4;
				
				if (strVN.length != 19)
					{
					bOK = false;
					alert('The full Voucher number has not been entered.');
					}
				else
					{
					document.frmRegistration.inpVoucherNo.value = strVN.toUpperCase();
					}		
				}		

			if (bOK == true)
				{
				document.frmRegistration.action = '../Products/CreateOrder.asp';
				document.frmRegistration.inpAction.value = 'EMAILINITIAL';
				}
			
			break
			
		case 'OTHEREMAIL':
		
			var strEmail = document.frmRegistration.inpEmailOther.value;
			if (strEmail.length == 0)
			{
				bOK=false;
				alert('Email address is mandatory');

				if(bGetElementById)
				{
					document.getElementById('tdEmail').className = 'dataerror';
				}
			}
			else
			{
				if(browserVersion >= 4.0)
				{
					// call Javascript1.2 validation
					if(checkEmail(strEmail) == false)
					{
						bOK=false;
						alert('Email address format is incorrect');
						if(bGetElementById)
						{
							document.getElementById('tdEmail').className = 'dataerror';
						}
					}
				}
				else
				{	//call Javascript1.1 validation
					if(ValidateEMail1(strEmail) == false)
					{
						bOK=false;
						alert('Email address format is incorrect');
						if(bGetElementById)
						{
							document.getElementById('tdEmail').className = 'dataerror';
						}
					}
				}	
			}
			
			if (bOK == true)
				{		
				document.frmRegistration.action = 'RegistrationType.asp';
				document.frmRegistration.inpAction.value = 'EMAILINITIAL';
				document.frmRegistration.inpEmailAddress.value = strEmail;
				document.frmRegistration.inpEmail.value = strEmail;
				}
				
			break			
			
		default: 
			// do nothing
			bOK=false;
			break;
		}

	if (bOK == true) document.frmRegistration.submit();
	
}

function windowOnload () {

	if (document.getElementById('SubmitOn')) {
		if (document.frmRegistration.elements['inpRegistrationDisabled']) {
			if (document.frmRegistration.elements['inpRegistrationDisabled'].value == 'True') {
				document.getElementById('SubmitOn').style['display'] = 'none';
				document.getElementById('SubmitOff').style['display'] = 'block';
			} else {
				document.getElementById('SubmitOn').style['display'] = 'block';
				document.getElementById('SubmitOff').style['display'] = 'none';
			}
		}
	}

}

window.onload = windowOnload;
