﻿function Page(iAbsolute, count) {
    if (iAbsolute == null || count == null) { return; }
    var countIndex = parseInt(parseInt(count) % parseInt(iAbsolute) == 0
    ? parseInt(count) / parseInt(iAbsolute) : parseInt(count) / parseInt(iAbsolute) + 1);
    if (countIndex >= 200) {
        count = 200;
        this.rowCount = (count * iAbsolute); //记录数
        this.pageCount = (count * iAbsolute); //页数
        this.__dataRows__ = (count * iAbsolute); //记录行引用
    } else {
        this.rowCount = count; //记录数
        this.pageCount = count; //页数
        this.__dataRows__ = count; //记录行引用
    }
    this.absolute = iAbsolute; //每页最大记录数
    this.pageIndex = 0; //页索引
    this.__init__(); //初始化;
    this.searchType = document.getElementById("hidSearchType").value;
    this.solidPath = "ProductDataProvider.aspx?";
};
/**//*
初始化
*/
Page.prototype.__init__ = function() {
    try {
        this.absolute = (this.absolute <= 0) || (this.absolute > this.rowCount) ? this.rowCount : this.absolute;
        this.pageCount = parseInt(this.rowCount % this.absolute == 0
    ? this.rowCount / this.absolute : this.rowCount / this.absolute + 1); //计算有多少页
    } catch (exception) { }
    //alert("aaa="+this.pageCount+"bb"+this.absolute+"cc"+this.rowCount);
    this.__updateTableRows__();

};

Page.prototype.GetBar = function(obj) {
    var bar = document.getElementById(obj.id);
    bar.innerHTML = "每页" + this.absolute + "条/共" + this.rowCount + "条"; // 第2页/共6页 首页 上一页 1 2 3 4 5 6 下一页 末页  
};

/**//*
下一页
*/
Page.prototype.nextPage = function() {
    if (this.pageIndex + 1 < this.pageCount) {
        document.getElementById("loadingdivShow").style.display = "";
        goTop();
        this.pageIndex += 1;
        // "&keyWord=" + encodeURIComponent(document.getElementById("keyword").value) +
        var hidPath = document.getElementById("hidPath").value;
        this.pageLayoutParams();
    }
};
/**//*
上一页
*/
Page.prototype.prePage = function() {
   goTop();
    document.getElementById("loadingdivShow").style.display = "";
    if (this.pageIndex >= 1) {
        this.pageIndex -= 1;
        this.__updateTableRows__(this.absolute);
        var hidPath = document.getElementById("hidPath").value;
        this.pageLayoutParams();
    }
};
/**//*
首页
*/
Page.prototype.firstPage = function() {
   goTop();
    document.getElementById("loadingdivShow").style.display = "";
    if (this.pageIndex != 0) {
        this.pageIndex = 0;
        var hidPath = document.getElementById("hidPath").value;
        this.pageLayoutParams();
    }
};
/**//*
尾页
*/
Page.prototype.lastPage = function() {
   goTop();
    if (this.pageIndex + 1 != this.pageCount) {
        document.getElementById("loadingdivShow").style.display = "";
        this.pageIndex = this.pageCount - 1;
        searchShow = new SearchShow();
        this.pageLayoutParams();
    }
};
/**//*
页定位方法
*/
Page.prototype.aimPage = function(iPageIndex) {
   goTop();
    document.getElementById("loadingdivShow").style.display = "";
    if (iPageIndex > this.pageCount - 1) {
        this.pageIndex = this.pageCount - 1;
    } else if (iPageIndex < 0) {
        this.pageIndex = 0;
    } else {
        this.pageIndex = iPageIndex;
    }
    var num = iPageIndex + 1;
    this.pageLayoutParams();

};
/**//*
跳转页面和药传的参数
*/
Page.prototype.pageLayoutParams = function() {
    searchShow = new SearchShow();
    var pathParams = "keyword=" + encodeURIComponent(searchShow.keyword) + "&categoryId=" + searchShow.categoryId + "&areaId=" + searchShow.areaId + "&pageIndex=" + (this.pageIndex + 1) + "&pageSize=" + this.absolute + "&proNameAndDes=" + searchShow.proNameAndDes + "&selSearchDate=" + searchShow.selSearchDate;
    var hidPath = document.getElementById("hidPath").value;
    var myId = document.getElementById("myID");
    $("#myID").load(this.solidPath + pathParams, function(data) {
        var show = data;
        myId.innerHTML = show;
        document.getElementById("loadingdivShow").style.display = "none";
    });
    this.__updateTableRows__(this.absolute);
}

/**//*
执行分页时，更新显示表格内容
*/
Page.prototype.__updateTableRows__ = function(ipageSize) {
    // alert("ok");
    var iCurrentRowCount = this.absolute * this.pageIndex;
    var iMoreRow = this.absolute + iCurrentRowCount > this.rowCount ? this.absolute + iCurrentRowCount - this.rowCount : 0;
    var tempRows = this.__cloneRows__();

    //页脚显示分页
    var divFood = document.getElementById("divFood"); //分页工具栏
    divFood.innerHTML = "";
    var btnFrist = document.createElement("a");
    var btnLast = document.createElement("a");
    var btnPre = document.createElement("a");
    var btnNext = document.createElement("a");
    btnFrist.style.textDecoration = "none";
    btnLast.style.textDecoration = "none";
    btnPre.style.textDecoration = "none";
    btnNext.style.textDecoration = "none";


    btnFrist.href = "javascript:page.firstPage()";
    btnFrist.innerHTML = "&nbsp;首页";
    divFood.appendChild(btnFrist);

    btnPre.href = "javascript:page.prePage()";
    btnPre.innerHTML = "&nbsp;上一页&nbsp;";
    divFood.appendChild(btnPre);

    divFood.appendChild(btnFrist);
    divFood.appendChild(btnPre);
    var isOK = "Y";
    var cssColor = "";
    // divFood.appendChild(divFood.innerHTML);
    var htmStr = "";
    if (this.pageIndex == 0) {
        btnFrist.style.color = "#ccc";
        btnPre.style.color = "#ccc";
        btnFrist.style.backgroundColor = "#ffffff";
        btnPre.style.backgroundColor = "#ffffff";
        btnFrist.style.border = "solid 1px #ddd";
        btnPre.style.border = "solid 1px #ddd";
    }
    if (this.pageIndex + 1 == this.pageCount) {
        btnNext.style.color = "#ccc";
        btnLast.style.color = "#ccc";
        btnNext.style.backgroundColor = "#ffffff";
        btnLast.style.backgroundColor = "#ffffff";
        btnNext.style.border = "solid 1px #ddd";
        btnLast.style.border = "solid 1px #ddd";
    }
    if (this.pageCount <= 10) {

        for (var i = 0; i < this.pageCount; i++) {
            htmStr += "<a id='" + (i + 1) + "' href='javascript:page.aimPage(" + i + ")'> " + (i + 1) + "</a> ";
        }
    }
    else {
        if (this.pageIndex < 10) {
            for (var i = 0; i < 10; i++) {
                htmStr += "<a id='" + (i + 1) + "' href='javascript:page.aimPage(" + i + ")' > " + (i + 1) + "</a> ";
            }
            htmStr += "<a id='" + (i + 1) + "' href='javascript:page.aimPage(" + 10 + ")'>...</a>";
        }
        else if (parseInt((this.pageIndex + 1) / 10) <= parseInt(this.pageCount / 10 - 1)) {
            htmStr += "<a href='javascript:page.aimPage(" + (parseInt(this.pageIndex / 10) * 10 - 10) + ")'>...</a> ";
            for (var i = parseInt(this.pageIndex / 10) * 10; i < parseInt(this.pageIndex / 10) * 10 + 10; i++) {
                htmStr += "<a id='" + (i + 1) + "' href='javascript:page.aimPage(" + i + ")'> " + (i + 1) + "</a> ";
            }
            htmStr += "<a href='javascript:page.aimPage(" + (parseInt(this.pageIndex / 10) * 10 + 10) + ")'>...</a>";

        }
        else {
            htmStr += "<a href='javascript:page.aimPage(" + (parseInt(this.pageIndex / 10) * 10 - 10) + ")'>...</a> ";
            for (var i = parseInt(this.pageIndex / 10) * 10; i < this.pageCount; i++) {
                htmStr += "<a id='" + (i + 1) + "' href='javascript:page.aimPage(" + i + ")'> " + (i + 1) + "</a> ";
            }
        }
    }
    divFood.innerHTML += htmStr;
    this.moveClass(this.pageIndex + 1);
    btnNext.href = "javascript:page.nextPage()";
    btnNext.innerHTML = "下一页";
    divFood.appendChild(btnNext);
    btnLast.href = "javascript:page.lastPage()";
    btnLast.innerHTML = "&nbsp;末页";
    divFood.appendChild(btnLast);
    divFood.appendChild(btnNext);
    divFood.appendChild(btnLast);
    htmStr = "<input id='txtPage' name='txtPage' onKeyDown='numbercheckKey(event.keyCode)' type='textbox'style='width:25px;' /><input type='button' value='go' onclick='page.pageToN()' />";

    divFood.innerHTML += htmStr;
    textboxChanged(this.pageIndex);
    if (this.pageIndex == 0) {
        btnPre.disabled = btnFrist.disabled = "disabled";
    }
    if (this.pageCount - 1 == this.pageIndex) {
        btnLast.disabled = btnNext.disabled = "disabled";
    }
    ////页脚显示分页结束

    this.__dataRows__ = tempRows;
};
/**//*
克隆原始操作行集合
*/
Page.prototype.__cloneRows__ = function() {
    var tempRows = [];
    for (var i = 0; i < this.__dataRows__.length; i++) {
        /**//*
code:this.dataRows[i].cloneNode(param), 
param = 1 or true:复制以指定节点发展出去的所有节点,
param = 0 or false:只有指定的节点和它的属性被复制.
*/
        tempRows[i] = this.__dataRows__[i].cloneNode(1);
    }
    return tempRows;
};
//go的方法
Page.prototype.pageToN = function() {
   goTop();
    var p = document.getElementById("txtPage").value;
    if (this.pageCount < p) { alert("页索引超出范围！"); }
    else if (p == 0) { alert("页索引不是有效的数值"); }
    else if (p == this.pageIndex + 1) { return; }
    else {
        page.aimPage(parseInt(p - 1));
        var hidPath = document.getElementById("hidPath").value;
        searchShow = new SearchShow();
        var pathParams = "keyword=" + encodeURIComponent(searchShow.keyword) + "&categoryId=" + searchShow.categoryId + "&areaId=" + searchShow.areaId + "&pageIndex=" + p + "&pageSize=" + this.absolute + "&proNameAndDes=" + searchShow.proNameAndDes;
        document.getElementById("myID").innerHTML = "";
        $("#myID").load("ProductDataProvider.aspx?" + pathParams, function(data) {
            document.getElementById("myID").innerHTML = data;
        });
    }
};
//控制样式
Page.prototype.moveClass = function(tagA) {
    //document.getElementById("" + tagA).style.setAttribute("fontWeight", "bold");
    document.getElementById("" + tagA).style.fontWeight = "bold";
    document.getElementById("" + tagA).style.border = "0";
    document.getElementById("" + tagA).style.color = "black";
    document.getElementById("" + tagA).style.fontSize = "13px";
    //document.getElementById("" + tagA).style.setAttribute("fontSize", "13px");
    document.getElementById("" + tagA).style.backgroundColor = "#ffffff";
};
// txtPage文本框显示的数据
function textboxChanged(pageIndex) {
    document.getElementById("txtPage").value = pageIndex + 1;
}
function numbercheckKey(key) {
    if (!(key >= 96 && key <= 105 || key >= 48 && key <= 57)) {
        event.returnValue = false;
    }
    if (key == 8) { event.returnValue = true; }
}
/*
Format格式化
*/
String.Format = function() {
    if (arguments.length == 0)
        return "";

    if (arguments.length == 1)
        return arguments[0];

    var reg = /{(\d+)?}/g;
    var args = arguments;
    var result = arguments[0].replace(
  reg,
  function($0, $1) {
      return args[parseInt($1) + 1];
  }
 )
    return result;
}
//搜索事件
function Search_Click() {
    document.getElementById("loadingdivShow").style.display = "";
    document.getElementById("YiShi").src = "http://img.etradenow.cn/images/10_1.gif";
    document.getElementById("ErShi").src = "http://img.etradenow.cn/images/20.gif";
    document.getElementById("SanShi").src = "http://img.etradenow.cn/images/30.gif";
    document.getElementById("SiShi").src = "http://img.etradenow.cn/images/40.gif";
    var searchDis = document.getElementById("hidSelAreaNameByBox").value;
    if (searchDis == null || searchDis == "") { document.getElementById("hidSelAreaNameByBox").value = "选择区域…" }
    if (document.getElementById("hidSelAreaNameByBox").value != null || document.getElementById("hidSelAreaNameByBox").value != "") {
        document.getElementById("selAreaNameByBox").value = document.getElementById("hidSelAreaNameByBox").value;
    }
    //要传的参数
    searchShow = new SearchShow();
    var pathParams = String.Format("ProductDataProvider.aspx?keyword={0}&categoryId={1}&areaId={2}&pageIndex={3}&pageSize={4}&proNameAndDes={5}&selSearchDate={6}", encodeURIComponent(searchShow.keyword), searchShow.categoryId, searchShow.areaId, 1, 10, searchShow.proNameAndDes, searchShow.selSearchDate);
    var myId = document.getElementById("myID");
    var hidPath = document.getElementById("hidPath").value;
    $("#myID").load(pathParams, function(data) {
        var show = data;
        myId.innerHTML = show;
        document.getElementById("loadingdivShow").style.display = "none";
        var objHidCountAndCurPageIndex = document.getElementById("hidCountAndCurPageIndex");
        if (objHidCountAndCurPageIndex != null) {
            var pageCountIndex = document.getElementById("hidCountAndCurPageIndex").value.split("_");
            var pageCount = parseInt(pageCountIndex[0]);
            page = new Page(10, pageCount);
            document.getElementById("hidCountAndCurPageIndex").value = "";
            document.getElementById("XianShiShuLiang").style.display = "block";
            document.getElementById("divFood").style.display = "block";
        }
        var objHidNoSearch = document.getElementById("hidNoSearch");
        if (objHidNoSearch != null) {

            if (document.getElementById("hidNoSearch").value == "1") {
                page = new Page(0, null);
                document.getElementById("XianShiShuLiang").style.display = "none";
                document.getElementById("divFood").style.display = "none";
            }
        }
    });
}
//点击20,30,40事件
function ErSanSiShi_Click(ipageSize) {
    document.getElementById("hidYESS").value = ipageSize;
    var myId = document.getElementById("myID");
    document.getElementById("loadingdivShow").style.display = "";
    isPithcOn(ipageSize);
    goTop();
    //实例化参数
   goTop();
    searchShow = new SearchShow();
    var pathParams = "keyword=" + encodeURIComponent(searchShow.keyword) + "&categoryId=" + searchShow.categoryId + "&areaId=" + searchShow.areaId + "&pageIndex=" + 1 + "&pageSize=" + ipageSize + "&proNameAndDes=" + searchShow.proNameAndDes + "&selSearchDate=" + searchShow.selSearchDate;
    $("#myID").load("ProductDataProvider.aspx?" + pathParams, function(data) {
        var show = data;
        myId.innerHTML = show;
        document.getElementById("loadingdivShow").style.display = "none";
        var objCount = document.getElementById("hidCountAndCurPageIndex");
        if (objCount != null) {
            var pageCountIndex = document.getElementById("hidCountAndCurPageIndex").value.split("_");
            var pageCount = parseInt(pageCountIndex[0]);
            page = new Page(ipageSize, pageCount);
            document.getElementById("XianShiShuLiang").style.display = "block";
            document.getElementById("divFood").style.display = "block";
        }
        var objNoSearch = document.getElementById("hidNoSearch");
        if (objNoSearch != null) {
            if (objNoSearch.value == "1") {
                document.getElementById("XianShiShuLiang").style.display = "none";
                document.getElementById("divFood").style.display = "none";
            }
        }
    });
}
//判断10，20，30，40是否选中
function isPithcOn(num) {
    if (num == 10) {
        document.getElementById("YiShi").src = "http://img.etradenow.cn/images/10_1.gif";
        document.getElementById("ErShi").src = "http://img.etradenow.cn/images/20.gif";
        document.getElementById("SanShi").src = "http://img.etradenow.cn/images/30.gif";
        document.getElementById("SiShi").src = "http://img.etradenow.cn/images/40.gif";
    }
    else if (num == 20) {
        document.getElementById("YiShi").src = "http://img.etradenow.cn/images/10.gif";
        document.getElementById("ErShi").src = "http://img.etradenow.cn/images/20_1.gif";
        document.getElementById("SanShi").src = "http://img.etradenow.cn/images/30.gif";
        document.getElementById("SiShi").src = "http://img.etradenow.cn/images/40.gif";
    }
    else if (num == 30) {
        document.getElementById("YiShi").src = "http://img.etradenow.cn/images/10.gif";
        document.getElementById("ErShi").src = "http://img.etradenow.cn/images/20.gif";
        document.getElementById("SanShi").src = "http://img.etradenow.cn/images/30_1.gif";
        document.getElementById("SiShi").src = "http://img.etradenow.cn/images/40.gif";
    }
    else if (num == 40) {
        document.getElementById("YiShi").src = "http://img.etradenow.cn/images/10.gif";
        document.getElementById("ErShi").src = "http://img.etradenow.cn/images/20.gif";
        document.getElementById("SanShi").src = "http://img.etradenow.cn/images/30.gif";
        document.getElementById("SiShi").src = "http://img.etradenow.cn/images/40_1.gif";
    }
}
///初始化要传的参数
function SearchShow() {
    this.categoryId = document.getElementById('HidCurCategoryId').value;
    this.searchType = document.getElementById("hidSearchType").value;
    this.areaId = document.getElementById('selAreaIdByBox').value;
    var keywords = Trim(document.getElementById("keyword").value);
    if (keywords.length > 10) {
        this.keyword = keywords.substring(0, 8);
    } else {
        this.keyword = keywords;
    }
    this.selSearchDate = document.getElementById("selSearchDate").value;
    var drlKeyWordType = document.getElementById("DrlKeywordType");
    if (drlKeyWordType != null) {
        this.proNameAndDes = document.getElementById("DrlKeywordType").value;
    } else { this.proNameAndDes = 0; }
}

function LeftTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}
/*去除右边的空格*/
function RightTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
            i--;
        }
        s = s.substring(0, i + 1);
    }
    return s;
}
/*回到顶部*/
function goTop() {
    document.body.scrollLeft = 0;
    document.body.scrollTop = 0;
 }
/* 去除前后空格 */
function Trim(str) {
    return RightTrim(LeftTrim(str));
}
function keyContent(e) {
    var e = e || event;
    var keyCode = e.keyCode ? e.keyCode : e.which;
    if (keyCode == 13 || keyCode == 108)
        Search_Click();
}
document.onkeypress = keyContent;
