﻿/* Category */
function GetCategories()
{
    //Call Ajax call for requested page
    YAHOO.Ajax.Call.Start(null,'GetCategories',null,'');
}

/* Search Products */
function SearchStoreProducts()
{
    //Call Ajax call for requested search
    if (!Salem.String.IsNullOrEmpty(StoreSearchBox.value)) {
        YAHOO.Ajax.Call.Start(null, 'GetSearchResults', null, Salem.String.Format('searchstring={0}', StoreSearchBox.value));
        MenuValue['Search'] = StoreSearchBox.value;
    } else {
        alert("Please enter a search phrase.");
    }
}

/* Product by Category */
function GetProductsByCategory(categoryID)
{
    //Set Quick Jump menu value
    MenuValue['CategoryId'] = categoryID;
    //Call Ajax call for requested page
    YAHOO.Ajax.Call.Start(null,'GetProductsByCategory',null, Salem.String.Format('categoryID={0}', categoryID));
}

/* Product by Category */
function GetProductsByProductList(productList)
{
    //Set Quick Jump menu value
    MenuValue['CategoryId'] = '';
    MenuValue['ProductList'] = productList;
    productList += BizOpString;
    //Call Ajax call for requested page
    YAHOO.Ajax.Call.Start(null,'GetProductsByProductList',null, Salem.String.Format('ProductList={0}', productList));
}

/* Product Details */
function GetProductDetails(productId)
{
    //Call Ajax call for requested page
    YAHOO.Ajax.Call.Start(null,'GetProductDetails',null,'productID='+productId);
}

/* Product Details */
function GetFeaturedProductDetails(productId)
{
    //Call Ajax call for requested page
    YAHOO.Ajax.Call.Start(null,'GetProductDetails',null,'productID='+productId); 
}

/* Shopping Cart */
function GetShoppingCart()
{
    //Call Ajax call for requested page
    YAHOO.Ajax.Call.Start(null,'GetShoppingCart',null,'');
}

/* UpdateShoppingCart Totals */
function UpdateShoppingCart()
{
    //Since we use Ajax the cart actually update the total onBlur of the Quantity element
    //so this just ensures they blur from the input box forcing the cart to refresh
    //You could uncomment the GetShoppingCart line below if you really want to refresh twice
    //GetShoppingCart(storeId, cartId);
}

/* Add Items to Cart */
function AddItemQuantityToCart(productId, quantity)
{
    //Call Ajax call for requested page
    YAHOO.Ajax.Call.Start(null,'AddItemQuantityToCart',null,'productID='+productId+'&quantity='+quantity);
}

/* Update Cart item this warns of removal if quantity is 0 */
function UpdateCartItem(productId, quantity)
{
    //Lets make sure the quantity is a number
    if (!isNaN(parseFloat(quantity)))
    {
        if (quantity < 1)
        {
            //User entered a 0 so we confirm they want to remove the item from their cart
            var removeItem = confirm("Remove item from cart?");
            if (removeItem)
            {
                //Call Ajax call for requested page
                YAHOO.Ajax.Call.Start(null,'UpdateCartItem',null, Salem.String.Format('productID={0}&quantity={1}', productId, quantity));
            }
        }
        else
        {
            //Call Ajax call for requested page
            YAHOO.Ajax.Call.Start(null, 'UpdateCartItem', null, Salem.String.Format('productID={0}&quantity={1}', productId, quantity));
        }
    }
    else
    {
        //Notify user that they entered an invalid quantity
        alert("Please enter a number!");
    }
}

/* CheckOut Handler */
function CheckOutShoppingCart()
{
    if (Salem.WebProfile.UserID != '' || Salem.WebProfile.UserID > 0)
    {
        //Call Ajax Checkout Page
        var form = Salem.String.Format('AllPassUserID={0}&CouponCode={1}&MenuLocation={2}', Salem.WebProfile.UserID, couponCode, Salem.Global.MenuLocation);
        YAHOO.Ajax.Call.Start(null,'CartCheckoutAllPassUserWithCouponCode',null, form);
    }
    else
    {
        //Send User to login
        //alert("Please Login before checking out! Don't worry we'll save your cart for your convienence!");
        document.location.href = RedirectUrl;
    }
}

/* Promotion Handler */
function ApplyPromotion(CouponCode)
{
    //Create Parameters list
    couponCode = CouponCode;
    //Call Ajax Checkout Page
    var form = Salem.String.Format('AllPassUserID={0}&CouponCode={1}&MenuLocation={2}', Salem.WebProfile.UserID, couponCode, Salem.Global.MenuLocation);
    YAHOO.Ajax.Call.Start(null,'CartCheckoutAllPassUserWithCouponCode',null, form);
}

/* Send user to PayPal to pay */
function ProceedToCheckOut()
{
    if (Salem.WebProfile.UserID != '' || Salem.WebProfile.UserID > 0)
    {
        //alert("test");
        if (loadContainer != null)
        {
            //This fixes an issue inwhich posting to long of a hidden input value breaks PayPal.
            loadContainer.value = '';
        }
        //alert(loadContainer.value);
        document.forms[0].action = 'https://payflowlink.paypal.com'; 
        document.forms[0].submit();
    }
    else
    {
        //Send User to login
        //alert("Please Login before checking out! Don't worry we'll save your cart for your convienence!");
        document.location.href = RedirectUrl;
    }
}

function FinishNoCheckOut()
{
    if (Salem.WebProfile.UserID != '' || Salem.WebProfile.UserID > 0)
    {
        //alert("test");
        if (loadContainer != null)
        {
            //This fixes an issue inwhich posting to long of a hidden input value breaks PayPal.
            loadContainer.value = '';
        }
        //alert(loadContainer.value);
        document.forms[0].action = Salem.String.Format('{0}Store/ProcessPostCheckout.aspx', Salem.Global.BaseUrl);
        //alert(document.forms[0].action);
        document.forms[0].submit();
    }
    else
    {
        //Send User to login
        //alert("Please Login before checking out! Don't worry we'll save your cart for your convienence!");
        document.location.href = RedirectUrl;
    }
}

/* TitleManager*/

function PageManager(pagecall)
{
    switch (pagecall)
    {
        case "GetCategories":
            PageTitle.innerHTML = "Personnel &amp Staffing Products";
            PageDescription.innerHTML = "Welcome to the online store, your source for church hiring and staffing resources. Please browse our inventory for books and services to assist you in your needs.";
            BuildQuickLinkMenu(['GetLastSearch','GoToShoppingCart']);
            break;
        
        case "GetProductsByCategory":
            PageTitle.innerHTML = "Products";
            PageDescription.innerHTML = "";
            BuildQuickLinkMenu(['GetCategories','GetLastSearch','GoToShoppingCart']);
            break;
            
        case "GetProductsByProductList":
            PageTitle.innerHTML = "Products";
            PageDescription.innerHTML = "";
            BuildQuickLinkMenu(['GetCategories','GetLastSearch','GoToShoppingCart']);
            break;
        
        case "GetSearchResults":
            PageTitle.innerHTML = "Search Results";
            PageDescription.innerHTML = "";
            BuildQuickLinkMenu(['GetCategories','GetProductList','GoToShoppingCart']);
            break;
            
        case "GetProductDetails":
            PageTitle.innerHTML = "Product Details";
            PageDescription.innerHTML = "";
            BuildQuickLinkMenu(['GetCategories','GetProductList','GetLastSearch','GoToShoppingCart']); 
            break;
            
        case "GetShoppingCart":
            PageTitle.innerHTML = "";
            PageDescription.innerHTML = "";
            BuildQuickLinkMenu(['GetProductList','GetLastSearch','ContinueShopping']);
            break;
           
        case "CartCheckoutAllPassUserWithCouponCode":
            PageTitle.innerHTML = "Store Checkout";
            PageDescription.innerHTML = "Please review your order before checking out.";
            BuildQuickLinkMenu(['GoToShoppingCart']);
            break;      
        
        default:
            PageTitle.innerHTML = "";
            PageDescription.innerHTML = "";
            break;
    }
    return;
}
