﻿/// <reference path="jquery-1.2.6-intellisense.js" />
/// <reference path="json2.js"/>

function addItemToBasketDetails(pid) {
    var inBasket = false;    
    var row = $('#pnlDetails');
    var selQuantity = row.find("*[ID$='tbQuantity']").val();
    var selWeight = row.find("*[ID$='tbQuantityDouble']").val();
    var note = row.find("*[ID$='tbItemNote']").val();
    var substitute = row.find("*[ID$='cbDoNotSubstitute']").attr("checked");

    if (row.find("IMG[ID$='imgBasket']").is(":visible")) {
        inBasket = true;
     }
    else {
        inBasket = false;
    }

    // validation
    if (note == undefined) {
        note = "";
    }
    else {
        // parse with Regex
    }
    if (substitute == undefined) {
        substitute = false;
    }
    
    var quantity;
    if (selQuantity == undefined) {
        quantity = selWeight;
    }
    else if (selWeight == undefined) {
    quantity = selQuantity;
    }
    else {
        quantity = 0.00;
    }

    quantity = parseFloat(quantity);
    if (quantity == 0 && !inBasket) {
        alert('You cannot have a qty of less than 0');
        
    }
    else if (quantity == 0) {
        RemoveItemFromBasket(pid);
    }
    else {

        var params = { 'ProductID': pid, 'Quantity': quantity, 'Note': note, 'DoNotSubstitute': substitute };

        var jsonString = "{json: '" + JSON.stringify(params) + "'}";
        var serviceURL = resolveURL("WebServices/BasketServices.asmx/AddItemToBasket");
        // JQuery AJAX call to Webservice with JSON data format
        $.ajax({
            type: "POST",
            url: serviceURL,
            data: jsonString,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result, eventArgs) {

                var miniBasketData = JSON.parse(result.d);

                if (miniBasketData == undefined) {
                    return;
                }

                UpdateMiniBasket(miniBasketData);

                var imgBtn = $("#addProductContainer *[ID$='imgAddToBasket']");
                var imgPath = imgBtn.attr("src");
                var arrPath = imgPath.split('/');
                arrPath[arrPath.length - 1] = "bttn_large_update.gif";
                imgBtn.removeAttr("style");
                imgBtn.attr("src", arrPath.join('/'));

                $("#addProductContainer A[ID$='lnkInBasket']").css("display", "block");
                $("#addProductContainer IMG[ID$='imgBasket']").css("display", "block");

                //var quantity = $("#addProductContainer *[ID$='tbQuantity']").val();
                $("#addProductContainer *[ID$='lblItemSelectedQuantity']").text(" x " + quantity);

            }
        });
    }
}

function IncrementDetailsWeight(tdId, increment, price, priceLabel, oldPrice, oldPriceLabel) {
    fnWeightPlus(tdId, increment);
    UpdateWeightDetailsPriceLabel(tdId, increment, price, priceLabel, oldPrice, oldPriceLabel)
}
function DecrementDetailsWeight(tdId, increment, price, priceLabel, oldPrice, oldPriceLabel) {
    fnWeightMinus(tdId, increment);
    UpdateWeightDetailsPriceLabel(tdId, increment, price, priceLabel, oldPrice, oldPriceLabel)
}
function IncrementDetailsTotalPrice(tdId, price, priceLabel,oldPrice,oldPriceLabel) {
    fnpl(tdId);
    UpdateDetailsLabel(tdId, price, priceLabel, oldPrice, oldPriceLabel);
}
function DecrementDetailsTotalPrice(tdId, price, priceLabel, oldPrice, oldPriceLabel) {
    fnmi(tdId);
    UpdateDetailsLabel(tdId, price, priceLabel, oldPrice, oldPriceLabel);
}
function UpdateWeightOnChange(tdId, increment, price, priceLabel, oldPrice, oldPriceLabel) {
    fnPopupValidateWeightBlur(tdId);
    UpdateWeightDetailsPriceLabel(tdId, increment, price, priceLabel, oldPrice, oldPriceLabel);
}

function UpdateDetailsLabel(tdId, price, priceLabel, oldPrice, oldPriceLabel) {
    fnValidateQuantityRange(tdId);
    var q = $("#" + tdId).val();
    var total = fnFormatNum(parseFloat(q) * parseFloat(price), 2);
    var oldtotal = fnFormatNum(parseFloat(q) * parseFloat(oldPrice), 2);
    $("#" + priceLabel).text("€" + total);
  //  if (parseFloat(oldPrice) > 0) {
   //     $("#" + oldPriceLabel).text("€" + oldtotal);
   // }
}

function UpdateWeightDetailsPriceLabel(tdId, increment, price, priceLabel, oldPrice, oldPriceLabel) {
    var q = $("#" + tdId).val();
    var total = fnFormatNum(parseFloat(q) * parseFloat(price), 2);
    var oldtotal = fnFormatNum(parseFloat(q) * parseFloat(oldPrice), 2);
    $("#" + priceLabel).text("€" + total);

   // if (parseFloat(oldPrice) > 0) {
    //    $("#" + oldPriceLabel).text("€" + oldtotal);
   // }
    if (q < 1) {
        $("#addProductContainer").find("*[ID$='lblGramEquivalent']").show();
        $("#addProductContainer").find("*[ID$='lblGramEquivalent']").text(q * 1000 + "g");
    }
    else
        $("#addProductContainer").find("*[ID$='lblGramEquivalent']").hide();    
}
