function shopCart() {
	this.item_id = 0;
}

shopCart.prototype = {	
	
	manageCart : function(item_id,act) {	
		
		var color_id = 0;
		var el_col = document.getElementById('selcolor_' + item_id);
		if(el_col) {
			color_id = el_col.options[el_col.selectedIndex].value;
		}
        var _this = this;                     
        var queryOpts = {
            method:'post',
            asynchronous:true,
            postBody:'item_id=' + item_id + '&color_id=' + color_id + '&act=' + act,
            //onLoading:_this.startedFunction.bind(this),
            //onComplete:_this.completeFunction.bind(this),
            onSuccess:_this.successAddFunction.bind(this)
        }               
        if(act == 1) {
        	if(confirm('Добавить в корзину выбранный товар?')) var data = new Ajax.Request('/ajax/manageCart', queryOpts);
        }        
        if(act == 2) {
        	if(confirm('Вы действительно хотите удалить из корзины выбранный товар?')) var data = new Ajax.Request('/ajax/manageCart', queryOpts);
        }                
	},
	
	successAddFunction : function(handler)
    {    	      	 
    	var resp = handler.responseText;      	
		if(resp.length > 0) {
			var div_el = document.getElementById('cart_items');
			if(div_el) div_el.innerHTML = resp;
		}
    },
      
        
    calcTotalPrice : function() {
    	var total_count = 0;
    	var total_price = 0;
    	var el = document.getElementById('total_quant');
    	if(el) total_count = el.value;    	
    	if(total_count > 0) {
    		for(var i=1;i<=total_count;i++) {
    			var el_q  = document.getElementById('quant_' + i);	
    			var el_pr = document.getElementById('price_' + i);	
    			if(el_q && el_pr) {    				
    				total_price += el_q.value * el_pr.value;
    			}
    		}
    	}
    	return total_price;    	
    },
    
    removeTableItem : function(item_id) {
		var parent = document.getElementById('items_tb'); 
		var el = document.getElementById(item_id);
		if(el && parent) {			
    		var gr = parent.removeChild(el);
    		gr = null;			   		
		}    	
    },
    
	removeCartItem : function(item_id) {
		this.item_id = item_id;
        var _this = this;                     
        var queryOpts = {
            method:'post',
            asynchronous:true,
            postBody:'item_id=' + item_id,
            //onLoading:_this.startedFunction.bind(this),
            //onComplete:_this.completeFunction.bind(this),
            onSuccess:_this.successRemoveFunction.bind(this)
        }               
        if(confirm('Вы действительно хотите удалить выбранный товар?')) var data = new Ajax.Request('/ajax/removeItem?ajax=1', queryOpts);                       
	},
	
	successRemoveFunction : function(handler)
    {    	
    	var resp = handler.responseXML.documentElement.childNodes; 
    	var t = this.getNodesByName(resp,'cart_deleteItem','childs'); 
    	var res = 0;
    	if(t.length > 0) {    	
    		var res = 0;
	    	for(var i=0;i<t.length;i++) {
	    		res = t[i].nodeValue;
	    	}
	    	if(res == 1) {
    			this.removeTableItem(this.item_id);
    			var el = document.getElementById('total_price');
    			if(el) el.innerHTML = this.calcTotalPrice();
	    	}
    	}
    },

    recalcPrice: function() {
    	var el = document.getElementById('total_price');
    	if(el) el.innerHTML = this.calcTotalPrice();    	
    },
    
    getNodesByName : function (nodes,name,mode) {
    	var res_nodes = false;
		if(nodes.length > 0) {    		
    		for(var i=0;i<nodes.length;i++) {      				
    			if(nodes[i].nodeName == name) {    				
    				if(mode == 'childs') {
    					res_nodes = nodes[i].firstChild.childNodes;
    				} 	
    				if(mode == 'node') {
    					res_nodes = nodes[i];
    				}
    				if(mode == 'value') {
    					res_nodes = nodes[i].nodeValue;
    				}     				 	    				
    				break;
    			}
    		}    		
		}
		return res_nodes;	    	
    },
    
    checkFields : function() {
    	var can_sumbit = false;
    	
    	var nm = document.getElementById('name');    	    	
    	if(nm.value.length < 3 ) {
    		alert('Вы не заполнили обязательное поле "Имя".');
    		return;
    	}
    	
    	var ph = document.getElementById('phone');    	    	
    	if(ph.value.length < 7 ) {
    		alert('Вы не заполнили обязательное поле "Телефон".');
    		return;
    	}
    	
    	var ad = document.getElementById('address');    	    	
    	if(ad.value.length < 5 ) {
    		alert('Вы не заполнили обязательное поле "Адрес доставки".');
    		return;
    	}    	
    	
    	document.getElementById('order_info').submit();    	
    }

       
      	
}
