﻿/*
Store 1.0.0 - Javascript Store class. Collection of general properties/methods for use with AspDotNetStorefront.

Copyright (c) Aydus (www.aydus.com)

Date: 5/29/2010

ChangeLog:
*/

function Store() {
    //this.property = value;
    
    // Returns the current page name including any extension e.g. default.aspx.
    this.GetPageName = function() {
        var path = window.location.pathname;
        return path.substring(path.lastIndexOf('/') + 1);
    }

    // Returns the current page type (Product, Category, Department, Manufacturer, Section)
    this.GetPageType = function() {

        var pageType = "";
        var pagePrefix = this.GetPageName().slice(0, 2);
        switch (pagePrefix) {
            case "p-":
                pageType = "Product";
                break;
            case "c-":
                pageType = "Category";
                break;
            case "d-":
                pageType = "Department";
                break;
            case "m-":
                pageType = "Manufacturer";
                break;
            case "s-":
                pageType = "Section";
                break;
        }
        return pageType;
    }

    // Adds class names to various core controls/objects. AspDotNetStorefront does not contain many classes which makes it difficult to style pages.
    // Note that this is a patch.  Classes are added after the DOM is loaded so styles are applied late and can cause page flickering.
    this.SetClassHelpers = function() {

        // Buttons.
        $("input[id$=ctrlLogin_LoginButton]").addClass('Helper LoginButton');
        $("input[id$=UserNameContainerID_btnRequestNewPassword]").addClass('Helper RequestNewPasswordButton');
        $("input[id$=PageContent_btnRequestEstimates]").addClass('Helper GetEstimatesButton');
        $("input[id$=PageContent_btnSignInAndCheckout]").addClass('Helper SignInAndCheckoutButton');
        $("input[id$=PageContent_RegisterAndCheckoutButton]").addClass('Helper RegisterAndCheckoutButton');
        $("input[id$=PageContent_btnCheckOut]").addClass('Helper ContinueCheckoutFinalButton');
        $("input[id$=PageContent_btnRecalcShipping]").addClass('Helper GetShippingOptionsButton');

        // Product.
        $("input[id^=TextOption]").addClass('TextOption');

        // Order summary.
        $("#PageWrapper.checkout1 div[id$=pnlOrderSummary]").addClass('Helper OrderSummary');

        // Checkout1 Page
        $("#PageWrapper.checkout1 img[id$=billinginfo_gif]").addClass('Helper BillingInfoGif');
        $("#PageWrapper.checkout1 img[id$=shippinginfo_gif]").addClass('Helper ShippingInfoGif');
        $("#PageWrapper.checkout1 img[id$=accountinfo_gif]").addClass('Helper AccountInfoGif');
        $("#PageWrapper.checkout1 input[id$=ctrlBillingAddress_Company]").addClass('Helper ctrlShippingAddress_Company');
        $("#PageWrapper.checkout1 input[id$=ctrlShippingAddress_Company]").addClass('Helper ctrlShippingAddress_Company');
        $("#PageWrapper.checkout1 input[id$=ctrlBillingAddress_City]").addClass('Helper ctrlShippingAddress_City');
        $("#PageWrapper.checkout1 input[id$=ctrlShippingAddress_City]").addClass('Helper ctrlShippingAddress_City');
        $("#PageWrapper.checkout1 select[id$=ctrlBillingAddress_Country]").addClass('Helper ctrlShippingAddress_Country');
        $("#PageWrapper.checkout1 select[id$=ctrlShippingAddress_Country]").addClass('Helper ctrlShippingAddress_Country');
        $("#PageWrapper.checkout1 span[id$=Signin]").removeAttr('style');
        $("#PageWrapper.checkout1 span[id$=lblRecalcShippiingMsg]").removeAttr('style').addClass('Helper RecalcShippingMsg');
    }

    // Return the numeric price given a size/color attribute. e.g. "Colored Black[+6.00]," = 6.00
    this.GetPriceFromModifier = function(modifier) {

        var price = 0;
        var re = /\[(\+?-?\d+)\.?\d*\]/;
        var result = modifier.match(re);
        if (result != null && result.length >= 2) {
            price = result[1];
        }
        return parseFloat(price);
    }

    // Set client side required field validator on variant drop down list. Expects the list to have a named default option.
    // e.g. <option selected="selected" value="0">Length</option>
    // Prompts for value 'Length'.
    this.RequiresVariantOption = function() {

        returnValue = true;

        var oVariants = document.getElementById('variants');
        if (oVariants != null) {
            if (oVariants.selectedIndex == 0) {
                returnValue = false;
                msg = 'Please select a ' + oVariants.options[0].text;
                alert(msg);
            }
        }
        return returnValue;
    }

    // Display shipping option in admin (hide for customer).
    // Parameter is end of shipping option id. AspDotNetStorefront does not have static id or classname for selector.
    this.DisplayAdminOnlyShippingOption = function(endOfShippingID) {
        // If element is within an iframe then we know it's an admin phone order. Display the shipping options.
        // If element is not within iframe then it's the front-end site. Hide the shipping option.
        if (this.GetPageName() == 'checkout1.aspx' || this.GetPageName() == 'checkoutshipping.aspx') {
            if (!$('iframe', parent.document).length) {
                $option = $('input[id$=' + endOfShippingID + ']');
                if ($option.length && $option.parent().parent().length) {
                    $option.parent().parent().hide();
                }
            }
        }
    }

    // Display payment option in admin (hide for customer).
    // Parameter is end of payment option id. AspDotNetStorefront does not have static id or classname for selector.
    this.DisplayAdminOnlyPaymentOption = function(endOfPaymentID) {
        // If element is within an iframe then we know it's an admin phone order. Display the payment options.
        // If element is not within iframe then it's the front-end site. Hide the payment option.
        if (this.GetPageName() == 'checkout1.aspx' || this.GetPageName() == 'checkoutpayment.aspx') {
            if (!$('iframe', parent.document).length) {
                $option = $('input[id$=' + endOfPaymentID + ']');
                if ($option.length && $option.parent().parent().length) {
                    $option.parent().parent().hide();
                }
            }
        }
    }

    this.Test = function() {
        alert('Hello World!');
    }
}