/*
     $Revision$
     $Id$
*/

// Afgekeken van de vroegere protocolChange()
function onChangeAddress() {
    var ordermode = $("#ordermode").val();
    var protocol = getProtocolFromAddress($("#address").val());
    var options = getOptionsFromProtocol(protocol);
    // Pas nu de ordermode-<select> aan met relevante opties/labels
    $("#ordermode").empty();
    options.forEach(option => {
        $("#ordermode").append(`<option value="${option.value}">${option.label}</option>`);
    });
    $("#ordermode").val(ordermode);
}

function getProtocolFromAddress(address) {
    var protocol = "";
    if (typeof address !== "string") {
        return protocol;
    }
    var arr = address.split(":");
    if (arr.length == 1 && email_regexp.test(address)) {
        protocol = "mailto:";
        $("#address").val(protocol + address);
    } else if (arr.length > 1) {
        switch (arr[0]) {
            case "mailto":
            case "file":   protocol = arr[0] + ":"; break;
            case "https":
            case "http":
            case "ftp":
            case "sftp":   protocol = arr[0] + "://"; break;
        }
    }
    return protocol
}

function getOptionsFromProtocol(protocol) {
    var options= [];
    if (protocol == "mailto:") {
        options = [
            { value: '0', label: L("lcl_mail_ordermode0") },
            { value: '1', label: L("lcl_mail_ordermode1") }
        ];
    } else if (protocol == "https://") {
        // TODO: dit staat bij PutOrders, wat zijn dan netto nog de opties?
        // p_bedrijfadres.ordermode
        //              bit 0 [0=xml, 1 soap] (1)
        //              bit 1 [alleen voor mail attachment encryptie] (2)
        //              bit 2 [0=2*base64 encoding, 1=plain] (4)  JGL Aug-2016: komt volgens mij in de praktijk niet voor
        //              bit 3 [0=xml tag encoding, 1=plain] (8)
        options = [
            { value: '0',  label: L("lcl_https_ordermode0") },
            { value: '1',  label: L("lcl_https_ordermode1") },
            { value: '5',  label: L("lcl_https_ordermode5") },  // 4+1
            { value: '13', label: L("lcl_https_ordermode13") }  // 8+4+1
        ];
    }
    return options;
}

$(_ => {
    $("#address").on("change", onChangeAddress);
    onChangeAddress();
});

function resetLockSecret(p_bedr_adr_key)
{
    FcltMgr.confirm(L("lcl_prs_bedrijfadres_del_locksecret"), function() {
            var data = {bdr_adr_key: p_bedr_adr_key};
            protectRequest.dataToken(data);
            var url = "../prs/prs_update_ajax.asp?req_info=bedrijfadres";
            $.post(url, data, FcltCallback, "json")
        });
}