﻿var strStoreCustomerOrderGUID = "";
var strStoreCustomerGUID = "";
var strSourceID = "";
var intItemID = 0;
var intStoreID = 0;
var strViralType = "";
var dblItemPrice = 0;
var strWishListGUID = "";
var intItemID = 0;
var intSelectedImage = 0;

function MPStoreInitItemValues(RootURL, StoreCustomerOrderGUID, StoreCustomerGUID, SourceID, StoreID, ItemID) {
    MPRootURL = RootURL;
    strStoreCustomerGUID = StoreCustomerGUID;
    strStoreCustomerOrderGUID = StoreCustomerOrderGUID;
    strSourceID = SourceID;
    intItemID = ItemID;
    intStoreID = StoreID;
    InitRadioOption();
}

function MPStoreAddToCart(ItemID, intQuantity)
{
    try
    {
        intQuantity = (intQuantity) ? intQuantity : 1;
        
        //if (!ValidateAddToCart())
        //    return false;
            
        Render.AddToCart(intStoreID, strStoreCustomerOrderGUID, strStoreCustomerGUID, strSourceID, ItemID, intQuantity, GetItemOptions(), AddToCart_callback)
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "MPStoreAddToCart()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }    
}


function MPStoreInternalAddToCart(intItemID)
{
    try
    {   intItemID = intItemID + "";
        if (!ValidateAddToCart())
            return false;
        document.getElementById("butAddToCart").value = "Adding...";
        document.getElementById("butAddToCart").style.background = "#555555";
        document.getElementById("butAddToCart").onclick = function() { return false; };
        var intQuantity = document.getElementById("selectQuantity").value;
        Render.AddToCart(intStoreID, strStoreCustomerOrderGUID, strStoreCustomerGUID, strSourceID, intItemID, intQuantity, GetItemOptions(), AddToCart_callback)
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "MPStoreInternalAddToCart()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }    
}


function ChangeRadioOption()
{
    try
    {
        if (document.getElementById("divItemPrice"))
        {
            if (dblItemPrice == 0)
                dblItemPrice = parseFloat(document.getElementById("divItemPrice").innerHTML.replace("$", ""));
                
            var dblTotalPrice = dblItemPrice;
            var dblOptionPrice = 0;
            var dblDifference = 0;
            
            // radios
            for (var index = 0; document.getElementsByName("radioOption" + index).length > 0; index++)
            {
                dblOptionPrice = 0;
                for (var j = 0; j < document.getElementsByName("radioOption" + index).length; j++)
                {
                    if (document.getElementsByName("radioOption" + index).item(j).checked)
                    {
                        dblOptionPrice = parseFloat(document.getElementsByName("radioOption" + index).item(j).value);
                        dblTotalPrice += dblOptionPrice;
                        break;
                    }
                }
                
                // reset the text on the options
                for (var j = 0; j < document.getElementsByName("radioOption" + index).length; j++)
                {
                    dblDifference = parseFloat(document.getElementsByName("radioOption" + index).item(j).value) - dblOptionPrice;
                    if (dblDifference == 0)
                        document.getElementById("spanRadio" + index + "Option" + j).innerHTML = "";
                    else if (dblDifference > 0)
                        document.getElementById("spanRadio" + index + "Option" + j).innerHTML = "[Add " + formatCurrency(dblDifference) + "]";
                    else if (dblDifference < 0)
                        document.getElementById("spanRadio" + index + "Option" + j).innerHTML = "[Subtract " + formatCurrency(dblDifference) + "]";
                }        
            }
            
            document.getElementById("divItemPrice").innerHTML = formatCurrency(dblTotalPrice);
        }
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "ChangeRadioOption()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function InitRadioOption()
{
    try {
        for (var index = 0; document.getElementsByName("radioOption" + index).length > 0; index++)
        {
        
            if (document.getElementsByName("radioOption" + index).item(0))
                document.getElementsByName("radioOption" + index).item(0).checked = true;
        }

        if (document.getElementsByName("radioOption" + index).length > 0)
            ChangeRadioOption();
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "InitRadioOption()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}


function GetItemOptions()
{
    try
    {
        var strItemOptions = "";
        
        try
        {
            if (document.getElementById("selectOption0") || document.getElementsByName("radioOption0").length > 0) // make sure there is at least 1 option
            {
                strItemOptions = "<ItemOptions>";
                
                // for loop select options here
                for (var index = 0; document.getElementById("selectOption" + index); index++)
                {
                    strItemOptions += "<ItemOption>";
                    strItemOptions += "<Name>" + document.getElementById("divOptionSelect" + index).innerHTML + "</Name>";
                    strItemOptions += "<Value>" + document.getElementById("selectOption" + index).value + "</Value>";        
                    strItemOptions += "<Price>0</Price>";        
                    strItemOptions += "</ItemOption>";
                }
                
                // for loop radio options here
                for (var index = 0; document.getElementsByName("radioOption" + index).length > 0; index++)
                {
                    for (var j = 0; j < document.getElementsByName("radioOption" + index).length; j++)
                    {
                        if (document.getElementsByName("radioOption" + index).item(j).checked)
                        {
                            strItemOptions += "<ItemOption>";
                            strItemOptions += "<Name>" + document.getElementById("divOptionRadio" + index).innerHTML + "</Name>";
                            strItemOptions += "<Value>" + document.getElementsByName("radioOption" + index).item(j).id + "</Value>";        
                            strItemOptions += "<Price>0</Price>";        
                            strItemOptions += "</ItemOption>";
                            break;
                            
                        }
                    }
                }
                strItemOptions += "</ItemOptions>";
            }
        }
        catch (ex)
        {
            ;
        }
        return strItemOptions;
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "GetItemOptions()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }    
}

function ValidateAddToCart()
{
    try
    {
        var strHTML = "";
        var intQuantity = parseInt(document.getElementById("selectQuantity").value);
        if (isNaN(intQuantity) || intQuantity <= 0)
            strHTML += "• Please enter a valid quantity.<br />";
            
        // radios
        for (var index = 0; document.getElementsByName("radioOption" + index).length > 0; index++)
        {
            var blnChecked = false;
            for (var j = 0; j < document.getElementsByName("radioOption" + index).length; j++)
            {
                if (document.getElementsByName("radioOption" + index).item(j).checked)
                    blnChecked = true;
            }
            
            if (!blnChecked)
                strHTML += "• Please select " + document.getElementById("divOptionRadio" + index).innerHTML + ".<br />";        
        }

        // dropdowns
        for (var index = 0; document.getElementById("selectOption" + index); index++)
        {
            if (document.getElementById("selectOption" + index).value.length < 1)
                strHTML += "• Please select " + document.getElementById("divOptionSelect" + index).innerHTML + ".<br />";        
        }

        if (strHTML.length > 0)
        {
            strHTML = "<span style=\"font-weight: bold\">Please correct or provide the following information:</span> <br /><br />" + strHTML;
            document.getElementById("divValidation").style.display = "block";
            document.getElementById("divValidation").innerHTML = strHTML;
            return false;
        }
        else
        {
            document.getElementById("divValidation").style.display = "none";
            return true;
        }
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "ValidateAddToCart()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }    
}

function AddToCart_callback(objResponse)
{
    try
    {
        if (objResponse)
        {
            if (objResponse.value != "")
            {
                SetStoreCustomerOrderID(objResponse.value);
                window.location.href = MPRootURL + "shoppingcart";
            }
        }
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "AddToCart_callback()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }  
}

function ShowCart(strURL)
{
    try
    {
        window.location.href = MPRootURL + "shoppingcart";
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "ShowCart()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }  
}

function ShowViral(strType)
{
    try
    {
        strViralType = strType;
        if (strType == "ShareWithFriend")
            document.getElementById("MPViralHeader").innerHTML = "Share With a Friend";
        else if (strType == "SuggestGift")
            document.getElementById("MPViralHeader").innerHTML = "Suggest a Gift";
        document.getElementById("MPViralPopup").style.display = "block";
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "ShowViral()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function SendViral()
{
    try
    {
        if (!ValidateViral())
            return false;
            
        var strYourName = document.getElementById("inputViralYourName").value;
        var strYourEmail = document.getElementById("inputViralYourEmail").value;
        var strFriendName = document.getElementById("inputViralFriendName").value;
        var strFriendEmail = document.getElementById("inputViralFriendEmail").value;
        var strPersonalMessage = document.getElementById("inputViralPersonalMessage").value;
        
        Render.SendViral(
            intStoreID,
            strStoreCustomerGUID,
            strViralType,
            strYourName,
            strYourEmail,
            strFriendName,
            strFriendEmail,
            strPersonalMessage,
            intItemID,
            MPRootURL,
            SendViral_callback);
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "SendViral()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function SendViral_callback(objResponse)
{
    try
    {
        // hide
        document.getElementById("inputViralFriendName").value = '';
        document.getElementById("inputViralFriendEmail").value = '';
        document.getElementById("inputViralPersonalMessage").value = '';
        document.getElementById("MPViralPopup").style.display = "none";
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "SendViral_callback()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function ValidateViral()
{
    try
    {
        var strHTML = "";
        
        if (document.getElementById("inputViralYourName").value.length < 1)
            strHTML += "• Please enter your name.<br />";
        if (document.getElementById("inputViralYourEmail").value.length < 1)
            strHTML += "• Please enter your email.<br />";
        if (document.getElementById("inputViralFriendName").value.length < 1)
            strHTML += "• Please enter your friend's name.<br />";
        if (document.getElementById("inputViralFriendEmail").value.length < 1)
            strHTML += "• Please enter your friend's email.<br />";
        
        if (strHTML.length > 0)
        {
            strHTML = "<span style=\"font-weight: bold\">Please correct or provide the following information:</span> <br /><br />" + strHTML;
            document.getElementById("divViralValidation").style.display = "block";
            document.getElementById("divViralValidation").innerHTML = strHTML;
            return false;
        }
        else
            return true;
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "ValidateViral()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function ToggleWishList()
{
    try
    {
        if (document.getElementById("MPAddToWishListSelect").style.display == "none")
            document.getElementById("MPAddToWishListSelect").style.display = "block";
        else
            document.getElementById("MPAddToWishListSelect").style.display = "none";
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "ToggleWishList()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function AddItemToWishList(itemID, wishListGUID)
{
    try
    {
        var options = GetItemOptions();
        strWishListGUID = wishListGUID;
        Render.AddItemToWishList(itemID, wishListGUID, document.getElementById("selectQuantity").value, options, AddItemToWishList_callback);
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "AddItemToWishList()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function AddItemToWishList_callback(objResponse)
{
    try
    {
        ToggleWishList();
        if (!ValidateAddToCart())
            return false;
            
        document.getElementById("MPWishListResult").style.display = "block";
        
        if (objResponse.value == true)
            document.getElementById("MPWishListResult").innerHTML = "Item added:<br />&nbsp;<a href=\"" + MPRootURL + "wishlist/" + strWishListGUID + "\">view wishlist</a>";
        else
            document.getElementById("MPWishListResult").innerHTML = "Error!";
        setTimeout("HideWishListResult()", 3000);
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "AddItemToWishList_callback()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function HideWishListResult()
{
    try
    {
        document.getElementById("MPWishListResult").style.display = "none";
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "HideWishListResult()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function ShowCreateWishList()
{
    try
    {
        var elForm = document.getElementById("MPWishListForm");
        var elNewButton = document.getElementById("MPWishListCreate");

        if (elForm && elNewButton)    
        if (elForm.style.display == "none")
        {
            elNewButton.style.display = "none";
            elForm.style.display = "block";
            document.getElementById("MPWishListName").focus();
        }
        else
        {
            elNewButton.style.display = "block";
            elForm.style.display = "none";
        }
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "ShowCreateWishList()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function CreateWishList(itemID)
{
    try
    {
        intItemID = itemID;
        Render.CreateWishList(strStoreCustomerGUID, document.getElementById("MPWishListName").value, CreateWishList_callback);
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "CreateWishList()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function CreateWishList_callback(objResponse)
{
    try
    {
        if (objResponse)
        {
            if (objResponse.value != "" && objResponse.value != null && intItemID > 0)
                AddItemToWishList(intItemID, objResponse.value);
                //window.location.href = MPRootURL + "wishlist/" + objResponse.value;
        }
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "CreateWishList_callback()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function Logout()
{
    try
    {
        Render.Logout(Logout_callback);
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "Logout()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

function Logout_callback(objResponse)
{
    try
    {
        window.location.reload();
    }
    catch (exception)
    {
        MPLogStoreFrontException("RenderPage.aspx", "Logout_callback()", "StoreCustomerOrderGUID=" + strStoreCustomerOrderGUID, exception);
    }
}

/*
document.body.onload = function() {
eraseCookie("MarketpathStoreFrontCustomerID");
eraseCookie("MarketpathStoreFrontCustomerOrderID");
};
*/

// image functions

function SelectStoreFrontImage(intImageID, strName, strImageRootURL)
{
    try
    {
    
        if (intSelectedImage != 0)
        {
            if (document.getElementById("divImageThumb" + intSelectedImage))
                document.getElementById("divImageThumb" + intSelectedImage).style.border = "1px solid #eeeeee";
        }

        intSelectedImage = intImageID;

        if (document.getElementById("divImageThumb" + intSelectedImage))
            document.getElementById("divImageThumb" + intSelectedImage).style.border = "1px solid #4a6d9c";
        
        if (document.getElementById("divImage"))
            document.getElementById("divImage").innerHTML = "<img src=\"" + strImageRootURL + "images/storefront/" + intImageID + "/300x300/" + strName + "\" />";
    }
    catch (ex)
    {
        MPLogStoreFrontException("JAVASCRIPT: MPStoreFrontItem.js", "SelectStoreFrontImage()", "URL: " + strImageRootURL, ex);
    }
}


