
/**************************** PRODUCT CART **************************/
Product.Cart = Class.create();
Product.Cart.prototype = {
	initialize: function(form, saveUrl){
        this.form 				= form;
        this.formObject			= new VarienForm(form);
        this.saveUrl 			= saveUrl;
        this.onAddSuccess 		= this.addProductSuccess.bindAsEventListener(this);
        this.onAddFail 			= this.addProductFail.bindAsEventListener(this);
    },
    getResponse: function(transport) {
    	if(transport && transport.responseText) {
            try { var response = eval('('+transport.responseText+')'); }
            catch(e) { var response = {}; }
        }
        return response;
    },
    addProduct: function() {
    	$$('.messages').each(function(element){
    		$(element).remove();
    	});
    	if(this.validate()) {
	  		// Set info states
	    	$(this.form).down('.ajax-cart-info').hide();
	    	$(this.form).down('.ajax-cart-button').addClassName('ajax-loading');
			// Make request
			new Ajax.Request(this.saveUrl, {
	    		method: 		'post',
	      		parameters: 	Form.serialize(this.form),
	      		onFailure:		this.onAddFail,
	      		onSuccess:		this.onAddSuccess
			});
		}
		return false;
    },
    addProductSuccess: function(transport) {
    	var response = this.getResponse(transport);
    	$(this.form).down('.ajax-cart-button').removeClassName('ajax-loading');
  		$(this.form).down('.ajax-cart-info').update(response.message).show();
  		if(!response.error) {
  			$(this.form).down('.ajax-cart-button')
  				.addClassName('ajax-added')
  					.down('.button')
  					.writeAttribute('disabled', 'disabled');
  			$('ajax-cart-count').update(response.count > 0 ? '('+response.count+')' : '');
  		}
    },
    addProductFail: function(transport) {	
   		$(this.form).down('.ajax-cart-button').removeClassName('ajax-loading');
  		$(this.form).down('.ajax-cart-info').update('Product could not be added. Please try again.').show();
    },
    validate: function() {
    	return this.formObject.validator.validate();
    }
 }
 /**************************** CONFIGURABLE PRODUCT **************************/
 // Fill select
Product.Config.prototype.fillSelect = function(element){
	var attributeId = element.id.replace(/^p\d+\-[a-z]*/, '');
    var attributeId = attributeId.replace(/[a-z]*/, '');
    var options = this.getAttributeOptions(attributeId);
   	var label = element.options[0].text;
    this.clearSelect(element);
    element.options[0] = new Option(label, '');
    var prevConfig = false;
    if(element.prevSetting){
        prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
    }
    if(options) {
        var index = 1;
        for(var i=0;i<options.length;i++){
            var allowedProducts = [];
            if(prevConfig) {
                for(var j=0;j<options[i].products.length;j++){
                    if(prevConfig.config.allowedProducts
                        && prevConfig.config.allowedProducts.indexOf(options[i].products[j])>-1){
                        allowedProducts.push(options[i].products[j]);
                    }
                }
            } else {
                allowedProducts = options[i].products.clone();
            }
            if(allowedProducts.size()>0){
                options[i].allowedProducts = allowedProducts;
                element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
                element.options[index].config = options[i];
                index++;
            }
        }
    }
}
