﻿// JScript File
// handles adding new items to a trolley

var MAX_VALUE = 999;
var MIN_VALUE = 0;

//// ADD TO TROLLEY ////
// Add an item to the trolley
function NAd(controlIDRoot, productId, variantId, basketName, updateGreen, updateRed, isDeleted, inTrolleyID, rowID, highlightMode)
{
    var quantityElement = document.getElementById(controlIDRoot+'_v');
    AdOrSub(+quantityElement.value+1, controlIDRoot, productId, variantId, basketName, updateGreen == '1' ? 'True' : 'False', updateRed == 1 ? 'True' : 'False', isDeleted == 1 ? 'True' : 'False', inTrolleyID, rowID, highlightMode);
}

// Add an item to the trolley
function NSu(controlIDRoot, productId, variantId, basketName, updateGreen, updateRed, isDeleted , inTrolleyID, rowID, highlightMode)
{
    var quantityElement = document.getElementById(controlIDRoot+'_v');
    AdOrSub(+quantityElement.value-1, controlIDRoot, productId, variantId, basketName, updateGreen == '1' ? 'True' : 'False', updateRed == 1 ? 'True' : 'False', isDeleted == 1 ? 'True' : 'False', inTrolleyID, rowID, highlightMode);
}

// Add an item to the trolley
function NTo2(controlIDRoot, productId, variantId, basketName, updateGreen, updateRed, isDeleted, inTrolleyID, rowID, highlightMode)
{
    var quantityElement = document.getElementById(controlIDRoot+'_v');
    AdOrSub(+quantityElement.value, controlIDRoot, productId, variantId, basketName, updateGreen == '1' ? 'True' : 'False', updateRed == 1 ? 'True' : 'False', isDeleted == 1 ? 'True' : 'False', inTrolleyID, rowID, highlightMode);
}

//// PLUS/MINUS CONTROL - NOT TROLLEY MODS ////
// Add an item to the trolley
function CAd(controlIDRoot, productId, variantId, basketName, updateGreen, updateRed, isDeleted, inTrolleyID, rowID, highlightMode)
{
    var quantityElement = document.getElementById(controlIDRoot+'_v');
    UpdateRow(+quantityElement.value+1, controlIDRoot, updateGreen == '1' ? 'True' : 'False', updateRed == 1 ? 'True' : 'False', inTrolleyID, rowID, highlightMode);
    gPageNotSaved = true;
}

// Add an item to the trolley
function CSu(controlIDRoot, productId, variantId, basketName, updateGreen, updateRed, isDeleted , inTrolleyID, rowID, highlightMode)
{
    var quantityElement = document.getElementById(controlIDRoot+'_v');
    UpdateRow(+quantityElement.value-1, controlIDRoot, updateGreen == '1' ? 'True' : 'False', updateRed == 1 ? 'True' : 'False', inTrolleyID, rowID, highlightMode);
    gPageNotSaved = true;
}

// Add an item to the trolley
function CTo2(controlIDRoot, productId, variantId, basketName, updateGreen, updateRed, isDeleted, inTrolleyID, rowID, highlightMode)
{
    var quantityElement = document.getElementById(controlIDRoot+'_v');
    UpdateRow(+quantityElement.value, controlIDRoot, updateGreen == '1' ? 'True' : 'False', updateRed == 1 ? 'True' : 'False', inTrolleyID, rowID, highlightMode);
    gPageNotSaved = true;
}


function AdOrSub(newValue, controlIDRoot, productId, variantId, basketName, updateGreen, updateRed, isDeleted, inTrolleyID, rowID, highlightMode)
{
  
    // Update the row
    UpdateRow(newValue, controlIDRoot, updateGreen, updateRed, inTrolleyID, rowID, highlightMode)
  
    // Find the discount URL
    var discountUrl = null;
    var discountURLControl = document.getElementById(controlIDRoot + '_DiscountOrPromoURL');
    if (discountURLControl)
        discountUrl = discountURLControl.value;
    
    // Hidden field is the textbox control    
    touchItemInArray(productId, variantId, isDeleted, basketName, controlIDRoot+'_v', discountUrl, controlIDRoot+'_o')    
}

function UpdateRow(newValue, controlIDRoot, updateGreen, updateRed, inTrolleyID, rowID, highlightMode)
{
    // controlIDRoot + 'v' = control value element
    // controlIDRoot + 'd' = control discount element
    // controlIDRoot + 'o' = control origional value element

    var noHighlight = '';
    var redHighlight = 'productHighlightRed';
    var greenHighlight = 'productHighlightGreen';
    
    // Set the highlight class
    if (highlightMode == 1)
    {
        noHighlight = 'pr';
        redHighlight = 'prr';
        greenHighlight = 'prg';
    }

    var quantityElement = document.getElementById(controlIDRoot+'_v');
    
    if (+newValue <= MAX_VALUE && +newValue >= MIN_VALUE)
        quantityElement.value = newValue;
       
    
    if (quantityElement.value > 0)
    {
        // Show the trolley ctl
       
        var inTrolleyElement = document.getElementById(inTrolleyID);
        if (inTrolleyElement != null)
            inTrolleyElement.className = 'itiS';
            
        if (updateGreen == "True")
        {
            var rowElement = document.getElementById(rowID);
            if (rowElement)
                rowElement.className = greenHighlight;
        }
        else
        {
            // set to blank
            var rowElement = document.getElementById(rowID);
            if (rowElement)
                rowElement.className = noHighlight;
        }
    }
    else
    {
        // Hide the trolley ctl
        
        var inTrolleyElement = document.getElementById(inTrolleyID);
        if (inTrolleyElement)
            inTrolleyElement.className = 'itiH';
          
        if (updateGreen == "True")
        {
            var rowElement = document.getElementById(rowID);
            if (rowElement)
                rowElement.className = noHighlight;
        }
           
        if (updateRed == "True")
        {
            var rowElement = document.getElementById(rowID);
            if (rowElement)
                rowElement.className = redHighlight;
        } 
    }
}

// handle key presses too
function NTo(evt)
{
    var key;
    if (evt && evt.which) 
    {
        key = evt.which; 
    }
    else 
        if (window.event && window.event.keyCode) 
        { 
            key = window.event.keyCode;
        }
        
    if (!key)return false;
    
    if (key == 8 || key == 46 || key == 37 || key == 39)
        return true;
        
    if (key >= 48 && key <= 57)
        return true;
        
    if (key >= 96 && key <= 105)
        return true;
        
    return false;
}

// User has tried to add an item but they have no trolley yet...
function NCs()
{
    var oldurl = window.location.href;
    var newurl = "/account/loginregister/registrationrequired.aspx?PreviousAction=AddToTrolley&ReturnUrl=" + encodeURIComponent(oldurl);
    
    window.location.href = newurl;
    
}

// Add To ShoppingList
function ATSLF(quantity, productID, productVariantID)
{
    var url = '/account/shoppinglists/addtolist.aspx?PID={0}&PVID={1}&Quantity={2}&ReturnUrl={3}';
    url = url.replace('{0}', productID);
    url = url.replace('{1}', productVariantID);
    url = url.replace('{2}', quantity);
    
    var oldurl = window.location.href;
    url = url.replace('{3}', encodeURIComponent(oldurl));
    
    window.location = url;
}

function ATSL(quantityFieldID, productID, productVariantID)
{
    var quantityFieldIDElement = document.getElementById(quantityFieldID);
    var url = '/account/shoppinglists/addtolist.aspx?PID={0}&PVID={1}&Quantity={2}&ReturnUrl={3}';
    url = url.replace('{0}', productID);
    url = url.replace('{1}', productVariantID);
    url = url.replace('{2}', quantityFieldIDElement.value);
    
    var oldurl = window.location.href;
    url = url.replace('{3}', encodeURIComponent(oldurl));
    
    window.location = url;
}

// Add Note
function AN(rowid, pID, pvID)
{
    var oldurl = window.location.href;
    retURL = encodeURIComponent(oldurl);

    var tr = document.getElementById(rowid);
    if (tr)
    {
        // Check if the row is highlighted (to ensure it has been added to the trolley)
        if (tr.className == "productHighlight" || tr.className == "productHighlightGreen" || tr.className == "prg")
        {
            var redirectURL = "/catalog/productinstructions.aspx?PID=" + pID + "&PVID=" + pvID + "&ReturnUrl=" + retURL;
            window.location.href = redirectURL;
            return;
        }
    }   
    alert ("Please add this product to your trolley before adding a note. ");
}

