


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

var HTTP_GET = GetXmlHttpRequest();

var sessionId = "ciyisljuyhmzlu2pwez5ydzp";
var guid = "c670bd9c-b769-45b0-a66d-a5ff975ba97e";

var currentRequest = 0;

var clickedMenuCode;
var clickedMenuLevel;

var selectSubMenu = "";
var randomSubMenu = "";

var chosenCarriage = "";
var chosenCarriageElemId = "";

// the below variable stores the HTML when a user performs a search
// needed because when we add the progress bar the search result will show all progress bars instead of just one
var tempSearchHtmlOutput = "";


// Create suitable xmlDoc object

// var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlDoc = null;

if (document.implementation && document.implementation.createDocument)
{
    xmlDoc = document.implementation.createDocument("", "", null);
    xmlDoc.async = false;
}
else if (window.ActiveXObject)
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
}

try //Internet Explorer
{
	//xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
catch(e) // Proper browsers
{
    //xmlDoc = new window.XMLHttpRequest();
}

function updateShippingSetting(elem)
{
    if(elem.checked == true)
    {
        document.PlaceOrderBasketForm.shipAtSameTimeSetting.value = elem.value;
    }
    else
    {
        document.PlaceOrderBasketForm.shipAtSameTimeSetting.value = "";
    }
}

function ProceedToPayment()
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
       //HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?GetViewState=true&dm=" + v + ""), true);
       HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?GetViewState=true"), true);
        
       HTTP_GET.onreadystatechange = RefreshBasketViewState_Callback;
       HTTP_GET.send(null);
   }
}

function RefreshBasketViewState_Callback()
{
    if (HTTP_GET.readyState == 4) 
    {
        var viewState = HTTP_GET.responseText;
        
        GetById("PlaceOrderBasket").value = viewState;
        
        document.forms["PlaceOrderBasketForm"].submit();
    }
}

function SetCarriage(carriage, carriageElem)
{
   chosenCarriage = carriage;
   chosenCarriageElemId = carriageElem.id;
   SetSpanInnerHTMLText("content", "<i>Updating Carriage.</i>");
        
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        if(carriage != "")
        { 
           HTTP_GET.open("GET", 
                 MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?Review=true&SetCarriage=" + carriage), true);
        }
        else
        {
           HTTP_GET.open("GET", 
                 MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?Review=true&RemoveCarriage=true"), true);
        }
        
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
        setTimeout("setChosenCarriage(chosenCarriageElemId)", 500);
   }
}

function SetCountry(country)
{
   if(country != "")
   {
       SetSpanInnerHTMLText("ShippingOptions", "<i>Updating.</i>");
            
       if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
       {
            HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?SetCountry=" + country), true);
            HTTP_GET.onreadystatechange = SetCountry_Callback;
            HTTP_GET.send(null);
       }
   }
}

function RefreshShippingOptions()
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/ShippingOptionsServer.aspx"), true);
        HTTP_GET.onreadystatechange = RefreshShippingOptions_Callback;
        HTTP_GET.send(null);
        
        // Change
        
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?Review=true&RemoveCarriage=true"), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}

function RefreshShippingOptions_Callback()
{
    if (HTTP_GET.readyState == 4) 
    {
         SetSpanInnerHTMLText("ShippingOptions", HTTP_GET.responseText);
    }
}

function SetCountry_Callback()
{
    if (HTTP_GET.readyState == 4) 
    {
        RefreshShippingOptions();
    }
}

function ReviewBasket()
{   
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?Review=true"), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}

function MakeUrlUnique(url)
{
     currentRequest = currentRequest + 1;
     
     if(url.indexOf("?") != -1)
     {
          url += "&";
     }
     else
     {
         url += "?";
     }
     
     url += "Guid=" + guid + "&CurrentRequest=" + currentRequest;
     
     return url;
}


function IncreaseBasketQty(productId)
{
    SetSpanInnerHTMLText("BasketDisplay", 
             '<p>Updating. <img src="http://www.thebookshoponline.com/images/basket.jpg" width="29" height="21" alt="basket"/><br /><input name="search" type="text" value="search" size="20" maxlength="100" /><input name="go" type="submit" value="GO" class="submitgo" /></p>');
              
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?AddProductId=" + 
              productId + "&AddProductQty=1"), true);
        HTTP_GET.onreadystatechange = AdjustQty_Callback;
        HTTP_GET.send(null);
   }
}

function DecreaseBasketQty(productId)
{
    SetSpanInnerHTMLText("BasketDisplay", "<i>Updating.</i>");
        
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?RemoveProductId=" + 
              productId + "&RemoveProductQty=1"), true);
        HTTP_GET.onreadystatechange = AdjustQty_Callback;
        HTTP_GET.send(null);
   }
}


function AdjustQty_Callback()
{
    if (HTTP_GET.readyState == 4) 
    {
         SetSpanInnerHTMLText("BasketDisplay", HTTP_GET.responseText);
         
         ReviewBasket();
    }
}

var doReviewBasket = "";

function Buy(productId, textboxId)
{
//     doReviewBasket = "true";
     
     AddToBasket(productId, textboxId);
//     window.scroll(0,0);
}


function AddToBasket(productId, textboxId)
{
    
    var addBox = null;
    
    if(textboxId == null)
    {
        addBox = GetById("BasketQty" + productId);
    }
    else 
    {
        addBox = GetById(textboxId);
    }
    

    if(IsInt(addBox.value)&&(addBox.value > 0))
    {
        SetSpanInnerHTMLText("BasketDisplay", 
             '<p>Updating.<img src="http://www.thebookshoponline.com/images/basket.jpg" width="29" height="21" alt="basket"/><br /><input name="search" type="text" value="search" size="20" maxlength="100" /><input name="go" type="submit" value="GO" class="submitgo" /></p>');
              
       if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
       {
            HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?AddProductId=" + 
                  productId + "&AddProductQty=" + addBox.value), true);
            HTTP_GET.onreadystatechange = ShowBasket_Callback;
            HTTP_GET.send(null);
       }
    }
    else
    {
        alert("Basket qty is invalid.");
    }
}

function RemoveFromBasket(productId, textboxId)
{
    var addBox = null;
    
    if(textboxId == null)
    {
        addBox = GetById("BasketQty" + productId);
    }
    else 
    {
        addBox = GetById(textboxId);
    }
    
    if(IsInt(addBox.value))
    {
        SetSpanInnerHTMLText("BasketDisplay", "<i>Updating.</i>");
            
       if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
       {
            HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?RemoveProductId=" + 
                  productId + "&RemoveProductQty=" + addBox.value), true);
            HTTP_GET.onreadystatechange = ShowBasket_Callback;
            HTTP_GET.send(null);
       }
    }
    else
    {
        alert("Basket qty is invalid.");
    }
}

function ShowBasket()
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/BasketServer.aspx", true);
        HTTP_GET.onreadystatechange = ShowBasket_Callback;
        HTTP_GET.send(null);
   }
}


function ShowProduct(productId, listCategory)
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
	    // this is what happens when the user clicks on an individual product
        SetSpanInnerHTMLText("content", "<i>Please wait while the product you chose is displayed.</i>");
           
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/ProductServer.aspx?ProductId=" + 
                          productId), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
        // Check if user is on static page. If so set marginLeft to 142
        if(window.location.href == "http://www.thebookshoponline.com" || window.location.href == "http://www.thebookshoponline.com/" || window.location.href == "http://www.thebookshoponline.com/Default.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/AdvancedSearch.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Contact.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Delivery.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/FurtherLinks.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Help.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/InternationalContacts.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Links.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/SiteMap.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Terms.aspx")
        {
            document.getElementById('content').style.marginLeft = '190';
        }
        window.scroll(0,0);
   }
}

function ShowProductNoCat(productId)
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
	// this is what runs when the user clicks on individual product
        SetSpanInnerHTMLText("content", "<i>Please wait while the product you chose is displayed.</i>");
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/ProductServer.aspx?ProductId=" + 
                          productId), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
        // Check if user is on static page. If so set marginLeft to 190
        if(window.location.href == "http://www.thebookshoponline.com" || window.location.href == "http://www.thebookshoponline.com/" || window.location.href == "http://www.thebookshoponline.com/Default.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/AdvancedSearch.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/AdvancedSearch.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Contact.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Delivery.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/FurtherLinks.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Help.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/InternationalContacts.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Links.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/SiteMap.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Terms.aspx")
        {
            document.getElementById('content').style.marginLeft = '190';
        }
        window.scroll(0,0);
   }
}


function ShowProductPage(code, page, pageSize, sort)
{    
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        SetSpanInnerHTMLText("content", "<i>Please wait while your chosen products are displayed.</i>");
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?Page=" + page + 
                        "&ListCategoryCode=" + code + 
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}


//Called When Items Per Page is changed
function ShowProductListWithRecords(code, ctrl, sort)
{    
    var listControl = ctrl;
   
    pageSize = listControl.options[listControl.selectedIndex].value;
        
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Updating.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?ListCategoryCode=" + code + 
                        "&PageSize=" + pageSize + "&Sort=" + sort, true); 
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

//Called When "Sorted By" is changed
function ShowProductListSorted(code, ctrl, pageSize)
{     
    var listControl = ctrl;
    sort = listControl.options[listControl.selectedIndex].value;
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Updating.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?ListCategoryCode=" + code + 
                        "&Sort=" + sort + "&PageSize=" + pageSize, true);                                                        
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

function ShowProductList(code)
{
    var MENUITEM = GetById("LHSMenu" + code);
    var menuOptions = GetMenuOptions(xmlDoc.documentElement);
    var pName = GetChildNodeText(xmlDoc.documentElement, "parent");
    var PARENT = GetById(pName);
    if (PARENT.id != "sidemenu" & PARENT.id != pName) // this was MENUITEM.ID as of 08/05/2009
    {
        if (PARENT.id.substring(7, 11) != MENUITEM.id.substring(7, 11))
        {
            RemovedSubMenus(PARENT);
        }
    }
    
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
	// this is what runs when user clicks subcategory (level 2)
        SetSpanInnerHTMLText("content", "<i>Please wait while your chosen products are displayed.</i>");
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?ListCategoryCode=" + code, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
        
        // Check if user is on static page. If so set marginLeft to 190
        if(window.location.href == "http://www.thebookshoponline.com" || window.location.href == "http://www.thebookshoponline.com/" || window.location.href == "http://www.thebookshoponline.com/Default.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/AdvancedSearch.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Contact.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Delivery.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/FurtherLinks.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Help.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/InternationalContacts.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Links.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/SiteMap.aspx" || window.location.href == "http://www.thebookshoponline.com/Site/Terms.aspx")
        {
            document.getElementById('content').style.marginLeft = '190';
        }        
    }
    window.scroll(0,0);
}

function ShowMainDisplay_Callback()
{
    if (HTTP_GET.readyState == 4) 
    {
         SetSpanInnerHTMLText("content", HTTP_GET.responseText);  
    }
}

function ShowBasket_Callback()
{
    if (HTTP_GET.readyState == 4) 
    {
         SetSpanInnerHTMLText("BasketDisplay", HTTP_GET.responseText);
         
         if(doReviewBasket != "")
         {
             doReviewBasket = "";
             
             ReviewBasket();
         }
    }
}

// Change by A.Buchan on 13/07/2009
// Purpose: Trying to convert site to be cross browser

//var xmlDoc = null;

try
  {
    // IE Support
    //xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
    // Proper browsers
    //xmlDoc = new DOMParser();
  }

//var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

var randomHttpRequest = GetXmlHttpRequest();

function ShowRandomProducts(code)
{
   if (randomHttpRequest.readyState == 4 || randomHttpRequest.readyState == 0) 
   {
        SetSpanInnerHTMLText("content", "<i>Updating.</i>");
           
        randomHttpRequest.open("GET", 
              "http://www.thebookshoponline.com/Products/ProductServer.aspx?RandomTopCategory=" + 
                          code, true);
        randomHttpRequest.onreadystatechange = ShowRandomProducts_Callback;
        randomHttpRequest.send(null);
   }
}

function ShowRandomProducts_Callback()
{
    if (randomHttpRequest.readyState == 4) 
    {
         SetSpanInnerHTMLText("content", randomHttpRequest.responseText);
    }
}

function ShowRandomProductList(code)
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        SetSpanInnerHTMLText("content", "<i>Updating.</i>");
           
        HTTP_GET.open("GET", 
              MakeUrlUnique("http://www.thebookshoponline.com/Products/ProductServer.aspx?RandomTopCategory=" + 
                          code), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}


function ShowMenu_Callback()
{
      //alert("Inside SM_CB");
      if (xmlDoc.readyState == 4 || xmlDoc.readyState == undefined || xmlDoc.readyState == null)
      {
          //alert("Ins");
          var menuOptions = GetMenuOptions(xmlDoc.documentElement);
          var pName = GetChildNodeText(xmlDoc.documentElement, "parent");
          var PARENT = GetById(pName);

//          var dv = document.createElement("<div>");
//          var dp = document.createElement("<p>");
//          var pt = document.createTextNode("SECTION HEADING");
          
 //         dp.appendChild(pt);          
   //       dv.appendChild(dp);

     //     PARENT.appendChild(dv);
     
     // Change, A.Buchan
     var menuOptionsTitles = new Array();
     menuOptionsTitles[0] = "Art &amp; Architecture";
     menuOptionsTitles[1] = "Astrology &amp; the unexplained";
     menuOptionsTitles[2] = "Audio Video";
     menuOptionsTitles[3] = "Business &amp; Management";
     menuOptionsTitles[4] = "Careers";
     
     var menuOptionsCodes = new Array();
     menuOptionsCodes[0] = "BK03";
     menuOptionsCodes[1] = "BK04";
     menuOptionsCodes[2] = "BK05";
     menuOptionsCodes[3] = "BK06";
     menuOptionsCodes[4] = "BK07";
     
     var menuOptionsActions = new Array();
     menuOptionsActions[0] = "javascript: ShowMenu('" + menuOptionsCodes[0] + "', '2')";
     menuOptionsActions[1] = "javascript: ShowMenu('" + menuOptionsCodes[1] + "', '2')";
     menuOptionsActions[2] = "javascript: ShowMenu('" + menuOptionsCodes[2] + "', '2')";
     menuOptionsActions[3] = "javascript: ShowMenu('" + menuOptionsCodes[3] + "', '2')";
     menuOptionsActions[4] = "http://www.thebookshoponline.com/Shop/Default.aspx?TopLevelCategory=BK&CategoryTop=BK&SelectSubCategory=BK07#Careers";
     
     var UL = document.createElement("<ul>");
     
     for(var j = 0; j < menuOptionsCodes.length; j++)
     {
          //alert("Looping through menuOptionsCodes");
          var id = "LHSMenu" + menuOptionsCodes[j];
          var LI = document.createElement("<li>");
              
          LI.setAttribute("id", id);

          var A = document.createElement("<a>");
              
          A.setAttribute("href", menuOptionsActions[j]);
             
          var TEXT = document.createTextNode(menuOptionsTitles[j]);
              
          //A.appendChild(TEXT);
             
          //LI.appendChild(A);
          //UL.appendChild(LI);
     }
     //PARENT.appendChild(UL);
     
     // End Change

          
          var UL = document.createElement("<ul>");
          
          for(var i = 0; i < menuOptions.length; i++)
          {
              var id = "LHSMenu" + menuOptions[i].code;
              var LI = document.createElement("<li>");
              
              LI.setAttribute("id", id);

              var A = document.createElement("<a>");
              
              A.setAttribute("href", menuOptions[i].href);
              
              var TEXT = document.createTextNode(menuOptions[i].title);
              
              A.appendChild(TEXT);
             
              LI.appendChild(A);
              UL.appendChild(LI);
          }
          
          PARENT.appendChild(UL);
          
          if(selectSubMenu != "")
          {
              DoSelectSubMenu()
          }
     }
}

function DoSelectSubMenu()
{
    var tmp = selectSubMenu;
    selectSubMenu = "";
    
    ShowMenu(tmp, "2");
}

function GetChildNodeText(xmlDoc, childName)
{
    var answer = "";
    
    for(var i = 0; i < xmlDoc.childNodes.length; i++)
    {
        if(xmlDoc.childNodes(i).tagName == childName)
        {
            answer = xmlDoc.childNodes(i).text.trim();
        }
    }
    
    return answer;
}

function GetMenuOptions(xmlDoc)
{
    var answer = new Array();
    var optionNo = 0;
    for(var i = 0; i < xmlDoc.childNodes.length; i++)
    {
        if(xmlDoc.childNodes(i).tagName == "option")
        {
            var obj = new Object();
            
            var option = xmlDoc.childNodes(i);
            
            for(var x = 0; x < option.childNodes.length; x++)
            {
                if(option.childNodes(x).tagName == "title")
                {
                     obj.title = option.childNodes(x).text.trim();
                }
                else if(option.childNodes(x).tagName == "code")
                {
                     obj.code = option.childNodes(x).text.trim();
                }
                else if(option.childNodes(x).tagName == "href")
                {
                     obj.href = option.childNodes(x).text.trim();
                }
            }
            
            answer[optionNo] = obj;
            
            optionNo++;
        }
    }
    
    return answer;
}

function TestAlert()
{
    alert("TEST 1");
}

function GetXmlHttpRequest() 
{
	if (window.XMLHttpRequest) 
	{
		return new XMLHttpRequest(); 
	} 
	else if(window.ActiveXObject) 
	{
		return new ActiveXObject("Microsoft.XMLHTTP"); // ie
	} 
}

function GetById(name)
{
     if(document.all)
     {
         return document.all[name];
     }
     else
     {
         return document.getElementById(name);
     }
}

function SetSpanInnerHTMLText(name, text)
{
    GetById(name).innerHTML = text;
}

function SiteToShopRedirect()
{
   var searchTerm = GetById("SearchBox");
   if(searchTerm.value.length < 1)
   {
      alert("Please enter a search term.");
   }else{
      window.location.href = "http://www.thebookshoponline.com/Shop/Default.aspx?SearchTerm="+searchTerm.value;
   }
}

function ClearSearchBox()
{
    var searchBox = GetById("SearchBox");
    
    if(searchBox.value == "search")
    { 
       searchBox.value = ""; 
    }
}

// setSearchBox added by A.Buchan on 06/05/2009
// purpose: set search box to "search" if field is empty

function setSearchBox()
{
    var searchBox = GetById("SearchBox");
    if(searchBox.value == "")
    {
        searchBox.value = "search";
    }
}

// Name: setChosenCarriage
// Desc: Sets focus to the chosen carriage option after selection
function setChosenCarriage(elem)
{
/*
  var royalAirMailPos = chosenCarriage.indexOf('Royal Air Mail ');
  if(royalAirMailPos >= 0)
  {
        var selectEl = document.getElementById(elem);
        selectEl.selectedIndex = selectEl.selectedIndex + 1;
  }
  
  var royalMailPos = chosenCarriage.indexOf('Royal Mail ');
  if(royalMailPos >= 0)
  {
        var selectEl = document.getElementById(elem);
        // Check if length is 3
        if(selectEl.length == 3) // Root, DHL and ROYAL MAIL
        {
            // If so: set selectedIndex to 2
            selectEl.selectedIndex = selectEl.selectedIndex + 2;
        }
        if(selectEl.length == 2) // ROOT AND ROYAL MAIL
        {
            // If so: set selectedIndex to 1
        }
  }
  */
}

function IsInt(sText)
{
   if(sText.length == 0)
   {
       return false;
   }
   else
   {
       var validChars = "0123456789";
       var answer = true;
       var c;

       for (i = 0; i < sText.length && answer == true; i++) 
       { 
          c = sText.charAt(i); 
          
          if (validChars.indexOf(c) == -1) 
          {
             answer = false;
          }
       }
   }
   
   return answer;
}

// Added By A.Buchan on 15/05/2009 for New Releases page
function ShowNewReleasesProductPageByCategory(releaseMode, categoryCode, page, pageSize, sort)
{
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Please wait while the New Releases are displayed.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?Page=" + page + 
                        "&NewReleases=" + releaseMode + 
                        "&CategoryTop=" + categoryCode +
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

// Added By A.Buchan on 15/05/2009 for New Releases Page
function ShowNewReleasesProductListWithRecordsByCategory(releaseMode, ctrl, categoryCode, page, pageSize, sort)
{
    var listControl = ctrl;
    pageSize = listControl.options[listControl.selectedIndex].value;
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Please wait while the New Releases are displayed.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?Page=" + page + 
                        "&NewReleases=" + releaseMode + 
                        "&CategoryTop=" + categoryCode +
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

// Added By A.Buchan on 15/05/2009 for New Releases
function ShowNewReleasesProductListByCategory(releaseMode, ctrl, categoryCode, page, pageSize, sort)
{
    var listControl = ctrl;
    sort = listControl.options[listControl.selectedIndex].value;
  
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Please wait while the New Releases are displayed.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?Page=" + page + 
                        "&NewReleases=" + releaseMode + 
                        "&CategoryTop=" + categoryCode +
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

// Added By A.Buchan on 18/05/2009 for ComingSoon page
function ShowComingSoonProductPageByCategory(comingSoonMode, categoryCode, page, pageSize, sort)
{
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Please wait while the Coming Soon products are displayed.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?Page=" + page + 
                        "&ComingSoon=" + comingSoonMode + 
                        "&CategoryTop=" + categoryCode +
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

// Added By A.Buchan on 18/05/2009 for Coming Soon page
function ShowComingSoonProductListWithRecordsByCategory(comingSoonMode, ctrl, categoryCode, page, pageSize, sort)
{
    var listControl = ctrl;
    pageSize = listControl.options[listControl.selectedIndex].value;
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Please wait while the Coming Soon products are displayed.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?Page=" + page + 
                        "&ComingSoon=" + comingSoonMode + 
                        "&CategoryTop=" + categoryCode +
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

// Added By A.Buchan on 18/05/2009 for Coming Soon page
function ShowComingSoonProductListByCategory(comingSoonMode, ctrl, categoryCode, page, pageSize, sort)
{
    var listControl = ctrl;
    sort = listControl.options[listControl.selectedIndex].value;
  
    if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
    {
        SetSpanInnerHTMLText("content", "<i>Please wait while the Coming Soon products are displayed.</i>");
           
        HTTP_GET.open("GET", "http://www.thebookshoponline.com/Products/ProductServer.aspx?Page=" + page + 
                        "&ComingSoon=" + comingSoonMode + 
                        "&CategoryTop=" + categoryCode +
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

// Change by A.Buchan
        window.onerror = displayErrorMessage;

        function displayErrorMessage(msg, url, linenumber)
        {
            //alert("Error: " + msg + " Line: " + linenumber);
            //return true;
        }
// End Change

// Change by A.Buchan
// Date: 18/06/2009
// Purpose: Clear Basket link

function clearBasket()
{

}

// Added by E.Gordon
// Date: 2/7/2009
// Purpose: To allow users to add and deduct values to be added on product page and list

    function adder(txtid, txtid2) {
        var quantity = parseInt(txtid.value);
        var secondQuantity = 0;
        quantity = quantity + 1;
        txtid.value = quantity;
        if (txtid2 != undefined)
        {
            secondQuantity = parseInt(txtid2.value);
            secondQuantity = quantity;
            txtid2.value = secondQuantity;
        }
    }
    
    function deducter(txtid, txtid2) {
        if (parseInt(txtid.value) > 1) {
            txtid.value = (parseInt(txtid.value) - 1);
        }
        if (txtid2 != undefined)
        {
            txtid2.value = txtid.value;
        }
    }
    
    function dosubmit(spanid) {
        document.getElementById(spanid).innerHTML = "<a href='#' onclick='javascript: ReviewBasket();'>Added to Basket</a>";
    }
    
// Added by E.Gordon
// 7/7/2009    
// New Function to add items into the basket. 
// Purpose: To all confirmations to be displayed (only if basket quantity to add is valid)

function AddToBasketOne(productId, textboxId, confirmBoxId, confirmBoxId2)
{
    var addBox = null;
    
    if(textboxId == null)
    {
        addBox = GetById("BasketQty" + productId);
    }
    else 
    {
        addBox = GetById(textboxId);
        //alert(textboxId + " Exists");
        //alert(addBox.value);
    }
    

    if(IsInt(addBox.value)&&(addBox.value > 0))
    {
        SetSpanInnerHTMLText("BasketDisplay", 
             '<p>Updating...<img src="../images/basket.jpg" width="29" height="21" alt="basket"/><br /><input name="search" type="text" value="search" size="20" maxlength="100" /><input name="go" type="submit" value="GO" class="submitgo" /></p>');
              
       if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
       {
            HTTP_GET.open("GET", MakeUrlUnique("http://www.thebookshoponline.com/Products/BasketServer.aspx?AddProductId=" + 
                  productId + "&AddProductQty=" + addBox.value), true);
            HTTP_GET.onreadystatechange = ShowBasket_Callback;
            HTTP_GET.send(null);
            dosubmit(confirmBoxId);
            if (confirmBoxId2 != undefined)
            {
                    dosubmit(confirmBoxId2);
            }
       }
    }
    else
    {
        alert("Basket Quantity is invalid.");
    }
}

function BasketSearch()
{
    var searchTerm = GetById("SearchBox").value;
    window.location = ("http://www.thebookshoponline.com/shop/search.aspx?keywords=" + fixedEncodeURIComponent(searchTerm) + "&page=1&sort=0&display=10");
}


//Returns a string which can be a parameter of a URL
function fixedEncodeURIComponent (str) {   
  return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');   
}  