﻿
function AddToCart(pid) 
{
    $.ajax({
        url: '/webservices/cart.asmx/AddToCart?pk_pid=' + pid,
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        error: function(){
            document.getElementById("deptThumbwrapper").innerHTML='<strong>There was a error adding the item to the cart.  This error appears for testing purposes.</strong>';
        },
        success: function(xml){
            //UpdateCookieValue('LastProduct', pid);
            window.location.href='/basket.aspx';
        }
    });
}

function AddMultipleToCart()
{
    var elem = document.getElementById('frmMain').elements;

    for(var i = 0; i < elem.length; i++)
    {
        if(elem[i].value>0)
        {
            if(elem[i].name.indexOf('txtRelatedQty')>-1)
            {
                //alert(elem[i].value);
                //alert(elem[elem[i].name.replace(/txtRelatedQty/, "txtProductId")].value);
                
                $.ajax({
                    url: '/webservices/cart.asmx/AddToCartQty?pk_pid=' + elem[elem[i].name.replace(/txtRelatedQty/, "txtProductId")].value + '&qty=' + elem[i].value,
                    type: 'GET',
                    dataType: 'xml',
                    timeout: 1000,
                    error: function(){
                        document.getElementById("deptThumbwrapper").innerHTML='<strong>There was a error adding the item to the cart.  This error appears for testing purposes.</strong>';
                    },
                    success: function(xml){
                        //window.location.href='/basket.aspx';
                    }
                });
            }
        }
    }
    window.location.href='/basket.aspx'; 
}

function AddToCartOptionalRedirect(pid, qty, redirect, isAsync) 
{
    if(isAsync == null) isAsync = true;
    $.ajax({
        url: '/webservices/cart.asmx/AddToCartQty?pk_pid=' + pid + '&qty=' + qty,
        type: 'GET',
        dataType: 'xml',
        async: isAsync,
        timeout: 1000,
        error: function(){
            $("#ajaxError").html('<strong>There was a error adding the item to the cart.  This error appears for testing purposes.</strong>');
        },
        success: function(xml){
            if(redirect != null)
            {
                window.location.href = redirect;
            }
        }
    });
}