﻿///获取对象。
var _get = function (ID) {
    return document.getElementById(ID);
}

///清理所有空格。
var _trim = function (str) {
    return str.replace(/[\s]/g, "");
}

///纯数字验证。
var _getNum = function (str) {
    var ret = true;
    for (i = 0; i < str.length; i++) {
        if (!/[0-9]/.test(str.substring(i, i + 1))) {
            ret = false;
            break;
        }
    }
    return ret;
}

///Email验证。
function _getEmail(str) {
    if (str.match(/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi)) {
        return true;
    }
    else {
        return false;
    }
}

///回车到指定控件。
function _enterGo(event, ID) {
    var isie = (document.all) ? true : false;
    if (isie) {
        if (event.keyCode == 13) {
            _get(ID).focus();
        }
    }
    else {
        if (event.which == 13) {
            _get(ID).focus();
        }
    }
}

///默认表鼠标样式。
(function ($) {
    $.extend($.fn, {
        defaultTableMouseStyle: function () {
            var trs = this;
            if (!$(this).hasClass("ListContentElse2")) {
                $(this).removeClass();
                $(this).addClass("ListContent");
            }
            return $(this).hover(function () {
                $(this).removeClass("ListContent").addClass("ListContentElse1");
            }, function () {
                $(this).removeClass("ListContentElse1").addClass("ListContent");
            }
        ).click(function () {
            $(trs).removeClass("ListContentElse1").removeClass("ListContentElse2").addClass("ListContent");
            $(this).removeClass("ListContentElse1").removeClass("ListContent").addClass("ListContentElse2");
        });
        }
    });
})(jQuery);

///Ajax全局屏蔽层。
var showBody = false;
$(document).ajaxStart(function () {

    showBody = true;

    $("body").append('<div id="bodyDiv">' +
                     '    <img id="loaderImg" src="' + appPath + '/Img/ajax-loader.gif" alt="操作中…" title="操作中…" />' +
                     '</div>');

    var top = document.documentElement.clientHeight / 2;

    $("#loaderImg").attr("style", "padding-top:" + top + "px;");

    $("#bodyDiv").attr("style", "width:100%;height:" + (document.documentElement.scrollHeight > document.documentElement.clientHeight ? document.documentElement.scrollHeight : document.documentElement.clientHeight) + "px;left:0px;top:0px;background-color:#3399FF;filter:Alpha(Opacity=50);opacity:0.5;position:fixed;z-index:33554488;");

}).ajaxError(function (e, xhr, o) {

    if (xhr.responseText.indexOf("× 没有权限的动作。") >= 0) {

        alert("您的登陆已失效或没有权限执行此动作，请登陆。");

        parent.location.href = "logout.aspx?rnd=" + (Math.random() * 10);

    }

}).ajaxSuccess(function (e, xhr) {

    if (xhr.responseText.indexOf("× 没有权限的动作。") >= 0 && window.confirm("您的登陆已失效或没有权限执行此动作，重新登陆？")) {

        parent.location.href = "logout.aspx?rnd=" + (Math.random() * 10);

    }

}).ajaxStop(function (data) {

    if (showBody) {

        $("#bodyDiv").remove();

        showBody = false;

    }

});

///提示信息返回层的显示与隐藏。
var hiddenValue = "";
var msgTimer = "";
var _showMsg = function (type, str) {

    if (type == 3) { shieldStart(""); }

    if (_get("showMessageDiv") == null) {

        $("body").append('<div id="showMessageDiv" style="display:none;"></div>');

    }

    window.clearInterval(msgTimer);

    msgTimer = null;

    hiddenValue = 100;

    $("#showMessageDiv").html('<table cellpadding="3" cellspacing="1" align="center" style="border: #' + (type == 0 ? "CB6057" : (type == 1 ? "7CCC4E" : (type == 2 ? "F1965D" : "65CAF2"))) + ' solid 1px;' +
                              'background-color: #FFFFFF;">' +
                              '    <tr style="height: 15px;background-color: #' + (type == 0 ? "CB6057" : (type == 1 ? "7CCC4E" : (type == 2 ? "F1965D" : "65CAF2"))) + ';">' +
                              '        <td colspan="2" style="text-align: center; font-size:14px; font-weight:bold; color:#FFFFFF;">' +
                              '            <img src="' + appPath + '/Img/Close.gif" alt="关闭" title="关闭" style="cursor: pointer; vertical-align: bottom; float: right;" onclick="JavaScript: _get(\'showMessageDiv\').style.visibility = \'hidden\';' + (type == 3 ? " shieldStop();" : "") + '" />' +
                              '            ' + (type == 0 ? "错 误" : (type == 1 ? "成 功" : (type == 2 ? "提 醒" : "确 认"))) +
                              '        </td>' +
                              '    </tr>' +
                              '    <tr>' +
                              '        <td style="width: 38px; text-align: right;">' +
                              '            <img src="' + appPath + '/Img/' + (type == 0 ? "Error" : (type == 1 ? "Success" : (type == 2 ? "Warning" : "Affirmance"))) + '.gif" alt="' + (type == 0 ? "错误" : (type == 1 ? "成功" : (type == 2 ? "提醒" : "确认"))) + '" title="' + (type == 0 ? "错误" : (type == 1 ? "成功" : (type == 2 ? "提醒" : "确认"))) + '" />' +
                              '        </td>' +
                              '        <td style="text-align: left; vertical-align: middle; font-size:12px; color:#000000;">' +
                              '            ' + str +
                              '        </td>' +
                              '    </tr>' +
                              '    <tr>' +
                              '        <td colspan="2" style="text-align: center;">' +
                              (type == 3 ? '<input type="button" id="yesInput" name="yesInput" style="border: 1px solid #000000; cursor: pointer;" value="确 定" />&nbsp;&nbsp;' : "") +
                              (type == 3 ? '<input type="button" id="noInput" name="noInput" style="border: 1px solid #000000; cursor: pointer;" value="取 消" />' : "") +
                              '        </td>' +
                              '    </tr>' +
                              '</table>');

    _get("showMessageDiv").style.visibility = "";

    _get("showMessageDiv").style.filter = "Alpha(Opacity=100)";

    _get("showMessageDiv").style.opacity = hiddenValue / 100;

    if (type == 0 || type == 1 || type == 2) { hiddenMsg("showMessageDiv"); }

    if (type == 3) { $("#yesInput").focus(); }

    var left = (document.documentElement.clientWidth - $("#showMessageDiv").width()) / 2;

    var top = (document.documentElement.clientHeight - $("#showMessageDiv").height()) / 2;

    $("#showMessageDiv").attr("style", "left:" + left + "px;top:" + top + "px;position:fixed;z-index:33554488;");

}
var hiddenMsg = function (ID) {

    msgTimer = window.setInterval(function () {

        hiddenValue--;

        _get(ID).style.filter = "Alpha(Opacity=" + hiddenValue + ")"; //for IE

        _get(ID).style.opacity = hiddenValue / 100; //for FF

        if (hiddenValue < 0) {

            window.clearInterval(msgTimer);

            _get(ID).style.visibility = "hidden";

            msgTimer = null;

        }

    }, 44);

}

///屏蔽层。
var shieldStart = function (str, ID) {

    $("body").append('<div id="shieldDiv">' +
                     '    <table style="width:100%;height:100%;margin:0px;">' +
                     '        <tr>' +
                     '            <td style="text-align:center;vertical-align:middle;">' + str +
                     '            </td>' +
                     '        </tr>' +
                     '    </table>' +
                     '</div>');

    $("#shieldDiv").attr("style", "width:100%;height:" + (document.documentElement.scrollHeight > document.documentElement.clientHeight ? document.documentElement.scrollHeight : document.documentElement.clientHeight) + "px;top:0px;left:0px;background-color:#3399FF;filter:Alpha(Opacity=50);opacity:0.5;position:fixed;z-index:33554466;");

    if (ID != null && _get(ID) != null) {

        var left = (document.documentElement.clientWidth - $("#" + ID).width()) / 2;

        var top = (document.documentElement.clientHeight - $("#" + ID).height()) / 2;

        $("#" + ID).attr("style", "left:" + left + "px;top:" + top + "px;position:fixed;display:none;z-index:33554477;");

        $("#" + ID).fadeIn();

    }

}
var shieldStop = function (ID) {

    $("#" + ID).fadeOut();

    $("#shieldDiv").remove();

}

///Ajax列表获取分页数字字符串。
var getPageNumsStr = function (thisPage, allPage, lastpage, nextpage, allCount, thisCount, key) {

    var num = 11; //第一页或最后一页时显示的页码个数；页码前后均出现省略号时中间部分显示的页码个数。

    var htmlStr = "";

    if (num < 5) { num = 5; }

    if (allPage > num) {

        if (thisPage < num - 1) {

            for (var i = 0; i < num - 1; i++) {

                if (i + 1 == thisPage) {

                    htmlStr += '<label id="listThisPageLabel" class="PageNum">' + thisPage + '</label>';

                }
                else {

                    htmlStr += '<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'' + (i + 1) + '\');' : 'getList(\'' + (i + 1) + '\');') + '">' + (i + 1) + '</a>';

                }

            }

            htmlStr += '…<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'' + allPage + '\');' : 'getList(\'' + allPage + '\');') + '">' + allPage + '</a>';

        }
        else if (thisPage > allPage - num + 2) {

            htmlStr += '<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'1\');' : 'getList(\'1\');') + '">1</a>…';

            for (var i = allPage - num + 1; i < allPage; i++) {

                if (i + 1 == thisPage) {

                    htmlStr += '<label id="listThisPageLabel" class="PageNum">' + thisPage + '</label>';

                }
                else {

                    htmlStr += '<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'' + (i + 1) + '\');' : 'getList(\'' + (i + 1) + '\');') + '">' + (i + 1) + '</a>';

                }

            }

        }
        else {

            htmlStr += '<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'1\');' : 'getList(\'1\');') + '">1</a>…';

            for (var i = thisPage - parseInt(num / 2) - 1; i < thisPage + num - parseInt(num / 2) - 1; i++) {

                if (i + 1 == thisPage) {

                    htmlStr += '<label id="listThisPageLabel" class="PageNum">' + thisPage + '</label>';

                }
                else {

                    htmlStr += '<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'' + (i + 1) + '\');' : 'getList(\'' + (i + 1) + '\');') + '">' + (i + 1) + '</a>';

                }

            }

            htmlStr += '…<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'' + allPage + '\');' : 'getList(\'' + allPage + '\');') + '">' + allPage + '</a>';

        }

    }
    else {

        for (var i = 0; i < allPage; i++) {

            if (i + 1 == thisPage) {

                htmlStr += '<label id="listThisPageLabel" class="PageNum">' + thisPage + '</label>';

            }
            else {

                htmlStr += '<a class="PageNums" onclick="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'' + (i + 1) + '\');' : 'getList(\'' + (i + 1) + '\');') + '">' + (i + 1) + '</a>';

            }

        }

    }

    htmlStr = '<a class="PageNums" onclick="JavaScript: ' + ((parseInt(thisPage) - 1) <= 0 ? '_showMsg(2, \'已到第一页。\');' : (key != null ? 'getList(\'' + key + '\', \'' + lastpage + '\');' : 'getList(\'' + lastpage + '\');')) + '"><</a>' + htmlStr;

    htmlStr += '<a class="PageNums" onclick="JavaScript: ' + ((parseInt(thisPage) + 1) > allPage ? '_showMsg(2, \'已到最后一页。\');' : (key != null ? 'getList(\'' + key + '\', \'' + nextpage + '\');' : 'getList(\'' + nextpage + '\');')) + '">></a>&nbsp;&nbsp;默认每页显示<select style="border:1px #DDDDDD solid;" id="pageCountSelect" name="pageCountSelect" onchange="JavaScript: ' + (key != null ? 'getList(\'' + key + '\', \'1\');' : 'getList(\'1\');') + '"><option value="10">10</option><option value="30">30</option><option value="50">50</option><option value="100">100</option></select>项 总 <strong>' + allCount + '</strong> 项 分 <strong>' + allPage + '</strong> 页 当前页 <strong><label id="listThisCountLabel">' + thisCount + '</label></strong> 项 <input type="text" id="goPageInput" name="goPageInput" style="height:15px;width:20px;border:1px #DDDDDD solid;" onkeydown="JavaScript: _enterGo(event, \'goPageSubmitInput\');" />页<input type="button" id="goPageSubmitInput" name="goPageSubmitInput" style="background-color:#FFFFFF;height:21px;border:1px solid #DDDDDD;cursor:pointer;" onclick="JavaScript: if ($(\'#goPageInput\').val() == \'\') { _showMsg(2, \'请输入跳转到的页面。\'); $(\'#goPageInput\').focus(); } else if (parseInt($(\'#goPageInput\').val()) > parseInt(\'' + allPage + '\')) { _showMsg(2, \'跳转页面不能大于总页数。\'); $(\'#goPageInput\').focus(); } else if (parseInt($(\'#goPageInput\').val()) == \'0\') { _showMsg(2, \'跳转页面不能小于第一页。\'); $(\'#goPageInput\').focus(); } else { ' + (key != null ? 'getList(\'' + key + '\', $(\'#goPageInput\').val());' : 'getList($(\'#goPageInput\').val());') + ' }" value="Go!" />';

    return htmlStr;

}

///goPageInput相关方法重写。（跳转页面输入）
var RWgoPageInput = function () {

    $("#goPageInput").keydown(function () {

        if (!_getNum(this.value.substring(this.value.length - 1, this.value.length))) {

            this.value = this.value.substring(0, this.value.length - 1);

        } //强化验证，自动删除除数字以外的字符

    }).keyup(function () {

        if (!_getNum(this.value.substring(this.value.length - 1, this.value.length))) {

            this.value = this.value.substring(0, this.value.length - 1);

        } //强化验证，自动删除除数字点以外的字符

    }).blur(function () {

        if (!_getNum(this.value)) {

            this.value = "";

        } //强化验证，自动删除除数字以外的字符

    });

}

///格式化日期时间。
Date.prototype.format = function (format) {

    var o = {

        "M+": this.getMonth() + 1, //month

        "d+": this.getDate(), //day

        "h+": this.getHours(), //hour

        "m+": this.getMinutes(), //minute

        "s+": this.getSeconds(), //second

        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter

        "S": this.getMilliseconds() //millisecond

    };

    if (/(y+)/.test(format)) {

        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

    }

    for (var k in o) {

        if (new RegExp("(" + k + ")").test(format)) {

            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));

        }

    }

    return format;

};
