﻿function getMainImage()
{
if ($get(productImageServerVars.MainImageLarge) == null)
    return $get(productImageServerVars.MainImageMain);
else
    return $get(productImageServerVars.MainImageLarge);
}

function atcClicked(productID, productIndex, shipToIndex)
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm.get_isInAsyncPostBack())
        var listener = setInterval(function() { if (!prm.get_isInAsyncPostBack()) { clearInterval(listener); addToCart(productID,productIndex,shipToIndex,"",true); } }, 250);
    else
        addToCart(productID,productIndex,shipToIndex,"",true);
}

function addToCart(productID,productIndex,shipToIndex,newShipTo,checkInventory)
{
if (isWhitespace(productIndex) && attributeServerVars.SKUID != null)
{
    var addToCartSKUID = $get(attributeServerVars.SKUID);
    if (addToCartSKUID != null)
    {
        var SKUInventory = $get(attributeServerVars.SKUInventory).value;
        if (isWhitespace(addToCartSKUID.value) || parseInt(SKUInventory) <= 0)
        {
            var attributesSelected = "";
            for (var index = 0; eval("attributeServerVars.Attribute" + index) != null; index++)
            {
                if (index != 0)
                    attributesSelected += ", ";
                attributesSelected += selectText($get(eval("attributeServerVars.Attribute" + index)));
            }
            if (checkInventory)
            {
                var attributeLead;
                if (index == 1)
	                attributeLead = "The attribute you have chosen for this product";
                else
	                attributeLead = "The combination of attributes you have chosen";
	            attributeLead += " (" + attributesSelected + ") ";
    		    
	            var depletionStatus = attributeServerVars.DepletionStatus;
	            var alertText = "";
	            var confirmText = "";
	            if (isWhitespace(addToCartSKUID.value))
	                alertText = "is not available";
	            else if (depletionStatus == "Discontinued")
	                alertText = "has been discontinued";
	            else if (depletionStatus == "Sold Out")
	                alertText = "is currently " + cartServerVars.SoldOut.toLowerCase();
	            else if (depletionStatus == "Backordered")
	                confirmText = "is currently " + cartServerVars.BackorderCart.toLowerCase();
	            else if (depletionStatus == "Preordered")
	                confirmText = "is currently " + cartServerVars.PreorderCart.toLowerCase();
                if (!isWhitespace(alertText))
                {
	                alert(attributeLead + alertText + ".\n\nPlease make another selection.  Thank you.");
	                return;
                }
                else if (!isWhitespace(confirmText))
                {
                    if (!confirm(attributeLead + confirmText + ".\n\nWould you still like to add this item to your " + cartServerVars.ShoppingCart.toLowerCase() + "?"))
		                return;
                }
            }
        }
        $get(cartServerVars.SKUID).value = addToCartSKUID.value;
    }
}
if (isWhitespace(productIndex) && attributeServerVars.WriteInID0 != null)
{
    var writeInIDs = "";
    var writeInValues = "";
    var firstWriteIn = true;
    for (var index = 0; eval("attributeServerVars.WriteInID" + index) != null; index++)
    {
        var addToCartWriteInValue = $get(eval("attributeServerVars.WriteInValue" + index));
        var curWriteInValue;
        if (addToCartWriteInValue.type == "checkbox")
        {
            if (addToCartWriteInValue.checked == true)
                curWriteInValue = "Yes";
            else
                curWriteInValue = "No";
        }
        else
            curWriteInValue = addToCartWriteInValue.value;
        if (!isWhitespace(curWriteInValue))
        {
            if (firstWriteIn)
                firstWriteIn = false;
            else
            {
                writeInIDs += masterServerVars.Delimiter;
                writeInValues += masterServerVars.Delimiter;
            }
            writeInIDs += eval("attributeServerVars.WriteInID" + index);
            writeInValues += curWriteInValue;
        }
    }
    $get(cartServerVars.WriteInIDs).value = writeInIDs;
    $get(cartServerVars.WriteInValues).value = writeInValues;
}
if (isWhitespace(shipToIndex) && isWhitespace(newShipTo))
{
    $find("mpeEnterShipTo" + productIndex).show();
    return;
}
else if (!isWhitespace(newShipTo))
    $get(cartServerVars.NewShipTo).value = newShipTo;
else if (!isWhitespace(shipToIndex))
  $get(cartServerVars.ShipTo).value = shipToIndex;

var quantityTextBox = $get(eval("addToCart" + productIndex + "ServerVars.Quantity"));
$get(cartServerVars.Quantity).value = quantityTextBox.value;
if (masterServerVars.ResetQuantity == "true")
    quantityTextBox.value = quantityTextBox.defaultValue;
$get(cartServerVars.ProductID).value = productID;
if (eval("addToCart" + productIndex + "ServerVars.BidPrice") != null)
    $get(cartServerVars.BidPrice).value = $get(eval("addToCart" + productIndex + "ServerVars.BidPrice")).value;

if (isInteger(shipToIndex,true))
{
    try
    {
        setSelectValue($get(addToCartServerVars.ShipTo), shipToIndex);
    }
    catch(err)
    {
    }
    var atcServerVars;
    var continueFor = true;
    try
    {
        atcServerVars = addToCart0ServerVars;
    }
    catch(err)
    {
        continueFor = false;
    }
    var index = 0;
    while (continueFor)
    {
        if (eval("addToCart" + index + "ServerVars.ShipTo") != null && $get(eval("addToCart" + index + "ServerVars.ShipTo")) != null)
            setSelectValue($get(eval("addToCart" + index + "ServerVars.ShipTo")),shipToIndex);
        index++;
        try
        {
            atcServerVars = eval("addToCart" + index + "ServerVars");
        }
        catch(err)
        {
            continueFor = false;
        }
    }
}

__doPostBack(cartServerVars.ProductID, "ValueChanged");
}

function addToCartNewShipTo(productID, productIndex)
{
var newShipToName = $get(eval("newShipTo" + productIndex + "ServerVars.Name")).value;
$get(eval("newShipTo" + productIndex + "ServerVars.Name")).value = "";
if (eval("newShipTo" + productIndex + "ServerVars.GiftMessageCheckbox") != null && $get(eval("newShipTo" + productIndex + "ServerVars.GiftMessageCheckbox")).checked == true)
{
    $get(cartServerVars.GiftMessage).value = $get(eval("newShipTo" + productIndex + "ServerVars.GiftMessage")).value;
    $get(eval("newShipTo" + productIndex + "ServerVars.GiftMessageCheckbox")).checked = false;
    $get(eval("newShipTo" + productIndex + "ServerVars.GiftMessage")).value = "";
}
if (eval("newShipTo" + productIndex + "ServerVars.GiftWrapCheckbox") != null && $get(eval("newShipTo" + productIndex + "ServerVars.GiftWrapCheckbox")).checked == true)
{
    $get(cartServerVars.GiftWrap).value = "True";
    $get(eval("newShipTo" + productIndex + "ServerVars.GiftWrapCheckbox")).checked == false;
}
    
var shipToPrototype = $get(eval("addToCart" + productIndex + "ServerVars.ShipTo"));
var newShipToIndex = 1;
var newShipToPosition = 1;
if (masterServerVars.AddNewPosition == "first")
    newShipToPosition = 2;
while (newShipToPosition < shipToPrototype.length && !isWhitespace(shipToPrototype.options[newShipToPosition].value))
{
    if (masterServerVars.AddressOrder == "alpha" && newShipToName < shipToPrototype.options[newShipToPosition].text)
        break;
    newShipToIndex++;
    newShipToPosition++;
}

try
{
    addNewShipTo($get(addToCartServerVars.ShipTo),newShipToIndex,newShipToPosition,newShipToName);
}
catch(err)
{
}
var atcServerVars;
var continueFor = true;
try
{
    atcServerVars = addToCart0ServerVars;
}
catch(err)
{
    continueFor = false;
}
var index = 0;
while (continueFor)
{
    if (eval("addToCart" + index + "ServerVars.ShipTo") != null && $get(eval("addToCart" + index + "ServerVars.ShipTo")) != null)
        addNewShipTo($get(eval("addToCart" + index + "ServerVars.ShipTo")),newShipToIndex,newShipToPosition,newShipToName);
    index++;
    try
    {
        atcServerVars = eval("addToCart" + index + "ServerVars");
    }
    catch(err)
    {
        continueFor = false;
    }
}

addToCart(productID, productIndex, newShipToIndex, newShipToName, false);
}

function addNewShipTo(shipToSelectBox,newShipToIndex,newShipToPosition,newShipToName)
{
shipToSelectBox.length +=  1;
for (var index = shipToSelectBox.length - 1; index > newShipToPosition; index--)
{
    shipToSelectBox.options[index].text = shipToSelectBox.options[index - 1].text;
    if (isWhitespace(shipToSelectBox.options[index - 1].value))
        shipToSelectBox.options[index].value = shipToSelectBox.options[index - 1].value;
	else
    	shipToSelectBox.options[index].value = parseInt(shipToSelectBox.options[index-1].value) + 1;
}
shipToSelectBox.options[newShipToPosition].value = newShipToIndex;
shipToSelectBox.options[newShipToPosition].text = newShipToName;
}