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

function getNetworkPort() {
    $.ajax({
        url: config.baseUrl + "/Network/Port",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_xml_doc = xmlDoc;
            $('#HTTPport').val($(xmlDoc).find('Port').eq(0).find('HTTP').eq(0).text());
            $('#Commandport').val($(xmlDoc).find('Port').eq(0).find('Command').eq(0).text());
            $('#Mediaport').val($(xmlDoc).find('Port').eq(0).find('Media').eq(0).text());

        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function setNetworkPort() {
    $(gs_xml_doc).find('Port').eq(0).find('Command').eq(0).text($("#Commandport").val());
    $(gs_xml_doc).find('Port').eq(0).find('HTTP').eq(0).text($("#HTTPport").val());
    $(gs_xml_doc).find('Port').eq(0).find('Media').eq(0).text($("#Mediaport").val());
    var cyXml = gs_xml_doc;
    var cyURL = config.baseUrl + "/Network/Port";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: true,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr) {
            var state=   SaveState(xhr);
            if ("0" == state&&(sessionStorage.getItem("xystyle") == "p6s"||sessionStorage.getItem("xystyle") == "p2p")){
                var data={
                    key:config.NOTIFY_KEYS.NETWORK_PORT,
                    value:xmlObj2XmlStr(cyXml),
                    id:config.p2pId
                }
                notifyVms(data);
            }
        },
        error: function(xhr) {
            $.optionBootoast($.i18n.prop('i18n-savefail'), "danger")
        }
    });
}
