function JCCalculator(arModificators) {this.init(arModificators);}
JCCalculator.prototype.init = function(arModificators)
{
        var _this = this;
        this.arModificators = [];
        if(typeof(arModificators) != 'object')
                return false;

        var index = 0;
        var l = arModificators.length;
        for(var i = 0; i < l; i++)
        {
                if(typeof(arModificators[i]) == 'object'
                        && typeof(arModificators[i].getPrice) == 'function')
                {
                        this.arModificators[index] = arModificators[i];
                        index++;
                }
        }
}
JCCalculator.prototype.calculate = function()
{
        var price = 0;
        var l = this.arModificators.length;
        for(var i = 0; i < l; i++)
        {
                var modifier = this.arModificators[i];
                price += modifier.getPrice();
        }
        return Math.round(price * 100) / 100;
}
var Modifer = {

        base: function(widthId, heightId, obData)
        {
                if(typeof(obData.width) != 'object' || typeof(obData.height) != 'object' || typeof(obData.map) != 'object')
                        return alert('bad input data for base modifier');

                var width = new FormElement.JCTextField(widthId);
                var height = new FormElement.JCTextField(heightId);
                this.getPrice = function()
                {
                        var w = parseInt(width.getValue());
                        var h = parseInt(height.getValue());
                        if(!w || !h) return 0;

                        __findIndex = function(obData, v)
                        {
                                var l = obData.length;
                                var resultIndex = 0;
                                for(var i = 0; i < l; i++)
                                {
                                        if(obData[i] > v) break;
                                        resultIndex = i;
                                }
                                return resultIndex;
                        }
                        // find width column
                        var widthColumn = __findIndex(obData.width, w);
                        // find height cell
                        var heightCell = __findIndex(obData.height, h);
                        return (obData.map[heightCell] && obData.map[heightCell][widthColumn]) ?
                                (obData.map[heightCell][widthColumn] * (obData.factor ? obData.factor : 1)) :
                                 0;
                }
        },
        priceCheckBox: function(boxId, obData)
        {
                if(typeof(obData.price) != 'number')
                        return alert('bad input data for: ' + boxId);

                var obElement = new FormElement.JCCheckBox(boxId);
                this.getPrice = function()
                {
                        return  (obElement.getValue() ? (obData.price * (obData.factor ? obData.factor : 1)) : 0);
                }
        },
        percentComboBox: function(boxId, obData, obBaseModifier)
        {
                if(typeof(obData) != 'object' || obData.length <= 0)
                        return alert('bad input data for: ' + boxId);
                else if(typeof(obBaseModifier) != 'object')
                        return alert('empty base modifier');

                var arBoxValues = []; var arPercents = [];
                for(var i = 0; i < obData.length; i++)
                {
                        if(typeof(obData[i][1]) != 'number')
                                return alert('bad input data for: ' + boxId);

                        arBoxValues[i] = {'value': i,'text': obData[i][0] + ' [' + obData[i][1] + '%]'};
                        arPercents[i] = obData[i][1];
                }
                var obElement = new FormElement.JCComboBox(boxId, arBoxValues);
                this.getPrice = function()
                {
                        return obBaseModifier.getPrice() * arPercents[obElement.getValue()] / 100;
                }
        },
        priceComboBox: function(boxId, obData)
        {
                if(typeof(obData.box) != 'object' || obData.box.length <= 0)
                        return alert('bad input data for: ' + boxId);
                var arBoxValues = []; var arPrices = [];
                for(var i = 0; i < obData.box.length; i++)
                {
                        arBoxValues[i] = {'value': i,'text': obData.box[i][0]};
                        arPrices[i] = obData.box[i][1] * (obData.factor ? obData.factor : 1);
                }
                var obElement = new FormElement.JCComboBox(boxId, arBoxValues);
                this.getPrice = function()
                {
                        return (arPrices[obElement.getValue()] ? arPrices[obElement.getValue()] : 0);
                }
        }
};
