function number_format( number, decimals, dec_point, thousands_sep ) 
{
	var i, j, kw, kd, km;

	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, "$1" + 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;
}

function intval( mixed_var, base ) 
{	
	var tmp;

	if( typeof( mixed_var ) == 'string' ){
		tmp = parseInt(mixed_var);
		if(isNaN(tmp)){
			return 0;
		} else{
			return tmp.toString(base || 10);
		}
	} else if( typeof( mixed_var ) == 'number' ){
		return Math.floor(mixed_var);
	} else{
		return 0;
	}
}

function str_replace ( search, replace, subject ) 
{
	if(!(replace instanceof Array)){
		replace=new Array(replace);
		if(search instanceof Array){//If search	is an array and replace	is a string, then this replacement string is used for every value of search
			while(search.length>replace.length){
				replace[replace.length]=replace[0];
			}
		}
	}

	if(!(search instanceof Array))search=new Array(search);
	while(search.length>replace.length){//If replace	has fewer values than search , then an empty string is used for the rest of replacement values
		replace[replace.length]='';
	}

	if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
		for(k in subject){
			subject[k]=str_replace(search,replace,subject[k]);
		}
		return subject;
	}

	for(var k=0; k<search.length; k++){
		var i = subject.indexOf(search[k]);
		while(i>-1){
			subject = subject.replace(search[k], replace[k]);
			i = subject.indexOf(search[k],i);
		}
	}

	return subject;

}

/*######################################################################*/

function BasketAdd(obj, id)
{
	$.ajax({
    	url: "/sources/ajax/add_basket.php",
		type: 'post',
		data: ({'id' : id}),
	    dataType: "html",
	    beforeSend: function(){
	    },
	    success: function(answ){
	    	//alert(answ);
	    	if (answ != 'error') 
	    	{ 
	    		var mas = answ.split('|');
	    		$('#basket_price').html(mas[1]);
	    		$('#basket_order').show();
	    		$(obj).hide();
	    	}
		},
		error: function ( xhr, ajaxOptions, thrownError ) {
		}
    });
	return false;
}

function BasketRefresh()
{
	var summa = 0;
	var price = 0;
	var count = 0;
	var price_tovar = 0;
	
	$('.btr').each(function(index) {
		price = $(this).children().next().next().html();
		price = str_replace(' ', '', price);
		
		count = $(this).children().next().next().next().find('input').val();
		if (count == '') count = 1;
		
		price_tovar = parseFloat(price) * count;
		summa = parseFloat(summa) + parseFloat(price_tovar);
		
		price_tovar = number_format(price_tovar,2,'.',' ');
		index2 = index + 1;
		$('#tprice' + index2).html(price_tovar);
	});
	
	summa = number_format(summa,2,'.',' ');
	$('#endpriceinput').val(summa);
	$('#endprice').html(summa);
	$('#basket_price').html(summa + ' руб');
}

function BasketDelete(obj, id)
{
	var fl = confirm('Вы действительно хотите удалить товар из корзины?')
	
	if (fl)
	{
		$.ajax({
	    	url: "/sources/ajax/delete_basket.php",
			type: 'post',
			data: ({'id' : id}),
		    dataType: "html",
		    beforeSend: function(){
		    },
		    success: function(answ){
		    	//alert(answ);
		    	if (answ != 'error') 
		    	{ 
		    		var mas = answ.split('|');
		    		
		    		if (intval(mas[0]) == 0)
		    		{
		    			window.location = "/main/";
		    			return false;
		    		}
		    		var price_minus = mas[1];
		    		var price_all = $('#endpriceinput').val();
		    		price_all = str_replace(' ', '', price_all);
		    		
		    		var count_minus = $(obj).parent().prev().prev().find('input').val();
		    		
		    		var price_new = price_all - price_minus * count_minus;
		    		price_new = number_format(price_new,2,'.',' ');
		    		
		    		$('#endpriceinput').val(price_new);
		    		$('#endprice').html(price_new);
		    		$('#basket_price').html(price_new + ' руб');
		    		
		    		$(obj).parent().parent().remove();
		    		BasketRefresh();
		    	}
			},
			error: function ( xhr, ajaxOptions, thrownError ) {
			}
	    });
	}
	return false;
}

function BasketRefresh()
{
	var summa = 0;
	var price = 0;
	var count = 0;
	var price_tovar = 0;
	
	$('.btr').each(function(index) {
		price = $(this).children().next().next().html();
		price = str_replace(' ', '', price);
		
		count = $(this).children().next().next().next().find('input').val();
		if (count == '') count = 1;
		
		price_tovar = parseFloat(price) * count;
		summa = parseFloat(summa) + parseFloat(price_tovar);
		
		price_tovar = number_format(price_tovar,2,'.',' ');
		index2 = index + 1;
		$('#tprice' + index2).html(price_tovar);
	});
	
	summa = number_format(summa,2,'.',' ');
	$('#endpriceinput').val(summa);
	$('#endprice').html(summa);
	$('#basket_price').html(summa + ' руб');
}

$(document).ready(function () {
	$(".value").numeric();
	
	$(".value").keyup(function(){
		var count = intval($(this).val());
		if (count == 0) count = 1;
		
		BasketRefresh();
	});

});
	
