﻿
var PreSelectProductCode = "";
var CurrentProduct = 0;
var AjaxQueue = new Array;
var ThumbHeight = 85;

function AddToAjaxQueue(URL, ElementID, HTMLOnLoad, HTMLOnLoadDiv)
{
    var QSChar = "";
    
    if (URL.indexOf("?") == -1)
    {
        QSChar = "?";
    }
    else
    {
        QSChar = "&";
    }
    
    //Attempt to get round caching.
    URL += QSChar + "CD=" + new Date().getTime();
    
    AjaxQueue.push(new Array(URL, ElementID, 0, HTMLOnLoad, HTMLOnLoadDiv));
    if (AjaxQueue.length == 1)
    {
        AjaxDispatcher();
    }
}

function AjaxDispatcher()
{
    if (AjaxQueue.length > 0)
    {
        if (AjaxQueue[0][2] == 0)
        {
            AjaxQueue[0][2] = 1;
            if (window.XMLHttpRequest)
            {
                xmlhttp=new XMLHttpRequest();
                xmlhttp.onreadystatechange=postFileReady;
                xmlhttp.open("GET", AjaxQueue[0][0], true);
                xmlhttp.send(null);
            }
            else if (window.ActiveXObject)
            {
                xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
                if (xmlhttp)
                {
                    xmlhttp.onreadystatechange=postFileReady;
                    xmlhttp.open('GET', AjaxQueue[0][0], false);
                    xmlhttp.send();
                }
            }
        
        }    
    }
}

// function to handle asynchronous call
function postFileReady()
{
    if (xmlhttp.readyState==4)
    {
        if (xmlhttp.status==200)
        {
            document.getElementById(AjaxQueue[0][1]).innerHTML = xmlhttp.responseText;
            Finalise(AjaxQueue[0]);
            AjaxQueue.shift();
            if (AjaxQueue.length > 0)
            {
                AjaxDispatcher();
            }
        }
        else if (xmlhttp.status==500)
        {
            //alert(xmlhttp.responseText);
            document.getElementById(AjaxQueue[0][1]).innerHTML = "Server error";
            AjaxQueue.shift();
            if (AjaxQueue.length > 0)
            {
                AjaxDispatcher();
            }
        }
    }
}


function Finalise(CompletedElement)
{
    if (CompletedElement[4] != null)
    {
        document.getElementById(CompletedElement[4]).innerHTML = CompletedElement[3];
        document.getElementById(CompletedElement[4]).style.visibility = "visible";
    }
    window.scrollTo(0, 0);
}

//////////////////////////////
//////////////////////////////

function loadProductThumbnails(SeasonCode, GroupCode, PageIndex, OrderBy, ProductCode)
{
    document.getElementById("col_3").innerHTML = "<span class=\"AjaxLoadingMsg\">Loading</span>";
    AddToAjaxQueue('/controls/Ajax/Category/ProductThumbs.aspx?OB=' + OrderBy + '&SC=' + SeasonCode + '&GC=' + GroupCode + '&PI=' + PageIndex, 'col_3', null, null);
    PreSelectProductCode = ProductCode;
    highlightProduct(99999);
}

function loadProductBrowser(SeasonCode, TopLevelCategory, GroupCode, PageIndex, OrderBy)
{
    AddToAjaxQueue('/controls/Ajax/Category/ProductBrowser.aspx?OB=' + OrderBy + '&SC=' + SeasonCode + '&TLC=' + TopLevelCategory + '&GC=' + GroupCode + '&PI=' + PageIndex, 'browser_content', null, null);
}

function loadSearchThumbnails(SeasonCode, PageIndex, Keywords)
{
    document.getElementById("col_3").innerHTML = "<span class=\"AjaxLoadingMsg\">Loading</span>";
    AddToAjaxQueue('/controls/Ajax/Category/ProductThumbs.aspx?SC=' + SeasonCode + '&PI=' + PageIndex + '&K=' + Keywords, 'col_3', null, null);
    highlightProduct(99999);
}

function loadProductDescription(SeasonCode, ProductCode)
{
    AddToAjaxQueue('/controls/Ajax/Products/FullDescription.aspx?SC=' + SeasonCode + '&PC=' + ProductCode, 'col_2', null, null); 
}

function loadProductMainView(ProductCode)
{
    AddToAjaxQueue('/controls/Ajax/Products/MainView.aspx?PC=' + ProductCode, 'col_1', null, null);
}

function loadBasketQuickView(SeasonCode, GroupCode, ProductCode, ProductCodeQuantity)
{
    if ((ProductCode != '') && (ProductCodeQuantity != ''))
    {
        AddToAjaxQueue('/controls/Ajax/Basket/QuickView.aspx?SC=' + SeasonCode + '&GC=' + GroupCode + '&PC=' + ProductCode + '&Q=' + ProductCodeQuantity, 'viewbasket', 'Added &lt;&lt;', 'AddToBasketMsg_' + ProductCode);
    }
    else
    {
        AddToAjaxQueue('/controls/Ajax/Basket/QuickView.aspx?SC=' + SeasonCode + '&GC=' + GroupCode + '&PC=' + ProductCode + '&Q=' + ProductCodeQuantity, 'viewbasket', null, null);
    }
}

function loadBasketDetailedView(SeasonCode, GroupCode, Action, ItemList, ItemListQuantities, ProductCode, ProductCodeQuantity)
{
    var OrderNotes = "";
    try
    {
        OrderNotes = document.getElementById("OrderNotes").innerText;
    }
    catch(Exception){}
    
    AddToAjaxQueue('/controls/Ajax/Basket/DetailedView.aspx?SC=' + SeasonCode + '&GC=' + GroupCode + '&A=' + Action + '&IL=' + ItemList + '&ILQ=' + ItemListQuantities + '&PC=' + ProductCode + '&PCQ=' + ProductCodeQuantity + '&ON=' + OrderNotes, 'Basket_Content', null, null);
}

function highlightProduct(keyCode)
{
    if (document.getElementById("col_3"))
    {
        if ((document.getElementById("col_3").innerHTML.toLowerCase().replace(/\"/g, "") == "<span class=ajaxloadingmsg>loading</span>") || (document.getElementById("col_3").innerHTML == ""))
        {
            window.setTimeout("highlightProduct(99999)", 500);
        }
        else
        {
            var NextProduct = CurrentProduct;
            var ProductList = document.getElementById("CompleteList");
            var ProductArray = ProductList.value.substring(0, ProductList.value.length - 1).split(",");
            
            var PreviousCategoryLink = document.getElementById("PreviousCategoryLink");
            var NextCategoryLink = document.getElementById("NextCategoryLink");
            
            switch (keyCode)
            {
                //Left Key
                case 37:
                    if (PreviousCategoryLink != null)
                    {
                        PreviousCategoryLink.click();
                    }
                    break;
                
                //Right Key
                case 39:
                    if (NextCategoryLink != null)
                    {
                        NextCategoryLink.click();
                    }                
                    break;
            
                //Up key
                case 38:
                    NextProduct--;
                    if (NextProduct < 0)
                    {
                        NextProduct++;
                    }
                    break;
                    
                //Down key
                case 40:
                    NextProduct++;
                    if (NextProduct >= ProductArray.length)
                    {
                        NextProduct = (ProductArray.length - 1);
                    }
                    break;
                default:
                    if (PreSelectProductCode != "")
                    {
                        for (i = 0; i < ProductArray.length; i++)
                        {
                            if (ProductArray[i] == PreSelectProductCode)
                            {
                                NextProduct = i;
                            }
                        }
                        PreSelectProductCode = "";
                    }
                    break;
            }
            
            if ((NextProduct != CurrentProduct) || (keyCode == 99999))
            {
                CurrentProduct = NextProduct;
                
                if (keyCode != 99999)
                {
                    if ((keyCode == 40) && (CurrentProduct % 2 == 0))
                    {
                        //document.getElementById("ProductScroller").scrollTop = (ThumbHeight * (CurrentProduct / 2)) + ((CurrentProduct / 2) * 9);
                    }
                    else if ((keyCode == 38) && (CurrentProduct % 2 != 0))
                    {
                        //document.getElementById("ProductScroller").scrollTop = (ThumbHeight * ((CurrentProduct - 1) / 2)) + (((CurrentProduct - 1) / 2) * 9);
                    }
                }
                
                for (i = 0; i < ProductArray.length; i++)
                {
                    document.getElementById("gallerythumb_" + ProductArray[i]).className = "gallerythumb";
                }
                
                if (keyCode != 99999)
                {
                    document.getElementById("gallerylink_" + ProductArray[CurrentProduct]).onclick();
                }
                document.getElementById("gallerythumb_" + ProductArray[CurrentProduct]).className = "gallerythumb_selected";
            }
        }
    }
}

function highlightSingleProduct(productCode)
{

    var ProductList = document.getElementById("CompleteList");
    var ProductArray = ProductList.value.substring(0, ProductList.value.length - 1).split(",");
            
    for (i = 0; i < ProductArray.length; i++)
    {
        if (ProductArray[i] == productCode)
        {
            CurrentProduct = i;
        }
        document.getElementById("gallerythumb_" + ProductArray[i]).className = "gallerythumb";
    }
    
    document.getElementById("gallerythumb_" + productCode).className = "gallerythumb_selected";

}