﻿var SerializeVariants = null;
var contentUrl = null;
var HasVariants = null;
var Currency = null;
var PartNumber = null;
var CatalogName = null;
var SizePropertyName = null;

function ChangeHiddenFieldsValues(qtyAvailable, variantId) {
    $('#hiddenQuantityAvailable').val(qtyAvailable);
    $('#VariantId').val(variantId);

    updateQuantity();
    $('#ulQuantityError').hide();
    $('#gradientOrangeProductDetails').show();

    var msg = $('#hiddenQuantityError').val();
    $('#liQuantityError').text(msg.replace("xxxxx", qtyAvailable));

    msg = $('#hiddenQuantityWithCartError').val();
    $('#liQuantityWithCartError').text(msg.replace("xxxxx", qtyAvailable).replace("yyyyy", $('#hiddenQuantityInCart').val()));
}

function checkQtyValue(msg) {

    var value = $("#qtyInput").val();
    var quantityAvailable = $('#hiddenQuantityAvailable').val();
    var quantityInCart = $('#hiddenQuantityInCart').val();
    
    var message = $('#hiddenQuantityError').val();
    $('#liQuantityError').text(message.replace("xxxxx", quantityAvailable));

    message = $('#hiddenQuantityWithCartError').val();
    $('#liQuantityWithCartError').text(message.replace("xxxxx", quantityInCart).replace("yyyyy", quantityAvailable));

    if (value.length > 3 || value.length == 0 || value == 0 || !value.match(/^\d+$/)) {
        alert(msg);
        return false;
    }

    value = parseInt(value, 10);
    quantityAvailable = parseInt(quantityAvailable, 10);
    quantityInCart = parseInt(quantityInCart, 10);

    if (value > quantityAvailable) {
        $('#ulQuantityError').show();
        $('#liQuantityError').show();
        $('#liQuantityWithCartError').hide();
        $('#liProductOutOfStockError').hide();
        $('#lnkToOtherProducts').hide();
        return false;
    }
    else if (value + quantityInCart > quantityAvailable) {
        $('#ulQuantityError').show();
        $('#liQuantityError').hide();
        $('#liQuantityWithCartError').show();
        $('#liProductOutOfStockError').hide();
        $('#lnkToOtherProducts').hide();
        return false;
    }
    else {
        $('#ulQuantityError').hide();
    }

    return true;
}

function ChangeVariantByThumbnail(selectedColor, numberOfImage) {
    $('#btnShowLargeImage').show();
    var arr = eval("(" + SerializeVariants + ")");
    var urlMiddleImage = null;
    var urlLargeImage = null;

    for (var i = 0; i < arr.length; i++) {

        if (selectedColor == arr[i].Color) {
            urlMiddleImage = arr[i].MiddleImage[numberOfImage];
            urlLargeImage = arr[i].LargeImage[numberOfImage];
        }
    }
    // Update image boxes
    $("#MainPictureImg").attr("src", contentUrl + urlMiddleImage);
    $("#FullScreenImageImg").attr("src", contentUrl + urlLargeImage);

    //Update comboboxes with available variants
    $("#ColorDropDown").children().remove();
    $("#SizeDropDown").children().remove();


    for (var i = 0; i < arr.length; i++) {

        if (selectedColor != arr[i].Color) {
            $("#ColorDropDown").append($("<option></option>").attr("value", arr[i].Color).text(arr[i].Color));
        }
        else {
            $("#ColorDropDown").append($("<option></option>").attr("value", arr[i].Color).attr("SELECTED", "true").text(arr[i].Color));
        }
    }
    //arr[i].AvailableProperties[SizePropertyName][x]
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].Color == selectedColor) {
            for (x in arr[i].AvailableProperties[SizePropertyName])
                $("#SizeDropDown").append($("<option></option>").attr("value", x).text(x));
        }

    }

    ChangePriceAndArcticle();

}

function ChangePriceAndArcticle() {
    var selectedColor = $("#ColorDropDown").val();
    var selectedSize = $("#SizeDropDown").val();

    //alert(selectedColor + ', ' + selectedSize);
    var arr = eval("(" + SerializeVariants + ")");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].Color == selectedColor) {
            for (x in arr[i].AvailableProperties[SizePropertyName])
                if (x == selectedSize) {
                    $("#productPrice").html(arr[i].AvailableProperties[SizePropertyName][x].Price + " " + Currency);
                    $("#productDisplayName").html(arr[i].AvailableProperties[SizePropertyName][x].DisplayName);
                    $("#lblArticleNo").html(arr[i].AvailableProperties[SizePropertyName][x].VariantId.replace("(MerchandiseCatalog)", ""));
                    $("#lblArticleNoHiddenField").val(arr[i].AvailableProperties[SizePropertyName][x].VariantId);

                    ChangeHiddenFieldsValues(arr[i].AvailableProperties[SizePropertyName][x].OnhandQuantity, arr[i].AvailableProperties[SizePropertyName][x].VariantId);
                    break;
            }
        }
    }
}

function ChangePriceAndArcticleSimpleProduct(urlMiddleImage, urlLargeImage) {
    // Update image boxes
    $('#btnShowLargeImage').show(); 
    $("#MainPictureImg").attr("src", contentUrl + urlMiddleImage);
    $("#FullScreenImageImg").attr("src", contentUrl + urlLargeImage);
}

function BuyProduct() {

    var qty = $("#qtyInput").val();
    if (HasVariants == "True") {
        var variantId = $("#lblArticleNoHiddenField").val();
        //alert(variantId)
        addToCartVariantClick(PartNumber, variantId, "", CatalogName, "", qty);
    }
    else {
        addToCartClick(PartNumber, "", CatalogName, "", qty);
    }

    //updateQuantity();
    var qtyInCart = $('#hiddenQuantityInCart').val();
    $('#hiddenQuantityInCart').val(parseInt(qty) + parseInt(qtyInCart));
}

/// Show large image
function ShowLargeImage() {
    jQuery.each($('#LanguageMenuDropDown a'), function() {
        var newUrl = AddUrlParameter($(this).attr('href'), "showLargeImage", "true", true);
        $(this).attr('href', newUrl);
    });
   
    $("#ProductInfoBox").hide();
    $("#ProductImageBox").hide();
    $("#ReleatedProductsWidget").hide();
    $("#CampaignWidget").hide();
    $("#FullScreenImageBox").show();
}
//Hide large image
function hideImagePopup() {
    jQuery.each($('#LanguageMenuDropDown a'), function() {
    var newUrl = removeURLParam($(this).attr('href'), "showLargeImage");
        $(this).attr('href', newUrl);
    });

    $("#ProductInfoBox").show();
    $("#ProductImageBox").show();
    $("#ReleatedProductsWidget").show();
    $("#CampaignWidget").show();
    $("#FullScreenImageBox").hide();
}

//function onImgErrorBig(source) {
//    source.src = '<%= Url.Content("~/Content/images/no-image-400px.gif")%>';
//    // disable onerror to prevent endless loop
//    source.onerror = "";
//    return true;
//}




