/* basket manip
*/
function adjustQtyItemFromBasket( ev, iqty, catno)
{
   $.ajax({url: "/cb/adjustItemInBasket.ashx",
            cache: false, 
            async: true,
            data: {id:[ catno ], qty:[ iqty ] },
            success: function(result){
                if( result != 0 ) { /* opps - failed */
                    alert( "Sorry, an error occured");
                } 
                else
                {
                    $.ajax({ 
                        url: "/cb/BasketData.ashx",
                        cache: false,
                        success: function(html){ 
                        $("#compBasketData").html(html);                         
                        } 
                    })
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                /* */
            }
          });
         
    var ev = ev || window.event;
    if (ev.stopPropagation)
        ev.stopPropagation();
    else
        ev.cancelBubble = true;

    return false;
}

function addItemToBasket( ev, catno )
{ /* disabled - as confusing!! */
    /* alert( "remove this call please" ); */
    return true;
    $.ajax({url: "/cb/AddItemToBasket.ashx" ,
            cache: false, 
            async: false,
            data: {id:[ catno ]},
            success: function(result){
                if( result != 0 ) { // opps - failed
                    alert( "Sorry, this item can not be added to your basket");
                } 
                else
                {
                    $.ajax({ 
                        url: "/cb/BasketData.ashx",
                        cache: false,
                        success: function(html){ 
                        $("#compBasketData").html(html);                         
                        } 
                    })
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // hmm....
            }
          });
         
    var ev = ev || window.event;
    if (ev.stopPropagation)
        ev.stopPropagation();
    else
        ev.cancelBubble = true;
    return false;
}

function removeItemFromBasket( catno )
{ /* if no jscript this will not work!!! */
    $.ajax({url: "/cb/RemoveItemFromBasket.ashx" ,
            cache: false, 
            async: false,
            data: {id:[ catno ]},
            success: function(result){
                if( result != 0 ) { // opps - failed
                    alert( "Sorry, this item can not be added to your basket");
                } 
                else
                {
                    $.ajax({ 
                        url: "/cb/BasketData.ashx",
                        cache: false,
                        success: function(html){ 
                        $("#compBasketData").html(html); 
                        } 
                    })
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // hmm....
            }
          });
}


function BasketItemSetQty( ev, textbox, catno, img )
{
      $.ajax({url: "/cb/adjustItemInBasket.ashx",
            cache: false, 
            async: true,
            data: {id:[ catno ], qty:[ $( "#" + textbox).val() ] },
            success: function(result){
                if( result != 0 ) { /* opps - failed */
                    alert( "Sorry, an error occured");
                } 
                else
                {
                    $.ajax({ 
                        url: "/cb/BasketData.ashx",
                        cache: false,
                        success: function(html){ 
                        $("#compBasketData").html(html);                         
                        } 
                    });
                    
                    if ( $( "#" + textbox).val() < 0 )
                    {
                        $( "#" + textbox).val(0);
                    }
                    
                    $("#" + img).css("display", "inline");
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                /* */
            }
          });
         
    var ev = ev || window.event;
    if (ev.stopPropagation)
        ev.stopPropagation();
    else
        ev.cancelBubble = true;

    return false;
}

function BasketItemSetQtyXML( ev, textbox, catno, img )
{
      $.ajax({url: "/cb/adjustItemInBasket.ashx",
            cache: false, 
            async: true,
            data: {id:[ catno ], qty:[ $( "#" + textbox).val() ] },
            success: function(result){
                if( result != 0 ) { /* opps - failed */
                    alert( "Sorry, an error occured");
                } 
                else
                {
                    $.ajax({ 
                        url: "/cb/BasketDataXML.ashx",
                        cache: false,
                        data: {id:[ catno ]},
                        success: function(xml){
                            $("#totalPrice-" + catno).html($("itemtotal",xml).text());                         
                            $("#subTotal").html($("total",xml).text());

                            if ($("empty",xml).text() == "1")
                            {
                                $("#placeOrderLink").css("display", "none");
                            }
                            else if ($("#placeOrderLink").css("display") == "none")
                            {
                                $("#placeOrderLink").css("display", "inline");
                            }
                        } 
                    });
                    
                    if ( $( "#" + textbox).val() < 0 )
                    {
                        $( "#" + textbox).val(0);
                    }
                    
                    $("#" + img + "-2").css("display", "inline");
                    $("#" + img + "-1").css("display", "none");
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                /* */
            }
          });
         
    var ev = ev || window.event;
    if (ev.stopPropagation)
        ev.stopPropagation();
    else
        ev.cancelBubble = true;

    return false;
}

function AddToMailMe(ev, button, label, catno) {
    $.ajax({ url: "/cb/AddToMailMe.ashx",
        cache: false,
        async: true,
        data: { id: [catno] },
        success: function(result) { $("#" + button).css("display", "none"); $("#" + label).css("display", "inline"); },
        error: function(XMLHttpRequest, textStatus, errorThrown) { }
    });
    var ev = ev || window.event;
    if (ev.stopPropagation)
        ev.stopPropagation();
    else
        ev.cancelBubble = true;
    return false;
}

// hmmm probably shouldn't be here but enter.js is not included
function BasketItemSetQtyOnEnter(ev, textbox, catno, img)
{ 
    var key; 
    if(ev && ev.which)
        key = ev.which; 
    else
        key = ev.keyCode; 

    if (key != 13)
    {
        if (key != 9 && key != 37 && key != 38 && key != 39 && key != 40) //tab
        {
            $("#" + img).css("display", "none");
        }
        return true;
    }
    else
    {
        return BasketItemSetQty(ev, textbox, catno, img);
    }
}

function BasketItemSetQtyOnEnterXML(ev, textbox, catno, img)
{ 
    var key; 
    if(ev && ev.which)
        key = ev.which; 
    else
        key = ev.keyCode; 
        
    if (key != 13)
    {
        if (key != 9 && key != 37 && key != 38 && key != 39 && key != 40) //tab
        {
            $("#" + img + "-2").css("display", "none");
            $("#" + img + "-1").css("display", "inline");
        }
        return true;
    }
    else
    {
        return BasketItemSetQtyXML(ev, textbox, catno, img);
    }
}

function SetItemQtyInBasket(ev, textbox, catno, img) {
    $.ajax({ url: "/cb/SetItemQtyInBasket.ashx",
        cache: false,
        async: true,
        data: { id: [catno], qty: [$("#" + textbox).val()] },
        success: function(result) {
            if (!result.match(/^\d+:.*$/) || result < 0) {
                alert("Sorry, an error occured");
            }
            else {
                $.ajax({
                    url: "/cb/BasketData.ashx",
                    cache: false,
                    success: function(html) {
                        $("#compBasketData").html(html);
                    }
                });

                results = result.split(":");
                if (results[1] == "showWarning") {
                    // alert("sorry, we can only put " + result + " in your basket");
                    $.ajax({ url: "/cb/SetItemQtyInBasket.ashx", cache: false, async: false,
                        data: { notice: true, catno: catno, qty: results[0] },
                        success: function(result) { $("#BasketHook").html(result); },
                        error: function(XMLHttpRequest, textStatus, errorThrown) { }
                    });
                }

                $("#" + textbox).val(results[0]);
                $("#" + img).css("display", results[0] == 0 ? "none" : "inline");
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            /* */
        }
    });

    var ev = ev || window.event;
    if (ev.stopPropagation)
        ev.stopPropagation();
    else
        ev.cancelBubble = true;

    return false;
}
function SetItemQtyInBasketOnEnter(ev, textbox, catno, img) {
    var key;
    if (ev && ev.which)
        key = ev.which;
    else
        key = ev.keyCode;

    if (key == 13) {
        return SetItemQtyInBasket(ev, textbox, catno, img);
    }
    return true;
}

