var userID = "";
var srvID = "";
var ifrm;
var _frmData = {
    id: '',
    srvid: ''
};
$(function () {
    $('[data-toggle="popover"]').popover();
    GetGSHY();
    GetList("2DDEF2AA-2E8C-4705-9F9E-8C2C71117063", "sheng");
    $("#sheng").change(function () {
        GetList($(this).val(), "shi");
        $('#ComAddr').val('');
        if ($(this).find("option:selected").text()!="请选择") {
            $('#ComAddr').val( $(this).find("option:selected").text());
        }
        
    });
    $("#shi").change(function () {
        GetList($(this).val(), "qu");
        if ($(this).find("option:selected").text() != "请选择") {
            $('#ComAddr').val($('#sheng').find("option:selected").text() + $(this).find("option:selected").text());
        }
    });
    $("#qu").change(function () {
        if ($(this).find("option:selected").text() != "请选择") {
            $('#ComAddr').val($('#sheng').find("option:selected").text() + $("#shi").find("option:selected").text() + $(this).find("option:selected").text());
        }
    });
    $("#ComYm").focus(function () {
        $("#urlerror").hide();
    })
});

function GetList(contry, id) {
    $.ajax({
        dataType: 'json',
        type: "get",
        url: "../../Handlers/CustomerInformation/PublicManagement/PublicMg.aspx?Method=loadZone",
        async: false,
        data: {
            contry: contry
        },
        success: function (jsons) {
            var droplist = $("#" + id + "");
            $("#" + id + " option[value!='']").remove();
            for (i = 0; i < jsons.length; i++) {
                $option = $("<option></option>");
                $option.attr("value", jsons[i].ID);
                $option.text(jsons[i].Name);
                droplist.append($option)
            }
        },
        error: function () {
            layer.msg("网络错误，不能加载地区信息！", 2, 5)
        }
    })
}
function resize_r() {
    var s = 0;
    var ss = $(window.parent.document).find('iframe[id=' + ifrm + ']');
    var l = $(window.parent.document).height() - ss.offset().top;
    var t = $(document).height();
    if (l > t) {
        s = l
    } else {
        s = $(document).height()
    }
    $($(window.parent.document).find('iframe[id=' + ifrm + ']')).height(s);
    window.parent.sidebar_r()
}
function iniParms() {
    var coks = document.cookie.substring(document.cookie.indexOf('userinf_tcwl'));
    var ary = coks.split(';');
    var ary1 = ary[0].split('=');
    if (!ary1[2]) {
        $('html').empty();
        return;
    }
    userID = ary1[2].split('&')[0];
    srvID = ary1[3];
    var s = window.location.href.split('=');
    if (s.length == 4) {
        ifrm = s[3]
    }
    resize_r();
    $('#fileloglogo').uploadify({
        'auto': true,
        'buttonCursor': 'hand',
        'buttonText': '选择图片',
        'height': 30,
        'width': 100,
        'debug': false,
        'multi': false,
        'fileTypeDesc': '描述文件类型',
        'fileTypeExts': '*.gif; *.jpg; *.png',
        'preventCaching': true,
        'queueID': 'fileQueue',
        'removeTimeout': 1,
        'swf': '../Scripts/Uploadify/uploadify.swf',
        'uploader': '../Handlers/system/SetInfo.aspx?Method=upload',
        'fileSizeLimit': '300KB',
        'formData': {
            srvid: srvID
        },
        'folder': 'UploadFile',
        'cancelImage': '../Scripts/Uploadify/uploadify-cancel.png',
        'onUploadSuccess': function (file, data, response) {
            $("#imgloglogo").attr("src", data + "?r" + Math.random());
            $("#loglogo").val(data);
        },
        'onUploadComplete': function (file) { },
        'onQueueComplete': function (stats) { },
        'onSelect': function (file) {
            $("#fileloglogo").uploadify("settings", "formData", {
                "srv": srvID
            });
        }
    });
    $('#filelogleft').uploadify({
        'auto': true,
        'buttonCursor': 'hand',
        'buttonText': '选择图片',
        'height': 30,
        'width': 100,

        'debug': false,
        'multi': false,
        'fileTypeDesc': '描述文件类型',
        'fileTypeExts': '*.gif; *.jpg; *.png',
        'preventCaching': true,
        'queueID': 'fileQueue',
        'removeTimeout': 1,
        'swf': '../Scripts/Uploadify/uploadify.swf',
        'uploader': '../Handlers/system/SetInfo.aspx?Method=upload',
        'fileSizeLimit': '300KB',
        'formData': {
            srvid: srvID
        },
        'folder': 'UploadFile',
        'cancelImage': '../Scripts/Uploadify/uploadify-cancel.png',
        'onUploadSuccess': function (file, data, response) {
            $("#imglogleft").attr("src", data + "?r" + Math.random());
            $("#log_img").val(data);
        },
        'onUploadComplete': function (file) { },
        'onQueueComplete': function (stats) { },
        'onSelect': function (file) {
            $("#filelogleft").uploadify("settings", "formData", {
                "srv": srvID
            });
        }
    });
    $('#uploadify').uploadify({
        'auto': true,
        'buttonCursor': 'hand',
        'buttonText': '选择图片',
        'height': 30,
        'width': 100,
        'debug': false,
        'multi': false,
        'fileTypeDesc': '描述文件类型',
        'fileTypeExts': '*.gif; *.jpg; *.png',
        'preventCaching': true,
        'queueID': 'fileQueue',
        'removeTimeout': 1,
        'swf': '../Scripts/Uploadify/uploadify.swf',
        'uploader': '../Handlers/system/SetInfo.aspx?Method=upload',
        'fileSizeLimit': '300KB',
        'formData': _frmData,
        'folder': 'UploadFile',
        'cancelImage': '../Scripts/Uploadify/uploadify-cancel.png',
        'onUploadSuccess': function (file, data, response) {
            var mc = data.split('/')[7];
            $("#comlogo").val(data);
            $("#comimg").attr("src", data)
        },
        'onUploadComplete': function (file) { },
        'onQueueComplete': function (stats) { },
        'onSelect': function (file) {
            _frmData.id = "khid";
            _frmData.srvid = srvID;
            $("#uploadify").uploadify("settings", "formData", {
                "srv": srvID
            })
        }
    });

    $.ajax({ type: "get",
        url: "../Handlers/system/SetInfo.aspx?Method=getC",
        cache: false,
        dataType: "text",
        success: function (data) {
            $('.ck').find('input[type="radio"]').each(function (i, e) {
                if ($(e).attr('value') == data) {
                    $(e).attr('checked',true);
                }
            });
        }, error: function () {
            layer.msg('网络请求错误!', 2, 5);
        }
    });

    $.ajax({
        type: "get",
        url: "../Handlers/system/SetInfo.aspx?Method=Load&srvid=" + srvID + "&userid=" + userID + "",
        cache: false,
        dataType: "json",
        success: function (data) {
            getCopy();
            $("#ComName").val(data.rows[0].Name);
            $("#comimg").attr("src", data.rows[0].Logo);
            $("#imglogleft").attr("src", data.rows[0].Log_Img);
            $("#comlogo").val(data.rows[0].Logo);
            $("#loglogo").val(data.rows[0].Log_logo);
            $("#imgloglogo").attr("src", data.rows[0].Log_logo);
            $("#log_img").val(data.rows[0].Log_Img);
            if (data.rows[0].DomainName != "") {
                $("#ComYm").val(data.rows[0].DomainName)
            } else {
                $("#ComYm").val("http://")
            }
            $("#ComAddr").val(data.rows[0].Address);
            $("#ComLink").val(data.rows[0].Contacts);
            $("#ComPhone").val(data.rows[0].phone);
            $("#ComSort  option[value=" + data.rows[0].sort + "]").attr("selected", true);
            var area = data.rows[0].Area;
            if (area != "") {
                var sheng = area.split(",")[0];
                var shi = area.split(",")[1];
                var qu = area.split(",")[2];
                GetList(sheng, "shi");
                GetList(shi, "qu");
                $("#sheng option[value=" + sheng + "]").attr("selected", true);
                $("#shi option[value=" + shi + "]").attr("selected", true);
                $("#qu option[value=" + qu + "]").attr("selected", true)
            }
            resize_r()
        },
        error: function () {
            layer.msg('网络请求错误!', 2, 5);
        }
    })
}
function Update() {
    $("#fmSet").validation();
    if ($(".popover").length > 0) {
        return
    }
    if (!CheckFm()) {
        return false
    }
    var url = $("#comimg").attr("src");
    var comname = $("#ComName").val();
    var comym = $("#ComYm").val();
    var l = layer.load("正在提交...");
    $.ajax({
        type: "post",
        url: "../Handlers/system/SetInfo.aspx?Method=Update&srvid=" + srvID + "&userid=" + userID + "",
        cache: false,
        data: $("#fmSet").serialize(),
        success: function (data) {
            if (data == "success") {
                layer.msg('修改成功！', 1, 1);
                parent.$("#cmpname").text(comname);
                parent.$("#adomain").attr("href", comym).attr("title", comym);
                parent.$("#log").attr("src", url);
            } else {
                layer.msg('修改失败!', 1, 8)
            }
        },
        error: function () {
            layer.close(l);
            layer.msg('网络请求错误!', 2, 5)
        }
    })
}
function CheckFm() {
    if ($("#sheng").val() == "") {
        $("#addrerror").show();
        return false
    } else {
        $("#addrerror").hide()
    }
    var value = $("#ComYm").val();
    urlreg = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    if (value != "" && value != "http://") {
        if (!urlreg.test(value)) {
            var reg = /^[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
            var re = new RegExp(reg);
            if (!re.test(value)) {
                $("#urlerror").show();
                return false
            } else {
                $("#ComYm").val("http://" + value)
            }
        }
    }
    return true
}
function CheckDomain() {
    var value = $("#ComYm").val();
    urlreg = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    if (value != "" && value != "http://") {
        if (!urlreg.test(value)) {
            var reg = /^[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
            var re = new RegExp(reg);
            if (!re.test(value)) {
                $("#urlerror").show();
                return false
            } else {
                $("#ComYm").val("http://" + value)
            }
        }
    }
}
function GetGSHY() {
    $.ajax({
        dataType: 'json',
        type: "get",
        async: false,
        url: "../Handlers/Registration/CompanyInformation.aspx?Method=gshy",
        success: function (jsons) {
            var droplist = $("#ComSort");
            $("#ComSort option[value!='']").remove();
            for (i = 0; i < jsons.rows.length; i++) {
                $option = $("<option></option>");
                $option.attr("value", jsons.rows[i].id);
                $option.text(jsons.rows[i].text);
                droplist.append($option)
            }
        },
        error: function () {
            layer.msg('网络错误,加载行业信息失败！', 2, 5)
        }
    })
}
function ToPic(logo) {
    $("#comimg").attr("src", logo);
    $("#comlogo").val(logo)
}

function getCopy() {
    $.ajax({
        type: "post",
        url: "../Handlers/system/SetInfo.aspx?Method=GetCopy",
        cache: false,
        success: function (data) {
            if (data != "no") {
                UM.getEditor('ComCopy').setContent(data);
            } else {
                //var d = '<div class="\&quot;copydh\&quot;" style="\&quot;font-family:" font-size:="" margin:="" padding:="" 13px="" 0px="" color:="" line-height:="" white-space:="" background-color:=""><div class="copydh" style="text-align: center; font-family: Arial; font-size: 12px; margin: 0px; padding: 13px 0px 0px 16px; color: rgb(51, 51, 51); line-height: normal; white-space: normal; background-color: rgb(237, 239, 241);"><a href="http://www.yundiandi.cn" target="_blank" title="关于云点滴">关于云点滴</a>&nbsp;&nbsp;&nbsp;&nbsp;| &nbsp; &nbsp;<a href="http://www.yundiandi.com/Help/reg.htm" target="_blank" title="云点滴帮助">云点滴帮助</a>&nbsp; &nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.yundiandi.cn/jianjie.html" target="_blank" title="云点滴价格">云点滴价格&nbsp;</a> &nbsp;</div><div class="copycon" style="font-family: Arial; font-size: 12px; margin: 0px; padding: 8px 0px 0px 16px; color: rgb(51, 51, 51); text-align: center; line-height: 20px; white-space: normal; background-color: rgb(237, 239, 241);"><p style="margin-top: 0px; margin-bottom: 0px; padding-bottom: 1em; line-height: 18px;">Copyright @ 云点滴技术团队 (<a href="http://www.hntcwl.com/" style="color: rgb(51, 51, 51);">&nbsp;</a><a href="http://www.yundiandi.com/" target="_blank">www.yundiandi.com&nbsp;</a>), All Rights Reserved<br/></p></div>';
                var d = '<div class="\&quot;copydh\&quot;" style="\&quot;font-family:" font-size:="" margin:="" padding:="" 13px="" 0px="" color:="" line-height:="" white-space:="" background-color:=""><div class="copydh" style="text-align: center; font-family: Arial; font-size: 12px; margin: 0px; padding: 13px 0px 0px 16px; color: #d7d7d7; line-height: normal; white-space: normal; "><a href="http://www.yundiandi.cn" target="_blank" title="关于云点滴">关于云点滴</a>&nbsp;&nbsp;&nbsp;&nbsp;| &nbsp; &nbsp;<a href="http://www.yundiandi.com/Help/reg.htm" target="_blank" title="云点滴帮助">云点滴帮助</a>&nbsp; &nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.yundiandi.cn/jianjie.html" target="_blank" title="云点滴价格">云点滴价格&nbsp;</a> &nbsp;</div><div class="copycon" style="font-family: Arial; font-size: 12px; margin: 0px; padding: 8px 0px 0px 16px; color: rgb(51, 51, 51); text-align: center; line-height: 20px; white-space: normal; color:#d7d7d7;"><p style="margin-top: 0px; margin-bottom: 0px; padding-bottom: 1em; line-height: 18px;">Copyright @ 云点滴技术团队 (<a href="http://www.hntcwl.com/" style="color: rgb(51, 51, 51);">&nbsp;</a><a href="http://www.yundiandi.com/" target="_blank">www.yundiandi.com&nbsp;</a>), All Rights Reserved<br/></p></div>';
                UM.getEditor('ComCopy').setContent(d);
            }
        },
        error: function () { }
    });
}