﻿function killErrors()
{
    return true;
}
window.onerror = killErrors;

$(function()
{
    try {
        if (WebForm_OnSubmit != null) {
            var wos = WebForm_OnSubmit;
            WebForm_OnSubmit = function()
            {
                try {
                    return wos();
                } catch (err) {
                    return false;
                }
            }
        }
    } catch (err) {
        return false;
    }
});

var closeFormDefault = true;
var operationMistakeAlert = true;
var operationSucceedAlert = true;
var delAlert = true;

function Add(formName)
{
    getForm(formName, 0);
}

function Update(formName, id)
{
    getForm(formName, id);
}

function getForm(formName, id)
{
    var FormDiv = $("#__" + formName + "Form_");
    var updateBut = FormDiv.find("#__ButDiv_>input[value='__Form_Update']");
    var IDTxt = FormDiv.find("#__ButDiv_>input[id*='_FormID']");
    IDTxt.val(id);
    updateBut.attr("click")();
}
function openForm(formName, title, width, height, auto)
{
    function save()
    {
        var FormDiv = $("#__" + formName + "Form_");
        var saveBut = FormDiv.find("#__ButDiv_>input[value='__Form_Save']");
        saveBut.attr("click")();
    }
    $.form({
        title: title,
        auto: auto != null ? auto : false,
        w: width != null ? width : 620,
        h: height != null ? height : 320,
        but: { 保存: save }
    }, "__" + formName + "Form_");
}

function openFormAuto(formName, title)
{
    openForm(formName, title, 0, 0, "auto");
}

function openDivForm(divID, title, width, height, auto)
{
    $.form({
        title: title,
        auto: auto != null ? auto : false,
        w: width != null ? width : 680,
        h: height != null ? height : 320
    }, divID);
}

function openDivFormAuto(divID, title)
{
    openDivForm(divID, title, 0, 0, "auto");
}

function Del(listName)
{
    var ListDiv = $("#__" + listName + "List_");
    var ListBut = ListDiv.find("#__ListPage_>input[value='_ListBut']");
    var ListData = ListDiv.find("#__ListViewData_ table:eq(0)");
    var count = ListData.find("tr").find("td:eq(0) input:checked");
    if (count.length > 0) {
        if (delAlert) $.confirm({ msg: "你确实要删除这 " + count.length + " 条数据吗？", img: 3, ok: function() { ListBut.attr("click")(); } });
    } else {
        if (delAlert) $.alert("请选择要删除的数据!");
    }
}

function DelID(listName, id)
{
    var ListDiv = $("#__" + listName + "List_");
    var ListBut = ListDiv.find("#__ListPage_>input[value='_ListBut']");
    var IDTxt = ListDiv.find("#__ListPage_>input[id*='_ListID']");
    IDTxt.val(id);
    if (delAlert)
        $.confirm({ msg: "你确实要删除这条数据吗？", img: 3, ok: function() { ListBut.attr("click")(); } });
}

function Sel(listName, width, height)
{
    $.form({
        title: "查询",
        w: width != null ? width : 420,
        h: height != null ? height : 220,
        but: { 应用: function() { PageSet(listName); $("#__" + listName + "ListSel_").closeForm(); } }
    }, "__" + listName + "ListSel_");
}

function closeForm(formName, type, state)
{

    if (closeFormDefault) {
        $("#__" + formName + "Form_").closeForm();
    }
    if (state) {
        formName = formName.replace("_2", "").replace("_3", "");
        PageSet(formName);
    }
    msgForm(type, state);
}

function msgForm(type, state)
{
    if (state) {
        if (!operationSucceedAlert) return;
    } else {
        if (!operationMistakeAlert) return;
    }
    switch (type) {
        case "parameter":
            if (state) {
            } else {
                $.alert("参数错误！");
            }
            break;
        case "add":
            if (state) {
                $.alert("添加成功！");
            } else {
                $.alert("添加失败！");
            }
            break;
        case "update":
            if (state) {
                $.alert("修改成功！");

            } else {
                $.alert("修改失败！");
            }
            break;
        case "save":
            if (state) {
                $.alert("保存成功");
            } else {
                $.alert("保存失败");
            }
            break;
        case "del":
            if (state) {
                $.alert("删除成功！");

            } else {
                $.alert("删除失败！");
            }
            break;
    }
}

//Page
function PageSet(formName, pageIndex)
{
    var ListDiv = $("#__" + formName + "List_");
    if (ListDiv != null) {
        var _Index = ListDiv.find("#__ListPage_ > input[id*='_PageIndex']");
        var _But = ListDiv.find("#__ListPage_ > input[value='_PageBut']");
        if (pageIndex != null)
            _Index.val(pageIndex);
        _But.attr("click")();
    }
}
function PageMove(obj)
{
    obj.className = "td2 move";
}
function PageOut(obj)
{
    obj.className = "td2";
}

//GirdView
function GvCheckAll(checkall)
{
    var checks = $(checkall).parents("table:eq(0)").find("tr").find("td:eq(0) input[type='checkbox']");
    if ($(checkall).attr("checked")) {
        checks.attr("checked", "checked");
    } else {
        checks.removeAttr("checked");
    }
}
function GvCheckClick(check)
{
    $(check).parents("table:eq(0)").find("tr th:eq(0) input[type='checkbox']").removeAttr("checked");
}
function GvRowOns(GvID)
{
    var GvRows = $("#" + GvID).find("tr:gt(0)");
    GvRows.mouseover(function()
    {
        $(this).addClass("gvrowMove");
    }).mouseout(function()
    {
        $(this).removeClass("gvrowMove");
    }).click(function()
    {
        var check = $(this).find("td:eq(0) input[type='checkbox']");
        if (check.attr("checked")) {
            check.removeAttr("checked");
            $(this).removeClass("gvrowSel");
        } else {
            check.attr("checked", "checked");
            $(this).addClass("gvrowSel");
        }
    });
}

function parameter(key)
{
    var url = document.location.search;
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var r = url.replace("?", "").match(reg);
    if (r != null) return unescape(r[2]); return null;
}
