
var $ExamPlan_ExamId = $("#ExamPlan_ExamId"); //隐藏域对象
var $promptBg = $("#promptBg"); //正在加载模板
var isLoad = false; //是否正在异步加载

$(function () {
    $ExamPlan_ExamId = $("#ExamPlan_ExamId"); //隐藏域对象
    $promptBg = $("#promptBg");

    $("#Exam_Plan_ExamName").keydown(function (e) {
        var event = e || event; //兼容性处理
        if (event.keyCode == 27) {
            $promptBg.stop().hide(0, function () {
                $promptBg.html("");
            });
            return;
        } else if (event.keyCode == 37 || event.keyCode == 39) return;
        else if (event.keyCode == 13) {
            var $obj = $(".activePrompt");
            if ($obj.length < 1) return false;
            $ExamPlan_ExamId.val($obj.attr("data"));
            $("#Exam_Plan_ExamName").val($obj.text().substring(0, $obj.text().length - 2));
            $promptBg.stop().hide(0, function () {
                $promptBg.html("");
            });
            return false;
        }
        if ($("#loadIcon").length < 1) {
            if (event.keyCode == 38) { //向上
                if ($(".activePrompt").length < 1) {//第一个
                    $(".promptLink").last().addClass("activePrompt");
                    return false;
                }
                $currObj = $(".activePrompt").parent();
                if ($currObj.prev().length < 1) return false; //最后一个
                var $nextObj = $currObj.prev().children("a");
                $(".activePrompt").removeClass("activePrompt");
                $nextObj.addClass("activePrompt");
                return false;
            }
            if (event.keyCode == 40) { //向下
                if ($(".activePrompt").length < 1) {//第一个
                    $(".promptLink").eq(0).addClass("activePrompt");
                    return false;
                }
                $currObj = $(".activePrompt").parent();
                if ($currObj.next().length < 1) return false; //最后一个
                var $nextObj = $currObj.next().children("a");
                $(".activePrompt").removeClass("activePrompt");
                $nextObj.addClass("activePrompt");
                return false;
            }
        }

    });
    function textChange() {
        if (isLoad) return;
        $("#promptBg").show(0);
        var inputvalue = $("#Exam_Plan_ExamName").val();
        isLoad = true;

        $.get("/Training/ExamPlan/GetExamPrompt.ashx", { key: inputvalue }, function (data) {
            isLoad = false;
            if (!data || parseInt(data) == -1) {
                $promptBg.stop().hide(0, function () {
                    $promptBg.html("");
                });
                return; //没有查询到数据或者异常
            }
            $("#promptBg").html(data);
            $(".promptLink").hover(function () {//绑定事件
                $(this).addClass("activeLink");
            }, function () {
                $(this).removeClass("activeLink");
            });
            $(window).one("click", function () {
                $promptBg.stop().hide(0, function () {
                    $promptBg.html("");
                });
            });
        });
    }
    if ($.browser.msie) {//根据兼容性绑定不同的文本改变事件
        $("#Exam_Plan_ExamName").bind("input propertychange", textChange);
    } else {
        $("#Exam_Plan_ExamName").bind("input", textChange);
    }
    if ($("#ExamPlan_PlanStart").val().length > 0) {//时间特殊格式
        $("#ExamPlan_PlanStart").val($("#ExamPlan_PlanStart").val().replace(' 00:00:00', ''));
    }
    if ($("#ExamPlan_PlanEnd").val().length > 0) {//时间特殊格式
        $("#ExamPlan_PlanEnd").val($("#ExamPlan_PlanEnd").val().replace(' 00:00:00', ''));
    }

});

function choiseExam(obj, e, kemu) {
    var id = $(obj).attr("data");
    var name = $(obj).text().substring(0, $(obj).text().length - 2);
    addNew(id, name, kemu);
    $("#cslist").show();
    $promptBg.stop().hide(0, function () {
        $promptBg.html("");
    });
    stopBubble(e);
    function stopBubble(e) {
        if (e && e.stopPropagation) {
            e.stopPropagation();
        } else {
            window.event.cancelBubble = true;
        }
    }
}


//回车键
document.onkeydown = function (e) {
    if (!e) e = window.event; //火狐中是 window.event
    if ((e.keyCode || e.which) == 27) {
        //隐藏
        divonblur();
        CowtabHide();
    }
}


///查询直播
function GetLive() {
    var value = $("#ExamPlan_kemu").val();
    var name = $("#ExamPlan_LiveName").val();
    if (value == "") {
        return;
    }
    getAjax("GetLiveByExamPlan.ashx", "kemu=" + value + "&&name=" + name + "&&status=keyup", function (rs) {
        if (rs == 2) {
            showTipsMsg(lgweizhaodaoxinxin, 2000, 5);
        } else if (rs == "[]") {
            showTipsMsg(lgweizhaodaoxinxin, 2000, 5);
        }
        else {
            var json = eval('(' + rs + ')')
            DataHtml(json);
        }
    });
}
//选择
function selectlive(id, name) {

    $("#ExamPlan_LiveID").val(id);
    $("#ExamPlan_LiveName").val(name);
    divonblur();
}
///html
function DataHtml(obj) {
    $("#livediv").html();
    var strhtml = "<ul>";
    for (var i = 0; i < obj.length; i++) {
        strhtml += "<li  onclick=\"selectlive('" + obj[i].LiveTelecast_Id + "','" + obj[i].LiveTelecast_Name + "');\">" + obj[i].LiveTelecast_Name + "</li>";
    }
    strhtml += "</ul>";
    $("#livediv").html(strhtml);
    $("#livediv").show();
}
//隐藏
function divonblur() {
    $("#livediv").hide();
    $("#livediv").html("");
}
//增加新数据
var rowindex = 1;
function addNew(id, name, Type) {
    $("#table1 tbody").append("<tr><td><input type='checkbox' checked='checked' name='checkbox_cs' value='" + rowindex + "|" + id + "'></td><td><input  value='" + name + "' name='checkbox_name_" + rowindex + "' style='width: 200px;' /></td><td><input style='width:150px;'  value='" + Type + "' name='checkbox_Type_" + rowindex + "'/></td></tr>");
    rowindex++;
}

//选择试卷
function CowtabShow(ctid) {
    $(".iframe_left").attr("src", "/Training/plan/Plan_Courseware_CourType.aspx");
    $("#Cowtab").show();
    $.get("/Training/ExamPlan/GetExamTable.ashx", { orderfile: 'Examination_CreateDate', ctid: ctid },
                          function (data) {
                              $("#cwtbody").html(data);
                          });
}
//分页

function CowTabPage(index, typeid) {
    $.get("/Training/ExamPlan/GetExamTable.ashx", { page: index, orderfile: 'Examination_CreateDate', key: typeid },
                          function (data) {
                              if (data.indexOf("checkbox") < 0) {
                                  alert("已经到末尾或此分类为空");
                                  return;
                              }
                              $("#cwtbody").html(data);
                          });
}
///div隐藏
function CowtabHide() {
    $("#Cowtab").hide();
    $("#LiveTab").hide();
}
//选择提示内容
function setcs(obj, id, name, Type) {
    if ($(obj).attr("checked")) {
        $("#cslist").show();
        addNew(id, name, Type);
    }
}

$(function () {
    //把分页定位到屏幕下面
    divresize(63);
});
//保存
function SaveForm() {
    var checkResult = CheckDataValid('#form1');
    if (!checkResult) return false;
    var number = 0;
    var r = document.getElementsByName("users");
    for (var i = 0; i < r.length; i++) {
        if (r[i].checked) { number++; }
    }

    if (number == 0) {
        showTipsMsg(lgnizhishaoxuanzeyigeyonghu, 1500, 3);
        return false;
    }

    number = 0;
    var r = document.getElementsByName("checkbox_cs");
    for (var i = 0; i < r.length; i++) {
        if (r[i].checked) { number++; }
    }
    if (number == 0) {
        showWarningMsg(lgzhishaoxuanzeyigeshijuan);
        return false;
    }
    showConfirmMsg(lgquerenbaocuncaozuo, function (r) {
        if (r) {
            form1.submit();
        }
    });
    return false;
}



///手选直播
function ShowPlanLive(index) {

    $.get("/Training/ExamPlan/GetLiveByExamPlan.ashx", { PageIndex: index },
 function (data) {
     if (data == 2) {
         showTipsMsg(lgweizhaodaoxinxin, 2000, 5);
     } else if (data == "[]") {
         showTipsMsg(lgweizhaodaoxinxin, 2000, 5);
     }
     else {
         var json = eval('(' + data + ')')
         PlanLiveHtml(json, index);
         $("#LiveTab").show();
     }
 });
}
///手选直播页面
function PlanLiveHtml(obj, pageIndex) {

    if (obj.length > 0) {

        var strhtml = "";
        for (var i = 0; i < obj.length; i++) {
            strhtml += " <tr><td ><input type=\"checkbox\" onclick=\"setlive(this,'" + obj[i].LiveTelecast_Id + "','" + obj[i].LiveTelecast_Name + "');\" /></td><td>" + obj[i].LiveTelecast_Name + "</td></tr>";
        }
        strhtml += "<tr ><td style=\"border:0px;\"></td><td style=\"border:0px;\"><div class=\"Page\"><input type=\"button\" class=\"button\" value=\"<- " + lgshangyiye + " \" onclick=\"ShowPlanLive('" + (pageIndex - 1) + "')\"/> <input type=\"button\" class=\"button\"  value=\"" + lgxiayiye + " ->\" onclick=\"ShowPlanLive('" + (pageIndex + 1) + "')\"/></div></td>";
        $("#LiveBody").html(strhtml);
    }
}

//选择提示内容
function setlive(obj, id, name) {
    if ($(obj).attr("checked")) {
        $("#livelist").show();
        addLive(id, name);
    }
}

//增加新数据
var liverowindex = 1;
function addLive(id, name) {
    $("#table2 tbody").append("<tr><td><input type='checkbox' checked='checked' name='checkbox_live' value='" + liverowindex + "|" + id + "'></td><td><input  value='" + name + "' name='checkbox_live_name_" + liverowindex + "' style='width: 250px;' /></td></tr>");
    liverowindex++;
}