
/*
oTable： 	需要分页的表格
pageNum： 	当前页数
endPageNum：	最大页数
count： 		总数
imgSrc： 	图片的地址
trCss： 		行的样式
sumbitType: 提交类型(1或者缺省：form提交 其他：url提交)
sumbitFormOrUrl: 提交的目标（如果submitType=1 则输入form名称，否则输入url地址）
*/
function Pagination(oTable, pageNum, endPageNum, count, imgSrc, sumbitType, sumbitFormOrUrl, trCss) {
    if (oTable) {
        if (endPageNum >= 1) {
            this.count = count;
            this.setTable(oTable);
            this.setTrCss(trCss);
            this.setImg(imgSrc);
            this.setPageNumAndEndPageNum(pageNum, endPageNum);
            this.setSubmitType(sumbitType, sumbitFormOrUrl);
            this.initFoot();
            var oThis = this;
            this._imgEvent = function (e) {
                oThis.imgEvent(e);
            };
            oTable.all.img.attachEvent("onclick", this._imgEvent);
        }
    } else {
        alert("加载错误！");
    }
}
Pagination.prototype.setTable = function (oTable) {
    this.oTable = oTable;
    this.rowNum = this.oTable.rows.length;
    if (this.rowNum > 1) {
        var cellsNumTemp = this.oTable.rows[0].cells.length;
        this.cellsNum = this.oTable.rows[this.rowNum - 1].cells.length;
        if (cellsNumTemp > this.cellsNum) {
            this.cellsNum = cellsNumTemp;
        }
    } else {
        this.cellsNum = this.oTable.rows[0].cells.length;
    }
};
Pagination.prototype.setPageNumAndEndPageNum = function (pageNum, endPageNum) {
    this.pageNum = pageNum;
    this.endPageNum = endPageNum;
};

//行的样式
Pagination.prototype.setTrCss = function (trCss) {
    if (typeof (trCss) == "undefined") {
        this.trCss = "odd";
        if (this.rowNum % 2 == 1) {
            this.trCss = "even";
        }
    } else {
        this.trCss = trCss;
    }
};

//图片src
Pagination.prototype.setImg = function (imgSrc) {
    if (typeof (imgSrc) == "undefined") {
        this.imgSrc = "../images/go.gif";
    } else {
        this.imgSrc = imgSrc;
    }
};
//提交类型和提交的目标
Pagination.prototype.setSubmitType = function (sumbitType, sumbitFormOrUrl) {
    if (typeof (sumbitType) == "undefined" || sumbitType == "1") {
        this.sumbitType = "1";
        if (typeof (sumbitFormOrUrl) == "undefined") {
            this.sumbitFormOrUrl = "form2";
        } else {
            this.sumbitFormOrUrl = sumbitFormOrUrl;
        }
    } else {
        this.sumbitType = sumbitType;
        this.sumbitFormOrUrl = sumbitFormOrUrl;
    }
};
//分页标记
Pagination.prototype.initFoot = function () {
    var newFoot;
    var newRow;
    var newCell;
    var oldCell = "";
    if (this.oTable.tFoot) {
        newFoot = this.oTable.tFoot;
        newRow = newFoot.rows[newFoot.rows.length - 1];
        newCell = newRow.cells[newRow.cells.length - 1];
    } else {
        newFoot = this.oTable.createTFoot();
        newRow = newFoot.insertRow(0);
        newRow.className = this.trCss;
        newCell = newRow.insertCell(0);
        newCell.colSpan = this.cellsNum;
    }
    newCell.align = "right";
    var tdHTMl = "<table cellpadding='0' cellspacing='0' border='0' width='100%' class='top' valign='top'><tr><td align='left' style=\"padding-left: 0px;\">" + newCell.innerHTML;
    tdHTMl += "</td><td align='right' style=\"padding-right: 0px;\"><span style=\"vertical-align: bottom;\" name='spanName'>" + "\u603b\u8ba1&nbsp;<font color=\"#B00000\" style=\"position:relative;top:-2px;;\"><b>" + this.count + "</b></font>&nbsp;\u6761&nbsp;\u5171&nbsp;" + "<font color=\"#B00000\" style=\"position:relative;top:-2px;;\"><b>" + this.endPageNum + "</b></font>&nbsp;\u9875&nbsp;&nbsp;&nbsp;&nbsp;";
    if (this.pageNum != 1) {
        tdHTMl += "<a href=\"javaScript:Pagination.pagination(1," + this.sumbitType + ",'" + this.sumbitFormOrUrl + "','" + this.oTable.id + "');\" title=\"首页\">首页</a>&nbsp;&nbsp;";
    }
    if (this.pageNum > 1) {
        tdHTMl += "<a href=\"javaScript:Pagination.pagination(" + (this.pageNum - 1) + ", " + this.sumbitType + ",'" + this.sumbitFormOrUrl + "','" + this.oTable.id + "');\" title=\"上页\" >上页</a>&nbsp;&nbsp;";
    }
    if (this.pageNum < this.endPageNum) {
        tdHTMl += "<a href=\"javaScript:Pagination.pagination(" + (this.pageNum + 1) + ", " + this.sumbitType + ",'" + this.sumbitFormOrUrl + "','" + this.oTable.id + "');\" title=\"下页\" >下页</a>&nbsp;&nbsp;";
    }
    if (this.pageNum != this.endPageNum) {
        tdHTMl += "<a href=\"javaScript:Pagination.pagination(" + this.endPageNum + ", " + this.sumbitType + ",'" + this.sumbitFormOrUrl + "','" + this.oTable.id + "');\" title=\"尾页\">尾页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
    }
    tdHTMl += "跳转至<input type=\"text\" class=\"text\" style=\"width:30px\" name=\"showPage\" value='" + this.pageNum + "'>页&nbsp;&nbsp;";
    tdHTMl += "<img name=\"img\" style=\"cursor:hand\" src='" + this.imgSrc + "'>&nbsp;</span></td></tr></table>";
    newCell.align = "right";
    newCell.innerHTML = tdHTMl;
    this.showPage = newFoot.all.showPage;
    this.oldShowPageValue = this.showPage.value;
};

//分页跳转事件模型
Pagination.prototype.imgEvent = function (e) {
    Pagination.pagination(this.showPage.value, this.sumbitType, this.sumbitFormOrUrl, this.oTable.id, this.endPageNum, this.oldShowPageValue, this.showPage);
};


//分页的方法
Pagination.pagination = function (num, sumbitType, sumbitFormOrUrl, oTable, endPage, oldShowPageValue, showPageObj) {
    if (typeof (endPage) == "undefined") {
        Pagination.windowSubmit(num, sumbitType, sumbitFormOrUrl, oTable);
    } else {
        if (num.length == 0) {
            alert("请输入页数！");
            showPageObj.value = oldShowPageValue;
        } else {
            if (new RegExp("/^\d+$/", "g").test(num)) {
                alert("请输入数字！");
                showPageObj.value = oldShowPageValue;
            } else {
                if (num > 0 && num <= endPage) {
                    Pagination.windowSubmit(num, sumbitType, sumbitFormOrUrl, oTable);
                } else {
                    alert("页数超出范围！");
                    showPageObj.value = oldShowPageValue;
                }
            }
        }
    }
};
Pagination.windowSubmit = function (num, sumbitType, sumbitFormOrUrl, oTable) {
    if (sumbitType == "1") {
        if (typeof (sumbitFormOrUrl) == "string") {
            sumbitFormOrUrl = document.all[sumbitFormOrUrl];
        }
        sumbitFormOrUrl.page.value = num;
        sumbitFormOrUrl.submit();
    } else {
        sumbitFormOrUrl += "&page=" + num + "&oTable=" + oTable;
        window.location.href = sumbitFormOrUrl;
    }
};

