﻿// *************************************************************************************************************************
//  Web Services 
// *************************************************************************************************************************

/* Time On Page */
function PageActivity(actionID) {
  //  BF.Web.Helpers.PageActivityTracking.RecordPageActivity(window.location.href, actionID, null, null);
    //  setTimeout("PageActivity(2);", 20000);

    var i = 0;
}

// *************************************************************************************************************************
// Master Page  
// *************************************************************************************************************************

// View Shopping Cart
function OnViewShoppingCartClick() {
    var url = '/snapins/shoppingcart.aspx';
    ShowPopupSnapinConsole('Standard', 'My Shopping Cart', url, 650,450)
}

// Search Button
function OnSearchButtonClick() {
    cbRoot.PerformCallback("SearchProducts");
}

// Top Navigation Buttons
function OnTopNavButtonOver(elementID) {
    var x = document.getElementById(elementID);
    x.src = '/images/base/bf-web-v6-top-nav-over.jpg';
}
function OnTopNavButtonOut(elementID) {
    var x = document.getElementById(elementID);
    x.src = '/images/base/bf-web-v6-top-nav.jpg';
}


// Footer
function OnRootEndCallback(s, e) {

    // Check if email registration is the process that started the callback
    if (s.cpProcess == 'email') {
        // Clear email field
        beEmailSignUp.SetValue('');

        // Check that review was successfully submitted
        if (s.cpShowSuccessMsg) {
            // Close popup window
            ParentClosePopupSnapinConsole();

            // Show success message
            var msgType = 'confirmation';
            var msgTitle = 'Email Registered';
            var msgText = 'Your email address was registed in our mailing list. Please check your inbox for sale events, newsletters and other special offers.';
            ParentShowPopupMessage(msgType, msgTitle, msgText);
        }
    }
}

// Register user's email with Constant Contact
function OnEmailSignUpClick(s, e) {
    // Check that email meets validation criteria
    beEmailSignUp.Validate();
    if (beEmailSignUp.GetIsValid()) {
        // Trigger callback
        cbRoot.PerformCallback('RegisterEmail');
    }
}

// Wells Fargo Financing
function OnWellsFargoClick(){
    var url = 'https://financial.wellsfargo.com/retailprivatelabel/initApp.do?profileNum=7572&dealerId=190111118';  
    window.open('' + url + '', 'WellsFargo', "location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=1024,height=800,left=40,top=50");
}

// *************************************************************************************
// Templates 
// *************************************************************************************

/***** Product Grid *****/

function OnProductGridViewSortOrderChanged(s, e) {
    var sortOrderValue = s.GetValue();
    var params = 'ChangeSortOrder+' + sortOrderValue;
    cbPanelProductGrid.PerformCallback(params);
}
function OnProductGridViewItemsPerPageChanged(s, e) {
    var itemsPerPage = s.GetValue();
    var params = 'ChangeItemsPerPage+' + itemsPerPage;
    cbPanelProductGrid.PerformCallback(params);
}

/***** Product Page *****/

// Add To Cart
function OnProductAddToCartClick() {
    cbPanelProductInfo.SetLoadingPanelText('Adding to cart...');
    cbPanelProductInfo.PerformCallback('AddToCart');
}
function OnProductInfoEndCallback(s, e) {
    if (s.cpProductAdded) {
        var msgType = 'confirmation';
        var msgTitle = 'Shopping Cart';
        var msgText = 'Product was successfully added to shopping cart.';
        ShowPopupMessage(msgType, msgTitle, msgText);
    }
}

// Thumbnail Images
function OnProductImageThumbnailClick(imageName) {
    // Set the selected image value to display
    cbPanelProductImage.PerformCallback(imageName);
}
function OnProductImageShowLargerClick(productName, fileName) {
    var url = '/snapins/imageviewer.aspx?img=' + fileName;
    ShowPopupSnapinConsole('Standard', productName, url, 480, 480);
}

// Information Request
function OnProductInfoRequestClick(s, e) {
    // Validate phone and email inputs
    txtRequestPhone.Validate();
    txtRequestEmail.Validate();
    if (txtRequestPhone.GetIsValid() & txtRequestEmail.GetIsValid()) {
        // Start information request to callback
        cbPanelInfoRequest.PerformCallback();
    }
}
function OnProductInforRequestEndCallback(s, e) {
    if (s.cpShowSuccessMsg) {
        var msgType = 'confirmation';
        var msgTitle = 'Information Request Sent';
        var msgText = 'Your information request has been sent. One of our associates to contact you shortly.';
        ShowPopupMessage(msgType, msgTitle, msgText);
    }
}

// Product Review
function OnProductReviewShowClick(productID) {
    var url = '/snapins/itemreview.aspx?id=' + productID;
    ShowPopupSnapinConsole('Standard', 'Product Review', url, 510, 500);
}
function OnProductReviewSubmitClick(s, e) {
    
        cbPanelItemReview.PerformCallback();
}
function OnProductReviewEndCallback(s, e) {
    // Check that review was successfully submitted
    if (s.cpShowSuccessMsg) {
        // Close popup window
        ParentClosePopupSnapinConsole();
    
        // Show success message
        var msgType = 'confirmation';
        var msgTitle = 'Product Review Submitted';
        var msgText = 'Your product review has been successfully submitted and should be posted shortly. Thank you for your feedback.';
        ParentShowPopupMessage(msgType, msgTitle, msgText);
    }

    // Check if an error occurred
    if (s.cpErrorState) {
        // Close popup window
        ParentClosePopupSnapinConsole();

        // Show success message
        var msgType = 'exclamation';
        var msgTitle = 'Error';
        var msgText = 'Thank you for submitting a product review. Unforturnately an error occurred during the process. An administrator has been notified of this error. We appreciate your input.';
        ParentShowPopupMessage(msgType, msgTitle, msgText);
    }
}

// Option Swatches

function OnShowSwatchesClick(groupID) {
    var itemID = hidProductValues.Get("ItemID");

    alert('Item ID: ' + itemID + ' Option group: ' + groupID);
}

// Product Search

function OnProductSearchButtonClick() {
    cbPanelSearch.PerformCallback();
}

// *************************************************************************************
// User Controls  
// *************************************************************************************

// === Contact Form ===
function OnContactFormSubmitRequestClick(s, e) {
    if ((txtName.isValid) && (txtPhone.isValid) && txtEmail.isValid) {
        cbPanelContactForm.SetLoadingPanelText("Sending...");
        cbPanelContactForm.PerformCallback();
    }
}
function OnContactFormEndCallback(s, e) {
    if (s.cpShowSuccessMsg) {
        var msgType = 'confirmation';
        var msgTitle = 'Contact Request';
        var msgText = 'Thank you for submitting your request. One of our associates will be in contact with you.';
        ShowPopupMessage(msgType, msgTitle, msgText);
    }
}


// === Product Filter ===
function OnFilterClick(groupName, filterName, filterID) {
    var params = 'AddFilter+' + groupName + '+' + filterName + '+' + filterID;
    cbPanelProductGrid.SetLoadingPanelText("Refreshing...");
    cbPanelProductGrid.PerformCallback(params);
}
function OnFilterRemoveClick(groupName) {
    var params = 'RemoveFilter+' + groupName;
    cbPanelProductGrid.SetLoadingPanelText("Refreshing...");
    cbPanelProductGrid.PerformCallback(params);
}

// *************************************************************************************************************************
//  Snapins 
// *************************************************************************************************************************

// Window Names
//
// Standard -- used for most popup windows in application
// Message -- used for displaying popup messages can be layers on top of popup windows
//
//

/* Common Popup Snapin Functions */
function ShowPopupSnapinConsole(windowName, headerText, contentUrl, width, height) {
    // Get popup window
    var popupWindow = popupSnapinConsole.GetWindowByName(windowName);

    // Set popup window properties
    popupWindow.SetHeaderText(headerText);
    popupSnapinConsole.SetWindowContentUrl(popupWindow, contentUrl);
    popupSnapinConsole.ShowWindow(popupWindow);
    popupSnapinConsole.SetWindowSize(popupWindow, width, height);
    popupSnapinConsole.UpdateWindowPosition(popupWindow);
}
function ParentShowPopupSnapinConsole(headerText, contentUrl, width, height) {
    // This procedure is used to call the popup console from another popup console window.
    var parentWindow = window.parent;
    var popupWindow = parentWindow.popupSnapinConsole.GetWindowByName(windowName);
    
    // Set popup window properties
    parentWindow.popupSnapinConsole.SetHeaderText(headerText);
    parentWindow.popupSnapinConsole.SetContentUrl(contentUrl);
    parentWindow.popupSnapinConsole.Show();
    parentWindow.popupSnapinConsole.SetSize(width, height);
    parentWindow.popupSnapinConsole.UpdateWindowPosition(popupWindow);
}

function HidePopupSnapinConsole() {
    popupSnapinConsole.Hide();
}
function ParentClosePopupSnapinConsole(windowName) {
    // Use this procedure to close the popup snapin console via the parent form.
    var parentWindow = window.parent;
    var popupWindow = parentWindow.popupSnapinConsole.GetWindowByName(windowName);
    parentWindow.popupSnapinConsole.HideWindow(popupWindow);
}

// Message Snapin
function ShowPopupMessage(msgType, msgTitle, msgText) {
    // This procedure will show the system popup message.
    // The first parameter will be the message type.
    // Message Type Values
    //  exclamation
    //  confirmation
    //  information
    //  question
    //  warning
    // The second parameter will be the message title.
    // The third parameter will be the message text.
    //
    // If the message title is not supplied the default message for the message type will be used.
    var url = '/Snapins/PopupMessage.aspx?type=' + msgType + '&message=' + msgText;
    ShowPopupSnapinConsole('Message', msgTitle, url, 450, 192);
}
function ParentShowPopupMessage(msgType, msgTitle, msgText) {
    // This procedure will show the system popup message.
    // The first parameter will be the message type.
    // Message Type Values
    //  exclamation
    //  confirmation
    //  information
    //  question
    //  warning
    // The second parameter will be the message title.
    // The third parameter will be the message text.
    //
    // If the message title is not supplied the default message for the message type will be used.
    var parentWindow = window.parent;
    var url = '/Snapins/PopupMessage.aspx?type=' + msgType + '&message=' + msgText;
    parentWindow.ShowPopupSnapinConsole('Message', msgTitle, url, 450, 192);
}

// Error Handling
function OnCallbackError(s, e) {
    // Generic callback error function that can be assigned to OnCallbackError clientside event.
    // Display error in snapin popup message.
    e.handled = true;
    ShowPopupMessage('exclamation', 'Error', e.message);
}
function OnSnapinCallbackError(s, e) {
    // Generic callback error function that can be assigned to OnCallbackError clientside event.
    // Display error in snapin popup message.
    //
    // *** IMPORTANT ***
    e.handled = true;
    ParentShowPopupMessage('exclamation', 'Error', e.message);
}


// *************************************************************************************************************************
//  Shopping Cart
// *************************************************************************************************************************

// Shopping Cart Item Summary (Popup Window)
function OnShoppingCartItemDelete(id) {
    if (confirm('Are you sure you to remove this item from your cart?')) {
        gvCartItemSummary.PerformCallback('Delete:' + id);
    }
}
function OnShoppingCartItemUpdate(id) {
    // Create spin edit name
    var spinName = 'Spin' + id;
    // Set control to gain updated quantity value
    var spinControl = eval(spinName);

    // Get updated quantity value
    var newQty = spinControl.GetNumber();
    // Send update back to application
    gvCartItemSummary.PerformCallback('Update:' + id + ':' + newQty);
}
function OnShopCartContinueShoppingClick(s, e) {
    ParentClosePopupSnapinConsole('Standard');
}
function OnShopCartContinueShoppingClick(s, e) {
    ParentClosePopupSnapinConsole('Standard');
}
function OnShopCartCheckOutClick(s, e) {
    // Close window
    ParentClosePopupSnapinConsole('Standard');

    // Move parent window to check out screen
    var parentWindow = window.parent;
    parentWindow.location = "/checkout/addresses.aspx";
}


function OnShoppingCartItemShippingChanged(s, e) {
    var item = s.GetSelectedItem();
    var shippingUpdate = 'ShippingUpdate:' + item.value;
    cbPanelTotal.SetLoadingPanelText('Updating...');
    cbPanelTotal.PerformCallback(shippingUpdate);
}

function OnShoppingCartProcessShippingClick(s, e) {
    cbPanelTotal.SetLoadingPanelText('Processing...');
    cbPanelTotal.PerformCallback('Process');
}

function OnShoppingCartContinueShoppingClick(s, e) {
    cbPanelTotal.SetLoadingPanelText('Redireting..');
    cbPanelTotal.PerformCallback('ContinueShopping');
}

function OnShoppingCartProcessCreditCardClick(s, e) {
    if (txtBillAddress.GetIsValid() & txtBillCity.GetIsValid() &
        txtBillZipCode.GetIsValid() & txtCCName.GetIsValid() &
        txtCCNumber.GetIsValid() & txtCCcvv.GetIsValid()) {
     
        // Attempt to process credit card
        cbPanelPayment.SetLoadingPanelText('Processing Payment... This may take a minute. Do Not hit refresh. ');
        cbPanelPayment.PerformCallback('CreditCard');
    }
}

