﻿function createEmailVerificationPopUp(email, mode) {
    mode = parseInt(mode);

    var container = document.createElement("div");
    container.style.position = "absolute";
    container.id = "lostPasswordPopUp";
    container.style.zIndex = "5000";
    main = createTable(360);
    container.appendChild(main);
    main.createRows(1, 2);
    main.className = "ReferenceDialog";


    main.rowArray[0].cellArray[0].className = "TACContentDivHeader";
    main.rowArray[0].cellArray[0].style.padding = "10px";
    main.rowArray[0].cellArray[0].innerHTML = "Email Verification Required";

    var close = createAnchor("Close");

    close.onclick = function() {
        destroyObject(container);
    }

    main.rowArray[0].cellArray[1].style.padding = "10px";
    main.rowArray[0].cellArray[1].style.textAlign = "right";
    main.rowArray[0].cellArray[1].appendChild(close);

    var uneRow = main.createRow(1);
    uneRow.cellArray[0].colSpan = "2";
    uneRow.cellArray[0].style.padding = "0px 10px 10px 10px";
    uneRow.cellArray[0].style.textAlign = "justify";
    uneRow.cellArray[0].innerHTML = "Your email address needs to be validated before you can log into the site.<br><br>The current email on file is:";

    var tbRow = main.createRow(1);
    tbRow.cellArray[0].colSpan = "2";
    tbRow.cellArray[0].style.textAlign = "center";

    var txt = document.createElement("input");
    txt.id = "emailVerificationPopUp_Email"
    txt.className = "TextFields150px";
    tbRow.cellArray[0].appendChild(txt);
    txt.value = email;

    txt.onkeypress = function() {
        //return action_onkeypress(e, "recoverPassword()");
    }

    var statusRow = main.createRow(1);
    statusRow.cellArray[0].style.height = "20px";
    statusRow.cellArray[0].className = "OrangeText";
    statusRow.cellArray[0].style.textAlign = "center";
    statusRow.cellArray[0].id = "emailVerificationPopUp_status"
    statusRow.cellArray[0].colSpan = "2";


    var sRow = main.createRow(1);
    sRow.cellArray[0].colSpan = "2";
    sRow.cellArray[0].style.paddingBottom = "10px";
    sRow.cellArray[0].style.textAlign = "center";



    var submit = document.createElement("img");

    submit.onclick = function() {
        verifyEmail(mode);
    }

    submit.onmouseover = function() {
        image_onmouseover(this);
    }

    submit.style.marginTop = "10px";

    switch (mode) {
        case 1:
            sRow.cellArray[0].innerHTML = "Clicking submit will send a verification email to the email address above.<br>"
            break;
        case 2:
            sRow.cellArray[0].innerHTML = "Clicking submit will authorize an attempt to automatically verify the email address above.<br>"
            break;
        default:

    }


    SI.src("btForms_Submit.png", submit, SiteImagesType.BaseColorShadeLanguage)
    sRow.cellArray[0].appendChild(submit);

    document.body.appendChild(container);

    var ah = document.getElementById("Default_Header_MainTable")

    DockToObject(container, ah, "center", "top", "center", 0, 10);

    if (parseInt(container.style.top) < 10)
        container.style.top = "10px";

}


function verifyEmail(mode) {
    var txt = document.getElementById("emailVerificationPopUp_Email");
    var status = document.getElementById("emailVerificationPopUp_status");

    var UserName = document.getElementById("Default_Header_Login_UserName");
    var Password = document.getElementById("Default_Header_Login_Password");

    if (!txt) return;
    if (!status) return;

    var sendData = "email=" + txt.value;
    sendData += "&username=" + UserName.value;
    sendData += "&password=" + Password.value;

    status.innerHTML = "Submitting Request...";

    this.readyFunction = function(responseText) {

        var code = parseInt(responseText)
        switch (code) {

            case 2:
                status.innerHTML = "Please check your email for the verification message."
                break;
            case 1:
                status.innerHTML = "Your email has been verified, please close this window and login."
                break;

            case -1:
                status.innerHTML = "Missing Username."
                break;
            case -2:
                status.innerHTML = "Missing Password."
                break;
            case -3:
                status.innerHTML = "Missing Email Address."
                break;
            case -4:
            case -5:
                status.innerHTML = "Account not found, please close this window and try to login again."
                break;
            case -6:
                status.innerHTML = "Email server reports mailbox doesn't exist, please choose another email address."
                break;
            case -7:
                status.innerHTML = "Automated attempt to verify email rejected, please try again later."
                break;
            case -8:
            case -9:
            case -10:
                status.innerHTML = "Error sending verification message, please try again later."
                break;
            case -11:
                status.innerHTML = "Email address already exists, please choose another."
                break;
            default:
                status.innerHTML = "Error verify email, please contact technical support.";
        }
    }

    AJAXPost(top.document.getElementById("websiteURL").value + "/data/EmailVerification.aspx", "AddParams[1].readyFunction", sendData, this);
}
