﻿/*
=====================
    SHOPPING CART
=====================
Created: September 2008
Updated: February 2009

Description:

These are the javascript functions that call all shopping cart related webservice.
This file is included in all pages that have the shopping cart pane.

*/

var offerItemIdToRemove=-1;
    
function AddPadding(itemId)
{   
    if (document.cookie != "")
    {
        if (InCheckoutProcess()) Checkout_ShowPleaseWait("Please Wait ...");
        eCarpetGallery.UI.ShoppingCartWebService.AddPaddingToRug(itemId, OnCartChangeComplete, OnTimeOut, OnError);
    }
    else
    {
        NoCookies();
    }
}

function RemovePadding(itemId)
{   
    if (document.cookie != "")
    {
        if (InCheckoutProcess()) Checkout_ShowPleaseWait("Please Wait ...");
        eCarpetGallery.UI.ShoppingCartWebService.RemovePaddingFromRug(itemId, OnCartChangeComplete, OnTimeOut, OnError);
    }
    else
    {
        NoCookies();
    }
}

function AddToCart(itemId)
{   
    if (document.cookie != "")
    {
        if (InCheckoutProcess()) Checkout_ShowPleaseWait("Please Wait ...");
        eCarpetGallery.UI.ShoppingCartWebService.AddRugToCart(itemId, OnCartChangeComplete, OnTimeOut, OnError);
    }
    else
    {
        NoCookies();
    }
}

function RemoveFromCart(itemId)
{
    if (document.cookie != "")
    {
        if (InCheckoutProcess()) Checkout_ShowPleaseWait("Please Wait ...");
        offerItemIdToRemove=itemId;   
        eCarpetGallery.UI.ShoppingCartWebService.RemoveRugFromCart(itemId, OnCartChangeComplete, OnTimeOut, OnError);
    }
    else
    {
        NoCookies();
    }
}

function NoCookies()
{
    self.location = 'http://www.ecarpetgallery.com/Cookies-Required.aspx';
}

function GetCart()
{   
    eCarpetGallery.UI.ShoppingCartWebService.GetCart(OnComplete, OnTimeOut, OnError);
}

function OnCartChangeComplete(args) {
    switch(args)
    {
        case "Offer Item Cannot Be Removed":
          if (confirm('WARNING!\nYou are about to remove an offer that has already been accepted!\nIf you wish to purchase this item at a later date, you will have to remake your offer .') && offerItemIdToRemove!=-1)
            eCarpetGallery.UI.ShoppingCartWebService.RemoveOfferFromCart(offerItemIdToRemove, OnCartChangeComplete, OnTimeOut, OnError);
          else
            offerItemIdToRemove=-1;  
          break;    
        case "Items Won on eBay Cannot Be Removed":
          alert("Items Won on eBay Cannot Be Removed.");
          break;    
        case "User Not Logged In!":
          alert("In order to remove an offer from your cart you must be logged in.");
          break;    
        case "Item Already In Cart":
          alert("The item requested is already in your cart.");
          break;    
        case "Item Already In Cart as Offer":
          alert("You already have an accepted offer for item selected in your cart.");
          break;    
        default:
          offerItemIdToRemove=-1;
          GetCart();
    }
}

function OnComplete(args) {
    if (!InCheckoutProcess())
    {
        //Display the shopping cart content
        if (args=="") getPageElement('shoppingCartContent').style.display='none';
        else getPageElement('shoppingCartContent').style.display='block';
        getPageElement('shoppingCartContent').innerHTML = args;
    }
    else
    {
        Checkout_RemovePleaseWait();
        switch(document.getElementById('ctl00_ContentPlaceHolder1_checkoutStep').value)
        {
            case "SubmitOrder":
            case "SubmitOrderNotLoggedIn":
                GetSubmitOrderTable();
                break;
            default:  
                self.location='SubmitOrder.aspx';
        }
    }
}

function OnTimeOut(args) {
    if (InCheckoutProcess()) Checkout_RemovePleaseWait();
    offerItemIdToRemove=-1;
    //alert("Service call timed out.");
}

function OnError(args) {
    if (InCheckoutProcess()) Checkout_RemovePleaseWait();
    offerItemIdToRemove=-1;
    //alert("Error calling service method.");
}

//Helper
//Returns whether we are on a checkout page
function InCheckoutProcess()
{
    if (!document.getElementById('ctl00_ContentPlaceHolder1_checkoutStep')) return false;
    return true;
}   
    
