
//code to generate pagination
//link_Count is the total count.
//docURL is the URl from which page number is captured

function paging(link_Count, docURL, appName, pagingNodeId)
{
    //checking if linkcount is greater than 0
    if(link_Count>1)
    {
        var page_number 
        var str = "";
        var i;
        var totlink=link_Count;
        var lastlink;
        var count = totlink;
        var click_number;
        
         //if page number is not clicked then default page will be 1
        if(page_number=="")
        {
            page_number=1;
        }
        else
        {
            page_number=docURL;
        } 
        //total number of links to be displayed on the page
        if(totlink<=13)
        {
            var displaylink=totlink;
        }
        else
        {
             var displaylink=8;
        }
        //calculation of interval
        if(totlink>=13)
        {
            var interval=parseInt(totlink)/parseInt(displaylink);
            interval=parseInt(interval);
        }
        else
        {
             var interval=1;
        }
        //if the no of pages generated is less than or equal to 5
        //page number clicked is less than or equal to 3  
        if(count<=5 && page_number<=3)
        {
            var arrIndex = new Array();
            for(j=0;j<count;j++)
            {
                arrIndex[j]=j+1;
            }
        }
        else
        {
            //code for when total link is greater than 5 and page clicked is <=3
            //storing the values in the array arrIndex 
            if(page_number<=3)
            {
                var s=0;
                var arrIndex = new Array();
                for(j=1;j<=5;j++)
                {
                    arrIndex[s]=j;
                    s=s+1;
                }
                for(j=5+interval;j<=count;j=j+interval)
                {
                    arrIndex[s]=j;
                    s=s+1;
                }
                if(j!=count || count<j+interval)
                {
                    lastlink=count;
                    if (arrIndex[arrIndex.length-1] != count) arrIndex[arrIndex.length] = count                    
                }
            }	
            //code for when total link is greater than 5 and page clicked is >3 and interval <=1
            //Generated value is stored in the array
            if(page_number>3 && interval<=1)
            {
                var a=0;
                var arrIndex = new Array();
                for(i=1;i<=count;i=i+interval)
                {
                    arrIndex[a]=i;
                    a=a+1;
                }
                var varirep=i-interval;
                if(varirep!=count)
                { 
                    if(i!=count || count<i+interval)
                    {
                        lastlink=count;
                        arrIndex[a]=lastlink;
                    }
                }
             }		
            //code for when total link is greater than 5 and page clicked is >3 and interval >1
            //Generated value is stored in the array

            if(page_number>3 && interval>1)
            {
                var arrIndex = new Array();
                var k=0;
                if(parseInt(page_number)-parseInt(interval) == 0 )
                {
                    for(i=1;i<=page_number;i=i+interval)
                    {
                        arrIndex[k]=i;
                        k=k+1;
                    }
                } 
                else
                {
                    arrIndex[k]=1;
                    for(i=1;i<=parseInt(page_number)-parseInt(interval);i=i+interval)
                    {
                        arrIndex[k]=i;
                        k=k+1;
                    }
                } 
                if(parseInt(page_number)-parseInt(interval)<0)
                {
                    arrIndex[1]=1;  
                    k=k+1;  
                }     
                 var exist_no = i - interval
                //split
                for(i=parseInt(page_number)-parseInt(2);i<=parseInt(page_number)+parseInt(2);i++)
                {
                    if (exist_no != i) 
                    {
                        if(i>count)
                        {
                            break;      
                        }
                        arrIndex[k]=i;
                        k=k+1;
                    }
                }
                if (i==count)i-=1;
                var valexist_no=i-1
                if(i<count )
                { 
                    if(parseInt(page_number)+parseInt(interval) <= count)
                    {
                        for(i=parseInt(page_number)+parseInt(interval);i<count;i=i+interval)
                        {
                            if(valexist_no!=i)
                            {
                                arrIndex[k]=i;
                                k=k+1;
                            }
                        } 
                        //if last link is not displayed then the following for loop
                        var repeat=i-interval; 
                        if(parseInt(repeat)+parseInt(interval)>count && repeat!=count && interval!=1)
                        {
                            lastlink=count;
                            arrIndex[k]=lastlink;
                        }
                        else 
                        {
                            if(parseInt(repeat)-parseInt(interval)<count)
                            {
                                arrIndex[k]=count
                            }
                        }
                    }
                    
                }
                if (arrIndex[arrIndex.length-1] != count) arrIndex[arrIndex.length] = count
        }   
    }
        var pagingBoxId = _getPagingBoxId (pagingNodeId);
    str += '<input type="text" size="1" maxlength="4" class="pagingRibbonBox" ' +
        'name="' + pagingBoxId + '" id="' + pagingBoxId + '" value="' + page_number + '"> ' +
        '<input type="button" value="Go" class="pagingRibbonGoButton" ' +
        'onclick="GoTo(' + link_Count + (pagingNodeId ? (',' + pagingNodeId) : '') + ')" >';
    if(page_number!=1)
    {
        str+="<a href='"
        str+=generationhref(parseInt(page_number)-parseInt(1), pagingNodeId);
        str+="' " +"border=0><img src='/" + appName +"/images/previous1.gif' border=0></a>";
    }
    var cnt=0;
    
    //retriving back the values from the array and displaying the values.
    for(o=0;o<=arrIndex.length-1;o++)
    {
        if(arrIndex[o]==page_number)
        {
            str +="<span class='ClickLinkPage'>"+arrIndex[o]+"</span>";
	   click_number=arrIndex[o];
        }
         
        if(arrIndex[o]!=page_number)
        {
	   str+="<a href='"
            str +=  generationhref(arrIndex[o], pagingNodeId);
            str+="' " +"border=0><span class='LinkPage'> " + arrIndex[o] + " </span></a>"
        }
        if(arrIndex[(o+1)]){
           if((arrIndex[o+1] - arrIndex[o]) == 1){
         
           }
          else
          {
              if(arrIndex[o]!=page_number-1 && arrIndex[o]!=page_number-2 &&
              arrIndex[o]!=page_number && arrIndex[o]!=parseInt(page_number)
              +parseInt(1)&& arrIndex[o]!=parseInt(page_number)+parseInt(2)
              ||cnt==4 )     
              { 
                 str +="<class='LinkPage'>"+"..";
              } 
          }
        }
        if(arrIndex[o]==page_number-1 || arrIndex[o]==page_number-2 || 
        arrIndex[o]==page_number || arrIndex[o]==parseInt(page_number)+parseInt(1)
        ||arrIndex[o]==parseInt(page_number)+parseInt(2))
        {
            cnt=cnt+1;
        }
    }
    if(page_number==count)
    {
        str+="<a href='"
        str+=generationhref(page_number, pagingNodeId);
        str+="' " +"border=0></a>"
    }
    else
    {
        str+="<a href='"
        str+=generationhref(parseInt(page_number)+parseInt(1), pagingNodeId);
        str+="' " +"border=0><img src='/" + appName +"/images/next1.gif' border=0></a>"
    }
    document.write (str);
    }
}
// function called when the user inputs the number into the text box and 
//clicks go button. 
function GoTo (linkCount, pagingNodeId) {
    var pagingBox = document.getElementById (_getPagingBoxId (pagingNodeId));
    var page_number = pagingBox.value;
    if (parseInt(page_number) > parseInt(linkCount) ||
        parseInt(page_number) <= 0) {
        alert ("Please provide a valid page number (1 to " + linkCount + ")");
        pagingBox.value="";
        pagingBox.focus();
    } else {
        document.location.href = generationhref(page_number, pagingNodeId);
    }
}


function _getPagingBoxId (pagingNodeId) {
    var boxId = "zc__page__number__box";
    if (pagingNodeId) boxId += "__" + pagingNodeId;
    // This computation allows for pagingNodeId to be undefined, to accommodate "legacy" HTML pages.
    return boxId;
}


