// Корзина
var Basket = function()
{
	var _this = this;
	var _path_ajax = "/basket";
	
	this.Add = function( id )
	{
		var jqxhr = $.ajax({ url: _path_ajax+"/request", type: "post", dataType: "json", cache: false, data:{ id: id, action: 'add' }});
		jqxhr.success(function( resp )
		{
			if( resp.count != 0 )
				 $('#basket').html( resp.count );
			else $('#basket').html( '' );
			
			$('#book'+id).remove();
		});
		jqxhr.error(function(){});
	}
	
	this.Delete = function( id )
	{
		var jqxhr = $.ajax({ url: _path_ajax+"/request", type: "post", dataType: "json", cache: false, data:{ id: id, action: 'delete' }});
		jqxhr.success(function( resp )
		{
			if( resp.count != 0 )
				 $('#basket').html( resp.count );
			else $('#basket').html( '' );
			
			$('#basket-tbl-count').html( resp.allcount+' шт.' );
			$('#basket-tbl-price').html( resp.allprice+' <em class="rubl"><ins>&ndash;</ins>Р</em><span>' );
			$('#basket-book'+id).remove();
			
			if( resp.allcount == 0 ) $('#basket-form-containers').hide();
		});
		jqxhr.error(function(){});
	}

	this.AddSubscribe = function( id )
	{
		var jqxhr = $.ajax({ url: _path_ajax+"/request", type: "post", dataType: "json", cache: false, data:{ id: id, action: 'addsubscribe' }});
		jqxhr.success(function( resp )
		{
			if( resp.count != 0 )
				 $('#basket').html( resp.count );
			else $('#basket').html( '' );
			
			$('#subscribe'+id).remove();
		});
		jqxhr.error(function(){});
	}
	
	this.DeleteSubscribe = function( id )
	{
		var jqxhr = $.ajax({ url: _path_ajax+"/request", type: "post", dataType: "json", cache: false, data:{ id: id, action: 'deletesubscribe' }});
		jqxhr.success(function( resp )
		{
			if( resp.count != 0 )
				 $('#basket').html( resp.count );
			else $('#basket').html( '' );
			
			$('#basket-tbl-count').html( resp.allcount+' шт.' );
			$('#basket-tbl-price').html( resp.allprice+' <em class="rubl"><ins>&ndash;</ins>Р</em><span>' );
			$('#basket-subscribe'+id).remove();
			
			if( resp.allcount == 0 ) $('#basket-form-containers').hide();
		});
		jqxhr.error(function(){});
	}
	
	this.ChangeCount = function( id )
	{
		var _count = $('#basket-tbl-count'+id).val();

		var jqxhr = $.ajax({ url: _path_ajax+"/request", type: "post", dataType: "json", cache: false, data:{ id: id, count: _count, action: 'changecount' }});
		jqxhr.success(function( resp )
		{
			$('#basket-tbl-count').html( resp.allcount+' шт.' );
			$('#basket-tbl-price').html( resp.allprice+' <em class="rubl"><ins>&ndash;</ins>Р</em><span>' );
		});
		jqxhr.error(function(){});
	}
	
	this.ChangeCountSubscribe = function( id )
	{
		var _count = $('#basket-tbl-countsubscribe'+id).val();

		var jqxhr = $.ajax({ url: _path_ajax+"/request", type: "post", dataType: "json", cache: false, data:{ id: id, count: _count, action: 'changecountsubscribe' }});
		jqxhr.success(function( resp )
		{
			$('#basket-tbl-count').html( resp.allcount+' шт.' );
			$('#basket-tbl-price').html( resp.allprice+' <em class="rubl"><ins>&ndash;</ins>Р</em><span>' );
		});
		jqxhr.error(function(){});
	}
	
	this.CheckChange = function( evt )
	{
		evt = (evt) ? evt : window.event;
        if(evt)
        {
             var elm = (evt.target) ?
                 evt.target : evt.srcElement;
             if(elm)
            {
                 var code = (evt.charCode)?
                     evt.charCode : evt.keyCode;
                if( ((code>=48) && (code<=57)) || (code == 8) )
                {
                    return true;
                }
                 else
                {
                    return false;
                }
             }
        }
	}
	
	this.ShowForm = function( number )
	{
		if( number == 1 )
		{
			$('.basket-mr').removeClass('basket-mselect');
			$('.basket-ml').addClass('basket-mselect');

			$('#field-company').hide();
			$('#field-inn').hide();
			$('#field-kpp').hide();
			$('#field-bank').hide();
			$('#field-bik').hide();
			$('#field-office').hide();
			
			$('#payment-fizik').show();
			$('#payment-jur').hide();
			
			$('#basket_save').val('1');
		}
		
		if( number == 2 )
		{
			$('.basket-ml').removeClass('basket-mselect');
			$('.basket-mr').addClass('basket-mselect');

			$('#field-company').show();
			$('#field-inn').show();
			$('#field-kpp').show();
			$('#field-bank').show();
			$('#field-bik').show();
			$('#field-office').show();
			
			$('#payment-fizik').hide();
			$('#payment-jur').show();
			
			$('#basket_save').val('2');
		}
	}
}

var basket = new Basket();
