﻿function updateQuantity(viewUrl, basketItemId) {

    var textbox = $('#txtUpdateQuantity' + basketItemId).get(0);

    if (textbox.value == textbox.defaultValue) {
        return;
    }

    if (!isInteger(textbox.value)) {
        alert("Please specify numeric value for quantity");
        textbox.focus();
        return;
    }

    $(":input").attr("disabled", true);
    $(".btnOrderNow").attr("href", "javascript:void");


    updateQuantityPost(viewUrl, basketItemId, textbox.value);
}

function updateQuantityPost(viewUrl, basketItemId, Quantity) {
    
    var data = {
        BasketItemId: basketItemId,
        Quantity: Quantity
    };

    $.post(viewUrl, data, updateQuantityCallBack);
}

function updateQuantityCallBack(message) {
    window.location.reload(true);
    
}

function addItemToCookieBasket(viewUrl, productId,CatalogueId) {

    var newUrl = viewUrl + '?id=' + guid();
    
    var data =
    {
        productId: productId,
        CatalogueId: CatalogueId
    }

    jsonResultData = null;
    
    // Submit with callback
    try {
        $.getJSON(newUrl, data, cookieBasketCallBack);
    }
    catch (err) {
        alert("oops");
    }    
}

cookieBasketCallBack = function (jsonResultData) {

    $(".itemsInBasket").html(jsonResultData.Data + ' ');

    if (jsonResultData.Data > 0) {
        $("nextstepActive").addClass("nextstepActive");
    }

    alert(jsonResultData.Message);
    reloadOnceOnly();//FF added 28 Sept 2011 to reload RecruitmentControl
};

function updateRecruitmentBasketQuantity(viewUrl, productId) {

    var textbox = $('#txtUpdateQuantity' + productId).get(0);

    if (textbox.value == textbox.defaultValue) {
        return;
    }

    if (!isInteger(textbox.value)) {
        alert("Please specify numeric value for quantity");
        textbox.focus();
        return;
    }

    $(":input").attr("disabled", true);
    $(".btnOrderNow").attr("disabled", true);

    var data =
    {
        ProductId: productId,
        Quantity: textbox.value
    };

    $.post(viewUrl, data);

    window.location.reload(true);
}

function isInteger(text) {
    return (text.match(/^\d+$/) != null);
}

//FF added 28 Sept 2011 to reload RecruitmentControl
function reloadOnceOnly() {
    window.location.reload(true); 

}
