//保存z-index
var z_index = 1;

//部门服务地址
var depSrv = "http://" + window.location.host + "/SysMain/Setting/DepSrv.aspx";
//用户服务地址
var UserSrv = "http://" + window.location.host + "/SysMain/Setting/UserSrv.aspx";

//主窗口数据地址
var MainOp = "http://" + window.location.host + "/SysMain/MainOp.xml";


//构造 功能窗口式DIV 
function ActionDiv(action, id, title) {
    var divStr = "<div class=\"Action_Box\" isClose=\"f\" id=\"" + action + id + "\"><div class=\"Box_info\">" + title + "<div class=\"Box_Close\">关闭</div></div><div class=\"Action_Box_Content\" id=\"" + action + "_Con_" + id + "\"></div></div>";
    $("body").append(divStr);
    var divId = "#" + action + id;
    $(divId).css("z-index", "99999");
    $(divId).fadeIn("slow", function () {
        $(this).animate({ left: '200px' }, "slow");
    });
    Close_Main_Box();
    Action_BoxClick();
    DragDiv();
}


//构造 标准窗口式DIV 
function StanderDiv(id, title,menuHtml) {
    var divStr = "<div class=\"Main_Box\" isClose=\"f\" id=\"Div_" + id + "\"><div class=\"Box_info\">" + title + "<div class=\"Box_Close\">关闭</div></div><div class=\"Box_Menu\"><table id=\"menu_" + id + "\"><tr>" + menuHtml + "</tr></table></div><div class=\"Box_Content\" id=\"Con_" + id + "\"></div></div>";
    $("body").append(divStr);
    var divId = "#Div_" + id;
    $(divId).css("z-index", z_index + 1);
    z_index = z_index + 1;
    $(divId).fadeIn("slow", function () {
        $(this).animate({ left: '200px' }, "slow");
    });
    Close_Main_Box();
    Main_BoxClick();
    DragDiv();
}

//构造 标准菜单(搜索,增加,删除,修改)
function StanderMenu(id) {
    return "<td><input id=\"searchT_" + id + "\" type=\"text\"/><input value=\"搜索\" id=\"searchB_" + id + "\" type=\"button\"></td><td><input value=\"新增\" id=\"Insert_" + id + "\" type=\"button\"></td><td><input value=\"修改\" id=\"Update_" + id + "\" type=\"button\"></td><td><input value=\"删除\" id=\"Del_" + id + "\" type=\"button\"></td>";
}
//菜单扩展
function Vmenu(menuid, text) {
    var tid = "#menu_" + menuid + " tr";
    $(tid).append("<td>" + text + "</td>");
}

//左侧菜单隐藏和显示
function ShowLeftMenu() {
    $(".Div_leftMenu_Btn").mouseover(function () {
        $(this).animate({ left: '-100px' }, "slow");
        $(".leftNav").animate({ left: '0px' }, "slow");
    });

    $(".leftNav").mouseleave(function () {
        $(this).animate({ left: '-100px' }, "slow");
        $(".Div_leftMenu_Btn").animate({ left: '0px' }, "slow");
    });
}

//关闭Main_Box
function Close_Main_Box() {
    $(".Box_Close").click(function () {
        $(this).parent().parent().attr("isClose", "t");
        $(this).parent().parent().fadeOut("slow");
    });
}
//关闭普通窗口
function CloseBox(id) {
    var tid = "#" + id;
    $(tid).attr("isClose", "t");
    $(tid).fadeOut("slow");
}

//改变Main_Box Z-index 或显示Main_Box
function Show_Main_Box(id) {
    var divId = "#" + id;
    $(divId).css("z-index", z_index + 1);
    z_index = z_index + 1;
    if ($(divId).attr("isClose") == "t") {
        $(divId).fadeIn("slow");
        $(divId).attr("isClose","f");
    }

}

//显示Action_Box
function Show_Action_Box(id) {
    var divId = "#" + id;
    $(divId).css("z-index", "99999");
    if ($(divId).attr("isClose") == "t") {
        $(divId).fadeIn("slow");
        $(divId).attr("isClose", "f");
    }
}


//构造Main_Box后,对Main_Box的操作
function Opera_Main_Box(id) {
    var divId = "#" + id;
    $(divId).unbind();
    $(divId).bind("click", function () {
        Show_Main_Box("Div_" + id);
    });
}

//左边菜单点击基本事件
function Left_Menu_Click(id, name,hasMenu) {
    if (hasMenu) {
        StanderDiv(id, name, StanderMenu(id));
    } else {
        StanderDiv(id, name, "");
    }
    Opera_Main_Box(id);
}

//Main_Box 点击 ,提高Main_Box z-index
function Main_BoxClick() {
    $(".Main_Box").click(function () {
        $(this).css("z-index", z_index+1);
        z_index = z_index + 1;
    });
}
//Action_Box 点击 ,提高Action_Box z-index
function Action_BoxClick() {
    $(".Action_Box").click(function () {
        $(this).css("z-index", z_index + 1);
        z_index = z_index + 1;
    });
}

//窗口式DIV拖动
function DragDiv() {
    $(function () {
        $(".Main_Box").draggable({ handle: ".Box_info", scroll: false });
    });
    $(function () {
        $(".Action_Box").draggable({ handle: ".Box_info", scroll: false });
    });
}

//判断输入是否为数字
function IsNum(str) {
    var ex = /^\d+.?\d*$/;
    if (ex.test(str)) {
        return true;
    } else {
        return false;
    }
}

$(document).ready(function () {
    ShowLeftMenu();
    $("#UPWD").unbind();
    $("#UPWD").bind("click", function () {
        UPWD();
    });
});



//按钮加载图片开始
function loading(id) {
    var tid = "#" + id;
    $(tid).fadeOut();
    $(tid).parent().append("<img class=\"datagrid-mask-msg\" src=\"http://" + window.location.host + "\\Images\\loading.gif\"/>");
    var f = "stopW(\"" + id + "\")";
    setTimeout(f, 10000);
}
//按钮加载图片结束
function stopW(id) {
    var tid = "#" + id;
    $(tid).fadeIn();
    $('.datagrid-mask-msg').remove();
}

//内容加载图片开始
function Cloading(id) {
    var tid = "#" + id;
    $(tid).html("<img class=\"datagrid-mask-msg\" src=\"http://" + window.location.host + "\\Images\\loading.gif\"/>");
    setTimeout('Cstop()', 10000);
}
//内容加载图片结束
function Cstop() {
    $('.datagrid-mask-msg').remove();
}

//清空table 里面 的表单数据
function clearDataForm(id) {
    var inputid = "#" + id + " input";
    var textid = "#" + id + " textarea";
    $(inputid).val("");
    $(textid).val("");
}

//判断标准表的checkbox选中的值 用','分开
function CheckBoxVal(id) {
    var ids = "";
    var tid = "#" + id + " input[type='checkbox']";
    $(tid).each(function () {
        if ($(this).is(':checked')) {
            ids += $(this).val() + ",";
        }
    });
    if (ids != "") {
        ids=ids.substring(0, ids.length - 1);
    }
    return ids;
}

//判断标准表的checkbox未选中的值 用','分开
function NoCheckBoxVal(id) {
    var ids = "";
    var tid = "#" + id + " input[type='checkbox']";
    $(tid).each(function () {
        if (!$(this).is(':checked')) {
            ids += $(this).val() + ",";
        }
    });
    if (ids != "") {
        ids = ids.substring(0, ids.length - 1);
    }
    return ids;
}


//获取日期
function GetDateStr(addDayCount) {
    var dd = new Date();
    var ddd = dd.DateAdd('d', addDayCount); 
    var y = ddd.getYear();
    var m = ddd.getMonth() + 1; //获取当前月 
    var d = ddd.getDate();
    return y + "-" + m + "-" + d;
}

//传入一个日期yyyy/mm/dd,格式化成yyyy-MM-dd
function DateF(dateStr) {
    var dateS = dateStr.split('/');
    for (var i = 0; i < dateS.length; i++) {
        if (dateS[i].length <= 1) {
            dateS[i] = "0" + dateS[i];
        }
    }
    return dateS[0] + "-" + dateS[1] + "-" + dateS[2];
}

//获取部门选择框
function SelectDep(id, selectVal) {
    var tid = "#" + id;
    //执行ajax
    $.get(depSrv, { type: "SelectDep" }, function (data) {
        if (data != "") {
            $(tid).html("");
            $(tid).append(data);
            if (selectVal != "") {
                var selectI = tid + " option[value='" + selectVal + "']";
                $(selectI).attr("selected", true);
            }
        } else {
            $(tid).html("");
            alert('没有可选择的部门,请到系统设置添加部门');
        }
    });
}
//获取用户选择框
function SelectUser(id, d,selectVal) {
    var tid = "#" + id;
    //执行ajax
    $.get(UserSrv, { type: "SelectUser", depid: d }, function (data) {
        stopW(id);
        if (data != "") {
            $(tid).html("");
            $(tid).append(data);
            if (selectVal != "") {
                var selectI = tid + " option[value='" + selectVal + "']";
                $(selectI).attr("selected", true);
            }
        } else {
            $(tid).html("");
            alert('没有可选择的用户,请到系统设置添加用户');
        }
    });
}

//用户修改密码
function UPWD() {
    //构造 修改密码DIV
    ActionDiv("Update", "PWD", "修改密码");
    //读取XML 获取修改表单
    $.get(MainOp, function (data) {
        $("#Update_Con_PWD").html($(data).find("Action[name='UpdatePWD']").html());
        $("#PWDUpdate").unbind();
        $("#PWDUpdate").bind("click", function () {
            UPWDGo();
        });
    });
    //重新定义修改密码点击事件
    $("#UPWD").unbind();
    $("#UPWD").bind("click", function () {
        Show_Action_Box("UpdatePWD");
    });
}
//修改密码ajax
function UPWDGo() {
    //获取数据
    var o = $("#OPWD").val();
    var n = $("#NPWD").val();
    var rn = $("#RNPWD").val();

    if (n != rn) {
        alert('新密码不一样,请重新输入');
        return;
    }
    //加载等待
    loading("PWDUpdate");
    
    //执行ajax
    $.get(UserSrv, { type: "Upwd", oPwd: o, nPwd: n }, function (data) {
        //等待结束
        stopW("PWDUpdate");

        if (data == "true") {
            alert('修改成功!');
            CloseBox("UpdatePWD");             //关闭窗口
        }
        else {
            alert("修改失败...失败原因:" + data);
        }
    });
}
//输出指定位数的随机数的随机整数
function RandNum(n) {

    var rnd = "";

    for (var i = 0; i < n; i++)

        rnd += Math.floor(Math.random() * 10);

    return rnd;

}

//单位数时间前加0
function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

//获取当前时间
function NowTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();

    m = checkTime(m);
    s = checkTime(s);

    return h + ":" + m + ":" + s;
}