


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

var HTTP_GET = GetXmlHttpRequest();

var sessionId = "l1op5myxr02l3oycurxkelzz";
var guid = "dfaf7bbe-0d9d-4b49-9561-07b2dc14ae5e";

var currentRequest = 0;

var clickedMenuCode;
var clickedMenuLevel;

var selectSubMenu = "";
var randomSubMenu = "";

var chosenCarriage = "";
var chosenCarriageElemId = "";


// Create suitable xmlDoc object

// var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlDoc = "blah";

try //Internet Explorer
{
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
	try //Firefox, Mozilla, Opera, etc.
	{
		xmlDoc=document.implementation.createDocument("","",null);
	}
	catch(e)
	{
		alert(e.message);
	}
}


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.thomasgunn.com/onlineshop/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.thomasgunn.com/onlineshop/Products/BasketServer.aspx?Review=true&SetCarriage=" + carriage), true);
        }
        else
        {
           HTTP_GET.open("GET", 
                 MakeUrlUnique("http://www.thomasgunn.com/onlineshop/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.thomasgunn.com/onlineshop/Products/BasketServer.aspx?SetCountry=" + country), true);
            HTTP_GET.onreadystatechange = SetCountry_Callback;
            HTTP_GET.send(null);
            //setTimeout("document.getElementById('shippingList').selectedIndex = 0", 500);
            //setTimeout("document.getElementById('shoppingBasketProceed').style.visibility = 'hidden'", 500);
            
       }
   }
}

function RefreshShippingOptions()
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thomasgunn.com/onlineshop/Products/ShippingOptionsServer.aspx"), true);
        HTTP_GET.onreadystatechange = RefreshShippingOptions_Callback;
        HTTP_GET.send(null);
        
        //Change
        
        HTTP_GET.open("GET", 
        MakeUrlUnique("http://www.thomasgunn.com/onlineshop/Products/BasketServer.aspx?Review=true&RemoveCarriage=true"), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
        //setTimeout("setChosenCarriage(chosenCarriageElemId)", 500);
        //setTimeout("document.getElementById('shippingList').selectedIndex = 0", 100);
        //setTimeout("alert('test')", 100);
   }
}

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) 
   {
        // Change: commented out the below line because calling this function from the Default page fails so specify full URL
        // HTTP_GET.open("GET", MakeUrlUnique("../Products/BasketServer.aspx?Review=true"), true);
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thomasgunn.com/onlineshop/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="../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.thomasgunn.com/onlineshop/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.thomasgunn.com/onlineshop/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="../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.thomasgunn.com/onlineshop/Products/BasketServer.aspx?AddProductId=" + 
                  productId + "&AddProductQty=" + addBox.value), true);
            HTTP_GET.onreadystatechange = ShowBasket_Callback;
            HTTP_GET.send(null);
       }
    }
    else
    {
        alert("Basket Quantity 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.thomasgunn.com/onlineshop/Products/BasketServer.aspx?RemoveProductId=" + 
                  productId + "&RemoveProductQty=" + addBox.value), true);
            HTTP_GET.onreadystatechange = ShowBasket_Callback;
            HTTP_GET.send(null);
       }
    }
    else
    {
        alert("Basket Quantity is invalid.");
    }
}

function ShowBasket()
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        HTTP_GET.open("GET", "http://www.thomasgunn.com/onlineshop/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.thomasgunn.com/onlineshop/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.thomasgunn.com/onlineshop" || window.location.href == "http://www.thomasgunn.com/onlineshop/" || window.location.href == "http://www.thomasgunn.com/onlineshop/Default.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Contact.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Delivery.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/FurtherLinks.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Help.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/InternationalContacts.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Links.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Sitemap.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails2.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/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.thomasgunn.com/onlineshop/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.thomasgunn.com/onlineshop" || window.location.href == "http://www.thomasgunn.com/onlineshop/" || window.location.href == "http://www.thomasgunn.com/onlineshop/Default.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Contact.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Delivery.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/FurtherLinks.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Help.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/InternationalContacts.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Links.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Sitemap.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails2.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Terms.aspx")
        {
            document.getElementById('content').style.marginLeft = '190';
        }
	window.scroll(0, 0);
   }
}

function ShowSearchProduct(productId, searchTerm)
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        SetSpanInnerHTMLText("content", "<i>Please wait while the product you chose is displayed...</i>");
           
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thomasgunn.com/onlineshop/Products/ProductServer.aspx?ProductId=" + productId + "&SearchTerm=" + searchTerm), 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.thomasgunn.com/onlineshop" || window.location.href == "http://www.thomasgunn.com/onlineshop/" || window.location.href == "http://www.thomasgunn.com/onlineshop/Default.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Contact.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Delivery.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/FurtherLinks.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Help.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/InternationalContacts.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Links.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Sitemap.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails2.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Terms.aspx")
        {
            document.getElementById('content').style.marginLeft = '190';
        }
   }
}

function ShowBasketProduct(productId)
{
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        SetSpanInnerHTMLText("content", "<i>Updating....</i>");
           
        HTTP_GET.open("GET", MakeUrlUnique("http://www.thomasgunn.com/onlineshop/Products/ProductServer.aspx?ProductId=" + 
                          productId), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}


function ShowSearchProductPage(searchTerm, page, pageSize, sort)
{  
   if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
   {
        SetSpanInnerHTMLText("content", "<i>Please wait while your search results are displayed...</i>");
           
        HTTP_GET.open("GET", "http://www.thomasgunn.com/onlineshop/Products/ProductServer.aspx?Page=" + page + 
                        "&SearchTerm=" + searchTerm + 
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}

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.thomasgunn.com/onlineshop/Products/ProductServer.aspx?Page=" + page + 
                        "&ListCategoryCode=" + code + 
                        "&PageSize=" + pageSize +
                        "&Sort=" + sort, true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}

function ShowSearchedProductListWithRecords(searchTerm, 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.thomasgunn.com/onlineshop/Products/ProductServer.aspx?SearchTerm=" + searchTerm + 
                        "&PageSize=" + pageSize + "&Sort=" + sort, true);                                                        
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}


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.thomasgunn.com/onlineshop/Products/ProductServer.aspx?ListCategoryCode=" + code + 
                        "&PageSize=" + pageSize + "&Sort=" + sort, true);                                                        
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

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.thomasgunn.com/onlineshop/Products/ProductServer.aspx?ListCategoryCode=" + code + 
                        "&Sort=" + sort + "&PageSize=" + pageSize, true);                                                        
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

function ShowSearchedProductListSorted(searchTerm, 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.thomasgunn.com/onlineshop/Products/ProductServer.aspx?SearchTerm=" + searchTerm + 
                        "&Sort=" + sort + "&PageSize=" + pageSize, true);                                                        
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
    }
}

function ShowProductList(code)
{
    // Check if code == BK00 - if so redirect to the book shop online
    if(code == "BK00")
    {
        window.location.href = "http://www.thebookshoponline.com";
    }
    else
    {
        var MENUITEM = GetById("LHSMenu" + code);

        var menuOptions = GetMenuOptions(xmlDoc.documentElement);
        var pName = GetChildNodeText(xmlDoc.documentElement, "parent");
        var PARENT = GetById(pName);
    
        if ((PARENT != null) && (MENUITEM != null) && (PARENT.id != null) && (MENUITEM.id != null))
        {
            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.thomasgunn.com/onlineshop/Products/ProductServer.aspx?ListCategoryCode=" + code, true);
            HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
            HTTP_GET.send(null);
        }
        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();
         }
    }
}

// XML OBject declaration was here, 23/04/2009

function ShowMenu(code, level)
{
    if(xmlDoc.documentElement != undefined)
    {
        var MENUITEM = GetById("LHSMenu" + code);
        var menuOptions = GetMenuOptions(xmlDoc.documentElement);
        var pName = GetChildNodeText(xmlDoc.documentElement, "parent");
        var PARENT = GetById(pName);
     //   alert(PARENT.id.toString() + "  " + MENUITEM.id.toString() );    
        
        //PARENT.id 
        //if coming from another page == "sidemenu"
        //if on a page == "LHS" + CurrentMenuID (ex. EQ01)
        
        //if (parent menu and menu item chosen are different remove the other submenu items)#
        //Change by EG 15/7/2009 
        //PARENT.id > PARENT.id.toString()
        
        if ((PARENT != null) && (MENUITEM != null) && (PARENT.id != null) && (MENUITEM.id != null))
        {
            if ((PARENT.id.toString() != "sidemenu")  && (PARENT.id != MENUITEM.id))
            {
                RemovedSubMenus(PARENT);
            }
        }
                 
        if(!RemovedSubMenus(MENUITEM))
        {
           if (xmlDoc.readyState == 4)
           {
               xmlDoc.async = "false";
               xmlDoc.onreadystatechange = ShowMenu_Callback;
               xmlDoc.load("../Menus/LHSMenuServer.aspx?CategoryCode=" + 
                                  code + "&CategoryLevel=" + level);
           }
        }
    }
    else
    {
        if (xmlDoc.readyState == 4)
        {
           xmlDoc.async = "false";
           xmlDoc.onreadystatechange = ShowMenu_Callback;
           xmlDoc.load("../Menus/LHSMenuServer.aspx?CategoryCode=" + 
                              code + "&CategoryLevel=" + level);
        }
    }
}

function RemovedSubMenus(PARENT)
{
    var answer = false;
    
    if(PARENT != null)
    {
        for(var i = 0; i < PARENT.childNodes.length; i++)
        {
             if(PARENT.childNodes(i).tagName == "UL" || PARENT.childNodes(i).tagName == "ul")
             {
                  PARENT.removeChild(PARENT.childNodes(i));
                  answer = true;
             }
        }
    }
    return answer;
}

function ShowTopLevelMenu(code)
{
   if (xmlDoc.readyState == 4)
   {       
        xmlDoc.async = "false";
        xmlDoc.onreadystatechange = ShowMenu_Callback;
        xmlDoc.load("../Menus/LHSMenuServer.aspx?TopLevelCategory=" + 
                          code);
   }
}


var randomHttpRequest = GetXmlHttpRequest();

function ShowRandomProducts(code)
{
   if (randomHttpRequest.readyState == 4 || randomHttpRequest.readyState == 0) 
   {
        SetSpanInnerHTMLText("content", "<i>Updating....</i>");
           
        randomHttpRequest.open("GET", 
              "../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.thomasgunn.com/onlineshop/Products/ProductServer.aspx?RandomTopCategory=" + 
                          code), true);
        HTTP_GET.onreadystatechange = ShowMainDisplay_Callback;
        HTTP_GET.send(null);
   }
}


function ShowMenu_Callback()
{
      if (xmlDoc.readyState == 4)
      {
          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);


          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);
          }
          if (PARENT != null)
          {          
              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.thomasgunn.com/onlineshop/Shop/Default.aspx?SearchTerm="+searchTerm.value;
   }
}

var searchHttpRequest = GetXmlHttpRequest();

function SearchProducts(searchTerm)
{
   searchHttpRequest = GetXmlHttpRequest();

   var searchTerm = GetById("SearchBox");

   if(searchTerm.value.length < 1)
   {
      alert("Please enter a search term.");
   }
   else
   {

     if (HTTP_GET.readyState == 4 || HTTP_GET.readyState == 0) 
     {
	// this is what runs when the user performs a search
         SetSpanInnerHTMLText("content", "<i>Please wait while your search results for <span style=\"color: green; font-weight: bold;\">" + searchTerm.value + "</span> are displayed...</i>");
           
         searchHttpRequest.open("GET", "http://www.thomasgunn.com/onlineshop/Products/ProductServer.aspx?SearchTerm=" + 
                          searchTerm.value, true);
         searchHttpRequest.onreadystatechange = SearchProducts_Callback;
         searchHttpRequest.send(null);
        // Check if user is on static page. If so set marginLeft to 142
        if(window.location.href == "http://www.thomasgunn.com/onlineshop" || window.location.href == "http://www.thomasgunn.com/onlineshop/" || window.location.href == "http://www.thomasgunn.com/onlineshop/Default.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Contact.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Delivery.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/FurtherLinks.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Help.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/InternationalContacts.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Links.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Sitemap.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/StaffDetails2.aspx" || window.location.href == "http://www.thomasgunn.com/onlineshop/Site/Terms.aspx")
        {
            document.getElementById('content').style.marginLeft = '190';
        }
     }
   }
}
function SearchProducts_Callback()
{
    if (searchHttpRequest.readyState == 4) 
    {
         SetSpanInnerHTMLText("content", searchHttpRequest.responseText);
    }
}

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";
    }
}

function setChosenCarriage(elem)
{
  var dhlPos = chosenCarriage.indexOf('DHL ');
  if(dhlPos >= 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 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) {
        txtid.value = parseInt(txtid.value) + 1;
        if (txtid2 != undefined)
        {
            txtid2.value = txtid.value;
        }
    }
    
    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) {
        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);
    }
    

    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.thomasgunn.com/onlineshop/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.");
    }
}

