var gs_xml_doc, user = config.user,
    pwd = config.password;
$(document).ready(function() {
    getProactivelyCfg();
    $('#portSave').click(function() {
        if ("admin" == user || "super_admin" == user) {
            setProactivelyCfg();
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });
})

function getProactivelyCfg() {
    $.ajax({
        url: config.baseUrl + "/System/ActiveRegistCfgUIDesign",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_xml_doc = xmlDoc;
            $('#zhuport').val($(xmlDoc).find('ActiveRegistCfgUIDesign').eq(0).find('Port').eq(0).text());
            $('#zhuhost').val($(xmlDoc).find('ActiveRegistCfgUIDesign').eq(0).find('DeviceDomain').eq(0).text());
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function setProactivelyCfg() {
    var tag = /^([1-9](\d{0,3}))$|^([1-5]\d{4})$|^(6[0-4]\d{3})$|^(65[0-4]\d{2})$|^(655[0-2]\d)$|^(6553[0-5])$/
    hzc = tag.test($("#zhuport").val())
    if (hzc == true) {
        $(gs_xml_doc).find('ActiveRegistCfgUIDesign').eq(0).find('Port').eq(0).text($("#zhuport").val());
        $(gs_xml_doc).find('ActiveRegistCfgUIDesign').eq(0).find('DeviceDomain').eq(0).text($("#zhuhost").val());
        var cyXml = gs_xml_doc;
        var cyURL = config.baseUrl + "/System/ActiveRegistCfgUIDesign";
        $.ajax({
            type: "PUT",
            url: cyURL,
            async: true,
            processData: false,
            data: cyXml,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("If-Modified-Since", "0");
            },
            complete: function(xhr) {
                SaveState(xhr);
            },
            error: function(xhr) {
                $.optionBootoast($.i18n.prop('i18n-savefail'), "danger")
            }
        });
    } else {
        $.optionBootoast($.i18n.prop('i18n-savefail'), "danger")
        return
    }
}