﻿var mf_asp_id_prefix;
var lcId;
var flashProxy;
var currentModelId;
var hasSelectedVehicleModel;
var selTopMenuItem;



var flashCalls = $({});
function queueFlashCall(method, args) {
    flashCalls.queue(function(next) {
        flashProxy.call(method, args);
        next();
    });
    flashCalls.delay(1000);
}

var currentFlashRim = "";
function transferRimToFlash(rimValue) {
    if (rimValue != currentFlashRim) {
        queueFlashCall("setRim", rimValue);
        currentFlashRim = rimValue;
    }
}

function transferCarToFlash(carValue) {
    queueFlashCall("setCar", carValue);
}

var currentRimSize = -1;
function setRimSize(inch) {
    if (inch != currentRimSize) {
        queueFlashCall("setRimSize", inch);
        currentRimSize = inch;
    }
}

function flashShowCar() {
    queueFlashCall("setActiveFlash", "car");
}

function flashShowBanner() {
    queueFlashCall("setActiveFlash", "banner");
}


function switchMenuItem(sel) {
    if (sel == 'rim')
        changeMenuItemStyle(mf_asp_id_prefix + 'liTopMenuItemRim', 'TopMenuItemOn TopMenuItemOn70');
    else if (sel == 'tire')
        changeMenuItemStyle(mf_asp_id_prefix + 'liTopMenuItemTire', 'TopMenuItemOn TopMenuItemOn56');
    else if (sel == 'cart')
        changeMenuItemStyle(mf_asp_id_prefix + 'liTopMenuItemCart', 'TopMenuItemOn TopMenuItemOn115');
}

function changeMenuItemStyle(idName, className) {
    var li = document.getElementById(idName);
    li.setAttribute("class", className);
    selTopMenuItem = li;
}

function addItem(item) {
    // save selected car color to cookie
    $.get("/Common/CommonDataHandler.ashx?method=setTopCarColorCookie&color=" + item, function() {
    });
}

function switchVehicleModel(modelId) {
    if (modelId != currentModelId) {
        flashShowCar();
        var id = document.getElementById(mf_asp_id_prefix + "hdIdForFlashProxy").value;
        $.get("/Common/VehicleDataHandler.ashx?action=getVehiclePictureString&lcId=" + id + "&modelId=" + modelId, function(s) {
            transferCarToFlash(s);
        });
        currentModelId = modelId;
    }
}

function getCart() {
    $.getJSON("/Common/CartDataHandler.ashx?method=getcart", function(obj) {
        if (obj.result.IsOk == true) {
            $("#cartsum").html(obj.cartsum.CartIncVat == true ? obj.cartsum.TotalSumVatString : obj.cartsum.TotalSumString);
            $("#cartno").html(obj.cartsum.TotalQuantity);
        }
    });
}

function addToCart(productId, qty, modelid, priceCampaign, withoutRimSelect) {
    $.getJSON("/Common/CartDataHandler.ashx?method=addtocart&id=" + productId + "&qty=" + qty + "&modelid=" + modelid + "&priceCampaign=" + priceCampaign + "&woRimSel=" + withoutRimSelect, function(obj) {
        if (obj.result.IsOk == true) {
            $("#cartsum").html(obj.cartsum.CartIncVat == true ? obj.cartsum.TotalSumVatString : obj.cartsum.TotalSumString);
            $("#cartno").html(obj.cartsum.TotalQuantity);
        }
    });
}

function addToCartMultiple(productIds, qtys, modelid, priceCampaign, withoutRimSelect) {
    $.getJSON("/Common/CartDataHandler.ashx?method=addtocartmultiple&ids=" + productIds + "&qtys=" + qtys + "&modelid=" + modelid + "&priceCampaign=" + priceCampaign + "&woRimSel=" + withoutRimSelect, function(obj) {
        if (obj.result.IsOk == true) {
            $("#cartsum").html(obj.cartsum.CartIncVat == true ? obj.cartsum.TotalSumVatString : obj.cartsum.TotalSumString);
            $("#cartno").html(obj.cartsum.TotalQuantity);
        }
    });
}

function updateCartDisplay(price, no) {
    var currentPrice = new Number($("#cartsum").html());
    var currentNo = new Number($("#cartno").html());
    var newNo = currentNo + no;

    if (newNo > 0) {
        $("#cartsum").html(currentPrice + price);
        $("#cartno").html(newNo);
        $("#tocheckout").show();
    } else {
        $("#cartsum").html("0");
        $("#cartno").html("0");
        $("#tocheckout").hide();
    }
}

function updateDisplayLogonUser(user) {
    $("#lbCustomerName").html("");
}

function getUserFromCache() {
    $.getJSON("/Customer/CustomerDataHandler.ashx?method=getCachedUser", function(obj) {
        if (obj.result.IsOk == true) {
            $("#divLoginUser").css("display", "");
            $("#lbCustomerName").html(obj.customer.UserName);
            $("#injectCodeKonto").html("<a href=\"/Customer/Edit.aspx\" target=\"_self\">Redigera konto?</a> <a href=\"/Customer/Orders.aspx\" target=\"_self\">Visa mina ordrar&nbsp;&raquo;</a>");
            $("#log").html("<div style=\"background:url('/Img/Common/loginout1.png') no-repeat;color:#000;height:22px;padding:2px 0px 0px 6px;font-size:11px;font-weight:bold;font-family:arial;\" id=\"login0\"><a href=\"javascript:void(0);\" target=\"_self\" style=\"color:#000;\" onclick=\"logOut();\">LOGGA UT?</a></div><div><a href=\"/Customer/Edit.aspx\" target=\"_self\">Redigera konto?</a> <a href=\"/Customer/Orders.aspx\" target=\"_self\">Visa mina ordrar&nbsp;&raquo;</a></div>");
        }
        else {
            $("#divLoginUser").css("display", "none");
            $("#injectCodeKonto").html("<a href=\"/Customer/Register.aspx\" target=\"_self\">Skapa konto?</a> /");
            $("#log").html("<div id=\"login0\" style=\"background:url('/Img/Common/loginout1.png') no-repeat;color:#000;height:22px;padding:2px 0px 0px 8px;font-size:11px;font-weight:bold;font-family:arial;\" onmouseover=\"this.style.cursor = 'pointer';\" onclick=\"this.parentNode.style.display='none';document.getElementById('login1').style.display='block';\">LOGGA IN?</div>");
        }
    });
}
function selUserName(elem) {
    if (elem.value == "Användarnamn")
        elem.value = "";
}

function leaveUserName(elem) {
    if (elem.value == "")
        elem.value = "Användarnamn";
}

function selPassword(elem) {
    if (elem.value == "Lösenord")
        elem.value = "";
}

function leavePassword(elem) {
    if (elem.value == "")
        elem.value = "Lösenord";
}
function logOut() {
    $.getJSON("/Customer/CustomerDataHandler.ashx?method=logout", function(obj) {
        if (obj == 0) {
            alert("Du är nu utloggad!");
            $("#txtUserName").val("Användarnamn");
            $("#txtPassword").val("Lösenord");
            $("#lbCustomerName").text("");
            $("#divLoginUser").css("display", "none");
            $("#injectCodeKonto").html("<a href=\"/Customer/Register.aspx\" target=\"_self\">Skapa konto</a> /");
            document.location.href = '/Default.aspx';
            return true;
        }
        else {
            alert("Gick inte att logga ut!");
            return false;
        }
    });
}
function userLogin() {
    var user = $("#txtUserName").val();
    if (user == "" || user == "Användarnamn") {
        alert("Ange ditt användarnamn för att logga in.");
        return false;
    }

    var pwd = $("#txtPassword").val();
    if (pwd == "" || pwd == "Lösenord") {
        alert("Ange ditt lösenord för att logga in.");
        return false;
    }

    $.getJSON("/Customer/CustomerDataHandler.ashx?method=login&user=" + $("#txtUserName").val() + "&pwd=" + $("#txtPassword").val(), function(obj) {
        if (obj.result.IsOk == true) {
            if (obj.result.ReturnValue == 0) {
                $("#divLoginUser").css("display", "");
                $("#lbCustomerName").html(obj.customer.UserName);
                $("#injectCodeKonto").html("<a href=\"/Customer/Edit.aspx\" target=\"_self\">Redigera konto</a> /");
                alert('Välkommen! Du är nu inloggad.');
                document.location.href = '/Default.aspx';
                return true;
            }
            else if (obj.result.ReturnValue == 1 || obj.result.ReturnValue == 2) {
                $("#divLoginUser").css("display", "none");
                $("#lbCustomerName").html("");
                $("#injectCodeKonto").html("<a href=\"/Customer/Register.aspx\" target=\"_self\">Skapa konto</a> /");
                alert("Fel användarnamn eller lösenord. Kontrollera stavningen.\r\nOm du är en ny kund klicka på <Skapa konto>");
                return false;
            }
        }
    });
}
function popUpSendLoginInfoClose() {
    document.getElementById('theBackLid').style.display = 'none';
    $("#divPopUpSendLoginInfo").css("display", "none");
}

function popUpSendLoginInfoShow() {
    document.getElementById('theBackLid').style.display = 'block';
    $("#divPopUpSendLoginInfo").css("display", "block");
}

function popUpSendLoginInfoSend() {
    var email = $("#txtPopupEmail").val();
    if (email == "") {
        alert("Ange din e-postadress!");
        return;
    }

    $.getJSON("/Customer/CustomerDataHandler.ashx?method=sendLoginInfo&email=" + email, function(obj) {
        if (obj.result.IsOk == true) {
            popUpSendLoginInfoClose();
            alert("Dina inloggningsuppgifter har skickats till din e-postadress!");
        }
        else {
            popUpSendLoginInfoClose();
            alert(obj.result.Text);
        }
    });
}
