//***************************************************************************************************
//Purpose : functions used within the portal login functionality.
//Author : D Staniforth
//Last Revised Date : 03/02/2002
//Script Version : 1.0
//----------------------------------History----------------------------------------------------------
//***************************************************************************************************

function Login()
{
	var varLogin = document.frmLogin.txtLogin.value;
	var varPassword = document.frmLogin.txtPassword.value;
	
	if (varLogin.length == 0) {
		alert("You must enter a User ID");
		document.frmLogin.txtLogin.focus();
	}
	else if (varPassword.length == 0) {
		alert("You must enter a Password");	
		document.frmLogin.txtPassword.focus();
	}
	else {
		document.frmLogin.txtLoginAction.value = "LoggingIn";
		document.frmLogin.submit();
	}
}

function keyhandler(e) {
	//*******************************************
	//function to test which key has been pressed
	//*******************************************
    if (document.layers)
        Key = e.which;
    else
        Key = window.event.keyCode;
    if (Key == 13)
        Login();
}

function LoginFailed()
{
	alert('Login failed. Please input your login and password or register if you do not have a valid login and password');
	document.frmLogin.txtLogin.focus();
}

function LogOut()
{
	document.frmLogin.txtLoginAction.value = "LoggingOut";
	document.frmLogin.submit();
}

function ForgottenPassword(lngSessionID, lngCodePage)
{
	varLogin = document.frmSidebar.txtLogin.value;
	if (varLogin.length==0) {
		alert("You must enter a user id before you can find a forgotten password.");
		document.frmSidebar.txtLogin.focus();
		return;
	}
	
	var strUID = document.frmSidebar.txtLogin.value;
	
	document.location.href="PageServer.asp?S=" + lngSessionID + "&C=" + lngCodePage + "&FP=OK&UID=" + strUID
}

function AnswerCheck(lngSessionID, lngCodePage, strUID) {

	varAnswer = document.frmDetails.txtAnswer.value;
	if (varAnswer.length==0) {
		alert("You must enter an answer.");
		document.frmDetails.txtAnswer.focus();
		return;
	}
	
	document.location.href="PageServer.asp?S=" + lngSessionID + "&C=" + lngCodePage + "&FP=OK&UID=" + strUID + "&Answer=" + varAnswer	
}

function Register(lngSessionID, lngCodePage)
{
	document.location.href="PageServer.asp?S=" + lngSessionID + "&C=" + lngCodePage + "&Register=OK"
}

function SaveMemberDetails(strType, lngSessionID, lngCodePage)
{
	varFirstName = document.frmDetails.txtFirstName.value;
	if (varFirstName.length==0) {
		alert("You must enter a first name before you can save your details.");
		document.frmDetails.txtFirstName.focus();
		return;
	}
	varSurname = document.frmDetails.txtSurname.value;
	if (varSurname.length==0) {
		alert("You must enter a surname before you can save your details.");
		document.frmDetails.txtSurname.focus();
		return;
	}
	varEmail = document.frmDetails.txtEmail.value;
	if (varEmail.length==0) {
		alert("You must enter an email address before you can save your details.");
		document.frmDetails.txtEmail.focus();
		return;
	}
	if (strType=='Add') {
		varOrganisation = document.frmDetails.cboOrganisation.value;
		if (varOrganisation.length==0) {
			alert("You must enter an organisation code before you can save your details.");
			document.frmDetails.cboOrganisation.focus();
			return;
		}
	}
	varAddress1 = document.frmDetails.txtAddress1.value;
	if (varAddress1.length==0) {
		alert("You must enter address line 1 before you can save your details.");
		document.frmDetails.txtAddress1.focus();
		return;
	}
	varTown = document.frmDetails.txtTown.value;
	if (varTown.length==0) {
		alert("You must enter a town before you can save your details.");
		document.frmDetails.txtTown.focus();
		return;
	}
	varPostcode = document.frmDetails.txtPostcode.value;
	if (varPostcode.length==0) {
		alert("You must enter a postcode before you can save your details.");
		document.frmDetails.txtPostcode.focus();
		return;
	}
	varPassword = document.frmDetails.txtPassword.value;
	if (varPassword.length==0) {
		alert("You must enter a password before you can save your details.");
		document.frmDetails.txtPassword.focus();
		return;
	}
	if (varPassword.length<8) {
		alert("The password must be a minimum of 8 characters in length.");
		document.frmDetails.txtPassword.focus();
		return;
	}
	if (((varPassword.search(/[a-z]/) == -1) && (varPassword.search(/[A-Z]/) == -1)) || (varPassword.search(/[0-9]/) == -1)) {
		alert("The password must contain a mixture of numbers and letters.");
		document.frmDetails.txtPassword.focus();
		return;
	}
	varConfirm = document.frmDetails.txtConfirm.value;
	if (varConfirm.length==0) {
		alert("You must enter a confirmation password before you can save your details.");
		document.frmDetails.txtConfirm.focus();
		return;
	}
	if (varPassword != varConfirm) {
		alert("The password and confirmation password do not match.");
		document.frmDetails.txtPassword.focus();
		return;
	}
	varQuestion = document.frmDetails.txtQuestion.value;
	if (varQuestion.length==0) {
		alert("You must enter a question (to be asked if you forget your password) before you can save your details.");
		document.frmDetails.txtQuestion.focus();
		return;
	}
	varAnswer = document.frmDetails.txtAnswer.value;
	if (varAnswer.length==0) {
		alert("You must enter an answer (to be asked if you forget your password) before you can save your details.");
		document.frmDetails.txtAnswer.focus();
		return;
	}
	
	//If we get here then the validation checks have been passed
	document.frmDetails.txtSID.value = lngSessionID;
	document.frmDetails.txtFunction.value = strType;
	document.frmDetails.txtRequestType.value = "RegisterSave";
	document.frmDetails.submit();
	//document.location.href="PageServer.asp?S=" + lngSessionID + "&C=" + lngCodePage + "&Register=OK&Save=OK"
	
}