function setCookie(name, value, expires, path, domain, secure){document.cookie = name + "=" + escape(value)+((expires) ? "; expires=" + expires : "")+((path) ? "; path=" + path : "")+((domain) ? "; domain=" + domain : "")+((secure) ? "; secure" : "");}
function getCookie(name){var cookie = " " + document.cookie;var search = " " + name + "=";var setStr = null;var offset = 0;var end = 0;if (cookie.length > 0){offset = cookie.indexOf(search);if (offset != -1){offset += search.length;end = cookie.indexOf(";", offset);if (end == -1){end = cookie.length;}setStr = unescape(cookie.substring(offset, end));}}return(setStr);}  

$(document).ready(function(){
	
	DrawBasket();
	
	$('.btn_into_basket').click(function(){
		
		var regexp = new RegExp("[0-9]+");
		var count = $(this).parent().parent().find('input[name=count]').val();
		var result = regexp.exec(count);
		
		if (result == null || result == "null")
		{
			alert('Только числовые значения');
			$(this).parent().parent().find('input[name=count]').val('');
			return;
		}
		
		var id = $(this).parent().parent().find('input[name=id]').val();
		var title = $(this).parent().parent().find('input[name=title]').val();
		var price = $(this).parent().parent().find('input.price').val();
		AddToBasket(id, count, price);
		DrawNewProduct(id, count, price, title);	
		var el_id = '#pr_in_basket_'+id;
		if($("#pr_in_basket_div").html() != null)
			{
				$(this).parent().parent().css('display', 'none');
				$("#pr_in_basket_div").css('display', 'block');
			}	else {
				$(this).parent().parent().css('display', 'none');
				$(el_id).parent().css('display','block'); 
			}
		//DrawBasket();
		CancelOrder();
	});
	
});

function DrawNewProduct(id, count, price, title)
{
	$.ajax({
        url:  "/ajax.php?script=/scripts/design/setting.ajax.php",
        type : 'post',
        data : 'name=euro_tr&lng='+getCookie('lng')+'&t='+Math.random(),         
        success: function (data) { 
        	var text = "<tr><td><table border='0px' cellpadding='0px' cellspacing='0px' width='100%'><tr><td align='left' valign='middle'>"+title+"</td><td align='center' valign='top' width='80px'><img src='/i/i/images/minus.png' style='cursor: pointer;' onclick='DeleteOneProduct("+id+", this);' title='Удалить единицу товара' height='11' width='11'>&nbsp;<span class='count'>"+count+"</span>&nbsp;<img src='/i/i/images/plus.png' onclick='AddOneProduct("+id+", this);' style='cursor: pointer;' title='Добавить единицу товара' height='11' width='11'><br><span><span class='cost'>"+price+"</span>&nbsp;"+data+"</span></td><td width='20px' align='center' valign='middle'><img src='/i/i/images/delete.png' border='0' style='cursor: pointer;' onclick='DeleteProduct("+id+", this);'></td></tr><tr><td><div class='brendname_line_left'>&nbsp;</div></td><td></td><td></td></tr></table></td></tr>";
        	var el = $("#product_bay_list");
        	$(text).appendTo(el);
        	
        }
    });  
	DrawBasket();
}

function DeleteProduct(id, btn_obj)
{
	if (confirm('Вы уверены, что хотите\nудалить товар из корзины?'))
	{
		DeleteFromBasket(id, btn_obj);
	}
	
	var eli_id = '#pr_in_basket_'+id;
	var elt_id = '#pr_to_basket_'+id;
	if($("#pr_in_basket_div").html() != null)
	{
		$("#pr_in_basket_div").css('display', 'none');
		$(elt_id).css('display', 'block');
	}	else {
		$(eli_id).parent().css('display', 'none');
		$(elt_id).css('display','block'); 
	}
	
	DrawBasket();
	RefreshInfo();
}

function AddOneProduct(id, btn_obj)
{
	var currency = $("#currency").val();
	var i = FindProduct(id);
	var product = getCookie("product["+i+"]");
	var count = 0;

	product = product.split("|");
	count = product[1];
	count++;

	var price_euro = count*product[2]; 
	//var price_uah = count*product[2]*currency;
	
	setCookie("product["+i+"]", id+'|'+count+'|'+product[2], null, '/');
	$(btn_obj).parent().find('.count').html(count);
	$(btn_obj).parent().find('.cost').html(price_euro.toFixed(0));
	//$(btn_obj).parent().find('.price_uah').html(price_uah.toFixed(2));

	DrawBasket();
	RefreshInfo();
}

function DeleteOneProduct(id, btn_obj)
{
	var currency = $("#currency").val();
	var i = FindProduct(id);
	var product = getCookie("product["+i+"]");
	var count = 0;

	product = product.split("|");
	count = product[1];
	
	if (count == 1)
	{
		if (!confirm('Вы уверены, что хотите\nудалить товар из корзины?'))
		{
			return true;
		}
	}
	
	count--;

	var price_euro = count*product[2];
	var price_uah = count*product[2]*currency;
	
	setCookie("product["+i+"]", id+'|'+count+'|'+product[2], null, '/');
	$(btn_obj).parent().find('.count').html(count);
	$(btn_obj).parent().find('.price_euro').html(price_euro.toFixed(2));
	$(btn_obj).parent().find('.price_uah').html(price_uah.toFixed(2));

	if (count == 0)
	{
		DeleteFromBasket(id, btn_obj);
	}
	
	DrawBasket();
	RefreshInfo();
}

function RefreshInfo()
{
	var products_count = GetProductCount();
	var products_price = GetProductPrice();
	var currency = $("#currency").val();
	var uah = products_price * currency;
	var all_product_count = 0;
	
	var i = 0;
	var product = getCookie("product["+i+"]");	
	while(product != null)
	{
		if (product != "null")
		{
			var tmp = product.split('|');
			var tmp2 = tmp[1];
			tmp2++; tmp2--;
			all_product_count = all_product_count + tmp2;
		}
		i++;
		product = getCookie("product["+i+"]");
	}
	
	$(".all_count").html(all_product_count);
	$(".count_product").html(products_count);
	$(".all_price_euro").html(products_price.toFixed(2));
	$(".all_price_uah").html(uah.toFixed(2));
	
	products_count = GetProductCount();
}

function CreateOrder()
{
	$("#order_btn").css('display', 'none');
	//$("#create_order").slideDown("slow");
	$("#create_order").css('display', 'block');
}

function CancelOrder()
{
	$("#create_order").css('display', 'none');
	//$("#order_btn").css('display', 'block');
	$("#order_btn").css('display', 'block');
	//document.order_f.reset();
}

function DrawBasket()
{
	var products_count = GetProductCount();
	var products_price = GetProductPrice();
	if(products_count != 0) 
	{
		$("#price_order").css('display', 'block');
	} else {
		$("#price_order").css('display', 'none');
	}
	PrintBasketTitle(products_count);
	PrintPrice(products_price);
} 

function PrintBasketTitle(count)
{
	var basket = $("#basket");
	if(count != 0)
	{
		$.ajax({
	        url:  "/ajax.php?script=/scripts/design/setting.ajax.php",
	        type : 'post',
	        data : 'name=basket_count&lng='+getCookie('lng')+'&t='+Math.random(),         
	        success: function (data) { 
	        		var el = $("<div></div>");
	        		$(el).html(data+"  <span class='basket_title'>"+count+"</span>.");
	        		$(basket).html('');
	        		$(el).appendTo(basket);
	        }
	    });  
	}
	else
	{
		$.ajax({
	        url:  "/ajax.php?script=/scripts/design/setting.ajax.php",
	        type : 'post',
	        data : 'name=basket_empty&lng='+getCookie('lng')+'&t='+Math.random(),         
	        success: function (data) { 
	        		$(basket).html(data);
	        		return;
	        }
	    });  
	}
}

function PrintPrice(price)
{
	var price_td = $("#all_price");
	$.ajax({
        url:  "/ajax.php?script=/scripts/design/setting.ajax.php",
        type : 'post',
        data : 'name=euro_tr&lng='+getCookie('lng')+'&t='+Math.random(),         
        success: function (data) { 
        		var el = $("<div></div>");
        		$(el).html(price.toFixed(2)+"&nbsp;"+data);
        		$(price_td).html('');
        		$(el).appendTo(price_td);
        }
    });  
}

function GetProductPrice()
{
	var price = 0;
	var i = 0;
	var product = getCookie("product["+i+"]");
	
	while(product != null)
	{
		if (product != "null")
		{
			var tmp = product.split('|');
			var tmp2 = tmp[1] * tmp[2];
			price = price + tmp2;
		}
		i++;
		product = getCookie("product["+i+"]");
	}
	
	return price;
}

function GetProductCount()
{
	var count = 0;
	var i = 0;
	var product = getCookie("product["+i+"]");
	
	while(product != null)
	{
		if (product != "null")
		{
			count++;
		}
		i++;
		product = getCookie("product["+i+"]");
	}
	return count;
}

function DeleteFromBasket(id, object)
{
	var i = FindProduct(id);
	$(object).parent().parent().parent().parent().parent().parent().remove();
	setCookie("product["+i+"]", null, 1, '/');
}

function AddToBasket(id, count, price)
{
	var all_count = GetProductCount();
	var i = FindProduct(id);
	if (i == null)
	{
		setCookie("product["+all_count+"]", id+'|'+count+'|'+price, null, '/');
	}
	else
	{
		setCookie("product["+i+"]", id+'|'+count+'|'+price, null, '/');
	}
	
	DrawBasket();
}

function FindProduct(id)
{
	var i = 0;
	var product = getCookie("product["+i+"]");
	
	while(product != null)
	{
		if (product != "null")
		{
			var tmp = product.split('|');
			if (tmp[0] == id) return i;
		}
		i++;
		product = getCookie("product["+i+"]");
	}
	
	return null;
}
