function LoginStage1(username) 
{
    var username = document.loginform.username.value;
    document.loginform.username.value = '';
    document.loginform.username.value = username;
    document.loginform.username.className = 'login readonly';
    document.loginform.password.className = 'login readonly';
    AjaxRequest('javascript.php?action=LoginStage1&username=' + username,false);
}

function LoginStage2(seed1, seed2)
{
	var password = document.loginform.password.value;
	document.loginform.password.value = '';
	document.loginform.hash1.value = hex_hmac_sha1(password, seed1);
	document.loginform.hash2.value = hex_sha1(hex_hmac_sha1(password, seed2));
	document.loginform.action.value = "login";
	document.loginform.submit();
}

function ChangePasswordStage1(username) {
    var username = document.detailform.username.value;
    document.detailform.username.value = '';
    document.detailform.username.value = username;
    AjaxRequest('javascript.php?action=ChangePasswordStage1&username=' + username,false);
}

function ChangePasswordStage2(seed1, seed2) {
	//alert("OldChallenge:\'" + seed1 + "\'\nNewChallenge:\'" + seed2 + "\'");
	var password = document.detailform.currentpassword.value;
	var new_password1 = document.detailform.newpassword.value;
	var new_password2 = document.detailform.newpasswordrepeat.value;

	document.detailform.currentpassword.value = '';
	document.detailform.newpassword.value = '';
	document.detailform.newpasswordrepeat.value = '';

	if (password == '') {
		//alert('You must supply a valid password.');
		mdbox('vp');
	} else if ((new_password1 != '' || new_password2 != '') && (new_password1 != new_password2)) {
		//alert('The new passwords do not match.');
		mdbox('np!m');
	} else {	//If some password input is received and the new passwords match (val or blank)
		document.detailform.hash1.value = hex_hmac_sha1(password, seed1);
	//alert(hex_sha1(hex_hmac_sha1(password, seed1)));
		document.detailform.hash2.value = hex_sha1(hex_hmac_sha1(password, seed2));

		if (new_password1 != '') {
			document.detailform.hash3.value = hex_sha1(hex_hmac_sha1(new_password1, seed2));
		}
		if (new_password2 != '') {
			document.detailform.hash4.value = hex_sha1(hex_hmac_sha1(new_password2, seed2));
		}
	
		document.detailform.action.value = 'update';
		document.detailform.submit();
	}
}
function mdbox(field) {
	var div = document.getElementById('mdinfo');
	var ptit = document.getElementById('mdTitle');
	var pinf = document.getElementById('mdBody');
	//var tit = "";
	//var bod = "";
	var oldtit = ptit.innerHTML;
	var oldbod = pinf.innerHTML;
	if (field == "un") {
		tit = "<b>Username</b>";
		bod = "This field contains your username which is used to log in to the quiz.";
		div.style.backgroundColor='#FFF';
	} else if (field == "nom") {
		tit = "<b>Name</b>";
		bod = "This field contains your name.";
		div.style.backgroundColor='#FFF';
	} else if (field == "email") {
		tit = "<b>Email</b>"
		bod = "This field contains your email address which will be used to recover forgotton passwords.<br/><br/>You cannot recover your password if you do not have access to this email address.";
		div.style.backgroundColor='#FFF';
	} else if (field == "cpass") {
		tit = "<b>Current Password</b>";
		bod = "This field is for your current password which must be supplied in order to make any changes to your details.";
		div.style.backgroundColor='#FFF';
	} else if (field == "npass") {
		tit = "<b>New Password</b>";
		bod = "These fields are used to change your password. You must supply your current password and both new values must match.";
		div.style.backgroundColor='#FFF';
	} else if (field == "vp") {
		tit = "<b>Password Error!</b>";
		bod = "<p><img src=\"./images/x_icon.gif\"/> You did not supply a valid password.</p><p>You must supply a valid password in order to update your details.";
		div.style.backgroundColor='#FFE4E4';
	} else if (field == "np!m") {
		tit = "<b>Password Error</b>";
		bod = "<p><img src=\"./images/x_icon.gif\"/> The new passwords did not match.</p><p>If you intend to change your password then the new passwords must match otherwise leave these fields blank.</p>";
		div.style.backgroundColor='#FFE4E4';
	} else if (field == "unt") {
		tit = "<b>Username Error!</b>";
		bod = "<p><img src=\"./images/caution.png\"/> The username you have chosen is already in use.</p><p>Please choose another username</p>";
		div.style.backgroundColor='#FFD';
	} else if (field == "unc") {
		tit = "<b>Details Updated</b>";
		bod = "<p><img src=\"./images/tick_icon.gif\"/> Your username has been updated.</p>";
		div.style.backgroundColor='#E7FFDF';
	} else if (field == "nc") {
		tit = "<b>Details Updated</b>";
		bod = "<p><img src=\"./images/tick_icon.gif\"/> Your name has been updated.</p>";
		div.style.backgroundColor='#E7FFDF';
	} else if (field == "eu") {
		tit = "<b>Details Updated</b>";
		bod = "<p><img src=\"./images/tick_icon.gif\"/> Your email address has been updated.</p>";
		div.style.backgroundColor='#E7FFDF';
	} else if (field == "pu") {
		tit = "<b>Details Updated</b>";
		bod = "<p><img src=\"./images/tick_icon.gif\"/> Your password has been updated.</p>";
		div.style.backgroundColor='#E7FFDF';
	}
	if (tit != oldtit) {
		div.style.display = 'none';
		ptit.innerHTML = tit;
		pinf.innerHTML = bod;
		Effect.Appear('mdinfo');
	} else if ((oldbod != bod) && (field != "vp")) {
		div.style.display = 'none';
		ptit.innerHTML = tit;
		pinf.innerHTML = oldbod + bod;
		Effect.Appear('mdinfo');
	}
}