﻿/* grid 1.0*/
/* dialog 1.0*/

var serverPath = "/"; //虚拟目录名称/service

$.cls = "className";
var $table;
$.fn.grid = function(params)
{
    var o = {
        odd: 'odd',
        even: 'even',
        over: 'over',
        sel: 'sel',
        page: '',
        click: true,
        check: true,
        url: "",
        fn: "",
        items: []
    };
    $.extend(o, params);
    var table = $(this);
    table.find("thead").append("<tr><th width='18px'><input class='checkbox' type='checkbox'/></th></tr>");
    $.each(o.items, function(i, item)
    {
        if (i > 0 && item.n != null) {
            table.find("thead tr").append("<th>" + item.n + "</th>");
            var th = table.find("thead tr th:last-child");
            if (item.w != null) {
                th.attr("width", item.w);
            }
            if (item.h != null) {
                th.attr("height", item.h);
            }
            if (item.type != null) {
                th.data("type", item.type);
            }
        }
    });
    var checkall = table.find("thead th:eq(0) input[type='checkbox']");
    table.data("odd", o.odd);
    table.data("even", o.even);
    table.data("over", o.over);
    table.data("sel", o.sel);
    table.data("page", o.page);
    table.data("pageindex", 1);
    table.data("click", o.click);
    table.data("check", o.check);
    table.data("url", o.url);
    table.data("fn", o.fn);
    table.gridColor();
    table.find('tbody tr').each(function()
    {
        table.gridRowEvent($(this));
    });
    var cindex = 0;
    table.find('thead tr th').each(function()
    {
        var th = $(this);
        var format = "";
        var parameter = "";
        var content = "";
        if (o.items.length - 1 >= cindex) {
            format = o.items[cindex].f;
            format = format == null ? "" : format;
            parameter = o.items[cindex].p;
            content = o.items[cindex].c != null ? o.items[cindex].c : content;
        }
        format = format == "money" ? "number" : format;
        if (format != "") {
            if (format != "number")
                format = "string";
        }
        th.data("format", format);
        th.data("sort", 0);
        th.data("cindex", cindex);
        th.data("parameter", parameter);
        th.data("content", content);
        th.data("content", content);
        cindex++;
        if (format != "") {
            th.attr("title", "点击按[" + th.text() + "]排序");
            th.addClass("sort");
            th.click(function()
            {
                var th = $(this);
                var cindex = th.data("cindex");
                var sort = th.data("sort");
                var format = th.data("format");
                var tbody = table.find("tbody");
                var rows = tbody.children();
                for (var i = 0; i < rows.length; i++) {
                    for (var j = 0; j < i; j++) {
                        var tr = tbody.find("tr:eq(" + i + ")");
                        var td = tr.find("td:eq(" + cindex + ")").text();
                        var tr2 = tbody.find("tr:eq(" + j + ")");
                        var td2 = tr2.find("td:eq(" + cindex + ")").text();
                        var ws = false;
                        switch (format) {
                            case "number":
                                td = parseFloat(td.replace(/^[^\d\.]*/, ""));
                                td2 = parseFloat(td2.replace(/^[^\d\.]*/, ""));
                                if (!isNaN(td) && !isNaN(td2))
                                    ws = (sort == 0) ? (td < td2) : (td > td2);
                                break;
                            case "string":
                                var v = td.localeCompare(td2);
                                ws = (sort == 0) ? v == 1 : v == -1;
                                break;
                        }
                        if (ws) {
                            tr2.before(tr);
                        }
                    }
                }
                th.data("sort", sort == 0 ? 1 : 0);
                table.gridColor();
                table.find("thead tr th").each(function()
                {
                    var th2 = $(this);
                    if (th2.data("format") != "")
                        th2.attr($.cls, "sort");
                });
                th.attr($.cls, sort == 0 ? "asc" : "desc");
            });
        }
    });
    table.find("thead th:eq(0) :checkbox").click(function()
    {
        table.find("tbody tr td:first-child :checkbox").attr("checked", $(this).attr("checked"));
        table.gridColor();
    });
    $table = table;
    setTimeout("$gridDataLoad()", 800);
}
$.fn.gridRowEvent = function(tr)
{
    var table = $(this);
    var o = {
        over: table.data("over"),
        click: table.data("click")
    };
    var checkall = table.find("thead th:eq(0) input[type='checkbox']");
    var check = tr.find("td:eq(0) :checkbox");
    tr.mouseover(function()
    {
        if (!check.attr("checked"))
            tr.attr($.cls, o.over);
    });
    tr.mouseout(function()
    {
        if (!check.attr("checked"))
            tr.attr($.cls, tr.data("class"));
    });
    tr.click(function()
    {
        if (o.click) {
            check.attr("checked", !check.attr("checked"));
            checkall.attr("checked", false);
            table.gridColor();
        }
    });
    check.click(function()
    {
        checkall.attr("checked", false);
        if (o.click)
            check.attr("checked", !check.attr("checked"));
        else
            table.gridColor();

    });
}
$.fn.gridColor = function()
{
    var table = $(this);
    var o = {
        odd: table.data("odd"),
        even: table.data("even"),
        over: table.data("over"),
        sel: table.data("sel")
    };
    var tbody = table.find("tbody");
    tbody.find('tr:odd').attr($.cls, o.odd).data("class", o.odd);
    tbody.find('tr:even').attr($.cls, o.even).data("class", o.even);
    tbody.find("tr td:first-child :checkbox[checked=true]").parents("td").parents("tr").attr($.cls, o.sel);
}
$.fn.gridAdds = function(rowdatas)
{
    var table = $(this);
    $.each(rowdatas, function(i, rowdata)
    {
        table.gridAdd(rowdata);
    });
}
$.fn.gridAdd = function(rowdata)
{
    var table = $(this);
    var tbody = table.find("tbody");
    var check = table.data("check");
    tbody.append("<tr></tr>");
    var tr = tbody.find("tr:last-child");
    table.find("thead th").each(function(i)
    {
        var p = $(this).data("parameter");
        var v = "";
        $.each(rowdata, function(name, value)
        {
            if (name == p)
                v = value;
        });
        if (i == 0) {
            tr.append("<td width='18px'><input type='checkbox' class='checkbox' style='display:" + (check ? "block" : "none") + "' value='" + v + "'/></td>");
        } else {
            var content = $(this).data("content");
            if (content != "") {
                content = content.replace(/{\d*}/g, v);
            } else {
                content = v;
            }
            content = content == "" ? "&nbsp;" : content;
            tr.append("<td>" + content + "</td>");
            if (content != "") {
                var type = $(this).data("type");
                if (type != null) {
                    if (type == "checkbox") {
                        var input = tr.find("td:last-child input[type='checkbox']");
                        input.attr("checked", (v == true || v == 1 || v == "true" || v == "1") ? true : false);
                    }
                }
            }
        }
    });
    table.gridRowEvent(tr);
    $(this).gridColor();
};
$.fn.gridDelall = function()
{
    $(this).find("tbody tr").remove();
}
$.fn.gridDel = function(id)
{
    var table = $(this);
    var trs = table.find("tbody tr");
    if (id == null) {
        del(trs.find("td:first-child :checkbox[checked=true]"));
    }
    else {
        if (id.constructor == Array) {
            $.each(id, function(i, v)
            {
                del(trs.find("td:first-child :checkbox[value=" + v + "]"));
            });

        } else if (typeof (id) == "string") {

        } else if (typeof (id) == "number") {
            del(trs.find("td:first-child :checkbox[value=" + id + "]"));
        }
    }
    function del(chs)
    {
        $.each(chs, function()
        {
            $(this).parents("td").parents("tr").remove();
        });
    }
    table.gridColor();
}
$.fn.gridGet = function()
{
    var cids = $(this).find("tbody tr td:first-child :checkbox[checked=true]");
    var arr = new Array(cids.length);
    var str = "";
    cids.each(function(row)
    {
        var value = $(this).val();
        arr[row] = value;
        str += (str == "" ? "" : ",") + value;
    });
    return { length: cids.length, arr: arr, str: str }
}
$.fn.gridData = function(page)
{
    var table = $(this);
    if (page == null) {
        page = table.data("pageindex");
    }
    $.cookie(window.location.href, page); //
    var url = table.data("url");
    if (url != "") {
        var fn = table.data("fn");
        var par = {};
        if (fn != "") {
            par = fn();
        }
        var g = url.indexOf("?") > -1 ? "&" : "?";
        url += g + "page=" + page + "&t=" + Date();
        var il;
        $.ajax({
            type: "get",
            url: url,
            dataType: "json",
            data: par,
            beforeSend: function(XMLHttpRequest)
            {
                il = $.infoLoad();
            },
            success: function(data, textStatus)
            {
                //$.alert(data.msg);
                if (data.state) {
                    table.gridDelall();
                    table.gridAdds(data.data);
                    table.gridPage(data.size, data.pagesize, data.pagecount, data.page);
                    table.data("pageindex", page);
                } else {
                    $.alert(data.msg);
                }
            },
            complete: function(XMLHttpRequest, textStatus)
            {
                if (il != null)
                    il.infoClose();
            },
            error: function()
            {
                if (il != null)
                    il.infoClose();
                $.alert("加载数据出错！");
            }
        });
    }
}
$gridDataLoad = function()
{
    var pageindex = $.cookie(window.location.href);
    if (pageindex == null) {
        pageindex = 1;
    }
    $table.gridData(pageindex);
}

$.fn.gridPage = function(size, pagesize, pagecount, page)
{
    var table = $(this);
    var p = table.data("page");
    if (p != null) {
        var pdiv = $(p);
        pdiv.html("");
        pdiv.append("<label class='info'>共<span>" + size + "</span>条数据 <span>" + pagesize + "</span>条/页  <span>" + page + "</span>/<span>" + pagecount + "</span>页</label>");
        if (pagecount > 1) {
            var start = page - 4;
            start = start < 1 ? 1 : start;
            if (page > pagecount - 5) {
                start = pagecount - 9;
            }
            start = start < 1 ? 1 : start;
            if (page > start) {
                pdiv.append("<label class='info pa'><span style='display:none'>" + (page - 1) + "</span>上一页</label>");
            }
            for (var i = start; i < (start + 10) && i <= pagecount; i++) {
                pdiv.append("<label class='" + (i == page ? "sel" : "info item") + "'>" + i + "</label>");
            }
            if (page < pagecount) {
                pdiv.append("<label class='info pa'><span style='display:none'>" + (page + 1) + "</span>下一页</label>");
            }
            if (pagecount > 1) {
                pdiv.append("<input type='text' class='text' title='输入页数按“Enter”跳转'/>");
            }
            function retxt(t)
            {
                var v = t.val().replace(/[^\d*]/g, "");
                if (v == 0) {
                    v = 1;
                    t.val("");
                }
                if (v > pagecount) {
                    v = pagecount;
                    t.val(pagecount);
                }
                return v;
            }
            pdiv.find("input[type=text]").keyup(function()
            {
                retxt($(this));
            }).blur(function() { retxt($(this)); }).keypress(function(e)
            {
                if (e.which == 13) {
                    table.gridData(retxt($(this)));
                }
            });
            pdiv.find("label:gt(0)").mouseover(function()
            {
                var l = $(this);
                if (l.attr($.cls) != "sel") {
                    l.addClass("move");
                }
            }).mouseout(function()
            {
                var l = $(this);
                if (l.attr($.cls) != "sel") {
                    l.removeClass("move");
                }
            }).click(function()
            {
                var l = $(this);
                if (l.attr($.cls) != "sel") {
                    var p = l.text().replace(/[^\d*]/g, "");
                    table.gridData(p);
                }
            });
        }
    }
}
///弹出框
/* dialog 1.0*/
$.dialogIndex = 0;
$.dialogCount = 0;
$.dialog = function(params)
{
    var o = { title: "系统提示", w: 300, h: 160, maxw: 600, maxh: 400, minw: 300, minh: 160, bg: true, content: "&nbsp;", cancelbut: true, but: {}, close: function() { } };
    $.extend(o, params);
    o.w = o.w < 300 ? 300 : o.w;
    o.h = o.h < 160 ? 160 : o.h;
    o.minw = o.minw < 300 ? 300 : o.minw;
    o.minh = o.minh < 160 ? 160 : o.minh;
    o.content = o.content == "" ? "&nbsp;" : o.content;
    $.dialogIndex++;
    $("body").attr("scroll", "no");
    var hw = $(document).width();
    var hh = $(document).height();
    var dialogbg = $("<div id='dialog_bg" + $.dialogIndex + "' class='" + (o.bg ? "dialogbg" : "dialogbg2") + "' style='width:" + hw + "px;height:" + hh + "px;z-index:" + (1155 * $.dialogIndex + 50) + ";'></div>");
    var dialogp = $("<div id='dialog_p" + $.dialogIndex + "' style='width:" + (hw - 10) + "px;height:" + (hh - 15) + "px;z-index:" + (1155 * $.dialogIndex + 100) + "; position: absolute; left: 0px; top: 0px;'></div>");
    var dialog = $("<div id='dialog_" + $.dialogIndex + "' class='dialog' style='z-index:" + (1155 * $.dialogIndex + 200) + ";'><div class='title'><div class='titleb tleft'></div><div class='tcenter'><div style='position: relative;'><div class='titleb tico'></div><div class='ttext'></div><div class='titleb tclose'></div> </div> </div> <div class='titleb tright'></div> </div> <div class='center'><div class='border cleft'></div><div class='ccenter'><div class='cctop'><div class='cctoptxt'></div></div><div class='ccbottom'></div></div><div class='border cright'></div></div><div class='border bottom'></div></div>");
    $("body").append(dialogbg);
    $("body").append(dialogp);
    $("body").append(dialog);
    dialogbg.bgiframe();
    dialogp.bgiframe();
    dialog.bgiframe();
    dialog.data("dialogIndex", $.dialogIndex);
    var hst = $(document).scrollTop();
    var hsl = $(document).scrollLeft();
    var left = parseInt(($("html").width() - o.w) / 2) + hsl;
    var top = parseInt(($("html").height() - o.h) / 2) + hst;
    left = (left < 0 ? 10 : left);
    top = (top < 0 ? 5 : top);
    dialog.css("left", left + "px");
    dialog.css("top", top + "px");
    var title = dialog.find("div.title .ttext");
    var close = dialog.find("div.title .tclose");
    var center = dialog.find("div.center");
    var cctoptxt = center.find("div.cctoptxt");
    var ccbottom = center.find("div.ccbottom");
    title.text(o.title);
    cctoptxt.html(o.content);
    $.each(o.but, function(name, e)
    {
        var cbo = name.indexOf("__") > 0 ? true : false;
        if (cbo) {
            name = name.replace("__", "");
        }
        ccbottom.append("&nbsp;<input type='button' class='ccbutton' value='" + name + "'/>");
        if (e == "close") {
            e = dclose;
        }
        ccbottom.find("input[type=button]:last-child").click(e);
        if (cbo) {
            ccbottom.bind("click", dclose);
        }
    });
    if (o.cancelbut) {
        ccbottom.append("&nbsp;<input type='button' class='ccbutton' value='取消'/>");
        ccbottom.find("input[type=button]:last-child").click(dclose);
    }
    close.mouseover(function()
    {
        $(this).addClass("tclosemove");
    }).mouseout(function()
    {
        $(this).removeClass("tclosemove");
    }).click(function()
    {
        dclose();
    }).attr("title", "关闭");
    function dclose()
    {
        o.close();
        dialog.remove();
        dialogp.remove();
        dialogbg.remove();
        $.dialogCount--;
        if ($.dialogCount < 1)
            $("body").attr("scroll", "yes");
    }
    function dsize()
    {
        var heigth = dialog.height();
        var width = dialog.width();
        title.css("width", (width - 28) + "px");
        dialog.find("div.title .tright").css("left", (width - 4) + "px");
        dialog.find("div.title .tclose").css("left", (width - 25) + "px");
        dialog.find("div.title .tcenter").css("width", (width - 8) + "px");
        var center = dialog.find("div.center");
        center.css("height", (heigth - 28) + "px").css("width", (width - 2) + "px");
        center.find("div.cleft").css("height", (heigth - 28) + "px");
        center.find("div.ccenter").css("height", (heigth - 28) + "px").css("width", (width - 2) + "px");
        center.find("div.cright").css("height", (heigth - 28) + "px").css("left", (width - 1) + "px");
        center.find("div.cctoptxt").css("height", (heigth - 68) + "px");
        dialog.find("div.bottom").css("width", (width) + "px");
    }
    if ($.browser.mozilla || ($.browser.msie && $.browser.version >= 7.0)) {
        dialog.draggable({ containment: dialogp, handle: title, scroll: true });
    }
    if ($.browser.mozilla || ($.browser.msie && $.browser.version >= 7.0)) {
        dialog.resizable({ maxHeight: o.maxh, maxWidth: o.maxw, minHeight: o.minh, minWidth: o.minw });
    }
    dialog.width(o.w);
    dialog.height(o.h);
    dsize();
    if ($.browser.mozilla || ($.browser.msie && $.browser.version >= 7.0)) {
        dialog.resize(function() { dsize(); });
    }
    $.dialogCount++;
    return dialog;
}
//关闭窗口
$.fn.closeForm = function()
{
    var dialogIndex = $(this).data("dialogIndex");
    $(this).remove();
    $("#dialog_bg" + dialogIndex).remove();
    $("#dialog_p" + dialogIndex).remove();
    $.dialogCount--;
    if ($.dialogCount < 1)
        $("body").attr("scroll", "yes");
}
//获取窗口内容
$.fn.content = function()
{
    return $(this).find("div.ccenter").find("div.cctoptxt");
}
//警告框
$.alert = function(msg, img)
{
    $.confirm({ msg: msg, img: img, cancelbut: false });
}
//确认框
$.confirm = function(params)
{
    var o = {
        msg: "提示",
        img: 0,
        bg: true,
        cancelbut: true,
        ok: "",
        no: "",
        close: function() { }
    };
    $.extend(o, params);
    var eimg = "warning";
    var but = { 确定__: o.ok };
    if (o.ok != "" && o.no != "") {
        but = { 是__: o.ok, 否__: o.no }
        if (o.img == 0)
            o.img = 2;
    }
    if (o.img == 0)
        o.img = 1;
    switch (o.img) {
        case 2:
            eimg = "info";
            break;
        case 3:
            eimg = "question";
            break;
        case 4:
            eimg = "error";
            break;
    }
    var content = "<div style='margin:10px 20px 10px 20px;'>";
    content += "<div style='width: 32px; height: 32px; float: left;margin-top: 10px;' class='" + eimg + "'></div>";
    content += "<div style='margin-left: 40px;vertical-align:middle;'>" + o.msg + "</div></div>";
    $.dialog({ bg: o.bg, content: content, cancelbut: o.cancelbut, but: but, close: o.close });
}
//窗口
$.form = function(params)
{
    var o = {
        title: "新窗口",
        bg: true,
        w: 620,
        h: 420,
        mw: 920,
        mh: 550,
        url: "",
        content: "",
        cancelbut: true,
        but: {},
        close: function() { }
    };
    $.extend(o, params);

    if (o.url != "") {
        o.content = "<iframe src='" + o.url + "' style='width: 100%; height: 100%' frameborder='0' scrolling='yes'></iframe>";
    }
    return $.dialog({ title: o.title, bg: o.bg, w: o.w, h: o.h, maxw: o.mw, maxh: o.mh, content: o.content, cancelbut: o.cancelbut, but: o.but, close: o.close });
}
//列表加载提示
$.infoLoad = function(msg)
{
    msg = msg == null ? "正在加载..." : msg;
    var hw = $("html").width();
    $.dialogIndex++;
    $("body").append("<div id='infoLoad" + $.dialogIndex + "' class='dialogLoad'><div>" + msg + "</div></div>");
    var load = $("body").find("#infoLoad" + $.dialogIndex + "");
    load.css("left", (hw - load.width() - 50) + "px");
    load.css("top", "12px");
    return load;
}
//关闭列表加载提示
$.fn.infoClose = function()
{
    if ($(this) != null) {
        $(this).remove();
    }
}
//列表加载提示
$.Loading = function(msg)
{
    $.dialogIndex++;
    var hw = $(document).width();
    var hh = $(document).height();
    var dhtml = "<div id='dialog_Loading_bg" + $.dialogIndex + "' class='dialogbg'";
    dhtml += " style='width:" + hw + "px;height:" + hh + "px;z-index:" + (1000 + (20 * $.dialogIndex) + $.dialogIndex) + "'>";
    dhtml += "</div>";
    $("body").append(dhtml);
    $("body").append("<div id='dialog_Loading" + $.dialogIndex + "' class='dialogLoad2' style='left:" + (hw / 2 - 50) + "px;top:" + (hh / 2 - 50) + "px;z-index:" + (1000 + (20 * $.dialogIndex) + $.dialogIndex + 10) + ";'><div>" + (msg == null ? "处理中..." : msg) + "</div></div>");
    return $.dialogIndex;
}
//关闭列表加载提示
$.LoadingClose = function(index)
{
    if (index != null) {
        $("body").find("#dialog_Loading" + index).remove();
        $("body").find("#dialog_Loading_bg" + index).remove();
    }
}
//创建表单
$.fn.form = function(params)
{
    var o = {
        id: "",
        action: "",
        target: "",
        input: []
    };
    $.extend(o, params);
    var d = $(this);
    d.html("");
    d.append("<form enctype='multipart/form-data' id='" + o.id + "' name='" + o.id + "' action='" + o.action + "' target='" + o.target + "'>" +
            "<table id='tb" + o.id + "' name='tb" + o.id + "' style='vertical-align:top;' class='minfo' width='96%' border='0' cellpadding='0' cellspacing='0'></table></form>");
    var form = d.find("form[name='" + o.id + "']");
    var table = d.find("table[name='tb" + o.id + "']");
    form.data("params", o);
    $.each(o.input, function(i, pi)
    {
        var tr = "<tr><td class='td'>" + pi.label + "：</td>";
        if (pi.type == "checkbox" || pi.type == "radio") {
            var items = "<td>";
            $.each(pi.item, function(io, option_)
            {
                var ischecked = (option_.checked != null && option_.checked == true) ? " checked='checked' " : "";
                items += "<input id='" + pi.name + "_" + io + "' name='" + pi.name + "' class='" + (pi.type == "checkbox" ? "checkbox" : "radio") + "' type='" + pi.type + "' value='" + option_.value + "' " + ischecked + "></input>" + (option_.text == null ? option_.value : option_.text) + "&nbsp;";
            });
            tr += items + "</td><td><div class='tdval' id='" + pi.name + "v'></div></td>";
        } else if (pi.type == "textarea") {
            tr += "<td><textarea id='" + pi.name + "' name='" + pi.name + "'></textarea></td><td><div class='tdval' id='" + pi.name + "v'></div></td>";
        } else if (pi.type == "select") {
            var items = "";
            $.each(pi.item, function(io, option_)
            {
                items += "<option value='" + option_.value + "'>" + (option_.text == null ? option_.value : option_.text) + "</option>";
            });
            tr += "<td><select id='" + pi.name + "' name='" + pi.name + "'>" + items + "</select></td><td><div class='tdval' id='" + pi.name + "v'></div></td>";
        } else if (pi.type == "editor") {
            var path =
            tr += "<td colspan='2'><input type='hidden' id='" + pi.name + "' name='" + pi.name + "' value='' />";
            tr += "<input type='hidden' id='" + pi.name + "___Config' value='HtmlEncodeOutput=true' />";
            tr += "<iframe id='" + pi.name + "___Frame' src='" + serverPath + "inc/fckeditor/editor/fckeditor.html?InstanceName=" + pi.name + "&amp;Toolbar=" + (pi.boolbar != null ? pi.boolbar : "Default") + "' width='" + (pi.w != null ? (pi.w + "px") : "100%") + "' height='" + (pi.h != null ? pi.h : "220") + "px' frameborder='no' scrolling='no'></iframe>";
            tr += "<div class='tdval' id='" + pi.name + "v'></div></td>";
        } else if (pi.type == "label") {
            tr += "<td colspan=2 id='" + pi.name + "'>" + pi.content + "</td>";
        } else {
            var classname = "text";
            switch (pi.type) {
                case "file":
                    classname = "text";
                    break;
            }
            tr += "<td><input id='" + pi.name + "' name='" + pi.name + "' class='" + classname + "' type='" + pi.type + "' /></td><td><div class='tdval' id='" + pi.name + "v'></div></td>";
        }
        tr += "</tr>";
        table.append(tr);
        var input = table.find("[name='" + pi.name + "']");
        var inputv = table.find("#" + pi.name + "v");
        input.data("info", pi);
        if (pi.type == "radio" || pi.type == "checkbox") {
            $.each(input, function(i, item)
            {
                if ($(this).val() == pi.value) {
                    $(this).attr("checked", true);
                }
            });
        } else if (pi.type == "editor") {
            if (pi.value != null)
                input.val(pi.value);
            //FCKeditorAPI.GetInstance(pi.name).SetHTML(pi.value);
        } else {
            if (pi.value != null)
                input.val(pi.value);
        }
        if (pi.w != null)
            input.css("width", pi.w);
        if (pi.h != null)
            input.css("height", pi.h);
        if (pi.cls != null)
            input.addClass(pi.cls);
        if (pi.val != null) {
            input.data("isval", true);
            inputv.addClass("regs show");
            inputv.html(pi.val.s);
        }
        input.focus(function()
        {
            input.addClass("inputfocuscolor");
            if (pi.val != null && pi.val.type != null) {
                inputv.removeClass("success").removeClass("errors").addClass("focus");
                inputv.html(pi.val.f);
            }
        }).blur(function()
        {
            input.removeClass("inputfocuscolor");
            if (pi.val != null && pi.val.type != null) {
                if (input.inputVal(form)) {
                    inputv.removeClass("focus").removeClass("errors").addClass("success");
                    inputv.html(pi.val.c);
                }
                else {
                    inputv.removeClass("focus").removeClass("success").addClass("errors");
                    inputv.html(pi.val.e);
                }
                if (pi.val.type == "int" && pi.type == "text") {
                    input.val(input.val().replace(/[^\d*]/g, ""));
                }
            }
        });
    });
    return form;
}
//验证控件
$.fn.inputVal = function(form)
{
    var input = $(this);
    var info = input.data("info");
    var val = info.val;
    var value = "";
    if (info.type == "radio" || info.type == "checkbox") {
        $.each(input, function(i, item)
        {
            if ($(this).attr("checked") == true) {
                value = $(this).val();
            }
        });
    } else if (info.type == "editor") {
        value = FCKeditorAPI.GetInstance(input.attr("name")).GetXHTML(true);
        input.val(value);
    } else {
        value = input.val();
    }
    value = $.trim(value);
    var bo = true;
    if (val != null) {
        bo = false;
        switch (val.type) {
            case "int":
                var ireg = new RegExp("^[0-9]\\d*$");
                bo = ireg.test(value);
                if (bo && val.min != null) {
                    bo = (parseInt(value) >= val.min);
                }
                if (bo && val.max != null) {
                    bo = (parseInt(value) <= val.max);
                }
                break;
            case "string":
                if (val.min != null) {
                    bo = (value.length >= val.min);
                }
                if (bo && val.max != null) {
                    bo = (value.length <= val.max);
                }
                break;
            case "null":
                bo = value == "" ? false : true;
                break;
            case "exp":
                var ereg = new RegExp(val.reg);
                bo = ereg.test(value);
                break;
            case "date":
                bo = isDate(value);
                break;
            case "time":
                bo = isTime(value);
                break;
            case "datetime":
                bo = isDateTime(value);
                break;
            case "des":
                var des = form.find("[name='" + val.desid + "']");
                bo = (des.val() == value) ? true : false;
                break;
        }
    }
    return bo;
}
//验证表单
$.fn.formVal = function()
{
    var bo = true;
    var form = $(this);
    var inputs = form.data("params").input;
    var input1 = null;
    $.each(inputs, function(i, item)
    {
        var input = form.find("[name='" + item.name + "']");
        if (item.val != null && item.val.type != null) {
            var val = item.val;
            var inputv = form.find("#" + item.name + "v");
            var isval = input.inputVal(form);
            bo = isval == false ? false : bo;
            if (isval) {
                inputv.removeClass("focus").removeClass("errors").addClass("success");
                inputv.html(val.c);
            } else {
                inputv.removeClass("focus").removeClass("success").addClass("errors");
                inputv.html(val.e);
                if (input1 == null) {
                    input1 = input;
                }
            }

        }
    });
    if (input1 != null) {
        input1.focus();
    }
    return bo;
}
//设置表单数据
$.fn.formData = function(data)
{
    var form = $(this);
    var forminput = form.data("params");
    $.each(data, function(name, value)
    {
        $.each(forminput.input, function(i, pi)
        {
            if (name == pi.name) {
                if (pi.type == "radio" || pi.type == "checkbox") {
                    $.each(pi.item, function(j, item)
                    {
                        if (value.toString() == item.value.toString()) {
                            form.find("#" + pi.name + "_" + j).attr("checked", true);
                        }
                    });
                } else if (pi.type == "editor") {
                    form.find("[name='" + pi.name + "']").val(value);
                    if (typeof FCKeditorAPI == "object") {
                        FCKeditorAPI.GetInstance(pi.name).SetHTML(value, true);
                    }
                } else {
                    form.find("[name='" + pi.name + "']").val(value);
                }
            }
        });
    });
}

//正则表达式
var regexEnum =
{
    intege: "^-?[1-9]\\d*$", 				//整数
    intege1: "^[1-9]\\d*$", 				//正整数
    intege2: "^-[1-9]\\d*$", 				//负整数
    num: "^([+-]?)\\d*\\.?\\d+$", 		//数字
    num1: "^[1-9]\\d*|0$", 				//正数（正整数 + 0）
    num2: "^-[1-9]\\d*|0$", 				//负数（负整数 + 0）
    decmal: "^([+-]?)\\d*\\.\\d+$", 		//浮点数
    decmal1: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$", //正浮点数
    decmal2: "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$", //负浮点数
    decmal3: "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$", //浮点数
    decmal4: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$", //非负浮点数（正浮点数 + 0）
    decmal5: "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$", //非正浮点数（负浮点数 + 0）
    email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
    color: "^[a-fA-F0-9]{6}$", 			//颜色
    url: "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //url
    chinese: "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$", //仅中文
    ascii: "^[\\x00-\\xFF]+$", 			//仅ACSII字符
    zipcode: "^\\d{6}$", 					//邮编
    mobile: "^(13|15)[0-9]{9}$", 			//手机
    ip4: "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$", //ip地址
    notempty: "^\\S+$", 					//非空
    picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
    rar: "(.*)\\.(rar|zip|7zip|tgz)$", 							//压缩文件
    date: "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$", 				//日期
    qq: "^[1-9]*[1-9][0-9]*$", 			//QQ号码
    tel: "^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$", //电话号码的函数(包括验证国内区号,国际区号,分机号)
    username: "^\\w+$", 					//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
    letter: "^[A-Za-z]+$", 				//字母
    letter_u: "^[A-Z]+$", 				//大写字母
    letter_l: "^[a-z]+$", 				//小写字母
    idcard: "^[1-9]([0-9]{14}|[0-9]{17})$"	//身份证
}
//身份证
var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }
function isCardID(sId)
{
    var iSum = 0;
    var info = "";
    if (!/^\d{17}(\d|x)$/i.test(sId)) return "你输入的身份证长度或格式错误";
    sId = sId.replace(/x$/i, "a");
    if (aCity[parseInt(sId.substr(0, 2))] == null) return "你的身份证地区非法";
    sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, "/"));
    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "身份证上的出生日期非法";
    for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
    if (iSum % 11 != 1) return "你输入的身份证号非法";
    return true; //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
}
//短时间，形如 (13:04:06)
function isTime(str)
{
    var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
    if (a == null) { return false }
    if (a[1] > 24 || a[3] > 60 || a[4] > 60) {
        return false;
    }
    return true;
}
//短日期，形如 (2003-12-05)
function isDate(str)
{
    var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (r == null) return false;
    var d = new Date(r[1], r[3] - 1, r[4]);
    return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
}
//长时间，形如 (2003-12-05 13:04:06)
function isDateTime(str)
{
    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
    var r = str.match(reg);
    if (r == null) return false;
    var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
    return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
}
