﻿var showNav = true;
var savedtotalresults = 0;
var recursiveCount = 1;
function getPagination(noRecords1,CurrentPage1) {    
if(showNav)
    nav();
getRecordsNumber();
    }

    function getRecordsNumber() {

        if (savedtotalresults == 0) {
            savedtotalresults = 1;
            var recordCount = document.getElementById('numFoundRecords_hidden').value;
            var isStartPage = document.getElementById('currentpage').value;
            if (isStartPage == 0)
            {
            xmlHttp = isAjax();
            if (xmlHttp != null) {
                xmlHttp.open("GET", "UserNameValidation.aspx?recordCount=" + recordCount + "&Flag=" + 0, true);
                xmlHttp.send(null);
                xmlHttp.onreadystatechange = function() {
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        response = xmlHttp.responseText;
                        savedtotalresults = 1;
                      
                    }
                }
            }
            } 
        }
    }

    function checkForSaveAll() {
        //alert("in checkForSaveAll");
        xmlHttp = isAjax();
        if (xmlHttp != null) {
            xmlHttp.open("GET", "user_folder.aspx?saveAllValue='valuePlease'", true);
            xmlHttp.send(null);
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    response = xmlHttp.responseText;
                    //alert(response);
                    if (response == "-1") {
                        window.location.replace("SessionExpired.aspx");
                    }
                    else if (response == "true") {
                        checkAllCheckBoxes(true);
                        var SaveAllCheckBox = document.getElementsByName('cbxSaveAll').item(0);
                        SaveAllCheckBox.type = "checkbox";
                        SaveAllCheckBox.checked = true;
                    }
                    else if (response == "false") {
                        checkAllCheckBoxes(false);
                        var SaveAllCheckBox = document.getElementsByName('cbxSaveAll').item(0);
                        SaveAllCheckBox.type = "checkbox";
                        SaveAllCheckBox.checked = false;
                    }
                }
            }
        }
    }
    
    function nav()
    {    
    var s="";
    var i = 0;
    try {
        var searchKey = document.getElementById('searchKey_hidden').value;
    
try {
    var searchCountry = document.getElementById('searchCountry_hidden').value;
} catch (Error) {
searchCountry = "2.2";
}
    if (searchCountry != "2.2") {
       // alert("find country" + searchCountry)
    }
    
    var searchType="";
    var brandArray = searchKey.split(":");

    //alert(brandArray);
    
    if(brandArray[0]=="Brand")
        {
            searchType="Brand";
            searchKey=brandArray[1];
        }
    else if (brandArray[0] == "Width") 
        {
            searchType = "Size";

            var size_num = 0;
            var size_array = brandArray[1].split("x");
            var width = size_array[0];
            var Height = size_array[1];
            
            searchKey = width + 'x' + Height;
        }
    else if(brandArray[0]=="ParentPortal")    
        {
            searchType="Publisher";
            searchKey=brandArray[1];
        }    
    else
        {
            searchType="keyword";
        }

        
    var CountryArray=searchCountry.split(":");

    if(CountryArray[0]=="2.2" ){searchCountry="All";}else{searchCountry=CountryArray[1];}
} catch (Error) {
    ;
}
    var recordCount=document.getElementById('numFoundRecords_hidden').value;
    var start=document.getElementById('currentPage_hidden').value;
    
    var PageCount=Math.floor(recordCount/10);//alert(PageCount);
    var modVal=recordCount % 10;
    if(modVal>0){PageCount=PageCount+1;}
    var currentPage=1;
    if(start==0){currentPage=1;}else{currentPage= Math.round(start/10)+1;}
   
    if(PageCount>=2){
        if(currentPage!=0)
        {
            if(currentPage-2>=0){
           s="<a href='search.aspx?page="+ (currentPage-2) +"&SearchType=" + searchType + "&SearchKey=" + searchKey + "&SearchCountry=" + searchCountry+"'>"
              +"<img src='images/green_btn.gif' width='18' height='13'  border ='0'/>"
              +"</a>&nbsp;"
              }
        }
//"search.aspx?page="+i+"&SearchType=" + type + "&SearchKey=" + query + "&SearchCountry=" + country;
        
         if(PageCount<=20)
           { 
           if(PageCount==1){}
           else if(PageCount>=2)
           {
            for( i=1;i<=PageCount;i++)
                {
                    if(i==currentPage)
                    {
                        s=s+"<span class='font02_orange_navigation'> " + i  + " </span>";
                    }
                    else
                    {
                      s=s+""
                          +"<a href='search.aspx?page="+ (i-1) +"&SearchType=" + searchType + "&SearchKey=" + searchKey + "&SearchCountry=" + searchCountry
                          +"'><span class='font02_green_navigation'>(" + i  + ")</span></a> ";
                    }
                }
            }
        }
        else {
        
        var starter=0,end=0;
                 if (currentPage <= 10)
                    {
                        starter = 1;
                        end = 21;
                    }
                 else if (currentPage > 10)
                    {
                        starter = currentPage - 10;
                        end = currentPage + 10;

                        if (end >= PageCount)
                        {
                            end = PageCount;
                        }
                        
                     }
            for(;starter<=end;starter++)
            {
                if(starter==currentPage)
                {
                    s=s+"<span class='font02_orange_navigation'> " + starter  + " </span>";
                }
                else
                { 
                  s=s+"<a href='search.aspx?page="+(starter-1)+"&SearchType=" + searchType + "&SearchKey=" + searchKey + "&SearchCountry=" + searchCountry
                      +"'><span class='font02_green_navigation'>(" + starter  + ")</span></a> ";
                }
            }
        }
        if(currentPage!=PageCount && PageCount>1)
        {
            //alert("recordCount:"+recordCount+"\n PageCount:"+PageCount+"\nmodVal:"+modVal+"\nstart:"+start+"\ncurrentPage:"+currentPage);
            s=s+"<a href='search.aspx?page="+ (currentPage) +"&SearchType=" + searchType + "&SearchKey=" + searchKey + "&SearchCountry=" + searchCountry+"'>"
              +"<img src='images/green_btn1.gif' width='18' height='13'  border ='0'/>"
              +"</a>"
              //}
        }
     }
     //alert(s);
     recursiveCount = 1;
     showpaginationDiv(s);
     checkForSaveAll();
   }
    function showpaginationDiv(s)
    {
     var a = null;
     
         try {
             a = document.getElementById('paginationdiv');
             if (a != null) 
             {
                 document.getElementById('paginationdiv').innerHTML = s; //currentPage+"of"+PageCount;
                 recursiveCount = 1;
                 showNav = false;                 
             }
             else {
                 if (recursiveCount <= 1000) {
                     recursiveCount++;
                     showpaginationDiv(s);
                 }
             }
         } catch (Error) {;}
    }
function checkAllCheckBoxes(bChecked)
 {
    var i = 0;
    var noCheckBoxes = document.getElementsByName('cbxSaveIndividual').length;
    document.getElementsByName('checkAll').Value = "True";
    document.SearchForm.contentID_hidden.value = "-1";
     ajaxSaveAll(bChecked);
    if(noCheckBoxes==1)
    {
        var objcheckbox=document.getElementsByName('cbxSaveIndividual').item(0);
        //alert(noCheckBoxes+":"+document.getElementsByName('cbxSaveIndividual').length+":"+objcheckbox.value);
        objcheckbox.type="checkbox";
              if (bChecked) 
              {
                 objcheckbox.checked = true;
                document.SearchForm.contentID_hidden.value =  objcheckbox.value + "," + document.SearchForm.contentID_hidden.value;
              }
              else
              {            
                objcheckbox.checked = false;
              }
        
     }
     else
     {
        for (i = 0; i < document.SearchForm.cbxSaveIndividual.length; i++) 
        {
            if (bChecked)
             {
                document.SearchForm.cbxSaveIndividual[i].checked = true;
                document.SearchForm.contentID_hidden.value = document.SearchForm.cbxSaveIndividual[i].value + "," + document.SearchForm.contentID_hidden.value;
             }
             else 
             {
                 document.SearchForm.cbxSaveIndividual[i].checked = false;
             }
        //alert(document.SearchForm.cbxSaveIndividual[i].value+":"+document.SearchForm.cbxSaveIndividual[i].checked);
          }
    }
    //alert(document.SearchForm.contentID_hidden.value);
}


function isAjax() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
        return xmlHttp;
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            return xmlHttp;
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                return xmlHttp;
            }
            catch (e) {
                //alert("Ajax not supported");
                return null;
            }
        }
    }
}