var serviceID = "";
var userID = "";
var menuData;
var skn = "";
var arrstep = '[{"title":"系统参数设置","txt":"系统参数设置","content":"设置您公司名称、联系人、联系电话、邮箱、公司地址以及公司行业等等，公司电话以及邮箱用于找回密码。"},{"title":"管理公司用户","txt":"用户管理","content":"设置您公司的员工信息。其中，员工信息中邮箱以及电话用于找回密码。"},{"title":"设置角色权限","txt":"角色管理","content":"设置您公司的员工使用权限。"},{"title":"设置组织机构","txt":"组织机构管理","content":"设置您公司的组织结构。"},{"title":"添加数据字典","txt":"数据字典","content":"设置系统中需要用到的计量单位以及关键字段。"},{"title":"添加产品信息","txt":"产品信息管理","content":"对公司的产品进行添加、修改等"},{"title":"添加机会公海","txt":"机会公海管理","content":"管理您的销售机会，包括销售机会的添加、修改、申请、指派等等。"},{"title":"管理个人机会","txt":"个人机会管理","content":"对自己的销售机会进行管理，包括客户生成、机会丢弃、关闭等。"},{"title":"管理机会申请","txt":"机会申请管理","content":"申请销售机会，以获得销售机会"},{"title":"添加客户信息","txt":"客户信息管理","content":"管理您的客户，包括客户的添加、修改、申请、转移、共享以及客户公海的管理等等。"},{"title":"管理公海申请","txt":"公海申请管理","content":"对系统中已被员工申请的公海客户进行指派。"},{"title":"管理公海池","txt":"公海池管理","content":"对公海客户进行管理，包括添加、修改等。"},{"title":"增加合同","txt":"合同维护","content":"对合同进行添加、修改等操作。"},{"title":"审核合同","txt":"合同审核","content":"对提交的合同进行审核。"}]';
var mnudata;
function CheckState() {
    $.ajax({
        url: "Handlers/Login.aspx?Method=GetLog_ID&userid=" + userID + "",
        type: "post",
        cache: false,
        dataType: "text",
        success: function (data) {
            var log_time = $.cookie('TCWL_Time');
            var id = log_time.split('=')[1];
            if (data != id) {
                layer.alert("您的账号已经在别处登陆！", 7, function () {
                    window.location.href = "Default.html";
                });
            }
        },
        error: function () {
        }
    });
}
function MenuShow(linode) {
    linode.attr("data-bootstro-html", true);
    linode.attr("data-bootstro-placement", "top");
    linode.attr("data-bootstro-width", "600px");
    linode.addClass("bootstro");
}
function funish() {
    $.cookie('guide', userID, { expires: 50000 });
    bootstro.stop();
}
function AddAttr() {
    $("#tcmm li").eq(0).addClass("open");
    $("#tcmm li").eq(0).find("ul").eq(0).show();
    $("#tcmm li").eq(0).find("ul").eq(0).removeAttr("nav-hide");
    $("#tcmm li").eq(0).find("ul").eq(0).addClass("nav-show");

    $("[data-bootstro-step]").parent().parent().addClass("open");
    $("[data-bootstro-step]").parent().show();
    $("[data-bootstro-step]").parent().removeAttr("nav-hide");
    $("[data-bootstro-step]").parent().addClass("nav-show");
    bootstro.start('.bootstro', {
        nextButton: "",
        prevButton: "",
        finishButton: "",
        onExit: function (params) {
            $("#tcmm li").eq(0).removeClass("open");
            $("#tcmm li").eq(0).find("ul").eq(0).hide();
            $("#tcmm li").eq(0).find("ul").eq(0).removeAttr("nav-show");
            $("#tcmm li").find("ul").eq(0).addClass("nav-hide");
            $("[data-bootstro-step]").parent().parent().removeClass("open");
            $("[data-bootstro-step]").parent().hide();
            $("[data-bootstro-step]").parent().removeAttr("nav-show");
            $("[data-bootstro-step]").parent().addClass("nav-hide");
        },
        onStep: function (index) {

        },
        onComplete: function () {

        }
    });
}

function NextStep() {
    bootstro.next();
}
function Done() {
    bootstro.stop();
}

$(function () {
    if (document.cookie.indexOf('userinf_tcwl') < 0) {
        window.location.href = "Default.html";
        return;
    }
    var coks = document.cookie.substring(document.cookie.indexOf('userinf_tcwl'));
    var ary = coks.split(';');
    var ary = ary[0].split('=');
    if (!ary[2]) {
        $('html').empty();
        return;
    }
    userID = ary[2].split('&')[0];
    serviceID = ary[3];

    $.ajax({ url: '../Handlers/common/common.aspx?Method=loadmenu&svrid=' + serviceID + "&userID=" + userID, cache: false, dataType: 'json', type: 'post', success: function (data) {
        parseMenu(data);
        var steps = eval("{" + arrstep + "}");
        var q = 0;
        for (var j = 0; j < steps.length; j++) {
            var title = steps[j].title;
            var txt = steps[j].txt;
            for (var i = 0; i < data.length; i++) {
                for (var k = 0; k < data[i].rows.length; k++) {
                    if (txt == data[i].rows[k].text) {
                        var _li = $("a[txt='" + txt + "']").parent();
                        MenuShow(_li);
                        _li.attr("data-bootstro-title", title);
                        _li.attr("data-bootstro-content", "<div class='bangzhu_mmk'><h1>使用云点滴第" + (q + 1) + "步——" + steps[j].title + "</h1><dl><dd>" + steps[j].content + "</dd><dt><img src='images/bangzhu_xiayibu.png' id='imgdone' width='134' height='41' style='cursor: pointer;' onclick='NextStep()' /> <span style='cursor: pointer;' onclick='funish()'>不再显示</span></dt></dl></div>");
                        if (j == (steps.length - 1)) {
                            _li.attr("data-bootstro-content", "<div class='bangzhu_mmk'><h1>使用云点滴第" + (q + 1) + "步——" + steps[j].txt + "</h1><dl><dd>" + steps[j].content + "</dd><dt><img src='images/bangzhu_wancheng.png' id='imgdone' width='134' height='41' style='cursor: pointer;' onclick='Done()' /> <span style='cursor: pointer;' onclick='funish()'>不再显示</span></dt></dl></div>");
                        }

                        _li.attr("data-bootstro-step", q);
                        q++;
                    }
                }
            }
        }
        if (document.cookie.indexOf("guide") < -1) {
            //AddAttr();
        }
        else {
            var cguide = document.cookie.substring(document.cookie.indexOf('guide'));
            var cuserid = cguide.split("=")[1].split(";")[0];
            if (userID != cuserid) {
                //AddAttr();
            }
        }
    }, error: function () { alert('加载菜单错误！'); }
    });
    $.ajax({
        url: "Handlers/system/user.aspx?Method=selectuser&user=" + userID + "&page=1&rows=10",
        type: "post",
        cache: false,
        dataType: "json",
        success: function (data) {
            $("#spnUser").text(data.rows[0].userXM);
        },
        error: function () {
            layer.msg('用户信息加载错误！', 1, 5);
        }
    });
    loadCom();
});
function loadCom() {
    $.ajax({ url: 'Handlers/system/SetInfo.aspx?Method=Load&srvid=' + serviceID + "&userid=" + userID, cache: false, dataType: 'json', type: 'post', success: function (data) {
        if (data.rows.length > 0) {
            $("#cmpname").text(data.rows[0].Name);
            $("html title").text(data.rows[0].Name);
            $("#log").attr('src', data.rows[0].Logo);
            $("#log").attr('onload', 'javascript:ResizePic(this)');
            if (data.rows[0].DomainName.length > 0) {
                $("#spndomain").text('网址:' + data.rows[0].DomainName);
                if (data.rows[0].DomainName == "" || data.rows[0].DomainName == "http://") {
                    $('#adomain').attr('href', "#");
                    $('#adomain').attr('title', "请完善企业信息");
                } else {
                    $('#adomain').attr('href', data.rows[0].DomainName);
                    $('#adomain').attr('title', data.rows[0].DomainName);
                }
            }

        }

    }, error: function () { }
    });
}
function ResizePic(ImgTag) {
    var FitWidth = 100;
    var FitHeight = 35;
    var image = new Image();
    image.src = ImgTag.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= FitWidth / FitHeight) {
            if (image.width > FitWidth) {
                ImgTag.width = FitWidth;
                ImgTag.height = (image.height * FitWidth) / image.width;
            }
            else {
                ImgTag.width = image.width;
                ImgTag.height = image.height;
            }
        }
        else {
            if (image.height > FitHeight) {
                ImgTag.height = FitHeight;
                ImgTag.width = (image.width * FitHeight) / image.height;
            }
            else {
                ImgTag.width = image.width;
                ImgTag.height = image.height;
            }
        }
    }
}

function SQ_GetNews() {
    var l = $("#tcmm li").each(function (i, e) {
        $(e).find('a').each(function (j, e1) {
            if ($(e1).attr('txt') == '消息提醒') {
                $(e1).trigger('click');
            }
        })
    });
}

function close_over(_this) {
    $(_this).css({ color: 'red', cursor: 'hand' })
}

function close_out(_this) {
    $(_this).css({ color: 'black', cursor: 'hand' });
}

function _onClick(_this, u) {
    $('#tcmm li').removeClass('active');
    $(_this).parent().addClass('active');
    var l = tab_exist(_this);
    if (l.toString().length > 0) {
        $('#tctabs a:eq(' + l + ')').tab('show');
        return;
    }
    $('#tctabs').append($('<li ><a href="#a_' + $(_this).attr('rel') + '" data-toggle="tab">' + $(_this).attr("txt") + '<span class="fa fa-times" style=" margin-left:8px;width:10px;height:10px;" onclick="tab_close(this);" onmouseover ="close_over(this);" onmouseout ="close_out(this);"></span></a></li>'));
    $('#tctabcontent').append($('<div class="tab-pane" id="a_' + $(_this).attr('rel') + '" style="height:100%;margin-top:5px;"><iframe src="' + u + '?userid=' + escape(userID) + '&srvid=' + escape(serviceID) + '&name=frm_' + $(_this).attr('rel') + '" style="border:0px;width:100%;height:100%;position:absolute;" id="frm_' + $(_this).attr('rel') + '"></iframe></div>'));
    $('#tctabs a:last').tab('show');

}
function tab_exist(_this) {
    var c = $("#tctabs li a");
    var aid = "";
    for (var i = 0; i < c.length; i++) {
        if ($(c[i]).text() == $(_this).attr("txt")) {
            aid = i;
        }
    }
    return aid;
}
function tab_close(_this) {
    $(_this).parent().parent().remove();
    $('#' + $(_this).parent().attr("href").substring(1)).remove();
    $('#tctabs a:last').tab('show');
}
function parseMenu(data) {
    for (var i = 0; i < data.length; i++) {
        var _each_m = $("<li></li>");
        var _m_txt = $('<a></a>');
        _m_txt.attr('id', "all" + i.toString());
        _m_txt.append($('<i class="menu-icon fa ' + data[i].icon + '"></i>'));
        //                _m_txt.append($('<i class="menu-icon fa fa-bars"></i>'));
        _m_txt.attr('href', '#');
        _m_txt.addClass('dropdown-toggle');
        var _title = $("<span class=\"menu-text\"></span>");
        _title.text(data[i].title);
        _m_txt.append(_title);
        if (data[i].rows.length > 0) {
            _m_txt.append('<b class="arrow fa fa-angle-down"></b>');
        }
        _each_m.append(_m_txt);
        _each_m.append($('<b class="arrow"></b>'));
        var _sub_m = $('<ul class="submenu"></ul>');
        for (var j = 0; j < data[i].rows.length; j++) {
            var _sub_itm = $('<li></li>');
            var _sub_txt = $('<a id="a' + i.toString() + j.toString() + '" txt="' + data[i].rows[j].text + '" rel="' + i.toString() + j.toString() + '" onclick=" _onClick(this,\'' + data[i].rows[j].url + '\');"></a>');
            _sub_txt.attr('href', data[i].rows[j].url);
            _sub_txt.attr('href', '#');
            _sub_txt.html('<i class="menu-icon fa fa-caret-right"></i>' + data[i].rows[j].text);
            _sub_itm.append(_sub_txt);
            _sub_itm.append('<b class="arrow"></b>');
            _sub_m.append(_sub_itm);
        }
        _each_m.append(_sub_m);
        if (data[i].rows.length > 0) {
            $('#tcmm').append(_each_m);
        }
    }
}
function MdifyPass() {
    $("#fmPwd input").val("");
    $(".label-danger").text("");
    $("#Dialog").modal("show");
}
function PwdCheck() {
    $.post("Handlers/system/SetInfo.aspx?Method=PwdCheck&srvid=" + serviceID + "&userid=" + userID + "", $('#fmPwd').serialize(), function (data, status) {
        if (status == "success") {
            if (data != "success") {
                $("#lberror").text("原密码错误!");
                $("#btnadd").attr("disabled", true);
            }
            else {
                $("#lberror").text("");
                $("#btnadd").attr("disabled", false);
            }
        }
        else {
            alert("网络错误！");
        }
    }, "text");
}
function close1() {
    layer.confirm("确定退出吗？", function () {
        var l = layer.load('正在退出......');
        $.ajax({
            type: "post",
            url: "Handlers/Login.aspx?Method=OutLogin&serviceid=" + serviceID + "&user=" + userID,
            cache: false,
            success: function (msg) {
                layer.close(l);
                if (msg == "sucess") {
                    $.cookie("userinf_tcwl", '', { expires: -1 });
                    $.cookie("TCWL_All", '', { expires: -1 });
                    window.location.href = 'Default.html';
                }
                else {
                    window.location.href = 'Default.html';
                }
            },
            error: function () {
                layer.close(l);
                $.cookie("userinf_tcwl", '', { expires: -1 });
                $.cookie("TCWL_All", '', { expires: -1 });
                window.location.href = 'Default.html';
            }
        });
    });
}

function DelCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    document.cookie = name + "=; expires=" + exp.toGMTString();
}
function EditPwd() {
    $('#fmPwd').validation();
    var alm = $(".popover");
    if (alm.length > 0) {
        return;
    }
    $(".popover ").remove();
    if ($("#pwdConf").val() != $("#pwdNew").val()) {
        $("#lbcheck").text("两个密码不一致!");
        return false;
    }
    else {
        $("#lbcheck").text("");
    }
    $.post("../Handlers/system/SetInfo.aspx?Method=PwdModify&srvid=" + serviceID + "&userid=" + userID + "", $('#fmPwd').serialize(), function (data, status) {
        if (status == "success") {
            if (data == "success") {
                layer.msg("密码修改成功！", 2, 1);
                $("#Dialog").modal("hide");
            } else {
                layer.msg("密码修改失败！", 2, 8);
            }
        }
        else {
            layer.msg("网络错误！请稍后重试！");
        }
    }, "text");
}
var maxheight1=0;
function sidebar_r() {
    var erydiv = $("#tctabcontent").find('div iframe').each(function (i, e) {
        if (!$(e).is(':hidden')) {
            var ifm = document.getElementById($(e).attr('id'));
            var subWeb = document.frames ? document.frames['"' + $(e).attr('id') + '"'].document : ifm.contentDocument;
            if (ifm != null && subWeb != null) {
                var indexheight = $(document).height() - $(e).offset().top;
                var winheight = $(window).height() - $(e).offset().top;
                var childheight = subWeb.body.scrollHeight;
                if (winheight >= childheight) {
                    $(e).css({ 'height': winheight });
                }
                else {
                    $(e).css({ 'height': childheight });
                }
            }
           
            var s1 = $('#sidebar').height($(e).height() + 55);
        }

    });

}

function ini() {
    window.setInterval('sidebar_r()', 1000);
    skn11();
    GetStationNews();
    window.setInterval('GetStationNews()', 30000);
    window.setInterval('CheckState()', 60000);
}
function skn11() {
    var u = $.cookie('uid');
    skn = $.cookie('skn');
    if (skn && skn.length > 0 && u && u == userID) {
        $('#bdy').removeClass();
        $('#bdy').addClass(skn);
        $('#' + $.cookie('style_name')).text('★');
    }
}
function GetStationNews() {
    $.ajax({
        type: "post",
        url: "Handlers/StationNews/StationNews.aspx?Method=GetNumber&user=" + escape(userID),
        cache: false,
        success: function (data) {
            if (data != "0") {
                $("#News_WD").text(data);
                $("#js_N_navMailMsg").show();
                $("#js_N_navMailMsg").stop().animate({ fontSize: '30' }, 200, function () {
                    $("#js_N_navMailMsg").animate({ fontSize: '10' }, 200);
                });
            }
            else {
                $('#News_WD').text(0);
                $("#js_N_navMailMsg").hide();
            }
        },
        error: function () {
            layer.msg('网络请求错误!', 3, 5);
            layer.close(l);
        }
    });
}
var game = 0;
function Game() {
    game = game + 1;
    document.getElementById("game").style.display = "block";
    document.getElementById("game").innerHTML = "+" + game;
    var X = $("#cmpname").offset().left;
    var Top = $("#cmpname").offset().top;
    var Width = $("#cmpname").width();
    var kd = Width + X + 75;
    $("#game").css({ position: "absolute", color: "pink", left: kd, top: Top - 10 });
    $("#game").stop().animate({ fontSize: '30' }, 200, function () {
        $("#game").animate({ fontSize: '18' }, 200);
    });
}
function move(op) {
    $('#' + op).show();
}
function over(op) {
    document.getElementById(op).style.display = "none";
}
function Update_Style(op) {
    if (op == "438EB9") {
        $("#438EB9").text("★");
        $("#222A2D").text("");
        $("#C6487E").text("");
        $("#D0D0D0").text("");
        $("#bdy").removeClass();
        $("#bdy").attr('class', 'no-skin');
        $.cookie('skn', 'no-skin', { expires: 50000 });
        $.cookie('uid', userID, { expires: 50000 });
        $.cookie('style_name', '438EB9', { expires: 50000 });
    }
    if (op == "222A2D") {
        $("#438EB9").text("");
        $("#222A2D").text("★");
        $("#C6487E").text("");
        $("#D0D0D0").text("");
        $("#bdy").removeClass();
        $("#bdy").attr('class', 'skin-1');
        $.cookie('skn', 'skin-1', { expires: 50000 });
        $.cookie('uid', userID, { expires: 50000 });
        $.cookie('style_name', '222A2D', { expires: 50000 });
    }
    if (op == "C6487E") {
        $("#438EB9").text("");
        $("#222A2D").text("");
        $("#C6487E").text("★");
        $("#D0D0D0").text("");
        $("#bdy").removeClass();
        $("#bdy").attr('class', 'skin-2');
        $.cookie('skn', 'skin-2', { expires: 50000 });
        $.cookie('uid', userID, { expires: 50000 });
        $.cookie('style_name', 'C6487E', { expires: 50000 });
    }
    if (op == "D0D0D0") {
        $("#438EB9").text("");
        $("#222A2D").text("");
        $("#C6487E").text("");
        $("#D0D0D0").text("★");
        $("#bdy").removeClass();
        $("#bdy").attr('class', 'skin-3 no-skin');
        $.cookie('skn', 'skin-3 no-skin', { expires: 50000 });
        $.cookie('uid', userID, { expires: 50000 });
        $.cookie('style_name', 'D0D0D0', { expires: 50000 });
    }
}
function oponBinding() {
    $.ajax({
        url: "Handlers/system/user.aspx?Method=GetBinding&user=" + escape(userID),
        type: "post",
        cache: false,
        success: function (msg) {
            if (msg != "false") {
                $("#Phone").val(msg);
                $("#bangding").hide();
                $("#yibang").show();
                $("#div_yzm").hide();
                $("#btn_Bang").hide();
                $("#Phone").attr("readonly", "readonly");
            } else {
                $("#Phone").val("");
                $("#bangding").show();
                $("#yibang").hide();
                $("#div_yzm").show();
                $("#btn_Bang").show();
                $("#Phone").removeAttr("readonly", "readonly");
            }
        },
        error: function () {
            layer.msg('网络错误,请稍后再试！', 2, 5);
        }
    });
    $("#Binding").modal("show");
}
function Binding() {

    var phone = $("#Phone").val();
    if (validatemobile(phone)) {
        $("#span_yz").show();
        $("#Phone").focus();
        return;
    }
    var yzm = "";

    var l = layer.load('正在绑定....');
    $.ajax({
        type: "post",
        url: "Handlers/system/user.aspx?Method=Binding&user=" + escape(userID) + "&srvid=" + escape(serviceID) + "&phone=" + phone + "&yzm=" + yzm,
        cache: false,
        success: function (msg) {
            if (msg == "success") {
                layer.close(l);
                layer.msg('绑定成功！', 2, 1);
                $("#Binding").modal("hide");
            }
            if (msg == "exist") {
                layer.msg('该手机号已经绑定到其他帐号，不能再次绑定，请解绑原账号后再进行绑定！', 2, 5);
            }
            if (msg == "have") {
                layer.msg('绑定失败，该手机已绑定当前账号！', 2, 5);
            }
            if (msg == "nobindinf") {
                layer.msg('绑定失败，输入信息有误！', 2, 5);
            }
            if (msg == "noyzm") {
                layer.msg('验证码输入有误，请重新绑定！', 2, 5);
            }
            if (msg == "error") {
                layer.msg('网络错误，请稍后再试！', 2, 5);
            }
            layer.close(l);
        },
        error: function () {
            layer.msg('网络请求错误!', 3, 5);
            layer.close(l);
        }
    });
}
function validatemobile(mobile) {
    var regu = /^1[3,4,5,7,8]\d{9}$/;
    var re = new RegExp(regu);
    if (!re.test(mobile)) {
        return true;
    } else {
        return false;
    }
}