﻿////////////////////////////////////////////////////////////////
/// ChainBizz A/S Javascript Functions for Expert.dk Website ///
/// CopyRight 2010 - ChainBizz A/S                           ///
////////////////////////////////////////////////////////////////
var addToShoppingCartIsProcessing = false;

function getStringForStore(scId, itmId) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/SaveNearestStoreAndGetString",
        data: "{scId:'" + scId + "', itemId:'" + itmId + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            jQuery("#NearestStores").append(msg.d);
        }
    });
}
function LoadPictureNotMap() {
    if (jQuery("#GoogleMaps").html().trim() == '') {
        jQuery.ajax({
            type: "POST",
            url: "/layouts/Expert/Layouts/Service.asmx/GetImageForStoreList",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                jQuery("#GoogleMaps").html(msg.d);
            }
        });
    }
}
function expAddToShoppingCart(productCode) {
    if (!addToShoppingCartIsProcessing) {
        addToShoppingCartIsProcessing = true;
        setTimeout(function () {
            addToShoppingCartIsProcessing = false;
        }, 500);

        jQuery.ajax({
            type: "POST",
            url: "/layouts/Expert/Layouts/Service.asmx/AddToShoppingCart",
            data: "{productCode:'" + productCode + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xhr, err) {

                alert(xhr.status);
                alert(err);
            },
            success: function () {
                window.location.href = ('https:' == document.location.protocol ? 'https://' : 'http://') + window.location.hostname + "/functions/xsales.aspx?ec_trk=Go+to+Shopping+Cart";

            }
        });
        return false;
    }
}

function LoadSublayout(sublayout, placeholder, callback) {
    var id = "{E2F3D013-8952-4DA2-8ED4-D91B317DE0C0}";

    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/LoadSublayout",
        data: "{sublayout:'" + sublayout + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: (callback) ? callback : function (msg) {
            placeholder.html(msg.d);
        }
    });
}
function addToHistory(productCode) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/AddToHistory",
        data: "{productCode:'" + productCode + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}
function updateTopBasket() {
    LoadSublayout("Expert/Sublayouts/Top/ExpBasketTop", null, function (msg) {
        jQuery("#EXP-toppanel").replaceWith(msg.d);

    });
}
function saveCords(id, lat, lng) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/SaveStoreCords",
        data: "{SitecoreId:'" + id + "', lat:'" + lat + "', lng :'" + lng + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });

}
function redirectToSitecoreId(id) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/GetItemUrlFromId",
        data: "{SitecoreId:'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            window.location.href = ('https:' == document.location.protocol ? 'https://' : 'http://') + window.location.hostname + msg.d;
        }

    });
}
function PresentNearestItems(Items, counter) {
    var newIds = "";
    for (i = 1; i <= counter; i++) {
        if (Items[i] != null && Items[i].id != null)
            newIds += Items[i].id + ";";
    }
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/GetPString",
        data: "{SitecoreIds:'" + newIds + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var result = msg.d;
            var strings = result.split("|");
            showBigMarker(Items[0].lat, Items[0].lng);
            for (var i = 0; i < strings.length; i++) {
                showSmallMarkerWithLatLng(Items[i + 1].lat, Items[i + 1].lng, strings[i]);
            }
        }
    });
}

function PhoneToZip(phonenumber) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/GetZipFromPhone",
        data: "{phonenumber:'" + phonenumber + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            searchNearest(msg.d, "1");
        }

    });
}
function PhoneToZipSetStore(phonenumber) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Expert/Layouts/Service.asmx/GetZipFromPhone",
        data: "{phonenumber:'" + phonenumber + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            setSelectedStoreFromAddress(msg.d);
        }

    });
}
