﻿
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 ExecuteAjaxMethod(methodName, methodArgs) {
    $.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/" + methodName,
        data: "{" + methodArgs + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"        
    });
}

$("#goToCheckout").live("click", function() {
    ExecuteAjaxMethod("ShoppingCartSpotCheckout", "");
});

$("#goToCheckout").live("keypess", function() {
    ExecuteAjaxMethod("ShoppingCartSpotCheckout", "");
});

$("#goToCart").live("click", function() {
    ExecuteAjaxMethod("EditShopingCartClicked", "");
});

$("#goToCart").live("keypess", function() {
    ExecuteAjaxMethod("EditShopingCartClicked", "");
});

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"
    });
}

var addToShoppingCartIsProcessing = false;
function AddToShoppingCart(productId, quantity, uom) {
    alert("in add to basket. prudctid = " + productId + " and qty = " + quantity);
    if (!addToShoppingCartIsProcessing) {
        addToShoppingCartIsProcessing = true;
        setTimeout(function() {
            addToShoppingCartIsProcessing = false;
        }, 500);


        $.ajax({
            type: "POST",
            url: "/layouts/ecommerce/Ajax.aspx/AddToShoppingCart",
            data: "{productCode:'" + 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("DAH/eCom/DAHShoppingCart", null, function(msg) {
					var newString = replaceSubstring(msg.d, ',-', '');	
					var newString = replaceSubstring(newString, '.', ',');
					var newString = replaceSubstring(newString, 'KR,', 'KR');
					var newString = replaceSubstring(newString, ',aspx', '.aspx');
					var newString = replaceSubstring(newString, ',gif', '.gif');
					//alert(newString);
                    $("#small_ShoppingCart_container").animate({ opacity: 0.3 }, 200).replaceWith(newString);
                    $("#small_ShoppingCart_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
                });
            }
        });
		
    }

}



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("DAH/eCom/DAHShoppingCart", 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"
    });
}

