﻿function setActiveMenu(menuItemID) {
    if (document.getElementById) {
        menu = document.getElementById(menuItemID);
        if (menu) {
            menu.setAttribute("id", menuItemID + "_select");
        }
    }
}
function NewsletterSignup() {
    if (typeof j$ != 'undefined') {
        j$.ajax({
            type: "POST",
            url: "/ajax_door.aspx/NewsletterSignup",
            data: "{'email':'" + j$("iddrij-iddrij").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var result = msg.d;

                if (result == "Success") {
                    j$("#lbStatus").hide();
                    this.window.location = "/subscribe_confirm.asp";
                }
                else {                    
                    j$("#lbStatus").text(result);
                    j$("#lbStatus").show();
                }
            }
        });  
    }
}
var t;
function GetPdfCreatingStatus() {
    if (typeof j$ != 'undefined') {
        j$.ajax({
            type: "POST",
            url: "search_results.aspx/GetPdfCreatingStatus",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var result = msg.d;

                if (start_download == true && result == true) {
                    Hide_BusyBox1();

                    j$("#BusyBox1Overlay").remove();                    
                                        
                    start_download = false;
                    clearTimeout(t);
                }
            }
        });
    }
}


function QuickSearck() {
    if (typeof j$ != 'undefined') {
        var searchTerm = j$("#quick").val();
        if (searchTerm) {           
            window.location = "/search_results.aspx?q=" + searchTerm;
        }
    }
}
function MakeAnEnquiry() {
    if (typeof j$ != 'undefined' && typeof j$.url != 'undefined') {
        var book_id = j$.url.param("id");
        window.location = "/contact.asp?book=" + book_id;
    }
}
function DownloadPDF() {
    if (typeof j$ != 'undefined') {        
        window.location = 'pdfdownload.ashx';
    }
}

function main() {
    
    if ( j$("input[class='sign_up']") ) j$("input[class='sign_up']").click(function() { NewsletterSignup(); });
    if ( j$("input[class='quick_search']") ) j$("input[class='quick_search']").click(function() { QuickSearck() });
    if ( j$("input[class='save_pdf']")) j$("input[class='save_pdf']").click(function() { DownloadPDF() });
    if (j$("#btMakeEnquiry")) j$("#btMakeEnquiry").click(function() { MakeAnEnquiry() });
}

function init() {
    if (typeof jQuery != 'undefined') {
        var j$ = jQuery.noConflict();
        j$(document).ready(main);
    }
}

function init_timer() {
    t = setTimeout("init_timer()", 1000);
    GetPdfCreatingStatus();
}
var start_download;
function main_search_results() {

    if (j$("#BusyBoxButton1")) j$("#BusyBoxButton1").click(function() { start_download = true; init_timer(); });
}

function init_scripts_search_results() {
    if (typeof jQuery != 'undefined') {
        var j$ = jQuery.noConflict();
        j$(document).ready(main_search_results);
    }
}

/*pager functions moved here*/
function hidePager(pagerClientID) {    
    if (j$ != "undefined") {        
        var selector = '#' + pagerClientID + ' a';
        if (j$(selector).length == 0) {
            j$('.CurrentPage').hide();
        }
    }
}
function appendRightParent(pagerClientID) {    
    if (j$ != "undefined") {
        var selector = '#' + pagerClientID + ' > span, ' + '#' + pagerClientID + '> a';
        j$(selector).wrapAll('<div class=\"right\"><p></p></div>');
        var selectorLeftRight = '#' + pagerClientID + ' div';
        j$(selectorLeftRight).wrapAll('<div class=\"pagination\"></div>');
        j$('#' + pagerClientID + ' div.right').after('<div class=\"clear\"></div>');
        j$('#' + pagerClientID).replaceWith(j$('#' + pagerClientID + ' div.pagination'));
    }

}
function init_pager(pager_id) {
    if (typeof jQuery != 'undefined') {    
        var j$ = jQuery.noConflict();
        j$(document).ready(
            function() {                
                hidePager(pager_id);
                appendRightParent(pager_id);
            }
        );
    }   
}

