//********************************************************************************
//MODAL WINDOW FUNCTIONALITY
//********************************************************************************

//--------------------------------------------------------------------------------
// Events
//--------------------------------------------------------------------------------

//      -2  -1  0   1   2
//      9   10  11  12  13

//      currpage = 101
//      perpage = 10
//      currsection = 11
//      refsection = 2 (clicked 13)

//      newsection = currsection + refsection (13) (11+2)
function perpageclick(perpage)
{
    $('#perpage').val(perpage);
    HideDetailElements($('#totalpages').val());
    $('#currpage').val('1');
    $('#currsection').val('1');
    
    $('#aperpage10').css('color', '#FF6600'); 
    $('#aperpage25').css('color', '#FF6600'); 
    $('#aperpage50').css('color', '#FF6600'); 

    $('#bperpage10').css('color', '#FF6600'); 
    $('#bperpage25').css('color', '#FF6600'); 
    $('#bperpage50').css('color', '#FF6600'); 
        
    if (perpage == 10)
    {
        $('#aperpage10').css('color', 'black'); 
        $('#bperpage10').css('color', 'black'); 
    }
    if (perpage == 25)
    {
        $('#aperpage25').css('color', 'black'); 
        $('#bperpage25').css('color', 'black');         
    }
    if (perpage == 50)
    {
        $('#aperpage50').css('color', 'black'); 
        $('#bperpage50').css('color', 'black'); 
    }   
    
    ShowFirstTextDiv();
}

function HideDetailElements()
{
    var totalpages = parseInt($('#totalpages').val());
    var perpage = parseInt($('#perpage').val());
    var totalsections = parseInt(totalpages / perpage)
    if(totalpages % perpage > 0)
        totalsections++;
    
    var currsection = parseInt($('#currsection').val());
    var currpage = parseInt($('#currpage').val());


    switch(totalsections)
    {
        case 0:
            $('#refpage1').hide(); $('#refpage2').hide(); $('#refpage3').hide(); $('#refpage4').hide(); $('#refpage5').hide();             
            $('#refpage1x').show(); $('#refpage2x').show(); $('#refpage3x').show(); $('#refpage4x').show(); $('#refpage5x').show();             
            
            $('#b_refpage1').hide(); $('#b_refpage2').hide(); $('#b_refpage3').hide(); $('#b_refpage4').hide(); $('#b_refpage5').hide();
            $('#b_refpage1x').show(); $('#b_refpage2x').show(); $('#b_refpage3x').show(); $('#b_refpage4x').show(); $('#b_refpage5x').show();
//            $('#aFirst').hide(); $('#aPrevious').hide(); $('#aNext').hide(); $('#aLast').hide();
            break; 
        case 1:
            $('#refpage1').show(); $('#refpage2').hide(); $('#refpage3').hide(); $('#refpage4').hide(); $('#refpage5').hide();             
            $('#refpage1x').hide(); $('#refpage2x').show(); $('#refpage3x').show(); $('#refpage4x').show(); $('#refpage5x').show();             
            
            $('#b_refpage1').show(); $('#b_refpage2').hide(); $('#b_refpage3').hide(); $('#b_refpage4').hide(); $('#b_refpage5').hide();
            $('#b_refpage1x').hide(); $('#b_refpage2x').show(); $('#b_refpage3x').show(); $('#b_refpage4x').show(); $('#b_refpage5x').show();
//            $('#aFirst').hide(); $('#aPrevious').hide(); $('#aNext').hide(); $('#aLast').hide();
            break;    
        case 2:
            $('#refpage1').show(); $('#refpage2').show(); $('#refpage3').hide(); $('#refpage4').hide(); $('#refpage5').hide();
            $('#refpage1x').hide(); $('#refpage2x').hide(); $('#refpage3x').show(); $('#refpage4x').show(); $('#refpage5x').show();
            
            $('#b_refpage1').show(); $('#b_refpage2').show(); $('#b_refpage3').hide(); $('#b_refpage4').hide(); $('#b_refpage5').hide();
            $('#b_refpage1x').hide(); $('#b_refpage2x').hide(); $('#b_refpage3x').show(); $('#b_refpage4x').show(); $('#b_refpage5x').show();
//            $('#aFirst').hide(); $('#aPrevious').hide(); $('#aNext').hide(); $('#aLast').hide();
            break;
        case 3:
            $('#refpage1').show(); $('#refpage2').show(); $('#refpage3').show(); $('#refpage4').hide(); $('#refpage5').hide();
            $('#refpage1x').hide(); $('#refpage2x').hide(); $('#refpage3x').hide(); $('#refpage4x').show(); $('#refpage5x').show();
            
            $('#b_refpage1').show(); $('#b_refpage2').show(); $('#b_refpage3').show(); $('#b_refpage4').hide(); $('#b_refpage5').hide();            
            $('#b_refpage1x').hide(); $('#b_refpage2x').hide(); $('#b_refpage3x').hide(); $('#b_refpage4x').show(); $('#b_refpage5x').show();
//            $('#aFirst').hide(); $('#aPrevious').hide(); $('#aNext').hide(); $('#aLast').hide();
            break;    
        case 4:
            $('#refpage1').show(); $('#refpage2').show(); $('#refpage3').show(); $('#refpage4').show(); $('#refpage5').hide();
            $('#refpage1x').hide(); $('#refpage2x').hide(); $('#refpage3x').hide(); $('#refpage4x').hide(); $('#refpage5x').show();
            
            $('#b_refpage1').show(); $('#b_refpage2').show(); $('#b_refpage3').show(); $('#b_refpage4').show(); $('#b_refpage5').hide();            
            $('#b_refpage1x').hide(); $('#b_refpage2x').hide(); $('#b_refpage3x').hide(); $('#b_refpage4x').hide(); $('#b_refpage5x').show();
//            $('#aFirst').hide(); $('#aPrevious').hide(); $('#aNext').hide(); $('#aLast').hide();
            break;
        case 5:
            $('#refpage1').show(); $('#refpage2').show(); $('#refpage3').show(); $('#refpage4').show(); $('#refpage5').show();
            $('#refpage1x').hide(); $('#refpage2x').hide(); $('#refpage3x').hide(); $('#refpage4x').hide(); $('#refpage5x').hide();
            
            $('#b_refpage1').show(); $('#b_refpage2').show(); $('#b_refpage3').show(); $('#b_refpage4').show(); $('#b_refpage5').show();            
            $('#b_refpage1x').hide(); $('#b_refpage2x').hide(); $('#b_refpage3x').hide(); $('#b_refpage4x').hide(); $('#b_refpage5x').hide();
//            $('#aFirst').hide(); $('#aPrevious').hide(); $('#aNext').hide(); $('#aLast').hide();
            break;    
        default:
            
            $('#refpage1').show(); $('#refpage2').show(); $('#refpage3').show(); $('#refpage4').show(); $('#refpage5').show();
            $('#b_refpage1').show(); $('#b_refpage2').show(); $('#b_refpage3').show(); $('#b_refpage4').show(); $('#b_refpage5').show();
//            $('#aFirst').show(); $('#aPrevious').show(); $('#aNext').show(); $('#aLast').show();
            if(currsection >= totalsections)
//                {$('#aNext').hide(); $('#aLast').hide(); $('#refpage5').hide();}
            if(currsection == 1)
//                {$('#aFirst').hide(); $('#aPrevious').hide(); }
            break;
            
    }

    if(currsection == 1)
    {
        $('#aFirst').hide();$('#a_First').show(); $('#aPrevious').hide(); $('#a_Previous').show();
        $('#bFirst').hide();$('#b_First').show(); $('#bPrevious').hide(); $('#b_Previous').show();
    }
    else
    {
        $('#aFirst').show();$('#a_First').hide(); $('#aPrevious').show(); $('#a_Previous').hide();    
        $('#bFirst').show();$('#b_First').hide(); $('#bPrevious').show(); $('#b_Previous').hide();    
    }

    if(currsection >= totalsections)
    {
        $('#aNext').hide();$('#a_Next').show(); $('#aLast').hide(); $('#a_Last').show();
        $('#bNext').hide();$('#b_Next').show(); $('#bLast').hide(); $('#b_Last').show();
        
    }
    else
    {
        $('#aNext').show();$('#a_Next').hide();$('#aLast').show(); $('#a_Last').hide();
        $('#bNext').show();$('#b_Next').hide();$('#bLast').show(); $('#b_Last').hide();
    }

}

function ShowFirstTextDiv()
{
    

    $('.divPages').hide();
    var perpage 		= parseInt($('#perpage').val());
    var totalpages 	= parseInt($('#totalpages').val());
		var nores				= parseInt($('#noresX').val())
    var pagestart 	= 1;

    $('#refpage1').html(1);$('#refpage2').html(2); $('#refpage3').html(3); $('#refpage4').html(4); $('#refpage5').html(5);
    $('#b_refpage1').html(1);$('#b_refpage2').html(2); $('#b_refpage3').html(3); $('#b_refpage4').html(4); $('#b_refpage5').html(5);

    $('.pagerefs').css('color', '#FF6600'); 
    $('#refpage1').css('color', 'black');
    $('#b_refpage1').css('color', 'black');
		if (nores){totalpages=0;pagestart = 0;}
    if(pagestart + perpage - 1 > totalpages)
    {
        $('#spanresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');
        $('#b_panresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');
    }
    else
    {
        $('#spanresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');
        $('#b_panresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');
    }
		
    $('#currsection').val(1);
    $('#currpage').val(pagestart);

    for ( i = pagestart; i < (parseInt(pagestart) + parseInt(perpage)); i++ )    
    {
        $('#divPage' + i).show();
    }
    
    HideDetailElements();

}

function ShowLastTextDiv()
{
    

    $('.divPages').hide();
    var perpage = parseInt($('#perpage').val());
    var currsection = parseInt($('#currsection').val());
    var currpage = parseInt($('#currpage').val());

    var totalpages = parseInt($('#totalpages').val());
    var totalsections = parseInt(totalpages / perpage);
    
    var sectionmodifyer = 0
    if (totalsections < 5 )
        sectionmodifyer = 4 - totalsections;
    
    
    if(totalpages % perpage > 0)
        totalsections++;

    
    var newsection = 0;
    var pagestart = ((totalsections - 1) * perpage )+ 1;
    //Set Link click param
    $('#refpage1').html(totalsections - 4 + sectionmodifyer);
    $('#refpage2').html(totalsections - 3 + sectionmodifyer);
    $('#refpage3').html(totalsections - 2 + sectionmodifyer);
    $('#refpage4').html(totalsections - 1 + sectionmodifyer);
    $('#refpage5').html(totalsections + sectionmodifyer);

    $('.pagerefs').css('color', '#FF6600'); 
    $('#refpage5').css('color', 'black'); 
    $('#refpage5').focus();

    $('#spanresults').text(pagestart + ' - ' + totalpages  + ' of ' + totalpages + ' results');


    //Set Bottom link click param
    $('#b_refpage1').html(totalsections - 4 + sectionmodifyer);
    $('#b_refpage2').html(totalsections - 3 + sectionmodifyer);
    $('#b_refpage3').html(totalsections - 2 + sectionmodifyer);
    $('#b_refpage4').html(totalsections - 1 + sectionmodifyer);
    $('#b_refpage5').html(totalsections + sectionmodifyer);
    $('#b_refpage5').css('color', 'black'); 
    $('#b_refpage5').focus();
    $('#b_spanresults').text(pagestart + ' - ' + totalpages  + ' of ' + totalpages + ' results');

       
    $('#currsection').val(totalsections);
    $('#currpage').val(pagestart);

    for ( i = pagestart; i < (parseInt(pagestart) + parseInt(perpage)); i++ )    
    {
        $('#divPage' + i).show();
    }
    
    HideDetailElements();

}


function ShowNextTextDiv()
{
    
    $('.divPages').hide();
    
    var perpage = parseInt($('#perpage').val());
    var currsection = parseInt($('#currsection').val());
    var currpage = parseInt($('#currpage').val());

    var totalpages = parseInt($('#totalpages').val());
    var totalsections = parseInt(totalpages / perpage);
    if(totalpages % perpage > 0)
        totalsections++;

    var newsection = 0;
    var pagestart = 0;
 

    newsection = currsection + 1;
    // current page = 1
    

    if(newsection <= 4)
    {    
        
        $('#refpage1').html(1); $('#refpage2').html(2); $('#refpage3').html(3); $('#refpage4').html(4);$('#refpage5').html(5);
        $('#b_refpage1').html(1); $('#b_refpage2').html(2); $('#b_refpage3').html(3); $('#b_refpage4').html(4);$('#b_refpage5').html(5);
        
        $('.pagerefs').css('color', '#FF6600'); 
        
        switch(newsection)
        {
            case 1:
                $('#refpage1').css('color', 'black'); $('#refpage1').focus();
                $('#b_refpage1').css('color', 'black'); $('#b_refpage1').focus();
                break;                
            case 2:
                $('#refpage2').css('color', 'black'); $('#refpage2').focus();
                $('#b_refpage2').css('color', 'black'); $('#b_refpage2').focus();
                break;
            case 3:
                $('#refpage3').css('color', 'black'); $('#refpage3').focus();
                $('#b_refpage3').css('color', 'black'); $('#b_refpage3').focus();
                break;
            case 4:
                $('#refpage4').css('color', 'black'); $('#refpage4').focus();
                $('#b_refpage4').css('color', 'black'); $('#b_refpage4').focus();
                break;                
        }
    }
    else
    {
    
        if(newsection >= totalsections)
        {
            $('.pagerefs').css('color', '#FF6600'); 
            $('#refpage1').html(newsection - 4);
            $('#refpage2').html(newsection - 3);
            $('#refpage3').html(newsection - 2);
            $('#refpage4').html(newsection - 1);        
            $('#refpage5').css('color', 'black');
            $('#refpage5').html(newsection );
            $('#refpage5').focus();
            //Bottom links
            $('#b_refpage1').html(newsection - 4);
            $('#b_refpage2').html(newsection - 3);
            $('#b_refpage3').html(newsection - 2);
            $('#b_refpage4').html(newsection - 1);        
            $('#b_refpage5').css('color', 'black');
            $('#b_refpage5').html(newsection );
            $('#b_refpage5').focus();

            
        }
        else
        {
            $('.pagerefs').css('color', '#FF6600'); 
            $('#refpage1').html(newsection - 3);
            $('#refpage2').html(newsection - 2);
            $('#refpage3').html(newsection - 1);
            $('#refpage4').css('color', 'black');
            $('#refpage4').html(newsection );
            $('#refpage4').focus();
            $('#refpage5').html(newsection + 1);
            

            $('#b_refpage1').html(newsection - 3);
            $('#b_refpage2').html(newsection - 2);
            $('#b_refpage3').html(newsection - 1);
            $('#b_refpage4').css('color', 'black');
            $('#b_refpage4').html(newsection );
            $('#b_refpage4').focus();
            $('#b_refpage5').html(newsection + 1);
        }
    }
    
    pagestart = perpage * (newsection - 1) + 1;
    
    if (pagestart + perpage - 1 > totalpages)
    {
        $('#spanresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');
        $('#b_spanresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');
    }
    else
    {
        $('#spanresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');
        $('#b_spanresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');
    }
    
    //alert('perpage = ' + perpage + '\n currsection = ' + currsection + '\n currpage = ' + currpage + '\n newsection = ' + newsection + '\n pagestart = ' + pagestart + '\n refsection = ' + refsection );

    $('#currsection').val(newsection);
    $('#currpage').val(pagestart);

    for ( i = pagestart; i < (parseInt(pagestart) + parseInt(perpage)); i++ )    
    {

        $('#divPage' + i).show();
    }
    
    HideDetailElements();

}


function ShowPreviousTextDiv()
{
    
    $('.divPages').hide();
    
    var perpage = parseInt($('#perpage').val());
    var currsection = parseInt($('#currsection').val());
    var currpage = parseInt($('#currpage').val());

    var totalpages = parseInt($('#totalpages').val());
    var totalsections = parseInt(totalpages / perpage);
    if(totalpages % perpage > 0)
        totalsections++;

    var newsection = 0;
    var pagestart = 0;
 

    newsection = currsection - 1;
    // current page = 1
    

    if(newsection <= 4)
    {    
        
        $('#refpage1').html(1); $('#refpage2').html(2); $('#refpage3').html(3); $('#refpage4').html(4); $('#refpage5').html(5);
        $('#b_refpage1').html(1); $('#b_refpage2').html(2); $('#b_refpage3').html(3); $('#b_refpage4').html(4); $('#b_refpage5').html(5);
        
        $('.pagerefs').css('color', '#FF6600'); 
        
        switch(newsection)
        {
            case 1:
                $('#refpage1').css('color', 'black'); $('#refpage1').focus();
                $('#b_refpage1').css('color', 'black'); $('#b_refpage1').focus();
                break;
            case 2:
                $('#refpage2').css('color', 'black'); $('#refpage2').focus();
                $('#b_refpage2').css('color', 'black'); $('#b_refpage2').focus();
                break;                
            case 3:
                $('#refpage3').css('color', 'black'); $('#refpage3').focus();
                $('#b_refpage3').css('color', 'black'); $('#b_refpage3').focus();
                break;                
            case 4:
                $('#refpage4').css('color', 'black'); $('#refpage4').focus();
                $('#b_refpage4').css('color', 'black'); $('#b_refpage4').focus();
                break;                
        }
    }
    else
    {
    
        if(newsection >= totalsections)
        {
            $('.pagerefs').css('color', '#FF6600'); 
            $('#refpage1').html(newsection - 4);
            $('#refpage2').html(newsection - 3);
            $('#refpage3').html(newsection - 2);
            $('#refpage4').html(newsection - 1);        
            $('#refpage5').css('color', 'black');
            $('#refpage5').html(newsection );
            $('#refpage5').focus();

            $('#b_refpage1').html(newsection - 4);
            $('#b_refpage2').html(newsection - 3);
            $('#b_refpage3').html(newsection - 2);
            $('#b_refpage4').html(newsection - 1);        
            $('#b_refpage5').css('color', 'black');
            $('#b_refpage5').html(newsection );
            $('#b_refpage5').focus();            
        }
        else
        {
            $('.pagerefs').css('color', '#FF6600'); 
            $('#refpage1').html(newsection - 3);
            $('#refpage2').html(newsection - 2);
            $('#refpage3').html(newsection - 1);
            $('#refpage4').css('color', 'black');
            $('#refpage4').html(newsection );
            $('#refpage4').focus();
            $('#refpage5').html(newsection + 1);

            $('#b_refpage1').html(newsection - 3);
            $('#b_refpage2').html(newsection - 2);
            $('#b_refpage3').html(newsection - 1);
            $('#b_refpage4').css('color', 'black');
            $('#b_refpage4').html(newsection );
            $('#b_refpage4').focus();
            $('#b_refpage5').html(newsection + 1);

        }
    }
    
    pagestart = perpage * (newsection - 1) + 1;
    
    if (pagestart + perpage - 1 > totalpages)
    {
        $('#spanresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');
        $('#b_spanresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');        
    }
    else
    {
        $('#spanresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');
        $('#b_spanresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');        
    }
    //alert('perpage = ' + perpage + '\n currsection = ' + currsection + '\n currpage = ' + currpage + '\n newsection = ' + newsection + '\n pagestart = ' + pagestart + '\n refsection = ' + refsection );

    $('#currsection').val(newsection);
    $('#currpage').val(pagestart);

    for ( i = pagestart; i < (parseInt(pagestart) + parseInt(perpage)); i++ )    
    {

        $('#divPage' + i).show();
    }
    
    HideDetailElements();

}

function ShowFullTextDiv(refsection)
{   

    $('.divPages').hide();
    
    var perpage = parseInt($('#perpage').val());
    var currsection = parseInt($('#currsection').val());
    var currpage = parseInt($('#currpage').val());

    var totalpages = parseInt($('#totalpages').val());
    var totalsections = parseInt(totalpages / perpage);
    if(totalpages % perpage > 0)
        totalsections++;

    var newsection = 0;
    var pagestart = 0;
//alert(currsection); 
//alert(refsection);
    // current page = 1
    switch(currsection)
    {
        case 1:
            switch(refsection)
            {
                case -3:
                    newsection = 1;
                    break;
                case -2:
                    newsection = 2;
                    break;
                case -1:
                    newsection = 3;
                    break;
                case 0:
                    newsection = 4;
                    break;
                case 1:
                    newsection = 5;
                    break;
                default:
                    newsection = currsection + parseInt(refsection);
            }   
            break;
        case 2:
            switch(refsection)
            {
                case -3:
                    newsection = 1;
                    break;
                case -2:
                    newsection = 2;
                    break;
                case -1:
                    newsection = 3;
                    break;
                case 0:
                    newsection = 4;
                    break;
                case 1:
                    newsection = 5;
                    break;
                default:
                    newsection = currsection + parseInt(refsection);
            }             
            break;
        
        case 3:
            switch(refsection)
            {
                case -3:
                    newsection = 1;
                    break;
                case -2:
                    newsection = 2;
                    break;
                case -1:
                    newsection = 3;
                    break;
                case 0:
                    newsection = 4;
                    break;
                case 1:
                    newsection = 5;
                    break;
                default:
                    newsection = currsection + parseInt(refsection);
            }         
            break;
        case 4:
            switch(refsection)
            {
                case -3:
                    newsection = 1;
                    break;
                case -2:
                    newsection = 2;
                    break;
                case -1:
                    newsection = 3;
                    break;
                case 0:
                    newsection = 4;
                    break;
                case 1:
                    newsection = 5;
                    break;
                default:
                    newsection = currsection + parseInt(refsection);
            }      
            break;
        default:
            newsection = currsection + parseInt(refsection)  - 1
        
    }

    if(newsection <= 4)
    {    
        
        $('#refpage1').html(1); $('#refpage2').html(2); $('#refpage3').html(3); $('#refpage4').html(4); $('#refpage5').html(5);
        $('#b_refpage1').html(1); $('#b_refpage2').html(2); $('#b_refpage3').html(3); $('#b_refpage4').html(4); $('#b_refpage5').html(5);
        
        $('.pagerefs').css('color', '#FF6600'); 
        
        switch(newsection)
        {
            case 1:
                $('#refpage1').css('color', 'black'); $('#refpage1').focus();
                $('#b_refpage1').css('color', 'black'); $('#b_refpage1').focus();
                break;                
            case 2:
                $('#refpage2').css('color', 'black'); $('#refpage2').focus();
                $('#b_refpage2').css('color', 'black'); $('#b_refpage2').focus();
                break;                
            case 3:
                $('#refpage3').css('color', 'black'); $('#refpage3').focus();
                $('#b_refpage3').css('color', 'black'); $('#b_refpage3').focus();
                break;
            case 4:
                $('#refpage4').css('color', 'black'); $('#refpage4').focus();
                $('#b_refpage4').css('color', 'black'); $('#b_refpage4').focus();
                break;
        }
    }
    else
    {
        //reached max current section
        if(newsection >= totalsections)
        {
            $('.pagerefs').css('color', '#FF6600'); 
            $('#refpage1').html(newsection - 4);
            $('#refpage2').html(newsection - 3);
            $('#refpage3').html(newsection - 2);
            $('#refpage4').html(newsection - 1);        
            $('#refpage5').css('color', 'black');
            $('#refpage5').html(newsection );
            $('#refpage5').focus();

            $('#b_refpage1').html(newsection - 4);
            $('#b_refpage2').html(newsection - 3);
            $('#b_refpage3').html(newsection - 2);
            $('#b_refpage4').html(newsection - 1);        
            $('#b_refpage5').css('color', 'black');
            $('#b_refpage5').html(newsection );
            $('#b_refpage5').focus();
            
        }
        else
        {
            $('.pagerefs').css('color', '#FF6600'); 
            $('#refpage1').html(newsection - 3);
            $('#refpage2').html(newsection - 2);
            $('#refpage3').html(newsection - 1);
            $('#refpage4').css('color', 'black');
            $('#refpage4').html(newsection );
            $('#refpage4').focus();
            $('#refpage5').html(newsection + 1);

            $('#b_refpage1').html(newsection - 3);
            $('#b_refpage2').html(newsection - 2);
            $('#b_refpage3').html(newsection - 1);
            $('#b_refpage4').css('color', 'black');
            $('#b_refpage4').html(newsection );
            $('#b_refpage4').focus();
            $('#b_refpage5').html(newsection + 1);

        }
    }
    
    pagestart = perpage * (newsection - 1) + 1;
    
    if (pagestart + perpage - 1 > totalpages)
    {
        $('#spanresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');
        $('#b_spanresults').text(pagestart + ' - ' + totalpages + ' of ' + totalpages + ' results');        
    }
    else
    {
        $('#spanresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');
        $('#b_spanresults').text(pagestart + ' - ' + (pagestart + perpage - 1) + ' of ' + totalpages + ' results');
    }
    //alert('perpage = ' + perpage + '\n currsection = ' + currsection + '\n currpage = ' + currpage + '\n newsection = ' + newsection + '\n pagestart = ' + pagestart + '\n refsection = ' + refsection );

    $('#currsection').val(newsection);
    $('#currpage').val(pagestart);

    for ( i = pagestart; i < (parseInt(pagestart) + parseInt(perpage)); i++ )    
    {

        $('#divPage' + i).show();
    }
    
    HideDetailElements();

}


function Focus(str)
{
    //CHECK : Determine the text toggle
    if ( $("#search").val() == 'Search for ' + str )
    {
        //Remove the default text
        $("#search").val("");
    }
}

function Blur(str)
{
    //CHECK : Determine the text toggle
    if ( $("#search").val() == '' ) 
    {
        //Set the default text
        $("#search").val("Search for " + str);
        
        //Add all the item(s) to the source select box
        $('#filter option').appendTo('#source'); 
        
        //Sort the list    
        var list = document.getElementById('source');    
        SortOptions(list);
    }
}

function KeyUp(e)
{
    //CHECK : Determine the browser and act accordingly
    if(window.event) //IE
    {
        keynum = e.keyCode;
    }
    else if(e.which) //Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
       
    //CHECK : Determine if the 'Backspace' key was pressed
    if (keynum == 8) //Backspace
    {        
        //Add all the item(s) to the source select box
        $('#filter option').appendTo('#source');
        
        //Build the entry
        strEntry = $("#search").val();
        
        //Filter the search list
        FilterSearch(strEntry);
        
        return true;
    }
    
    //CHECK : Determine if the 'Delete' key was pressed
    if (keynum == 46) //Delete Key
    {
        //Add all the item(s) to the source select box
        $('#filter option').appendTo('#source'); 
        
        //Build the entry
        strEntry = $("#search").val();
        
        //Filter the search list
        FilterSearch(strEntry);
        
        return true;
    }
}

function KeyPress(e)
{
    //Initialize variables
    var keynum;
    var keychar;

    //CHECK : Determine the browser and act accordingly
    if(window.event) //IE
    {
        keynum = e.keyCode;
    }
    else if(e.which) //Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    //CHECK : Determine if the 'Enter' key was pressed
    if (keynum == 13) //Enter Key
    { 
        //Disable the 'Enter' key  
        return false;
    }

    //Build the entry
    keychar = String.fromCharCode(keynum);
    strEntry = $("#search").val() + keychar;

    //Filter the search list
    FilterSearch(strEntry);
}

//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------

function AddAll()
{   
    //Remove all the item(s) from the source select box, Add all the item(s) to the destination select box
    $('#source option').remove().appendTo('#destination'); 
    
    //Remove the 'selected' attribute from the item(s)
    $('#destination option:selected').removeAttr("selected");
    
    //Sort the list    
    var list = document.getElementById('destination');    
    SortOptions(list);    
}

function Add()
{   

    //Remove the selected item(s) from the source select box, Add all the selected item(s) to the destination select box
    $('#source option:selected').remove().appendTo('#destination');
    
    //Remove the 'selected' attribute from the item(s)
    $('#destination option:selected').removeAttr("selected");
    

    //Sort the list    
    var list = document.getElementById('destination');    
    SortOptions(list); 
}

function addsubject()
{

    var found = 0;
    var matchPos1;
    $('#destination option').each
    (
        function(i)
        {
            //Set the item to compare
            strFind = $(this).text();
            //LOOK FOR A MATCH

		    // matchPos1 = strFind.match(document.getElementById(tree_selected_id).innerHTML);
			// changed the above line to do the following. The following works, the previous didn't due to ( ) in the text
		    if (document.getElementById(tree_selected_id).innerHTML == strFind)
			{
				matchPos1 = 'found';
			}
			else
			{
				matchPos1 = null;
			}

			if (matchPos1 == null)
            {
                //Set the value
                matchPos1 = -1;
            }
            if(matchPos1 != -1)
            {
                found = 1;
            }
        }
    )
	
    if(found == 0)
    {
        var opts = $( 'option', '#destination' );

        var newoption = '<option value="' + tree_selected_id.replace('b', '') + '">' + document.getElementById(tree_selected_id).innerHTML + '</option>';

        opts = opts.add( $(newoption).appendTo('#destination')); 
    }
    
    
}


    
function Remove()
{   
    //Remove the selected item(s) from the destination select box, Add all the selected item(s) to the source select box
    $('#destination option:selected').remove().appendTo('#source');
    
    //Remove the 'selected' attribute from the item(s)
    $('#source option:selected').removeAttr("selected");
    
    //Sort the list    
    var list = document.getElementById('source');    
    SortOptions(list); 
}

function RemoveAll()
{   
    //Remove all the item(s) from the destination select box, Add all the item(s) to the source select box
    $('#destination option').remove().appendTo('#source');
    
    //Remove the 'selected' attribute from the item(s)
    $('#source option:selected').removeAttr("selected");
    
    //Sort the list    
    var list = document.getElementById('source');    
    SortOptions(list); 
}

function ResetList()
{   
    //Add all the item(s) to the source select box
    $('#filter option').appendTo('#source'); 
    
    //Set the focus
    $("#search").focus();
    
    //Build the entry    
    strEntry = $("#search").val();
    
    //CHECK : Verify 
    if (strEntry.length > 0)
    {    
        //Filter the search list
        FilterSearch(strEntry);
    }
}



// existing items are added to the destination
function AddExistingItem(SelectList, Text,Value)
{
	// Create an Option object        
    var opt = document.createElement("option");

	// Add an Option object to Drop Down/List Box
    SelectList.options.add(opt);

	// Assign text and value to Option object
    opt.text = Text;
    opt.value = Value;
	
}

// existing items are removed from the source
function DelExistingItem(SelectList, Text,Value)
{
	// the list and the position the items is in
	var SelectList  = document.getElementById("source");
  	var theposition= 0;
	// loop through the entire list  and when an item matches mark the index
	for (x = 1; x < SelectList.length; x++)
   	{
      if(SelectList.options[x].value == Value)
      {
	      theposition = SelectList.options[x].index;
      }
   	}
  	// remove the index
    SelectList.remove(theposition);
}




function FilterSearch(strEntry)
{   

    //Cycle through the item(s)
    strEntry = strEntry.toLowerCase()
    $('#source option:selected').removeAttr("selected");
    $('#source option').each
    (
        function(i)
        {
      
            
    
            //Set the item to compare
            if (strEntry == $(this).text().toLowerCase().substr(0, strEntry.length))
            {
                $(this).attr("selected", "selected");
                return false;
            }
            
//            strFind = $(this).text().toLowerCase();
//            //LOOK FOR A MATCH
//            
//            var matchPos1 = strFind.match(strEntry.toLowerCase());
//            
//           
//            //CHECK : Verify if the value is NULL
//            if (matchPos1 == null)
//            {
//                //Set the value
//                matchPos1 = -1;
//            }
//            
//            if(matchPos1 != -1)
//            {  
//                
//            }
//            else
//            {
//                //Match not found - Remove item from list
//                $(this).appendTo('#filter');
//            }
        }                
    ) 
        
    return false;
}

function SortOptions(list) 
{  
    //Initialize variable
    var items = list.options.length;  

    //Create array and make copies of options in list
    var tmpArray = new Array(items); 

    for ( i=0; i<items; i++ )    
    {
        tmpArray[i] = new Option(list.options[i].text,list.options[i].value);  
    }

    //Sort options using given function 
    tmpArray.sort(CompareOptionText); 

    //Make copies of sorted options back to list  
    for ( i=0; i<items; i++ )
    {
        list.options[i] = new Option(tmpArray[i].text,tmpArray[i].value); 
    }
}

function CompareOptionText(a,b) 
{  
    /*   
    return  >0 if a>b   
            0 if a=b           
            <0 if a<b   
    */
    
    //Textual comparison
    //return a.text != b.text ? a.text < b.text ? -1 : 1 : 0;
    if (TrimStartNumber(a.text) < TrimStartNumber(b.text))
    {
        return -1;        
    } 
    if (TrimStartNumber(a.text) > TrimStartNumber(b.text))
    {
        return 1;
    }
    else
    {
        return 0;
    } 

    //Numerical comparison
    //return a.text - b.text; 
} 

function TrimStartNumber(strText)
{
    //Initialize variables
    var ValidNumberChars = "0123456789.";
    var blnIsNumber = true;    
    var Char;
 
    //Cycle through all the character(s)
    for (i = 0; i < strText.length && blnIsNumber == true; i++) 
    { 
        //Set the value
        Char = strText.charAt(i);
        
        //CHECK : Validate if the character is numeric 
        if (ValidNumberChars.indexOf(Char) == -1) 
        {
            blnIsNumber = false;
        }
        else
        {
            //Remove the numeric character
            strText = strText.substring(1);
            i = i - 1;
        }
    }
    
    return $.trim(strText.toUpperCase());
}

function Exit(frmName)
{
    //Initialize variables
    var strSelectedItems = "";    
    var mySplitResult = "";
    var strPageName = "";
    var SelectList; 
    var ListCount = 0;
    if(frmName == "Publisher")
	    SelectList = parent.parent.parent.document.getElementById('selPublisher');
    else
        SelectList = parent.parent.parent.document.getElementById('selSubject');
        

	for(i=SelectList.length-1;i>=0;i--)
	{
		SelectList.remove(i);
	}


            
    //Gather all the selected item(s)
    $('#destination option').each
    ( 
        function() 
        {   
            ListCount ++;
            
            if(strSelectedItems == "")
                strSelectedItems = $(this).val() + "|" + $(this).text();
            else
                strSelectedItems += "," + $(this).val() + "|" + $(this).text();
            
            AddSelectOption(SelectList, $(this).text(), $(this).val(), false);            

        } 
    );     
//    alert(strSelectedItems);
	if (ListCount >= 3)
	{
	 	ListCount = 3; 
	}
    if(frmName == "Publisher")
    {
        parent.parent.document.getElementById('selPublisher').size = ListCount;
        parent.parent.document.getElementById('divPublisherList').style.display = 'block';
        parent.parent.document.getElementById('publisher').value = strSelectedItems;
		// if we need to we can write out the str selected items and hide the select
        parent.parent.document.getElementById('PublisherListWindow_close').onclick();
		parent.parent.document.getElementById('selPublisher').style.background = "white"
		parent.parent.document.getElementById('selPublisher').selectedIndex = -1;

        // if there are none added then hide the div and set the checked radios
        // else set the opposite radio
		if (ListCount ==0)
        {
            parent.parent.document.getElementById('divPublisherList').style.display = 'none';
            parent.parent.document.getElementById('publ0').checked = "checked";
            parent.parent.document.getElementById('publ1').checked = false;
        }
        else
        {
            parent.parent.document.getElementById('publ0').checked = false;
            parent.parent.document.getElementById('publ1').checked = "checked";
        }

		
    }
    else
    {
        parent.parent.document.getElementById('selSubject').size = ListCount;
        parent.parent.document.getElementById('divSubjectList').style.display = 'block';
        parent.parent.document.getElementById('subject').value = strSelectedItems;
        parent.parent.document.getElementById('SubjectListWindow_close').onclick();
		parent.parent.document.getElementById("selSubject").style.background = "white"
		parent.parent.document.getElementById("selSubject").selectedIndex = -1;

         // if there are none added then hide the div and set the checked radios
        // else set the opposite radio
		if (ListCount ==0)
        {
            parent.parent.document.getElementById('divSubjectList').style.display = 'none';
            parent.parent.document.getElementById('subj0').checked = "checked";
            parent.parent.document.getElementById('subj1').checked = false;
        }
        else
        {
            parent.parent.document.getElementById('subj0').checked = false;
            parent.parent.document.getElementById('subj1').checked = "checked";
        }

 
    }
    
}

function CloseModalWindow(response)
{
    //Fade out the Modal window
    tb_remove();
}

function cmdDone()
{

    var strFind;
    strFind = "";
    $('#destination option').each
    (
        function(i)
        {
            //Set the item to compare
            strFind += $(this).val().toLowerCase() + "~" + $(this).text().toLowerCase() + ",";
            
        }
    )
     alert(strFind);       
}


function AddSelectOption(selectObj, text, value, isSelected) 
{
    if (selectObj != null && selectObj.options != null)
    {
        selectObj.options[selectObj.options.length] = new Option(text, value);
    }

}


//--------------------------------------------------------------------------------