function activateShoppingCart(){
	jQuery("#maincart .td_remove input").bind('click', function() {
		var input = jQuery(this);
		var row_id = input.val();
		jQuery.ajax({
			type: "POST",
			url: "/ajax/?module=shoppingcart&method=DeleteItem",
			data: { 'row_id':row_id },
			success: function() {
				input.parents('tr').fadeOut(500, function() {
					calcPrice();
				});
			},
			error: function() {
				window.location.reload();
			}
		});
		return false;
	});
	
	jQuery("#maincart .td_quantity input").change(function() {
		var input = jQuery(this);
		var row_id = input.attr("name").slice(9, -1);
		var quantity = input.val();
		jQuery.ajax({
			type: "POST",
			url: "/ajax/?module=shoppingcart&method=UpdateItem",
			data: { 'row_id':row_id, 'quantity':quantity },
			success: function() {
				// var startColor = jQuery("#maincart td.quantity input[name*=" + row_id + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				var startColor = "#fff";
				input.parents('tr').animate({ backgroundColor:"#ff8" }, 100).animate({ backgroundColor:startColor }, 800);
				calcPrice();
			},
			error: function() {
				window.location.reload();
			}
		});
	});
	
	jQuery("#order_form").validate({	
		highlight: function(element, errorClass) {
			jQuery(element).fadeOut(function() {
				jQuery(element).fadeIn();
			});
		},	
		rules: { 'user[fio]':'required', 'user[phone]':'required', 'user[address]':'required' },
		//messages: { 'user[fio]':'required', 'user[city]':'required', 'user[phone]':'required', 'user[address]':'required' },
		//submitHandler: function(form) {},
		errorPlacement: function(error, element) {
			if (error.html().length>0) {
				$j(element).addClass("errorInput");					
			} else {
				$j(element).removeClass("errorInput"); 
			}			
		},
		success: function(label) {
           	// set &nbsp; as text for IE
           	label.html("&nbsp;").addClass("checked");
        }
	});
	
	jQuery('.order_more button').click(function() {
		window.location = jQuery(this).attr('name');
	});
}

function calcPrice() {
	var totalPrice = 0;
	var product_rows = jQuery("#maincart .td_product").parent();
	product_rows.each(function(i, item) {
		var quantity 	= jQuery(".td_quantity input", item).val();
		if( quantity > 500 ){
			quantity = 500;
			alert('Максимально 500');
		}
		var values 		= jQuery(".td_unit_price", this).text().split(' ');
		var CURRENCY 	= values[1];
		var unitPrice 	= values[0];				
		var extendedPrice = quantity*unitPrice;
		totalPrice += extendedPrice;
		jQuery(".td_price", this).html(number_format( extendedPrice, 2, ".", ",") + ' '+ CURRENCY);
		jQuery("#total_price").html(number_format( totalPrice, 2, ".", ",") + ' '+ CURRENCY);
	});
	if ( totalPrice == 0 ) {
		jQuery("#maincart").parent().replaceWith("<p class='center'>Ваша корзина пуста.</p>");
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) {    // Format a number with grouped thousands
    // 
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
 
    var i, j, kw, kd, km;
 
    // input sanitation & defaults
    if( isNaN(decimals = Math.abs(decimals)) ){
        decimals = 2;
    }
    if( dec_point == undefined ){
        dec_point = ",";
    }
    if( thousands_sep == undefined ){
        thousands_sep = ".";
    }
 
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
 
    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }
 
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "jQuery1" + thousands_sep);
    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
 
 
    return km + kw + kd;
}
