﻿/* --- Put_in_to_cart
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
function psAddToCart(obj, type)	// тип: 1-картина, 2-репродукция
	{
		var cartbox = document.getElementById('CartBox');	// id-шник блочка корзины (вверху)
		cartbox.style.display = 'block';

		var parent = obj.parentNode;
		obj.parentNode.innerHTML = '<img src="'+psIMG+'_cart_loader.gif" width="16" height="16" alt="Put into cart" />';	
						
		var dCount = document.getElementById('psCartCount'+type);		
		
		var url = obj.href;
		$.getJSON(url, {ajax:1}, function(json){

			parent.className = 'put-in-cart-full';
			if (json.js.cmd_errors)
				{
					parent.innerHTML = json.js.cmd_errors;
				}
			else
				{
					parent.innerHTML = json.js.cmd_messages;
				}
			var carttypebox = document.getElementById('CartTypeBox'+type);	// id-шник блочка с кол-вом
			carttypebox.style.display = '';
			
			if (dCount && json.js.cart_count)	// защита от повторнгого добавления картин
				{
					dCount.innerHTML = json.js.cart_count;	// выводим кол-во
					if (type==1)
						{
							dCount.nextSibling.innerHTML = Declension(json.js.cart_count, '&nbsp;painting', '&nbsp;paintings', '&nbsp;paintings');	// выводим окончания для типа 1
						}
					else
						{
							dCount.nextSibling.innerHTML = Declension(json.js.cart_count, '&nbsp;reproduction', '&nbsp;reproduction', '&nbsp;reproduction');	// выводим окончания для типа 2
						}
				}
		});
	
	};
/* --- // Put_in_to_cart // --- */


/* --- The correct terminations
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */
function Declension(count, form1, form2, form5)
	{
		var num = count;
		num = Math.abs(num)%100;
		var nums = num%10;
		if (num > 10 && num < 20)
			{
				return form5;
			}
		if (nums > 1 && nums < 5)
			{
				return form2;
			}
		if (nums == 1)
			{
				return form1;
			}
		return form5;
	}
/* --- // The correct terminations // --- */
