﻿function LoadRendering(rendering, placeholder) {
    var id = $("#scID").attr("content");

    $.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/LoadRendering",
        data: "{rendering:'" + rendering + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Replace the div's content with the page method's return.
            placeholder.html(msg.d);
        }
    });
}

function LoadSublayout(sublayout, placeholder, callback) {
    var id = $("#scID").attr("content");

    $.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/LoadSublayout",
        data: "{sublayout:'" + sublayout + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: (callback) ? callback : function(msg) {
            placeholder.html(msg.d);
        }
    });
}


function SetCurrentTab(tab) {
    $.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/SetCurrentTab",
        data: "{tab:'" + tab + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}


function AddReview(title, text, rate) {
    var id = $("#scID").attr("content");
    $.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/AddReview",
        data: "{title:'" + title + "', text :'" + text + "', rate:'" + rate + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}

function AddToCart(productId, quantity, uom){
	$("div.Accessories input:checked").each(function(n){
		AccessoryProductId = $(this).val().slice($(this).val().indexOf('|')+1,$(this).val().length);
		AccessoryQuantity = 1;
		$.ajax({
				type: "POST",
				url: "/layouts/ecommerce/Ajax.aspx/AddToShoppingCart",
				data: "{productId:'" + AccessoryProductId + "', quantity:'" + AccessoryQuantity + "', uom :'" + uom + "'}",
				contentType: "application/json; charset=utf-8",
				dataType: "json"
		});
	});
	AddToShoppingCart(productId, quantity, uom);
}

var addToShoppingCartIsProcessing = false;
function AddToShoppingCart(productId, quantity, uom) {
    if (!addToShoppingCartIsProcessing) {
        addToShoppingCartIsProcessing = true;
        setTimeout(function() {
            addToShoppingCartIsProcessing = false;
        }, 500);

        $.ajax({
            type: "POST",
            url: "/layouts/ecommerce/Ajax.aspx/AddToShoppingCart",
            data: "{productId:'" + productId + "', quantity:'" + quantity + "', uom :'" + uom + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                // Replace the div's content with the page method's return.
                //LoadSublayout("Ecommerce/UserControls/ShoppingCartSpot", null, function(msg) {
								LoadSublayout("Scandynashop/ShoppingCartMainPage", null, function(msg) {
										//msg.d = msg.d.replace(",-","");
										//msg.d = msg.d.replace(".00","");
										//alert(msg.d);
                    $("#CartReplace").animate({ opacity: 0.3 }, 200).replaceWith(msg.d);
                    $("#CartReplace").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
                });
            }
        });
    }
	window.location='http://scandynashop.us/en/Functions/CheckOutProcess/Basket.aspx?sc_trk=GotoShoppingCart';
}

function AddAccessory(Obj){
	var AccessoryPrice = Obj.value.slice(1,Obj.value.indexOf('|')-2)
	var ProductPrice = $('.ProductPrice').text().slice(1,$('.ProductPrice').text().indexOf(',')).replace('.','');
	if(Obj.checked){
		var SumTotal = parseInt(ProductPrice) + parseInt(AccessoryPrice);
	}
	else
	{
		var SumTotal = parseInt(ProductPrice) - parseInt(AccessoryPrice);
	}
	var rgx = /(\d+)(\d{3})/;
	SumTotal = SumTotal.toString().replace(rgx, '$1' + '.' + '$2');
	$(".ProductPrice").text("$"+SumTotal);
}

function DeleteFromShoppingCart(productId, uom) {
    $.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/DeleteFromShoppingCart",
        data: "{productId:'" + productId + "', uom :'" + uom + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Replace the div's content with the page method's return.
            LoadSublayout("Ecommerce/UserControls/ShoppingCartSpot", null, function(msg) {
                $("#small_ShoppingCart_container").animate({ opacity: 0.3 }, 200).replaceWith(msg.d);
                $("#small_ShoppingCart_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
            });
        }
    });
}

function UpdateShoppingCart(productId, quantity, uom) {
	$.ajax({
			type: "POST",
			url: "/layouts/ecommerce/Ajax.aspx/UpdateShoppingCart",
			data: "{productId:'" + productId + "' quantity:'" + quantity + "', uom :'" + uom + "'}",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function(msg) {
				LoadSublayout("Scandynashop/ShoppingCartMainPage", null, function(msg) {
					msg.d = msg.d.replace(",-","");
					msg.d = msg.d.replace(".00","");
											
					$("#CartReplace").animate({ opacity: 0.3 }, 200).replaceWith(msg.d);
					$("#CartReplace").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
				});
			}
	});
}

