﻿var qtyId = 'txtQuantity';
var summaryMode = false;

function CheckQuantity(){
    return CheckQuantity2(qtyId,orderMinimum, orderMultiple);	
}

function CheckQuantity2(elemId ,oMin, oMul)
{
    // setting global vars so these params are retained for followup call [this will apply when CheckQuantity2 is not called from CheckQuantity()]
    qtyId = elemId;
    orderMinimum = oMin;
    orderMultiple = oMul;

	var elem = document.getElementById(elemId);
	var re = /\d+/;                        
	
	if( !re.test(elem.value) ){
		alert("Please enter a quantity.");
		elem.focus();
		return -1;
	}
 
	var quantity = parseInt(elem.value, 10);

	if(quantity == 0)
	{
		alert("Please Enter Quantity more than zero")
		elem.focus();
		return -1;
	}
 
	if( quantity < oMin ){
		alert("Please increase the quantity to meet the Minimum Order Quantity level of " + orderMinimum + " items.")
		elem.focus();
		return -1;
	}
 
	if( quantity % oMul != 0 ){
		alert("This product is sold in multiples of " + orderMultiple + " for packaging reasons. Please re-enter quantity...");
		elem.focus();
		return -1;
	}

	if( typeof(CheckProductTypeQuantity) == 'function' )
	{
		return CheckProductTypeQuantity(elem);
	}
	else
	{
		return quantity;
	} 
}

var m_ItemId;

function AddToCart(itemId){
    AddToCart2(itemId, qtyId, orderMinimum, orderMultiple, listPrice, ourPrice);    
}

function AddToCart2(itemId, elemQtyId, oMin, oMul, lPrice, oPrice)
{
    quantity = CheckQuantity2(elemQtyId, oMin, oMul);
    listPrice = lPrice;
    ourPrice = oPrice;
    
    m_ItemId = itemId;

    if( quantity > 0 ){
        TotalWallcovering.WebStore.Web.WebServices.ShoppingCart.AddToCart(m_ItemId, quantity, false, ctReferer, AddToCart_Success, AddToCart_Success);
        //TotalWallcovering.WebStore.Web.ShoppingCartFacade.AddToCart(itemId, quantity, false, AddToCart_Success, AddToCart_Failure);
    }
}


function AddSampleToCart(productId)
{
    TotalWallcovering.WebStore.Web.WebServices.ShoppingCart.AddSampleToCart(productId, ctReferer,AddSampleToCart_FollowUp,AddSampleToCart_FollowUp);
}

function AddSampleToCart_FollowUp(result)
{
    var updtMsg = document.getElementById(updateMsgId);
    if(result == 0)
    {
        updtMsg.innerHTML = "<font color='maroon'>Product sample already added.</font>"
            + "<br/><a class='siteBtn btnSecure' href='" + viewCartUrl+ "'>Secure Checkout</a>";
    }
    else if (result == 1)
    {
        updtMsg.innerHTML = "Product sample added to cart."
            + "<br/><a class='siteBtn btnSecure' href='" + viewCartUrl+ "'>Secure Checkout</a>";
    }
    else
    {
        updtMsg.innerHTML = "<font color='red'>Sample could not be ordered. Please try later.</font>";
    }
}

function AddToCart_Success(result) 
{
    if( result > 0 ){
        ShowUpdateMessage(result, true);                
    } 
	else if( result < 0 )
	{
        var msg = "The item is already in your cart with a quantity of " + (-1 * result) + ". Do you want to add the additional quantity?";
    
        if( confirm(msg) )
		{
            quantity = CheckQuantity();
            if( quantity > 0 ){
                TotalWallcovering.WebStore.Web.WebServices.ShoppingCart.AddToCart(m_ItemId, quantity, true, ctReferer, AddToCart_Success);
                //TotalWallcovering.WebStore.Web.ShoppingCartFacade.AddToCart(m_ItemId, quantity, true, AddToCart_Success, AddToCart_Failure);
            }
        }
    }
	else
	{
        alert("There was an unknown error adding the item to the cart.");
    }
}


function AddToCart_Failure(error) {
    alert("Error: " + error.get_message());
}


function ShowUpdateMessage(cartItemQuantity, updated)
{
	var updtMsg = document.getElementById(updateMsgId);
	var subtotal = Math.round((cartItemQuantity * ourPrice) * 100)/100;
	var saveDollar = Math.round(((cartItemQuantity * listPrice) - subtotal) * 100)/100;
	var savePercent = Math.round((saveDollar / (cartItemQuantity * listPrice)) * 1000)/10;

	var msg = "";
	 
	if(summaryMode)
	{
	    msg = "&nbsp;<a title='Secure Checkout' href='"+viewCartUrl+"'><img src='/images/confirmation.gif' /></a>";
	}
	else
	{
	    if( updated ){
		    msg = "Your cart has been updated.<br>";
    		
		    document.getElementById(qtyId).value = '';
		    var tmpElem = document.getElementById(qtyId + '_total')
    		
		    if(tmpElem) // means we have elem in grid to show total
		    {
		        tmpElem.innerHTML = '(' + cartItemQuantity + ')';
		    }
	    }

	    msg +=	"You now have " + cartItemQuantity + " of this item in your cart. "
			    + "Your Subtotal = $" + subtotal + 
			    (savePercent>0? ("<br/>That's a savings of $" + saveDollar + " (" + savePercent + " %) off list price!"):""  ) + "<br />"
			    + "<br/><div style='background:url(/images/btn_bk.jpg) no-repeat;padding:6px;padding-left:21px'><a style='color:black;font-weight:bold' href='" + viewCartUrl+ "'>Secure Checkout</a></div>";
    }
	updtMsg.innerHTML = msg;
}
