var cart = Class.create(); cart.prototype = { initialize: function( price_container_class, to_cart_buttons_class, inbasket_class, toggler, toggler_container, cart_button, cart_content, total_sum, total_amount, products_count_in_cart, cut_down, order_form, user_full_name, user_feedback_info, status, close_order_form, hide_class ) { this.msg_delete_product = 'Are you really want to delete product?'; this.table = null; this.msg_empty = 'Empty field: '; this.msg_sent = 'Message successfully sent.'; this.msg_not_sent = 'Failed to send message.'; this.input = ''; this.textarea = ''; this.change_amount_in_process = false; this.add_to_cart_in_process = false; this.open_cart_in_process = false; this.cart_loaded = false; this.hide_class = hide_class; this.cart = {}; this.cart.ids = new Array(); this.cart.data = new Array(); this.first_amount = 0; // ФОРМА ЗАКАЗА this.status = $( status ); this.order_form = $( order_form ); this.user_full_name = $( user_full_name ); this.user_feedback_info = $( user_feedback_info ); this.close_order_form = $( close_order_form ); this.close_order_form.onclick = function(){ return false; }; this.order_form.onsubmit = function(){ return false; }; Event.observe( this.order_form, 'submit', this.sendOrderRequest.bind( this ) ); Event.observe( this.user_full_name, 'focus', this.showMessage.bind( this, '' ) ); Event.observe( this.user_feedback_info, 'focus', this.showMessage.bind( this, '' ) ); Event.observe( this.close_order_form, 'click', this.closeOrderForm.bind( this ) ); // ФОРМА ЗАКАЗА this.toggler = $( toggler ); this.toggler.onclick = function(){ return false; }; this.text_basket_empty = 'Корзина пуста'; this.text_selected = 'Выбрано '; this.text_products = ' товар(ов)'; this.products_count_in_cart = parseInt( products_count_in_cart ); this.toggler.innerHTML = ( this.products_count_in_cart ) ? this.text_selected + this.products_count_in_cart + this.text_products : this.text_basket_empty ; this.cart_button = $( cart_button ); if( !this.products_count_in_cart ) { Element.addClassName( this.cart_button, this.hide_class ); } Event.observe( this.cart_button, 'click', this.orderForm.bind( this ) ); this.cut_down = $( cut_down ); this.cut_down.onclick = function(){ return false; }; this.toggler_container = $( toggler_container ); this.cart_content = $( cart_content ); this.total_sum = $( total_sum ); this.total_amount = $( total_amount ); this.total_sum.innerHTML = '0'; this.total_amount.innerHTML = '0'; Element.addClassName( this.toggler_container, this.hide_class ); this.price_container_class = price_container_class; this.to_cart_buttons_class = to_cart_buttons_class; this.inbasket_class = inbasket_class; this.to_cart_buttons = $A( document.getElementsByClassName( to_cart_buttons_class ) ); for( i=0; i 0 ) { Element.update( this.status, '' ); Element.addClassName( this.status, this.hide_class ); } } else { Element.update( this.status, message ); Element.removeClassName( this.status, this.hide_class ); } return true; }, toggleCart: function( ) { if( !this.cart_loaded ) this.loadCart(); if( Element.hasClassName( this.toggler_container, this.hide_class ) ) Element.removeClassName( this.toggler_container, this.hide_class ) else Element.addClassName( this.toggler_container, this.hide_class ) }, init: function( obj ) { obj.onclick = function() { return false }; Event.observe( obj, 'click', this.toCart.bind( this, obj ) ); }, toCart: function( obj ) { if( this.add_to_cart_in_process ) return false; this.add_to_cart_in_process = true; this.product_cart = obj; this.request = new Ajax.Request ( 'add_to_cart', { method: 'post', parameters: { catalogue_stc_id: this.product_cart.id.split('_')[1], cart_loaded: this.cart_loaded }, onComplete: this.updateProductPrice.bind( this ) } ); }, updateProductPrice: function( request ) { this.json = eval( '(' + request.responseText + ')' ); if( request.responseText == '0' ) { this.add_to_cart_in_process = false; return false; } ++this.products_count_in_cart; this.toggler.innerHTML = ( this.products_count_in_cart ) ? this.text_selected + this.products_count_in_cart + this.text_products : this.text_basket_empty ; if( this.product_cart.id.split('_')[0] == 'tocart' ) { Element.removeClassName( this.product_cart, this.to_cart_buttons_class ); Element.addClassName( this.product_cart.up( '.' + this.price_container_class ), this.inbasket_class ); var random_product = $('random_' + this.product_cart.id.split('_')[1]); if( random_product ) { Element.removeClassName( random_product, this.to_cart_buttons_class ); Element.addClassName( random_product.up( '.' + this.price_container_class ), this.inbasket_class ); } } else { Element.removeClassName( this.product_cart, this.to_cart_buttons_class ); Element.addClassName( this.product_cart.up( '.' + this.price_container_class ), this.inbasket_class ); var tocart = $('tocart_' + this.product_cart.id.split('_')[1]); if( tocart ) { Element.removeClassName( tocart, this.to_cart_buttons_class ); Element.addClassName( tocart.up( '.' + this.price_container_class ), this.inbasket_class ); } } if( this.cart_loaded ) { var product_id = this.json[0].product_id; this.cart.ids.push( product_id ); this.cart.data.push( { price: this.json[0].price, amount: this.json[0].amount } ); this.json[0].totalprice = this.json[0].price * this.json[0].amount; var out = ''; out += this.cart_template.evaluate( this.json[0] ); var sum = 0; for( var i=0; i'); this.table = $( 'cart_products_container' ); } if( !this.table.down('tbody') ) { this.table.innerHTML = out; } else { var tbody = this.table.down('tbody'); out = tbody.innerHTML + out; Element.update( tbody, out ); } this.cartInit( false ); } if( this.products_count_in_cart ) { Element.removeClassName( this.cart_button, this.hide_class ); } this.add_to_cart_in_process = false; }, loadCart: function() { if( this.open_cart_in_process ) return false; this.open_cart_in_process = true; this.request = new Ajax.Request ( 'load_cart', { method: 'post', onComplete: this.completeLoadCart.bind( this ) } ); return false; }, completeLoadCart: function( request ) { this.json = eval( '(' + request.responseText + ')' ); var total_sum = 0; if( request.responseText != 0 ) this.products_count_in_cart = this.json.length; else this.products_count_in_cart = 0; this.toggler.innerHTML = ( this.products_count_in_cart ) ? this.text_selected + this.products_count_in_cart + this.text_products : this.text_basket_empty ; out = ''; for( var i=0; i 0 ) this.cartInit( false ); if( this.products_count_in_cart ) { Element.removeClassName( this.cart_button, this.hide_class ); } else { Element.addClassName( this.cart_button, this.hide_class ); } this.cart_loaded = true; this.open_cart_in_process = false; }, cartInit: function( product_id ) { if( product_id ) { up_amount_button = $( 'up_' + product_id ); down_amount_button = $( 'down_' + product_id ); input_amount_button = $( 'input_' + product_id ); delete_product_button = $( 'delete_' + product_id ); this.initChangeAmountButtons( up_amount_button, down_amount_button, input_amount_button, delete_product_button ); } else { up_amount_buttons = $A( this.toggler_container.getElementsByClassName( 'up' ) ); down_amount_buttons = $A( this.toggler_container.getElementsByClassName( 'down' ) ); input_amount = $A( this.toggler_container.getElementsByClassName( 'amount_value' ) ); delete_from_cart = $A( this.toggler_container.getElementsByClassName( 'delete_buttons' ) ); for( var i=0; i 1 ) { this.amount = parseInt(this.amount_obj.value) - 1; } else { this.amount_obj.value = this.first_amount; this.change_amount_in_process = false; return false; } break; case '': this.amount = parseInt(this.amount_obj.value); if( ( this.amount < 1 ) || ( this.first_amount == this.amount ) ) { this.amount_obj.value = this.first_amount; this.change_amount_in_process = false; return false; } break; } this.request = new Ajax.Request ( 'change_amount', { method: 'post', parameters: { amount_value: this.amount, product_id: this.amount_obj.id.split('_')[1] }, onComplete: this.completeChangeAmount.bind( this ) } ); }, completeChangeAmount: function( request ) { if( request.responseText == '1' ) { product_id = this.amount_obj.id.split('_')[1]; index = this.cart.ids.indexOf( product_id ); if( index != -1 ) { this.amount_obj.value = this.amount; this.cart.data[index].amount = this.amount; total_price = this.cart.data[index].price * this.cart.data[index].amount; $( 'total_price_' + product_id ).innerHTML = total_price; } sum = 0; for( var i=0; i