var CancelValidation = false;

function CalcXor(a, b)
{
    var Result = "";
    if(a.length == b.length)
    {
        for(var i=0; i<a.length; i++)
        {
            Result += (parseInt(a.charAt(i), 16) ^ parseInt(b.charAt(i), 16)).toString(16);
        }
        return Result;
    }
    else
    {
        return "";
    }
}

function HandlePassword(HashString, Password1SubmitField, Password1EntryField1, Password1EntryField2, Password2SubmitField, Password2EntryField)
{
	// Handles case when password changed and entered twice
    var Password1Entry1Obj = document.getElementById(Password1EntryField1);
    var Password1Entry2Obj = document.getElementById(Password1EntryField2);
    var Password1SubmitObj = document.getElementById(Password1SubmitField);
    var Password2EntryObj = document.getElementById(Password2EntryField);
    var Password2SubmitObj = document.getElementById(Password2SubmitField);

    var ValidationEnabled = typeof(Page_ValidationActive) != 'undefined';
    var ValidationActive = ValidationEnabled && Page_ValidationActive;
    var BlockSubmit = typeof(Page_BlockSubmit) != 'undefined' && Page_BlockSubmit;

    // alert("ValidationEnabled: "+ValidationEnabled);
    // alert("ValidationActive: "+ValidationActive);
    // alert("BlockSubmit: "+BlockSubmit);

    // We have to avoid encoding the password (which required we disable the input fields) when there is a problem, cos then the user can't change it again.
    if(CancelValidation || (ValidationEnabled && (!ValidationActive || BlockSubmit))) // Validation has been disabled so take special action
    {
        // 2 Reasons that I know of
        // 1. A control not requiring validation wants to submit in which case clear the input fields
        // 2. Validation has failed and will be blocked shortly, so don't touch the input fields cos they need to be editable
        if(!BlockSubmit) // Page is being submitted so clear the fields
        {
            if(Password1Entry1Obj != null) Password1Entry1Obj.value = "";
            if(Password1Entry2Obj != null) Password1Entry2Obj.value = "";
            if(Password2EntryObj != null) Password2EntryObj.value = "";
        }
        return true;
    }

    if(Password1Entry1Obj != null && Password1SubmitObj != null && Password1Entry1Obj.value != "")
    {
        if(Password1Entry2Obj != null && Password1Entry2Obj.value != Password1Entry1Obj.value) return false;

		if(Password2EntryObj != null && Password2EntryObj.value == "") return false; // Test this before disabling any fields.

        Password1SubmitObj.value = hex_sha1(HashString+Password1Entry1Obj.value);
		Password1Entry1Obj.disabled = true;

		if(Password1Entry2Obj != null) Password1Entry2Obj.disabled = true;

		if(Password2EntryObj != null && Password2EntryObj.value != "")
        {
            Password2SubmitObj.value = hex_sha1(HashString+Password2EntryObj.value);
            Password2EntryObj.disabled = true;
            return true;
		}

        return true;
    }
    if(Password2EntryObj != null || Password1Entry2Obj != null)
    {
        alert("Please ensure you have entered a password between 6 and 20 characters long, and that you have entered it twice identically.");
    }
//    else if(Password1Entry2Obj != null)
//    {
//        alert("Please ensure you have entered a password between 6 and 20 characters long.");
//    }
    else
    {
        alert("Please enter your password.");
    }
	return false;
}

function HandleNewPassword(HashString, RestrictedString)
{
    var PasswordElem = document.getElementById("Password");
    var NewPasswordElem = document.getElementById("NewPassword");
    var NewPasswordCheckElem = document.getElementById("NewPasswordCheck");
    var KeyElem = document.getElementById("Key");

    if(PasswordElem.value != "")
    {
        if(NewPasswordElem.value.length > 5)
        {
            if(NewPasswordElem.value == NewPasswordCheckElem.value)
            {
                if(NewPasswordElem.value != PasswordElem.value)
                {
                    if(RestrictedString.indexOf(NewPasswordElem.value.toLowerCase()) == -1)
                    {
                        PasswordElem.value = hex_sha1(KeyElem.value+hex_sha1(HashString+PasswordElem.value));
                        NewPasswordElem.value = hex_sha1(HashString+NewPasswordElem.value);
                        NewPasswordElem.value = CalcXor(PasswordElem.value, NewPasswordElem.value);
                        NewPasswordCheckElem.value = NewPasswordElem.value;

                        return true;
                    }
                    else
                    {
                        alert("That value isn't allowed. Please re-enter")
                    }
                }
                else
                {
                    alert("Password hasn't been changed. Please re-enter")
                }
            }
            else
            {
                alert("New password re-entered incorrectly")
            }
        }
        else
        {
            alert("New Password isn't long enough")
        }
    }
    else
    {
        alert("No value entered for old password")
    }

    return false;
}

