﻿/*
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).toLowerCase();
    }

    // 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() {

        // Account.aspx
        $j('#PageWrapper.account input[type=text]').addClass('Helper Text');
        $j('#PageWrapper.account input[type=password]').addClass('Helper Password');
        
        // CreateAccount.aspx
        $j('#PageWrapper.createaccount input[type=text]').addClass('Helper Text');
        $j('#PageWrapper.createaccount input[type=password]').addClass('Helper Password');
        
        // EditAddress.aspx
        $j('#PageWrapper.editaddress input[type=text]').addClass('Helper Text');
        $j('#PageWrapper.editaddress input[type=password]').addClass('Helper Password');
        
        // SelectAddress.aspx
        $j('#PageWrapper.selectaddress input[type=text]').addClass('Helper Text');
        $j('#PageWrapper.selectaddress input[type=password]').addClass('Helper Password');
    }

    // 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 COD payment option in admin (hide for customer).
    // Parameter is end of paymention 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 (!$('iframe', parent.document).length) {
            $('input[id$=' + endOfPaymentID + ']').parent().parent().hide();
        }
    }

    this.LogMessageAJAX = function(errorMessage, sourceFileName, methodName, errorData) {

        var params = new Array(4);
        for (i = 0; i < params.length; i++) params[i] = new Array(2);

        params[0][0] = 'errorMessage';
        params[0][1] = errorMessage;
        params[1][0] = 'sourceFileName';
        params[1][1] = sourceFileName;
        params[2][0] = 'methodName';
        params[2][1] = methodName;
        params[3][0] = 'errorData';
        params[3][1] = errorData;

        $.ajax({
            type: 'POST',
            url: 'ajaxError.asmx/LogMessage',
            //dataType: 'text'
            dataType: 'xml',
            //data: 'key1=value1&key2=value2',
            //Variable names are case sensitive.
            //Encode strings as required.
            data: _helper.CreateJQueryAJAXParams(params),
            success: function(data) {
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            }
        });
        return false;
    }

    this.Test = function() {
        alert('Hello World!');
    }
}
