function ajaxMasterPageFunction(handlerItem, controlId)
{
   var ajaxRequest = null;
   var url = "/handlers/Handler.ashx?item=" + handlerItem;

   try { ajaxRequest = new XMLHttpRequest(); }
   catch (e)
   {
      try { ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } 
      catch (e) 
      {
         try { ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
         catch (e)
         {
            alert("graceful alert message here!");
            return false;
         }
      }
   }
   
   ajaxRequest.onreadystatechange = function()
      {
         if(ajaxRequest.readyState == 4)
         {
            document.getElementById(controlId).innerHTML = ajaxRequest.responseText;
         }
      }

   ajaxRequest.open("GET", url, true);
   ajaxRequest.send(null);
}

// **** DOWNLOADS **** //// **** DOWNLOADS **** //// **** DOWNLOADS **** //
// **** DOWNLOADS **** //// **** DOWNLOADS **** //// **** DOWNLOADS **** //
// **** DOWNLOADS **** //// **** DOWNLOADS **** //// **** DOWNLOADS **** //

function ajaxDownloadFunction(action, orderDetailId, bookTitle, inventoryLookupId)
{
    var ajaxRequest = null;
   
    if (inventoryLookupId == null)
        var url = "/handlers/Downloads.ashx?action=" + action + "&orderDetailId=" + orderDetailId + "&bookTitle=" + bookTitle;
    else
        var url = "/handlers/Downloads.ashx?action=" + action + "&orderDetailId=" + orderDetailId + "&bookTitle=" + bookTitle + "&inventoryLookupId=" + inventoryLookupId; 

    try { ajaxRequest = new XMLHttpRequest(); }
    catch (e)
    {
      try { ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } 
      catch (e) 
      {
         try { ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
         catch (e)
         {
            alert("graceful alert message here!");
            return false;
         }
      }
    }

    ajaxRequest.onreadystatechange = function()
      {
         if(ajaxRequest.readyState == 4)
         {
            var controlId = "download" + bookTitle
            document.getElementById(controlId).style.visibility = "visible";
            document.getElementById(controlId).innerHTML = ajaxRequest.responseText;
            document.getElementById("Throbber" + bookTitle).style.visibility = "hidden";       
         }
      }

    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
}

function showDownloads(bookTitle, orderDetailId)
{
    document.getElementById("Throbber" + bookTitle).style.visibility = "visible";
    ajaxDownloadFunction("ShowDownloads", orderDetailId, bookTitle, null);
}

/*function downloadChapter(bookTitle, inventoryLookupId, orderDetailId)
{
    document.getElementById("Throbber" + bookTitle).style.visibility = "visible";    
    
    if (bookTitle != "" && inventoryLookupId != "")
    {
        var URL = "download.aspx?inventoryLookupId=" + inventoryLookupId + "&orderDetailId=" + orderDetailId;
        
        day = new Date();
        id = day.getTime();
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=no,statusbar=0,menubar=0,resizable=1,width=1,height=1,left = 500.5,top = 250');");
    }
    
    ajaxDownloadFunction("ShowDownloads", orderDetailId, bookTitle, inventoryLookupId);
}

function downloadMp3(URL)
{
    window.open(URL, "URL", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=1,height=1,left = 500.5,top = 250');
}*/

// **** DOWNLOADS **** //// **** DOWNLOADS **** //// **** DOWNLOADS **** //
// **** DOWNLOADS **** //// **** DOWNLOADS **** //// **** DOWNLOADS **** //
// **** DOWNLOADS **** //// **** DOWNLOADS **** //// **** DOWNLOADS **** //

function populateFields()
{
    ajaxMasterPageFunction("AccountName", "displayAccountName");
    ajaxMasterPageFunction("ShoppingCartCount", "displayShoppingCartCount");
}


function addToCart(sku, quantity)
{
    document.getElementById(sku + "Throbber").style.visibility = "visible";
            
    setTimeout("cartRedirect('"+sku+"', '"+quantity+"')", 1000);
}

function cartRedirect(sku, quantity)
{
    window.location = "/gShop/AddToCart.aspx?Product=" + sku + "&Qty=" + quantity;
}

function eBibleSearch()
{
    var query = document.getElementById("eBibleQuery").value;
    
    if (query != "" || query != null)
        window.open("http://www.ebible.com/bible/" + encodeURIComponent(query));
}

function affiliateSearch(URL) 
{
    day = new Date();
    id = "affiliateSearchWindow";
    var child = eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=439,height=400,left = 500.5,top = 250');");
}

function mailingListSubscribe()
{
    var emailAddress = document.getElementById("textMailingListSubscribe").value;
    
    if (emailAddress != "")
    {
        var URL = "/mailingListSubscribe.aspx?emailAddress=" + emailAddress;
        day = new Date();
        id = "mailingListSubscribeWindow";
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=1,height=1,left = 500.5,top = 250');");
    }
}