function LCostCalc () {
    var obj = $L.object(this);
    
    obj.packageData = '';
    
    obj.init = function( packageData ) {
        obj._packageData = packageData;
        $jq('.UISelectorSelect').bind('change', function(){
            obj._runFilter(this);
        });
        $jq('#bookCalcButton').bind('click', function () {
            $jq('#responseField').hide();
            $jq('#errorField').hide();
            $jq('#shippingNoteField').hide();
            $jq('#calculatingField').show();
            pageCount = parseInt($jq('#fPageCount').val());
            if ( isNaN(pageCount) ) {
                pageCount = 0;
            }
            $jq('#fPageCount').val(pageCount);
            bookCount = parseInt($jq('#fBookCount').val());
            if ( isNaN(bookCount) || bookCount <= 0 ) {
                bookCount = 1;
            }
            $jq('#fBookCount').val(bookCount);
            $jq.ajax({
                url: '/calculators/bookCalcEndpoint.php',
                data: 'fPackageID='+$jq('#fPackageID').val()+'&fPageCount='+pageCount+'&fBookCount='+bookCount+'&fCurrencyCode='+$jq('#fCurrencyCode').val(),
                dataType: 'json',
                success: function (msg) {
                    $jq('#calculatingField').hide();
                    if(msg.severity) {
                        $jq('#errorField').show()
                            .find('.minPage').text(msg.minPages).end()
                            .find('.maxPage').text(msg.maxPages);
                    } else {
                        if(msg.totalCost == msg.costPerUnit) {
                            $jq('#responseField #pageRange').hide();
                        } else {
                            $jq('#responseField #pageRange').show();                            
                        }
                        if (msg.usOnly) {
                            $jq('#shippingNoteField').show();
                        }
                        $jq('#responseField').show()
                            .find('.totalCost').text(msg.totalCost).end()
                            .find('.unitCost').text(msg.costPerUnit);
                    }
                }
            });
            return false;
        });
        if ($jq('#bookCostCalcDialog').length = 0) {
            obj.initOnOpen();
        }
    };
    
    obj.initOnOpen = function() {
        obj._runFilter( $jq('select#paperType').get(0) );
    };

    obj._runFilter = function (element) {
        var filterInfo = obj._getFilterInfo($jq(element).find('option:selected'));
        if (filterInfo.filter) {
            var filterParent = $jq(element).parents('.UISelector').attr('id');
            var optionNdx = $jq('.UISelector.filterOption').index($jq(element).parents('.UISelector').get(0));
            obj._filterOptions(filterInfo.filter, optionNdx, filterParent);
        }
        obj._setPackageId();
    };
    
    obj._filterOptions = function( filterClass, optionNdx, parentId ) {
        var filterClasses = '';
        var filterOptions = $jq('.UISelector.filterOption');
        for (var i=0; i <= optionNdx; i++) {
            var filterInfo = obj._getFilterInfo(filterOptions.eq(i).find('li:has(input:checked), option:selected'));
            filterClasses = filterClasses + '.' +filterInfo.filter;
        };
        
        for (var i=optionNdx+1; i < filterOptions.length; i++) {
            var optionId = filterOptions.eq(i).attr('id')
            $jq('#'+optionId+'.UISelector .UISelectorSelectItem').each(function(){
                obj._disableSelectOption($jq(this));
            });
            var optionIdArray = optionId.split('_');
            $jq('#'+optionIdArray[0]+'Hidden_'+optionIdArray[1]+'.UISelector .UISelectorSelectItem'+filterClasses).each(function(){
                obj._enableSelectOption($jq(this));
            });
            
            var filterInfo = obj._getFilterInfo(filterOptions.eq(i).find('li:has(input:checked), option:selected'));
            filterClasses = filterClasses + '.' +filterInfo.filter;
        };

    };
    
    obj._setPackageId = function () {
        var paperType = $jq('select[name=paperType]').val();
        var bookType = $jq('select[name=bookType]').val();
        var interiorInkColor = $jq('select[name=interiorInkColor]').val();
        var trimSize = $jq('select[name=trimSize]').val();
        var bindingType = $jq('select[name=bindingType]').val();
        var pd = jQuery.grep(obj._packageData, function(n, i){
          return (n.interiorPaperCoating == paperType && n.coverType == bookType && n.interiorInkColor == interiorInkColor && n.trimSizeId == trimSize && n.bindingType == bindingType);
        });

        if (pd.length > 0) {
            $jq('#fPackageID').val(pd[0].packageId);
        } else {
            $jq('#fPackageID').val('-1');
        }
    };
    
    obj._disableSelectOption = function ( element ) {
        var disabledID = element.parent().attr('id') + 'Hidden';
        element.appendTo('#'+disabledID);
    };
    
    obj._enableSelectOption = function ( element ) {
        var enabledID = element.parent().attr('id').replace('Hidden', '');
        element.appendTo('#'+enabledID);
    };
    
    obj._getFilterInfo = function ( element ) {
        var classes = element.attr('class').split(' ');
        var filter = false;
        var fields = Array();
        jQuery.each(classes, function (i, val) {
            if(val.substr(0,4) == 'fltr') {
                filter = val.substr(5);
            }
            if(val.substr(0,5) == 'field') {
                fields.push(val.substr(6));
            }
        });
        
        return {filter: filter, fields: fields};
    };

    return obj;
};
$L.add('LCostCalc', new LCostCalc());
function LBookCostCalculator ( )
{
    var obj = $L.object( this );

    // public methods

    obj.getMaxPages = function() {
        return obj._package ? obj._package.maxPages : 0;
    }

    obj.getMinPages = function() {
        return obj._package ? obj._package.minPages : 0;
    }

    obj.getCost = function() {
        return obj._package ? obj._getCost() : 0;
    }

    obj.getCurrencyCode = function() {
        return obj._package ? obj._getCurrencyCode() : '?';
    }

    obj.setPackage = function(package) {
        obj._package = package;
        obj._updatePages();
    }

    obj.getPages = function() {
        return obj._pages;
    }

    obj.setPages = function(pages) {
        obj._pages = pages;
        obj._updatePages();
    }

    // private methods

    obj._getCost = function() {
        if (obj._package.hasDist == true) {
            return (obj._package.pricing.basePrice + (obj._pages * obj._package.pricing.perPagePrice)) * 2;
        } else {
            return obj._package.pricing.basePrice + (obj._pages * obj._package.pricing.perPagePrice)
        }
    }

    obj._getCurrencyCode = function() {
        return obj._package.pricing.currencyCode;
    }

    obj._updatePages = function() {
        if (obj._package) {
            if (obj._pages < obj._package.minPages) {
                obj._pages = obj._package.minPages;
            }
            if (obj._pages > obj._package.maxPages) {
                obj._pages = obj._package.maxPages;
            }
        }
    }

    // private member variables

    obj._package = null;
    obj._pages = 0;

    return obj;
}
function LBookCostCalculatorView()
{
    var obj = $L.object(this);

    obj.setCurrencies = function(currencies) {
        obj._currencies = currencies;
    }

    obj.setPackage = function(package) {
        obj._calculator.setPackage(package);
        obj._update();
    }

    obj.setPages = function(pages) {
        obj._calculator.setPages(pages);
        obj._update();
    }

    // private methods

    obj._update = function() {
        obj._updateCost();
        obj._updateMaxPages();
        obj._updateMinPages();
        obj._updatePages();
    }

    obj._updateCost = function() {
        $('calculatedPriceValue').innerHTML = obj._getCurrencySymbol() + parseFloat(obj._calculator.getCost()).toFixed(2);
    }

    obj._getCurrencySymbol = function() {
        var code = obj._calculator.getCurrencyCode();
        var symbol = '?';
        $jq.each(obj._currencies, function(i, currency) {
            if (currency.code == code) {
                symbol = currency.symbol;
                return false; // this breaks the loop
            }
        });
        return symbol;
    }

    obj._getCurrency = function() {
        return obj._calculator.getCurrency();
    }

    obj._updateMaxPages = function() {
        $('maximumPageCountValue').innerHTML = obj._calculator.getMaxPages();
    }

    obj._updateMinPages = function() {
        $('minimumPageCountValue').innerHTML = obj._calculator.getMinPages();
    }

    obj._updatePages = function() {
        $('inputPageCount').value = obj._calculator.getPages();
    }

    // private member variables

    obj._calculator = new LBookCostCalculator();
    obj._currencies = new Array();

    return obj;
}

var luluBookCostCalculatorView = new LBookCostCalculatorView();
/**
 * LPricingCalculator.js
 *
 * @author Karim Nassar
 * @copyright Lulu Enterprises, 10 April, 2008
 **/

function LPricingCalculator ( )
{
    var obj = $L.object( this );

    obj.calculatePriceByRevenue = function( product ) {
        obj.initWarnings( product );

        var totalFees = 0;
        if ((product.revenue < 0) || isNaN(product.revenue)) {
            product.revenue = 0;
            product.invalidRevenue = true;
            return product;
        }

        if (product.revenue > 999999.99) {
            product.invalidPrice = true;
            product.price = 999999.99;
            product = obj.calculateRevenueByPrice(product);
            product.invalidRevenue = true;
            return product;
        }


        if (product.revenue == 0) {
            product.zeroRevenue = true;
            for ( var f=0; f < product.royaltyFees.length; f++ ) {
                product.royaltyFees[f].value = 0;
            }
        } else {
            if ( product.royaltyFees.length > 0 ) {
                 for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    var fee = obj.calculateFee( product.revenue, product.royaltyFees[f].percent, product.royaltyFees[f].minimum );
                    product.royaltyFees[f].value = fee;
                    totalFees += fee;
                 }
            }
        }
        product.price = parseFloat(product.revenue + product.manufacturingCost + totalFees);
        if (product.price > 999999.99) {
            product.invalidPrice = true;
            product.price = 999999.99;
            return obj.calculateRevenueByPrice(product);
        }

        return product;
    }

    obj.calculateRetailPriceByRevenue = function( product ) {
        obj.initWarnings( product );

        var totalFees = 0;
        if ((product.retailRevenue < 0) || isNaN(product.retailRevenue)) {
            product.retailRevenue = 0;
            product.invalidRevenue = true;
        }
        if (product.retailRevenue > 999999.99) {
            product.invalidPrice = true;
            product.price = 999999.99;
            return obj.calculateRetailRevenueByPrice(product);
        }


        if (product.retailRevenue == 0) {
            for ( var f=0; f < product.royaltyFees.length; f++ ) {
                product.royaltyFees[f].retailValue = 0;
            }
            product.zeroRevenue = true;
        } else {
            if ( product.royaltyFees.length > 0 ) {
                 for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    var fee = obj.calculateFee( product.retailRevenue, product.royaltyFees[f].percent, product.royaltyFees[f].minimum );
                    product.royaltyFees[f].retailValue = fee;
                    totalFees += fee;
                 }
            }
        }
        product.price = 2 * parseFloat(product.retailRevenue + product.retailManufacturingCost + totalFees);
        if (product.price < product.manufacturingCost) {
            product.price = product.manufacturingCost;
        }
        product.retailMarkup = obj.calculateRetailMarkup( product.price, (product.retailRevenue + product.retailManufacturingCost + totalFees) );
        if (product.price > 999999.99) {
            product.invalidPrice = true;
            product.price = 999999.99;
            return obj.calculateRetailRevenueByPrice(product);
        }
        obj._backCalculateNonRetailValues( product );
        return product;
    }

    obj.calculateRevenueByPrice = function( product ) {
        var totalMarkup = obj.calculateMarkup( product.price, product.manufacturingCost );
        var totalMinFees = obj.calculateTotalMinFees( product.royaltyFees );
        var totalFeePercentage = obj.calculateTotalFeePercentage( product.royaltyFees );
        obj.initWarnings( product );

        if ((product.price < 0) || isNaN(product.price)) {
            product.price = product.manufacturingCost;
            product.invalidPrice = true;
        } else if (product.price < product.manufacturingCost) {
            product.negativeMarkup = true;
        }

        if (product.price > 999999.99) {
            product.invalidPrice = true;
            product.price = 999999.99;
        }
        if ( totalMarkup < 0 ) {
            product.revenue = 0;
            product.zeroRevenue = true;
            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    product.royaltyFees[f].value = 0;
                }
            }
            product.price = product.manufacturingCost;
        } else if ( (totalMarkup >= 0) && (totalMarkup < totalMinFees) ) {
            product.revenue = 0;
            if ( product._firstLoad == false ) {
                product.zeroRevenue = true;
            }
            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    product.royaltyFees[f].value = 0;
                }
            }
            if (totalMarkup > 0) {
                product.insufficientMarkup = true;
            }
            product.price = product.manufacturingCost;
        } else {
            var tempRevenue = totalMarkup / (1 + totalFeePercentage);
            var totalFee = 0.0;
            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    var _fee = obj.calculateFee( tempRevenue, product.royaltyFees[f].percent, product.royaltyFees[f].minimum );
                    product.royaltyFees[f].value = _fee;
                    totalFee += _fee;
                }
            }
            product.revenue = totalMarkup - totalFee;
        }
        if (product.retailRevenue > 999999.99) {
            product.invalidRevenue = true;
            product.retailRevenue = 999999.99;
        }
        product._firstLoad = false;
        return product;
    };

    obj.calculateRetailRevenueByPrice = function( product, dontValidate ) {
        var totalMinFees = obj.calculateTotalMinFees( product.royaltyFees );
        var totalFeePercentage = obj.calculateTotalFeePercentage( product.royaltyFees );

        var calculatedRetailRevenue = ((product.price / 2) - product.retailManufacturingCost)/ (1 + totalFeePercentage);

        var totalFee = 0.0;
        if ( product.royaltyFees.length > 0 ) {
            for ( var f=0; f < product.royaltyFees.length; f++ ) {
                var _fee = obj.calculateFee( calculatedRetailRevenue, product.royaltyFees[f].percent, product.royaltyFees[f].minimum );
                product.royaltyFees[f].retailValue = _fee;
                totalFee += _fee;
            }
        }
        var differenceBetweenTotalAndMinFees = totalMinFees - totalFee;

        if ( (totalMinFees > totalFee) && (calculatedRetailRevenue > differenceBetweenTotalAndMinFees) ) {
                product.retailMarkup = product.price/2;
                if ( product.royaltyFees.length > 0 ) {
                    for ( var f=0; f < product.royaltyFees.length; f++ ) {
                        var _fee = obj.calculateFee( calculatedRetailRevenue, product.royaltyFees[f].percent, product.royaltyFees[f].minimum );
                        product.royaltyFees[f].retailValue = product.royaltyFees[f].minimum;
                    }
                }
                product.retailRevenue = calculatedRetailRevenue - totalMinFees;
        } else if ( (totalMinFees == totalFee) || (calculatedRetailRevenue <= differenceBetweenTotalAndMinFees) ) {
            product.retailMarkup = product.retailManufacturingCost;

            product.retailMarkup = (product.retailMarkup < 0) ? 0.0 : product.retailMarkup;
            product.retailRevenue = 0;
            product.price = product.retailManufacturingCost + product.retailMarkup;

            if ( product._firstLoad == false ) {
                product.zeroRevenue = true;
                product.insufficientMarkup = true;
            }

            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    product.royaltyFees[f].retailValue = 0;
                }
            }
        } else {
            product.retailMarkup = product.price/2;
            product.retailRevenue = calculatedRetailRevenue;
        }
        if (typeof dontValidate == 'undefined') {
            product = obj.validateRetailValues(product);
        }
        return product;
    };

    obj.validateRetailValues = function( product ) {

        if ((product.price < 0) || isNaN(product.price)) {
            product.price = product.manufacturingCost;
            product.invalidPrice = true;
            product.zeroRevenue = true;
            product = obj.calculateRetailRevenueByPrice( product, true );
        } else if (product.price < product.manufacturingCost) {
            product.negativeMarkup = true;
            product.zeroRevenue = true;
        }

        if (product.price > 999999.99) {
            product.invalidPrice = true;
            product.price = 999999.99;
            product = obj.calculateRetailRevenueByPrice( product, true );
        }

        var totalMinFees = obj.calculateTotalMinFees( product.royaltyFees );
        var minPrice = product.manufacturingCost + totalMinFees + 0.01;
        if ( product.price < minPrice ) {
            product.retailRevenue = 0;
            if ( product._firstLoad == false ) {
                product.zeroRevenue = true;
            }
            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    product.royaltyFees[f].retailValue = 0;
                }
            }
            product.price = product.retailManufacturingCost + product.retailMarkup;
            product = obj.calculateRetailRevenueByPrice( product, true );
        }

        if (product.retailRevenue > 999999.99) {
            product.invalidRevenue = true;
            product.retailRevenue = 999999.99;
            product = obj.calculateRetailPriceByRevenue( product, true );
        }

        obj._backCalculateNonRetailValues( product );

        product._firstLoad = false;
        return product;
    };


    obj._backCalculateNonRetailValues = function( product ) {
        var totalMarkup = obj.calculateMarkup( product.price, product.manufacturingCost );
        var totalMinFees = obj.calculateTotalMinFees( product.royaltyFees );
        var totalFeePercentage = obj.calculateTotalFeePercentage( product.royaltyFees );

        if ( totalMarkup <= 0 ) {
            product.revenue = 0;
            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    product.royaltyFees[f].value = 0;
                }
            }
            product.price = product.manufacturingCost;
        } else if ( (totalMarkup > 0) && (totalMarkup < totalMinFees) ) {
            product.revenue = 0;
            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    product.royaltyFees[f].value = 0;
                }
            }
            product.price = product.manufacturingCost;
        } else {
            var tempRevenue = totalMarkup / (1 + totalFeePercentage);
            var totalFees = 0.0;
            if ( product.royaltyFees.length > 0 ) {
                for ( var f=0; f < product.royaltyFees.length; f++ ) {
                    var _fee = obj.calculateFee( tempRevenue, product.royaltyFees[f].percent, product.royaltyFees[f].minimum );
                    product.royaltyFees[f].value = _fee;
                    totalFees += _fee;
                }
            }
            if ( totalFees+tempRevenue <= totalMarkup ) {
                product.revenue = tempRevenue;
            } else {
                product.revenue = totalMarkup-totalFees;
            }

        }
    };

    obj.calculateFee = function ( revenue, feePercent, feeMin ) {
        var rev = parseFloat(revenue);
        var perc = parseFloat(feePercent);
        var min = parseFloat(feeMin);

        var fee = ( rev * (1 + perc)) - rev;
        if ( fee < min ) {
            fee = min;
        }
        return fee;
    };

    obj.calculateMarkup = function ( price, cost ) {
        var markup = parseFloat(price) - parseFloat(cost);
        if (markup < 0) {
            markup = 0;
        }
        return markup;
    };

    obj.calculateRetailMarkup = function ( price, totalMarkup ) {
        var markup = parseFloat(price) - parseFloat(totalMarkup);
        if (markup < 0) {
            markup = 0;
        }
        return markup;
    };

    obj.calculateTotalMinFees = function( fees ) {
        var tot = 0;
        for ( var i=0; i<fees.length; ++i ) {
            if ( typeof fees[i].minimum != 'undefined') {
                tot += parseFloat( fees[i].minimum );
            }
        }
        return tot;
    };

    obj.calculateTotalFeePercentage = function( fees ) {
        var tot = 0;
        for ( var i=0; i<fees.length; ++i ) {
            if ( typeof fees[i].percent != 'undefined') {
                tot += parseFloat( fees[i].percent );
            }
        }
        return tot;
    };

    obj.initWarnings = function( product ) {
        product.invalidPrice = false;
        product.negativeMarkup = false;
        product.insufficientMarkup = false;
        product.invalidRevenue = false;
        product.zeroRevenue = false;
    };

    return obj;
}
/**
 * LPricingEditorManager.js
 *
 * @author Karim Nassar
 * @copyright Lulu Enterprises, 10 April, 2008
 **/

function LPricingEditorManager ( calculator )
{
    var obj = $L.object( this, new LManager( 'LPricingEditor' ) );

    obj._calculator = calculator;

    /****************************************************
     * ui methods
     ****************************************************/

    obj.startedEditing = function( fieldClass ) {
        for ( var i=0; i < obj._selected.length; i++ ) {
            obj._selected[i]._activeField = fieldClass;
            obj._$toggleFieldHighlights( obj._selected[i] );
        }
        return obj;
    };

    obj.changed = function( fieldClass ) {
        for ( var i=0; i < obj._selected.length; i++ ) {
            obj._selected[i]._activeField = '';
            obj._$updateDOM( obj._selected[i] );
        }
        return obj;
    };

    obj.stoppedEditing = function( fieldClass ) {
        for ( var i=0; i < obj._selected.length; i++ ) {
            if ( obj._selected[i]._activeField == fieldClass ) {
                obj._selected[i]._activeField = '';
            }
            obj._$toggleFieldHighlights( obj._selected[i] );
        }
        return obj;
    };

    obj.toggleDisabled = function ( ) {
        for ( var i=0; i < obj._selected.length; i++ ) {
            obj._$toggleDisabledClassOn( obj._selected[i].id );
        }
        return obj;
    };

    /****************************************************
     * calculations
     ****************************************************/

    obj.recalculateByRevenue = function() {
        for ( var i=0; i < obj._selected.length; i++ ) {
            var sel = obj._selected[i];
            obj._calculator.initWarnings( sel );
            if (sel.isRetail) {
                sel.retailRevenue = obj._$getRetailRevenueValueById( sel.id );
                sel = obj._calculator.calculateRetailPriceByRevenue( sel );
            } else {
                sel.revenue = obj._$getRevenueValueById( sel.id );
                sel = obj._calculator.calculatePriceByRevenue( sel );
            }
            obj._$updateDOM( sel );
        }
        return obj;
    };

    obj.recalculateByPrice = function() {
        for ( var i=0; i < obj._selected.length; i++ ) {
            var sel = obj._selected[i];
            obj._calculator.initWarnings( sel );
            sel.price = obj._$getPriceValueById( sel.id );
            if (sel.isRetail) {
                sel = obj._calculator.calculateRetailRevenueByPrice( sel );
            } else {
                sel = obj._calculator.calculateRevenueByPrice( sel );
            }
            obj._$updateDOM( sel );
        }
        return obj;
    };

    obj.performInitialCalculation = function() {

        for ( var i=0; i < obj._selected.length; i++ ) {
            var sel = obj._selected[i];
            if (sel.isRetail) {
                sel = obj._calculator.calculateRetailPriceByRevenue( sel );
            } else {
                sel = obj._calculator.calculateRevenueByPrice( sel );
            }
            obj._$updateDOM(sel);
        }
        return obj;
    };

    /****************************************************
     * Private Methods
     ****************************************************/

    obj._validateValues = function( element ) {
        if (parseFloat(element.price) < 0 || parseFloat(element.price) > 999999) {
            element.invalidPrice = true;
        } else {
            element.invalidPrice = false;
        }
        if (element.price < element.manufacturingCost) {
            element.negativeMarkup = true;
        } else {
            element.negativeMarkup = false;
        }
        var markup = element.price - element.manufacturingCost;
        var minFees = obj._calculator.calculateTotalMinFees(element.royaltyFees);

        if ((markup > 0) && (markup < minFees)) {
            element.insufficientMarkup = true;
        } else {
            element.insufficientMarkup = false;
        }
    };

    obj._checkDataBeforeAdd = function( element ) {
        obj._validateValues( element );
        element._firstLoad = true;
        return element;
    };

    obj._formatValue = function( value ) {
        var val = Number( value );
        if ( isNaN( val ) || val < 0 ) {
            val = Number( 0 );
        }
        return val.toFixed(2);
    };

    obj._$toggleDisabledClassOn = function( id ) {
        var element = $jq( '#' + id );
        if ( element.hasClass('LPricingEditorDisabled') ) {
            element.removeClass('LPricingEditorDisabled');
        } else {
            element.addClass('LPricingEditorDisabled');
        }
    };

    obj._$getRevenueValueById = function( id ) {
        return parseFloat( $jq( '#'+id+' input.revenueField').val().replace( /[a-zA-Z]/, '') );
    };

    obj._$getRetailRevenueValueById = function( id ) {
        return parseFloat( $jq( '#'+id+' input.retailRevenueField').val().replace( /[a-zA-Z]/, '') );
    };

    obj._$getPriceValueById = function( id ) {
        return parseFloat( $jq( '#'+id+' input.priceField').val().replace( /[a-zA-Z]/, '') );
    };

    obj._$updateDOM = function( element ) {

        var view = $jq( '#'+element.id );

        var cost = $jq( '#'+element.id+' .manufacturingCost');
        cost.text(obj._formatValue(cost.text()));
        if (element.isRetail) {
            var cost = $jq( '#'+element.id+' .retailManufacturingCost');
            cost.text(obj._formatValue(cost.text()));
        }

        if ( element._activeField != 'priceField' ) {
            view.find('input.priceField').val( obj._formatValue( element.price ) );
        }
        if (element.isRetail) {
            obj._$updateRetailFields( view, element );
        } else {
            obj._$updateNonRetailFields( view, element );
        }
        setTimeout( function() { obj._$updateWarnings( element ); }, 500);
    };

    obj._$updateNonRetailFields = function( view, element ) {
        if ( element._activeField != 'revenueField' ) {
            view.find('input.revenueField').val( obj._formatValue( element.revenue ) );
        }
        for ( var f=0; f < element.royaltyFees.length; f++ ) {
            $jq('#feeAmount_'+f+'_'+element.id).text( obj._formatValue( element.royaltyFees[f].value ) );
        }
    };

    obj._$updateRetailFields = function( view, element ) {
        if ( element._activeField != 'retailRevenueField' ) {
            view.find('input.retailRevenueField').val( obj._formatValue( element.retailRevenue ) );
        }
        view.find('input.revenueField').val( obj._formatValue( element.revenue ) );
        view.find('input.nonRetailPriceField').val( obj._formatValue( element.price ) );
        for ( var f=0; f < element.royaltyFees.length; f++ ) {
            $jq('#retailFeeAmount_'+f+'_'+element.id).text( obj._formatValue( element.royaltyFees[f].retailValue ) );
            $jq('#feeAmount_'+f+'_'+element.id).text( obj._formatValue( element.royaltyFees[f].value ) );
        }
        view.find('.retailMarkup').text(obj._formatValue(element.retailMarkup));
        view.find('span.revenueField').text(obj._formatValue(element.revenue));
        view.find('.priceField.displayField').text(obj._formatValue(element.price));
    };

    obj._$updateWarnings = function( element ) {
        var view = $jq('#'+element.id);
        if ( element.negativeMarkup ) {
            view.find('.negativeMarkupWarning').css({display:'block'});
        } else {
            view.find('.negativeMarkupWarning').css({display:'none'});
        }
        if ( element.insufficientMarkup ) {
            view.find('.insufficientMarkupWarning').css({display:'block'});
        } else {
            view.find('.insufficientMarkupWarning').css({display:'none'});
        }
        if ( element.invalidPrice ) {
            view.find('.invalidPriceWarning').css({display:'block'});
        } else {
            view.find('.invalidPriceWarning').css({display:'none'});
        }
        if ( element.invalidRevenue ) {
            view.find('.invalidRevenueWarning').css({display:'block'});
        } else {
            view.find('.invalidRevenueWarning').css({display:'none'});
        }
        if ( element.zeroRevenue ) {
            view.find('.zeroRevenueWarning').css({display:'block'});
        } else {
            view.find('.zeroRevenueWarning').css({display:'none'});
        }
    };

    obj._$toggleFieldHighlights = function( element ) {
        var view = $jq( '#'+element.id );
        view.find('.editableField' ).removeClass( 'notCurrentlyEditing' ).removeClass( 'currentlyEditing' );
        if ( element._activeField != '' ) {
            view.find('.editableField' ).addClass( 'notCurrentlyEditing' );
            view.find( 'input.'+element._activeField ).removeClass( 'notCurrentlyEditing' ).addClass( 'currentlyEditing' ).select();
        }
    };

    obj._$initDOM = function(element) {

    };

    return obj;
}
$L.addManager( 'PricingEditorManager', new LPricingEditorManager( new LPricingCalculator() ) );
$jq( 'document' ).ready(function(){$L.PricingEditorManager.select('*').performInitialCalculation();});
