/*--------------------------文档信息-------------------------------------------
///
///功能：    分页控件
///创建人：  王思波
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///说明：    --------------
///
-----------------------------------------------------------------------------*/

//创建分页控件
function createPage(strDivPageID, strTableID) {
    var strPageHtml = "";
    //清空分页的DIV
    $("#" + strDivPageID).empty();
    //分页控件内容
    strPageHtml = "<ul>";
    strPageHtml += "    <li class=\"pape_btn Page_li\">";
    strPageHtml += "        <a id=\"btnRowCount_Page\" onclick=\"btnRowCountClick_Page()\">确定</a>";
    strPageHtml += "    </li>";
    strPageHtml += "    <li class=\"Page_li\">";
    strPageHtml += "        每页显示<input id=\"txtRowCount_Page\" type=\"text\" style=\"width:35px;text-align:right;\" maxlength=\"4\" onkeydown=\"txtRowCountKeydown_Page()\" value=\"" + window.parent.iRowsPerPage + "\"/>行";
    strPageHtml += "    </li>";
    strPageHtml += "    <li class=\"Page_li\">";
    strPageHtml += "        共计<label id=\"labRecordCount_Page\">0</label>条记录";
    strPageHtml += "    </li>";
    strPageHtml += "    <li class=\"Page_li\">";
    strPageHtml += "        共<label id=\"labPageCount_Page\">0</label>页";
    strPageHtml += "    </li>";
    strPageHtml += "    <li class=\"Page_li\">";
    strPageHtml += "        <a id=\"btnTailPage_Page\" href=\"#\" disabled=\"true\" class=\"disable\" title=\"尾页\" onclick=\"btnTailPageClick_Page()\">&gt;&gt;</a>"; //尾页
    strPageHtml += "    </li>";
    strPageHtml += "    <li class=\"Page_li\">";
    strPageHtml += "        <a id=\"btnNextPage_Page\" href=\"#\" disabled=\"true\" class=\"disable\" title=\"下一页\" onclick=\"btnNextPageClick_Page()\">&gt;</a>"; //下一页
    strPageHtml += "    </li class=\"Page_li\">";
    strPageHtml += "    <li id=\"liPage4_Page\" style=\"display:none\">";
    strPageHtml += "        <a id=\"btnPage4_Page\" name=\"btnPage\" href=\"#\" onclick=\"btnPageClick_Page(this.id)\">4</a>"; //数字按钮四
    strPageHtml += "    </li>";
    strPageHtml += "    <li id=\"liPage3_Page\" class=\"Page_li\" style=\"display:none\">";
    strPageHtml += "        <a id=\"btnPage3_Page\" name=\"btnPage\" href=\"#\" onclick=\"btnPageClick_Page(this.id)\">3</a>"; //数字按钮三
    strPageHtml += "    </li>";
    strPageHtml += "    <li id=\"liPage2_Page\" class=\"Page_li\" style=\"display:none\">";
    strPageHtml += "        <a id=\"btnPage2_Page\" name=\"btnPage\" href=\"#\" onclick=\"btnPageClick_Page(this.id)\">2</a>"; //数字按钮二
    strPageHtml += "    </li>";
    strPageHtml += "    <li id=\"liPage1_Page\" class=\"Page_li\" style=\"display:none\">";
    strPageHtml += "        <a id=\"btnPage1_Page\" name=\"btnPage\" href=\"#\" class=\"active\" disabled=\"true\" onclick=\"btnPageClick_Page(this.id)\">1</a>"; //数字按钮一
    strPageHtml += "    </li>";
    strPageHtml += "    <li class=\"Page_li\">";
    strPageHtml += "        <a id=\"btnLastPage_Page\" href=\"#\" disabled=\"true\" class=\"disable\" title=\"上一页\" onclick=\"btnLastPageClick_Page()\">&lt;</a>";    //上一页
    strPageHtml += "    </li>";
    strPageHtml += "    <li class=\"Page_li\">";
    strPageHtml += "        <a id=\"btnHomePage_Page\" href=\"#\" disabled=\"true\" class=\"disable\" title=\"首页\" onclick=\"btnHomePageClick_Page()\">&lt;&lt;</a>"; //首页
    strPageHtml += "    </li>";
    strPageHtml += "</ul>";
    strPageHtml += "<input id=\"hidTableID_Page\" type=\"hidden\" value=\"" + strTableID + "\"/>";
    strPageHtml += "<input id=\"hidCurrentPage_Page\" type=\"hidden\" value=\"1\"/>";
    strPageHtml += "<input id=\"hidWhereJson_Page\" type=\"hidden\" />";
    strPageHtml += "<input id=\"hidOldRowCount_Page\" type=\"hidden\" value=\"" + window.parent.iRowsPerPage + "\"/>";
    $("#" + strDivPageID).append(strPageHtml);

    //绑定数字控件
    $("#txtRowCount_Page").autoNumeric({ mDec: 0, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");

    //释放内存
    strPageHtml = null;
}

//首页按钮事件
function btnHomePageClick_Page() {
    //更新显示的页数
    $("#btnPage1_Page").text("1");
    $("#btnPage2_Page").text("2");
    $("#btnPage3_Page").text("3");
    $("#btnPage4_Page").text("4");

    //设置样式、加载数据
    $("#hidCurrentPage_Page").val("1");     //当前页
    setButtonStyle_Page("btnPage1_Page");   //当前页按钮样式
    loadGridData_Page();                    //加载数据
}

//上一页按钮事件
function btnLastPageClick_Page() {
    var intCurrentPage = parseInt($("#hidCurrentPage_Page").val()); //当前页
    var strButtonID = $(".active")[0].id;                           //当前按钮的ID

    //上一页的页数
    intCurrentPage--;

    //当前页不是第一个数字按钮时
    if (strButtonID != "btnPage1_Page") {
        //上一页按钮的ID取得
        strButtonID = strButtonID.substr(7, 1);
        strButtonID = "btnPage" + (parseInt(strButtonID) - 1) + "_Page";
    }
    else {
        //更新显示的页数
        $("#btnPage1_Page").text(intCurrentPage);
        $("#btnPage2_Page").text(intCurrentPage + 1);
        $("#btnPage3_Page").text(intCurrentPage + 2);
        $("#btnPage4_Page").text(intCurrentPage + 3);
    }

    //设置样式、加载数据
    $("#hidCurrentPage_Page").val(intCurrentPage);
    setButtonStyle_Page(strButtonID);
    loadGridData_Page();

    //释放内存
    intCurrentPage = null;
    strButtonID = null;
}

//页按钮事件
function btnPageClick_Page(strButtonID) {
    //设置样式、加载数据
    $("#hidCurrentPage_Page").val($("#" + strButtonID).text());
    setButtonStyle_Page(strButtonID);
    loadGridData_Page();
}

//下一页按钮事件
function btnNextPageClick_Page() {
    var intCurrentPage = parseInt($("#hidCurrentPage_Page").val()); //当前页
    var strButtonID = "";   //当前按钮的ID

    //下一页的页数
    intCurrentPage++;

    //当前页超出显示的页数的操作
    if (intCurrentPage > 4) {
        //更新显示的页数
        $("#btnPage1_Page").text(intCurrentPage - 3);
        $("#btnPage2_Page").text(intCurrentPage - 2);
        $("#btnPage3_Page").text(intCurrentPage - 1);
        $("#btnPage4_Page").text(intCurrentPage);
        //当前页按钮的ID
        strButtonID = "btnPage4_Page";
    }
    else {
        strButtonID = "btnPage" + intCurrentPage.toString() + "_Page"; //当前页按钮的ID
    }

    //设置样式、加载数据
    $("#hidCurrentPage_Page").val(intCurrentPage);
    setButtonStyle_Page(strButtonID);
    loadGridData_Page();

    //释放内存
    intCurrentPage = null;
    strButtonID = null;
}

//尾页按钮事件
function btnTailPageClick_Page() {
    var intPageCount = $("#labPageCount_Page").text(); //总页数
    var strButtonID = ""; //当前按钮ID

    if (intPageCount < 4) {
        strButtonID = "btnPage" + intPageCount.toString() + "_Page";
    }
    else {
        strButtonID = "btnPage4_Page"; //当前按钮ID
        //更新显示的页数
        if (intPageCount > 4) {
            $("#btnPage1_Page").text(intPageCount - 3);
            $("#btnPage2_Page").text(intPageCount - 2);
            $("#btnPage3_Page").text(intPageCount - 1);
            $("#btnPage4_Page").text(intPageCount);
        }
    }

    //设置样式、加载数据
    $("#hidCurrentPage_Page").val(intPageCount);
    setButtonStyle_Page(strButtonID);
    loadGridData_Page();

    //释放内存
    intPageCount = null;
    strButtonID = null;
}

//确定按钮
function btnRowCountClick_Page() {
    //如果为空零时恢复成旧的数量
    if (parseInt($("#txtRowCount_Page").val()) <= 0) {
        $("#txtRowCount_Page").val($("#hidOldRowCount_Page").val());
    }
    //变更后的操作
    else if ($("#txtRowCount_Page").val() != $("#hidOldRowCount_Page").val()) {
        //更新显示的页数
        $("#btnPage1_Page").text("1");
        $("#btnPage2_Page").text("2");
        $("#btnPage3_Page").text("3");
        $("#btnPage4_Page").text("4");

        //设置样式、加载数据
        $("#hidOldRowCount_Page").val($("#txtRowCount_Page").val());
        $("#hidCurrentPage_Page").val("1");
        setButtonStyle_Page("btnPage1_Page");
        loadGridData_Page();
    }

    //设置焦点
    $("#txtRowCount_Page").focus();
}

//每页显示行数
function txtRowCountKeydown_Page() {
    if (window.event.keyCode == 13) {
        btnRowCountClick_Page();
    }
}

//加载数据
function loadGridData_Page(objParameter) {
    var objDataSet = {};        //数据源
    var intPageCount = 0;       //总页数
    var intRecordCount = 0;     //总记录数
    var intShowRowNum = 0;      //每页显示行数
    var intCurrentPage = 0;     //当前选择页数
    var strParameterJson = "";  //参数的取得
    var strTableID = $("#hidTableID_Page").val(); //表格ID

    //设置操作数据时，保持当前分页状态
    setDefaultState_Page(objParameter);

    //参数的取得
    strParameterJson = getParameterJson_Page(objParameter);

    //每页显示行数
    intShowRowNum = parseInt($("#txtRowCount_Page").val());

    //数据的取得
    objDataSet = ParseData(window.external.DoOptions(strTableID, "GetList", strParameterJson));

    //有返回值时，执行以下操作
    if (objDataSet) {
        //总记录数
        intRecordCount = objDataSet.total;

        //记录数大于零的操作
        if (intRecordCount > 0) {
            //记录总页数
            if ((intRecordCount / intShowRowNum) != parseInt(intRecordCount / intShowRowNum)) {
                intPageCount = parseInt(intRecordCount / intShowRowNum) + 1;
            }
            else {
                intPageCount = parseInt(intRecordCount / intShowRowNum);
            }
        }
        else {
            $("#hidCurrentPage_Page").val("1"); //没有数据时，当前页为零
        }

        //表格中当前页数
        intCurrentPage = parseInt($("#hidCurrentPage_Page").val())

        //总页数、总记录数的设定
        $("#labPageCount_Page").text(intPageCount);
        $("#labRecordCount_Page").text(intRecordCount);

        //显示、隐藏数字页按钮
        buttonShowHidden_Page();

        //按钮可操作状态的设置
        lockPageButton_Page();

        //表格的设置
        $("#" + strTableID).clearGridData();                     //清除表格数据
        $("#" + strTableID).setGridParam({ rowNum: intShowRowNum }); //设置每页显示的行数
        $("#" + strTableID)[0].addJSONData({ total: 1, page: intCurrentPage, records: objDataSet.Records.length, rows: objDataSet.Records }); //加载数据
    }

    //有滚动条时改变表格宽度
    resizeGridScroll(strTableID);

    //释放内存
    intPageCount = null;
    intRecordCount = null;
    intShowRowNum = null;
    intCurrentPage = null;
    strParameterJson = null;
    strTableID = null;

    //返回值
    return objDataSet;
}

//四个页按钮的可见性设置
function buttonShowHidden_Page() {
    var intPageCount = parseInt($("#labPageCount_Page").text()); //总页数

    //数字页按钮1
    if (intPageCount >= 1) {
        $("#liPage1_Page").css("display", "block");
    }
    else {
        $("#liPage1_Page").css("display", "none");
    }
    //数字页按钮2
    if (intPageCount >= 2) {
        $("#liPage2_Page").css("display", "block");
    }
    else {
        $("#liPage2_Page").css("display", "none");
    }
    //数字页按钮3
    if (intPageCount >= 3) {
        $("#liPage3_Page").css("display", "block");
    }
    else {
        $("#liPage3_Page").css("display", "none");
    }
    //数字页按钮4
    if (intPageCount >= 4) {
        $("#liPage4_Page").css("display", "block");
    }
    else {
        $("#liPage4_Page").css("display", "none");
    }

    //释放内存
    intPageCount = null;
}

//锁定按钮
function lockPageButton_Page() {
    var intCurrentPage = parseInt($("#hidCurrentPage_Page").val()); //当前页
    var intPageCount = parseInt($("#labPageCount_Page").text());    //总页数

    //首页、上一页、第一个数字按钮
    if (intPageCount == 0 || intCurrentPage == 1) {
        //锁定按钮
        $("#btnHomePage_Page").attr("disabled", true);
        $("#btnLastPage_Page").attr("disabled", true);
        $("#btnPage1_Page").attr("disabled", true);
        //修改按钮样式
        $("#btnHomePage_Page").addClass("disable");
        $("#btnLastPage_Page").addClass("disable");
    }
    else {
        //解除锁定
        $("#btnHomePage_Page").attr("disabled", false);
        $("#btnLastPage_Page").attr("disabled", false);
        $("#btnPage1_Page").attr("disabled", false);
        //修改按钮样式
        $("#btnHomePage_Page").removeClass("disable");
        $("#btnLastPage_Page").removeClass("disable");
    }

    //尾页、下一页、第四个数字按钮
    if (intPageCount == 0 || intCurrentPage == intPageCount) {
        //锁定按钮
        $("#btnTailPage_Page").attr("disabled", true);
        $("#btnNextPage_Page").attr("disabled", true);
        $("#btnPage4_Page").attr("disabled", true);
        //修改按钮样式
        $("#btnTailPage_Page").addClass("disable");
        $("#btnNextPage_Page").addClass("disable");
    }
    else {
        //解除锁定
        $("#btnTailPage_Page").attr("disabled", false);
        $("#btnNextPage_Page").attr("disabled", false);
        $("#btnPage4_Page").attr("disabled", false);
        //修改按钮样式
        $("#btnTailPage_Page").removeClass("disable");
        $("#btnNextPage_Page").removeClass("disable");
    }

    //释放内存
    intCurrentPage = null;
    intPageCount = null;
}

//设置按钮的选择状态
function setButtonStyle_Page(strButtonID) {
    if ($(".active")[0].id != strButtonID) {
        $("#" + $(".active")[0].id).removeClass("active");
    }
    $("#" + strButtonID).addClass("active");
}

//设置操作数据时，保持当前分页状态
function setDefaultState_Page(objParameter) {
    var intCurrentPage = parseInt($("#hidCurrentPage_Page").val());             //当前页
    var intRowCount = $("#" + $("#hidTableID_Page").val()).getDataIDs().length; //当前页的行数
    var intPage4Num = parseInt($("#btnPage4_Page").text());                     //第四个数字页按钮的页数
    var intPageCount = parseInt($("#labPageCount_Page").text());                //总页数
    var intRecordCount = 0;     //总记录数
    var intShowRowNum = 0;      //每页显示行数
    var intNewPageCount = 0;    //新总页数
    var strButtonID = "";       //页数按钮ID

    //分页内部加载数据时，没有此参数
    if (objParameter) {
        //点击查询按钮后，分页的处理
        if (objParameter.flag == 1) {
            //更新显示的页数
            $("#btnPage1_Page").text("1");
            $("#btnPage2_Page").text("2");
            $("#btnPage3_Page").text("3");
            $("#btnPage4_Page").text("4");

            //设置样式、加载数据
            $("#hidCurrentPage_Page").val("1");
            setButtonStyle_Page("btnPage1_Page");

            //每显示行数没有确认时，设置为有效状态
            $("#hidOldRowCount_Page").val($("#txtRowCount_Page").val());
        }
        //删除数据后，分页的处理
        else if (intPageCount > 1 && objParameter.flag == 2) {
            //删除最后一页，最后一行数据时的处理
            if (intRowCount == 1 && intCurrentPage == intPageCount) {
                //当前页是尾页，并且只有一条数据，删除数据后尾页改变(减1)
                intCurrentPage--; //当前页

                //当前页面大于四页时的处理
                if (intCurrentPage >= 4) {
                    //更新显示的页数
                    $("#btnPage1_Page").text(intCurrentPage - 3);
                    $("#btnPage2_Page").text(intCurrentPage - 2);
                    $("#btnPage3_Page").text(intCurrentPage - 1);
                    $("#btnPage4_Page").text(intCurrentPage);
                    //当前页按钮的ID
                    strButtonID = "btnPage4_Page";
                }
                //当前页面小于等于四页时的处理
                else {
                    strButtonID = "btnPage" + intCurrentPage.toString() + "_Page";
                }
            }
            //删除倒数第2-4页的最后一条数据时的处理
            else if (intPageCount > 4 && intPage4Num == intPageCount) {
                //计算删除数据后的总页数
                intRecordCount = parseInt($("#labRecordCount_Page").text()) - 1;
                intShowRowNum = parseInt($("#txtRowCount_Page").val());
                if ((intRecordCount / intShowRowNum) != parseInt(intRecordCount / intShowRowNum)) {
                    intNewPageCount = parseInt(intRecordCount / intShowRowNum) + 1;
                }
                else {
                    intNewPageCount = parseInt(intRecordCount / intShowRowNum);
                }

                //删除数据后，页数减少时的处理
                if (intNewPageCount != intPageCount) {
                    $("#btnPage1_Page").text(intNewPageCount - 3);
                    $("#btnPage2_Page").text(intNewPageCount - 2);
                    $("#btnPage3_Page").text(intNewPageCount - 1);
                    $("#btnPage4_Page").text(intNewPageCount);

                    //当前页按钮的ID
                    strButtonID = $(".active")[0].id;
                    strButtonID = strButtonID.substr(7, 1);
                    strButtonID = "btnPage" + (parseInt(strButtonID) + 1) + "_Page";
                }
            }

            //预防错误的判断，没有逻辑意思
            if (strButtonID != "") {
                //设置样式、加载数据
                $("#hidCurrentPage_Page").val(intCurrentPage);
                setButtonStyle_Page(strButtonID);
            }
        }
    }

    //当输入的显示行数没有确认时，恢复到原来设置的数值
    if ($("#txtRowCount_Page").val() != $("#hidOldRowCount_Page").val()) {
        $("#txtRowCount_Page").val($("#hidOldRowCount_Page").val());
    }

    //释放内存
    intCurrentPage = null;
    intRowCount = null;
    intPage4Num = null;
    intPageCount = null;
    intRecordCount = null;
    intShowRowNum = null;
    intNewPageCount = null;
    strButtonID = null;
}

//查询条件、分页信息的取得
function getParameterJson_Page(objParameter) {
    var strParameter = "";  //参数信息

    //参数的处理
    if (objParameter) {
        //记录参数
        $("#hidWhereJson_Page").val("sortfield:\"" + objParameter.sortfield + "\",orderby:\"" + objParameter.orderby + "\"" + (!objParameter.where ? "" : "," + objParameter.where)); //sIndex:\"\",
    }

    //分类参数、查询条件
    strParameter = "{page:\"" + $("#hidCurrentPage_Page").val() + "\",Rows:\"" + $("#txtRowCount_Page").val() + "\"," + $("#hidWhereJson_Page").val() + "}";

    //返回值
    return strParameter;
}