var App_Calculator = { _options : { changeClass : false, changeLength : false }, initSpecie: function () { var specieElement = $('.product_specie select'); specieElement.unbind('change'); specieElement.bind('change', function(){ var url = BASE_URL + '/' + CURRENT_PAGE + '?action=getProducts&productId=' + this.value + '&rowNum=' + specieElement.index(this); App_Calculator._options.changeClass = true; App_Calculator._options.changeLength = false; App_Calculator.sendParams(url); App_Calculator.initClass(); $("#product-" + specieElement.index(this) + "-class").change(); }); }, initClass: function () { var classElement = $('.product_class select'); classElement.unbind('change'); classElement.bind('change', function(){ var specieVal = classElement.parent().prev().children().last().val(); var url = BASE_URL + '/' + CURRENT_PAGE + '?action=getProducts&productId=' + specieVal + '&class=' + this.value + '&rowNum=' + classElement.index(this); App_Calculator._options.changeClass = false; App_Calculator._options.changeLength = true; App_Calculator.sendParams(url); }); }, sendParams: function (url) { $.ajax({ dataType: 'json', type: 'GET', cache: true, url: url, data: null, async: false, success: App_Calculator.ajaxContentLoaded }); }, ajaxContentLoaded: function (data) { data = json_decode(data); if (App_Calculator._options.changeClass) { App_Calculator.changeSelect(data['classVal'], 'class', data['rowNum']); } if (App_Calculator._options.changeLength) { App_Calculator.changeSelect(data['lengthVal'], 'length', data['rowNum']); } }, changeSelect: function (data, elementName, rowNum) { var element = $('#product-' + rowNum + '-' + elementName); var elementVal = element.val(); element.empty(); for (var valueClass in data) { element.prepend( $('')); } $("#product-" + rowNum + "-" + elementName +" [value='" + elementVal + "']").attr("selected", "selected"); }, init: function (options) { this._options = $.extend(this._options, options); this.initSpecie(); $('.product_specie select').change(); $('#product-add').bind('click', function(){ App_Calculator.initSpecie(); $('.product_specie select').last().change(); }) } };