﻿/* sets the save new user button enabled only if conditions are met */
function SetRegisterButton(obj) {
    var pb = document.getElementById('btnRegister');
    if (pb == null) return;
    if (obj != null && obj.checked) {
        pb.disabled = false;
        pb.src = '/Images/register.jpg';
        pb.style.cursor = 'pointer';
    } else {
        pb.disabled = true;
        pb.src = '/Images/register_disabled.jpg';
        pb.style.cursor = 'default';
    }
}

/* used when trying to register a new user */
function CheckPostItems() {
    // check if this button can be clicked
    var chkbox = document.getElementById("CheckBoxRegister");
    if (chkbox == null || !chkbox.checked) return;
    
    // hide the error message if it is active
    var errMsgObj = GetContentHolderItem('lblErrorMessage');
    if (errMsgObj != null && errMsgObj.style != null) errMsgObj.style.display = 'none';
    errMsgObj.innerHTML = 'Please complete all the required fields in order to register.';
    var okToSubmit = 0;
    // check for an email address
    if (CheckForBlankItem('txtEmailAddress', 'lblEmailAddress')) okToSubmit = 1;
    // check for a password
    if (!CheckPasswordsMatch(errMsgObj)) okToSubmit = 1;
    if (okToSubmit == 0) {
        __doPostBack('Register', '');
        return true;
    }
    // show the error message
    if (errMsgObj != null && errMsgObj.style != null) errMsgObj.style.display = '';    

    // that it
    return false;
}

/* Check for blank passwords */
function CheckPasswordsMatch(errMsgObj) {
    // check for a password
    if (CheckForBlankItem('txtPassword', 'lblPassword')) return false;
    if (CheckForBlankItem('txtPassword2', 'lblPassword2')) return false;
    // check that the passwords match
    var showError = 0;
    var ps1 = GetContentHolderItem('txtPassword');
    var ps2 = GetContentHolderItem('txtPassword2');
    if (ps1 == null || ps2 == null) showError=1;
    else if (ps1.value == null || ps2.value != ps1.value) showError=1;
    if (showError == 1) {
        // passwords do not match
        ShowValidationError(ps1, 'lblPassword');
        ShowValidationError(ps2, 'lblPassword2');
        // set the validation error message
        errMsgObj.innerHTML = 'The passwords you have entered do not match.'
        return false;
    } else {
        // check that the password length is correct
        if (ps1.value.length < 6) {
            // passwords are too short
            ShowValidationError(ps1, 'lblPassword');
            ShowValidationError(ps2, 'lblPassword2');
            errMsgObj.innerHTML = 'The password you have entered is too short.'
            return false;
        }
    }
    return true;
}

/* used when trying to update a user */
function CheckUserUpdate() {
    // hide the error message if it is active
    var errMsgObj = GetContentHolderItem('lblErrorMessage');
    if (errMsgObj != null && errMsgObj.style != null) errMsgObj.style.display = 'none';
    errMsgObj.innerHTML = 'Please complete all the required fields.';
    var okToSubmit = 0;
    // check for an email address
    if (CheckForBlankItem('txtEmailAddress', 'lblEmailAddress')) {
        errMsgObj.innerHTML = 'A valid email address is a required field.';
        okToSubmit = 1;
    }
    // check if a password change is requested
    var pass1 = GetContentHolderItem('txtPassword');
    var pass2 = GetContentHolderItem('txtPassword2');
    if ((pass1 != null && pass1.value != '') || (pass2 != null && pass2.value != '')) {
        // check that the old password was specified
        if (CheckForBlankItem('txtOldPassword', 'lblOldPassword')) okToSubmit = 1;
        else if (!CheckPasswordsMatch(errMsgObj)) okToSubmit = 1;
    }

    if (okToSubmit == 0) {
        __doPostBack('UpdateUser', '');
        return true;
    }
    // show the error message
    if (errMsgObj != null && errMsgObj.style != null) errMsgObj.style.display = '';

    // that it
    return false;
}

/* OnRegister mouse over events */
function OnRegisterMouseOver(obj) {
    var chkbox = document.getElementById("CheckBoxRegister");
    if (chkbox == null) return;

    if (chkbox.checked) {
        obj.src = '/Images/register_dn.jpg';
        obj.style.cursor = 'pointer';
    } else {
        obj.src = '/Images/register_disabled.jpg';
        obj.style.cursor = 'default';
    }
}
function OnRegisterMouseOut(obj) {
    var chkbox = document.getElementById("CheckBoxRegister");
    if (chkbox == null) return;

    if (chkbox.checked) {
        obj.src = '/Images/register.jpg';
        obj.style.cursor = 'pointer';
    } else {
        obj.src = '/Images/register_disabled.jpg';
        obj.style.cursor = 'default';
    }
}


