function showLogon()
{
    document.getElementById('logonName').value = "";
    document.getElementById('logonPassword').value = "";
    showdivbyid('logonBox', 'block');
    document.getElementById('logonName').focus();
}

function loginDCMD(username, pwd)
{
    createXMLHttpRequest();
    var url = "LogonAction.do";
    xmlHttpRequest.onreadystatechange = callbackLogin;
    xmlHttpRequest.open("POST", url, true);
    xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var params = "logonName=" + username + "&logonPassword=" + pwd;
    xmlHttpRequest.send(params);
}

function callbackLogin()
{
    if (xmlHttpRequest)
    {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var rtn = xmlHttpRequest.responseText;
                if (rtn.indexOf("OK") > -1)
                {
                    window.location.reload();
                }
                else
                {
                    showDlgMessage("error", rtn);
                }
            }
        }
    }
}

function showForgotPW()
{
    document.getElementById('forgotEmail').value = "";
    showdivbyid('forgotPasswordDiv', 'block');
    document.getElementById('forgotEmail').focus();
}

function forgotPwdDCMD(emaila)
{
    if (trim(emaila).length == 0)
    {
        showDlgMessage("errorForgotPW", "Please enter an email address.");
        return false;
    }

    if (trim(emaila).length > 0 && !validateEmail(emaila))
    {
        showDlgMessage("errorForgotPW", "Please enter a valid email address.");
        return false;
    }

    createXMLHttpRequest();
    var url = "RegistrationAction.do";

    xmlHttpRequest.onreadystatechange = callbackForgotPW;
    xmlHttpRequest.open("POST", url, true);
    xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var params = "email=" + emaila + "&command=resetPassword";
    xmlHttpRequest.send(params);
}

function callbackForgotPW()
{
    if (xmlHttpRequest)
    {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var rtn = xmlHttpRequest.responseText;
                if (rtn.indexOf("OK") > -1)
                {

                    showdivbyid('forgotPasswordConfirmationDiv', 'block');
                    document.getElementById("forgotStatusConfirm").innerHTML = rtn.substring(rtn.indexOf("OK") + 3);
                    hide('forgotPasswordDiv');
                } else
                {
                    showDlgMessage("errorForgotPW", rtn);
                }
            }
        }
    }
}

function initRegistartionOverlay() {
    if (document.getElementById("openRegistrationOverlay") != null) {
        var needWidth = 648;
        var needHeight = 490;

        document.getElementById("openRegistrationOverlay").href =
        "RegistrationAction.do?keepThis=true&TB_iframe=true&height=550&width=648&modal=true";
    }
}

function showRegister() {
    showdivbyid('registerBox', 'block');
    clearFieldsRegister();
}

function registerDCMD(username, pwd, fn, ln, em, ag)
{
    createXMLHttpRequest();
    var url = "RegistrationAction.do";

    xmlHttpRequest.onreadystatechange = callbackRegister;
    xmlHttpRequest.open("POST", url, true);
    xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var params = "modal=yes&username=" + username + "&password=" + pwd +
                 "&firstname=" + fn + "&lastname=" + ln + "&email=" + em + 
                 "&agreedOnTermsOfUse=" + ag;
    xmlHttpRequest.send(params);
}

function callbackRegister()
{
    if (xmlHttpRequest)
    {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var rtn = xmlHttpRequest.responseText;
                if (rtn.indexOf("OK") > -1)
                {
                    window.location.reload();
                }
                else
                {
                    showDlgMessage("errorReg", rtn);
                }
            }
        }
    }
}

function clearFieldsRegister()
{
    var divMessage = document.getElementById("errorReg");
    if (divMessage)divMessage.innerHTML = "";

    var form = document.getElementById("registrationForm");
    if (form) form.reset();
}


function loadUser(username)
{
    document.forms['registrationForm'].username.value = username;
    document.forms['registrationForm'].password.value = '';
}

function registerModal()
{
    var regForm = document.forms['registrationForm'];
    if (verifyFields())
        registerDCMD(regForm.username.value, regForm.password.value, regForm.firstname.value,
                regForm.lastname.value, regForm.email.value, regForm.agreedOnTermsOfUse.value);
}

function verifyFields()
{
    var errorId = "errorReg";
    var form = document.forms['registrationForm'];
    var email = form.email.value;

    if (trim(email).length > 0 && !validateEmail(email))
    {
        showDlgMessage(errorId, "Please enter a valid email address.");
        return false;
    }

    if (form.command.value == 'register')
    {
        if (form.username.value == '') {
            showDlgMessage(errorId, "Please enter a username.");
            return false;
        }

        if (form.password.value == '') {
            divMessage.innerHTML = "Please enter a password";
            return false;
        }

        if (form.password.value != form.passwordVerify.value) {
            showDlgMessage(errorId, "Please re-verify your password.");
            return false;
        }

        if (form.agreedOnTermsOfUse.value != "true") {
            showDlgMessage(errorId,
                    "Please agree with terms of use and privacy agreement in order to register.");
            return false;
        }
    }

    //	registerUserForForum(form.username.value, form.password.value, form.email.value);

    return true;
}

function fillAgreed()
{
    document.forms['registrationForm'].agreedOnTermsOfUse.value = "true";
}

function updateUser()
{
    if (verifyFields())
    {
        createXMLHttpRequest();
        var url = "UserAccount.do";
        var form = document.forms['registrationForm'];
        var command = form.command.value;
        var fname = form.firstname.value;
        var lname = form.lastname.value;
        var email = form.email.value;

        xmlHttpRequest.onreadystatechange = callbackUserUpdate;
        xmlHttpRequest.open("POST", url, true);
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var params = "command=" + command + "&firstname=" + fname + "&lastname=" + lname + "&email=" + email;
        params += "&iMedical=" + document.getElementById("iMedical").value;
        var userOccupations = document.getElementById("userOccupations");
        if (userOccupations.selectedIndex > -1) {
            params += userOccupations.options[userOccupations.selectedIndex].value;
        }
        xmlHttpRequest.send(params);
    }
}
function callbackUserUpdate()
{
    if (xmlHttpRequest)
    {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var rtn = xmlHttpRequest.responseText;
                hide('registerBox');
//                showMessageBox("Your information has been changed.");
                window.location.reload(false);
            }
        }
    }
}

function getUserOccupations(divId, selectOccpupationId) {
    createXMLHttpRequest();
    var url = "userOccupations.jsp?selectOccupationId=" + selectOccpupationId;

    xmlHttpRequest.onreadystatechange = function () {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                document.getElementById(divId).innerHTML = xmlHttpRequest.responseText;
            }
        }
    }
    xmlHttpRequest.open("POST", url, true);
    xmlHttpRequest.send("");
}

function resendVerificationEmail() {
    createXMLHttpRequest();
    xmlHttpRequest.onreadystatechange = function () {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
            }
        }
    }
    xmlHttpRequest.open("POST", "resendEmailVerifi.do", true);
    xmlHttpRequest.send("");
}

function MM_swapImgRestore() { //v3.0
    var i,x,a = document.MM_sr;
    for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if (!d) d = document;
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document;
        n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n];
    for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n);
    return x;
}

function MM_swapImage() { //v3.0
    var i,j = 0,x,a = MM_swapImage.arguments;
    document.MM_sr = new Array;
    for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) {
            document.MM_sr[j++] = x;
            if (!x.oSrc) x.oSrc = x.src;
            x.src = a[i + 2];
        }
}

function MM_swapImage1(img, addiotionSrc) {
    if (!img.additionSrc) {
        img.additionSrc = addiotionSrc;
    }
    var tempSrc = img.src;
    img.src = img.additionSrc;
    img.additionSrc = tempSrc;
}

function isReturnKey(e)
{
    var bReturn = false;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    } else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    if (keynum == 13)
    {
        bReturn = true;
    }

    return bReturn;
}

function clickOnEnterKey(id, e)
{
    if (isReturnKey(e))
    {
        obj = document.getElementById(id);
        if (obj != null) obj.onclick();
    }
}

function moveOnEnter(field, e)
{
    if (isReturnKey(e))
    {
        document.getElementById(field).focus();
        return false;
    }
}

function showBetaFeedback()
{
    scroll(0, 0);
    document.getElementById('feedbackBoxcontactFirst').value = "";
    document.getElementById('feedbackBoxcontactLast').value = "";
    document.getElementById('feedbackBoxcontactEmail').value = "";
    showdivbyid('feedbackBox', 'block');
    document.getElementById('feedbackBoxcontactFirst').focus();
}
function showContactUs()
{
    scroll(0, 0);
    document.getElementById('contactBoxcontactFirst').value = "";
    document.getElementById('contactBoxcontactLast').value = "";
    document.getElementById('contactBoxcontactEmail').value = "";
    showdivbyid('contactBox', 'block');
    document.getElementById('contactBoxcontactFirst').focus();
}
function sendUserMessage(typeObj, firstnameObj, lastnameObj, emailObj, commentsObj, errorId)
{
    var type = typeObj.value;
    var firstname = firstnameObj.value;
    var lastname = lastnameObj.value;
    var email = emailObj.value;
    var comments = commentsObj.value;

    if (trim(email).length > 0 && !validateEmail(email))
    {
        showDlgMessage(errorId, "Please enter a valid email address.");
        emailObj.focus();
        return false;
    }
    else if (trim(comments).length == 0)
    {
        showDlgMessage(errorId, "Please enter a comment.");
        commentsObj.focus();
        return false;
    }

    createXMLHttpRequest();
    var url = "Feedback.do";

    xmlHttpRequest.onreadystatechange = callbackUserMessage;
    xmlHttpRequest.open("POST", url, true);
    xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var params = "feedbackType=" + type + "&fName=" + firstname + "&lName=" + lastname +
                 "&email=" + email + "&comments=" + comments;
    xmlHttpRequest.send(params);
}

function callbackUserMessage()
{
    if (xmlHttpRequest)
    {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var rtn = xmlHttpRequest.responseText;
                hide('contactBox');
                showMessageBox(rtn);
            }
        }
    }
}

function showPwdBox()
{
    showdivbyid('pwdBox', 'block');
    document.getElementById('passwd').focus();
}

function verifyPwdFields()
{
    var pwd = document.getElementById('passwd');
    var newPwd = document.getElementById('newPwd');
    var newPwdV = document.getElementById('newPwdV');
    var divMessage = document.getElementById("errorPwd");

    if (pwd.value == '') {
        pwd.focus();
        showDlgMessage("errorPwd", "Please enter your existing password.");
        return false;
    }
    else if (newPwd.value == '') {
        newPwd.focus();
        showDlgMessage("errorPwd", "Please enter a new password.");
        return false;
    }
    else if (newPwdV.value == '') {
        newPwdV.focus();
        showDlgMessage("errorPwd", "Please verify your new password.");
        return false;
    }
    else if (newPwd.value != newPwdV.value) {
        newPwdV.focus();
        showDlgMessage("errorPwd",
                "Please re-verify your password. It doesn't match the new password.");
        return false;
    }

    return true;
}
function changePassword()
{
    if (verifyPwdFields())
    {
        var url = "UserAccount.do";
        var password = document.getElementById('passwd').value;
        var newPwd = document.getElementById('newPwd').value;
        createXMLHttpRequest();
        var url = "UserAccount.do";
        xmlHttpRequest.onreadystatechange = callbackChangePwd;
        xmlHttpRequest.open("POST", url, true);
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var params = "command=changePw&password=" + password + "&newPwd=" + newPwd;
        xmlHttpRequest.send(params);
    }
}

function callbackChangePwd()
{
    if (xmlHttpRequest)
    {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var rtn = xmlHttpRequest.responseText;

                if (rtn.indexOf("OK") > -1)
                {
                    hide('pwdBox');
                    showMessageBox(rtn.substring(rtn.indexOf("OK") + 3));
                }
                else
                {
                    showDlgMessage("errorPwd", rtn);
                }
            }
        }
    }
}

function showMessageBox(msg, onOK)
{
    scroll(0, 0);
    document.getElementById('dlgMessage').innerHTML = msg;

    if (onOK)
    {
        document.getElementById('messageBoxOK').onclick = onOK;
    }

    showdivbyid('messageBox', 'block');
}

function showMessageBoxWithoutUp(msg) {
    document.getElementById('dlgMessage').innerHTML = msg;
    showdivbyid('messageBox', 'block');
}

function showOkCancelBox(msg, onOK) {
    scroll(0, 0);
    document.getElementById('okcancelMessage').innerHTML = msg;
    document.getElementById('okcancelBoxOK').onclick = onOK;
    showdivbyid('okcancelBox', 'block');
}

function showDlgMessage(eid, value)
{
    var obj = document.getElementById(eid);
    obj.innerHTML = value;
    if (typeof(obj.style.opacity) != null) obj.style.opacity = 100;
    if (typeof(obj.style.filter) != null) {
        obj.style.filter = "alpha(opacity=100)";
    }
    show(eid);
    var newFunc = function () {
        fadeOut(eid);
    };
    setTimeout(newFunc, 3000);
}

function showLoginForSaveBox()
{
    var msg = "You must be logged in to save. " +
              "Please <a href='javascript:showLogon()'>login</a> or " +
              "<a href='RegistrationAction.do?keepThis=true&TB_iframe=true&height=680&width=350&modal=true' " +
              "class='thickbox'>register</a> before saving.";
    showMessageBox(msg);
}

function showYesNoBox(msg, yesFunction, noFunction)
{
    scroll(0, 0);
    document.getElementById('yesnoMessage').innerHTML = msg;
    document.getElementById('yesBtn').onclick = yesFunction;
    document.getElementById('noBtn').onclick = noFunction;
    showdivbyid('yesnoBox', 'block');
}
