﻿
function GetSkuDetailsForOptions() {

    var productId = document.getElementById('hdnProductId').value;
    var colourId = $('#ddlColours').val();
    var optionId = $('#ddlOptions').val();


    if (colourId == "-1" || optionId == "-1") {
        $('#productPriceDiv').html('');
        $('#hdnSkuId').val('');
        return;
    }


    $.ajax({
        type: "GET",
        dataType: "xml",
        url: "/handlers/product-sku-handler.ashx?ProductId=" + productId + "&ColourId=" + colourId + "&OptionId=" + optionId + "&WhyDoYouNeedThisIE=" + new Date().getTime(),

        success: function(xml) {
            var price = $(xml).find('price').text();
            var quantity = $(xml).find('quantity').text();
            var skuId = $(xml).find('skuId').text();
            var inSale = $(xml).find('inSale').text();
            var salePercentage = $(xml).find('salePercentage').text();
            var salePrice = $(xml).find('salePrice').text();
            var vatRate = $(xml).find('vatRate').text();
            var priceIncVat = $(xml).find('priceIncVat').text();
            var salePriceIncVat = $(xml).find('salePriceIncVat').text();

            updateFields(price, quantity, skuId, inSale, salePercentage, salePrice,priceIncVat,salePriceIncVat, vatRate);
        }
    });
}

function updateFields(price, quantity, skuId, inSale, salePercentage, salePrice,priceIncVat,salePriceIncVat, vatRate) {
    if (skuId == -1) {
        $('#productPriceDiv').html('<p>There are no products available with this option</p>');
        return;
    }

    if (inSale!='False') {
        $('#productPriceDiv').html('<p><em>' + salePrice + ' exc. vat | (' + salePriceIncVat + ' inc. vat) </em><br>(<span>' + price + ' exc. vat - ' + priceIncVat + ' inc. vat</span>)</p>');
    } else {
    $('#productPriceDiv').html('<p>' + price + ' exc. vat (' + priceIncVat + ' inc. vat)</p>');
    }

    $('#hdnSkuId').val(skuId);

}


$(document).ready(function() {
    $("#ddlOptions,#ddlColours").change(function() {
        GetSkuDetailsForOptions();
    });
});           