﻿function SelectMonthsFromSeason(strSeason,strMonthsObjectId) {
    objMonths = document.getElementById(strMonthsObjectId);
    arrMonthsCheckboxes = objMonths.getElementsByTagName("INPUT");
    switch (strSeason.toLowerCase()) {
        case "summer" :
            DeselectAllMonths(arrMonthsCheckboxes);
            arrMonthsCheckboxes[0].checked = true;
            arrMonthsCheckboxes[3].checked = true;
            arrMonthsCheckboxes[6].checked = true;
            break;
        case "autumn" :
            DeselectAllMonths(arrMonthsCheckboxes);
            arrMonthsCheckboxes[9].checked = true;
            arrMonthsCheckboxes[1].checked = true;
            arrMonthsCheckboxes[4].checked = true;
            break;
        case "winter" :
            DeselectAllMonths(arrMonthsCheckboxes);
            arrMonthsCheckboxes[7].checked = true;
            arrMonthsCheckboxes[10].checked = true;
            arrMonthsCheckboxes[2].checked = true;
            break;
        case "spring" : 
            DeselectAllMonths(arrMonthsCheckboxes);
            arrMonthsCheckboxes[5].checked = true;
            arrMonthsCheckboxes[8].checked = true;
            arrMonthsCheckboxes[11].checked = true;
            break;
    }
}

function DeselectAllMonths(arrMonthsCheckboxes) {
    for (i=0; i<=arrMonthsCheckboxes.length-1; i++) {
        arrMonthsCheckboxes[i].checked = false;
    }
}


//Paging for plants-animals search results
//Adapted from http://highoncoding.com/Articles/515_Repeater_Paging_using_JavaScript.aspx
//FS 20090917



var pgPrefix = "pg"; // Thats is for Div id as we don't want to pass number in div Id
var currPage = 1; // Index of the current page
var recCount = 1; // record count, will be set by the functions
var pgSize= 10; //this is the size of the page which can be changed according to the need
var pgCount = 1; // page count, will be set by the functions

function managePageIndex(){
    var strIndexText = "Page <strong>" + currPage + "</strong> of " + pgCount;
    document.getElementById("pgInfoTop").innerHTML = strIndexText;//in some cases you need to use textContent for mozila but that is rare.
    document.getElementById("pgInfoBottom").innerHTML = strIndexText;
}

function managePageNumbers(){
    var obj = document.getElementById("pageNumbersTop");
    var obj2 = document.getElementById("pageNumbersBottom");
    obj.innerHTML = "";
    obj2.innerHTML = "";
    
    for (i=1;i<=pgCount;i++){
        obj.innerHTML += "<a href='javascript:void(0);' onclick='showPage(" + i + ")'>" + i + "</a> ";
        obj2.innerHTML += "<a href='javascript:void(0);' onclick='showPage(" + i + ")'>" + i + "</a> ";
    }
}

function managePaging(){
    //make sure we're on the results page
    if(document.getElementById("dvRecords")){
        var searchresultdivs = new Array();

        var obj = document.getElementById("dvRecords");
        var divs = obj.getElementsByTagName("div");
            for(var i=0; i<divs.length; i++){
                if(divs[i].className == "searchResult"){
                    searchresultdivs.push(divs[i]);
                }
            }
            
            if(searchresultdivs.length > pgSize){
                document.getElementById('pgInfoTop').style.display = 'block';
                document.getElementById('PagingTop').style.display = 'block';
                document.getElementById('pgInfoBottom').style.display = 'block';
                document.getElementById('PagingBottom').style.display = 'block';
            }
            //hide paging on short pages and exit
            else {
                return false;
            }
            
            for(i=0; i<searchresultdivs.length; i++){
                if (recCount > pgSize){
                    recCount = 1;
                    pgCount++;
                }
        
            var pgName = pgPrefix + pgCount; // Create div name as per current page
            if (searchresultdivs[i].className == "searchResult"){
                searchresultdivs[i].setAttribute("name",pgName);             
            }
            
           recCount++; 
           
        }
    managePageNumbers();
    showPage(currPage);
    }
}
    
function showPage(pageNum){
    var pgName = pgPrefix + pageNum;
    var obj = document.getElementById("dvRecords");
    var divs = obj.getElementsByTagName("div");
    currPage = pageNum;
    for(i=0;i<=divs.length -1;i++){
        if (divs[i].getAttribute("name") == pgName){
            divs[i].style.display = "block";
        }
        else{
            if(divs[i].className == "searchResult"){
                divs[i].style.display = "none";
            }
        }
    }
    managePageIndex();
    window.location = "#PageTop";
}

function moveNext(){
    if ((currPage + 1) > pgCount){
        currPage = 1;
    }
    else{
        currPage++;
    }
    showPage(currPage);
    window.location = "#PageTop";
}
    
function movePrevious(){
    if ((currPage - 1) < 1){
        currPage = pgCount;
    }
    else {
        currPage--;
    }
    showPage(currPage);
    window.location = "#PageTop";
}
