
function Add_good(id, quantity) 
{
	/*var cookieName = "current_basket_goods[" + id + "]";
	var n = Number(readCookie(cookieName));

	n += quantity ;				
	writeCookie(cookieName, n, 8760);	*/
	
	//ajaxRead("classes/shopping_basket.class.php?shopping_basket_aj=1&good_id=" + id + "&total=" + quantity + "&basket_name=current_basket");
	var aj = new AJAX ("modules.php");
	aj.execute = true;
	aj.onCompletion = function() {
							Display_thumb_basket();
						};
	aj.run("module=shopping_basket&op=add_good&good=" + id);
}

function Update_good(str, display) 
{
	var aj = new AJAX ("modules.php");
	aj.execute = true;
	aj.onCompletion = function() {
							if (display) {
								Display_thumb_basket();
								Display_main_basket();
							}
						};
	aj.run("module=shopping_basket&op=update_quantity&" + str);
	
	//ajaxRead("actions.php?module=basket&op=update_quantity" + str + "&display=" + display);
	
}

function Remove_good(id) 
{	
	//var cookieName = "current_basket_goods[" + id + "]";
	
	//n = 0 ;			
	
	//writeCookie(cookieName, n, -1);
	
	//writeCookie("current_basket_changed", 1, 8760);
	
	//ajaxRead("actions.php?module=basket&op=remove_good&good=" + id);
	var aj = new AJAX ("modules.php");
	aj.element = "result";
	aj.execute = true;
	aj.onCompletion = function() {
							Display_thumb_basket();
							Display_main_basket();
						};
	aj.run("module=shopping_basket&op=remove_good&good=" + id);
}

function Update_basket(form, detail_page, id, name, value, quantity) {
	
	Add_good(id, quantity);
		
	writeCookie("current_basket_changed", 1, 8760);
	
	sid = 'good_' + id;
	n = sid.lastIndexOf('-'); // Extend product
	if (n != -1) sid = sid.substring(0, n);
	
	BeginAddToBasket(sid);
		
	//ajaxRead("modules.php?module=shopping_basket&op=thumb_basket", "thumb_basket", "innerHTML");
	
	/*if (!Add_thump_basket(detail_page, id, name, value, quantity)) {
		form.submit();
		return false;
	}*/
	return true;
}

// Add a good to thumb_basket

function Add_thump_basket(detail_page, id, name, value, quantity) {
	var table = document.getElementById("thumb_basket");	
	var price_label = document.getElementById("thumb_total_price");	
	
	if (!table || !price_label) return false;
		
	var total_price = price_label.innerHTML;				
	
	var strCheck = '.0123456789';
	pos1=0; pos2=0;
	for(var i=0; i < total_price.length; i++)  {
		if (strCheck.indexOf(total_price.charAt(i)) != -1) {
			if (!pos1) pos1=i;
			else pos2=i;
		}
	}
	if (pos2 < pos1) pos2 = pos1;
	
	dv = total_price.substr(0, pos1);
	
	total_price =  dv + (parseFloat(total_price.substr(pos1, pos2-pos1+1)) + value) + total_price.substr(pos2+1);
	
	var tbody = table.getElementsByTagName("tbody")[0];   
	var row = document.createElement("TR"); 
	var cell1 = document.createElement("TD"); 
	var cell2 = document.createElement("TD"); 
 			
	cell1.innerHTML = '<a class=good href="' + detail_page + '?good=' + id + '">' + name + '</a>';
	cell2.innerHTML = dv + (value * quantity);		
	cell2.setAttribute('align','right');	
	
	row.appendChild(cell1); 
	row.appendChild(cell2); 
	
	tbody.appendChild(row); 

	pos1 = total_price.indexOf(".");
	if (pos1 != -1) {
		total_price = total_price.substr(0, pos1) + total_price.substr(pos1, 3);
	}
	
	price_label.innerHTML = total_price;
	
	table.style.display='block';	
	document.getElementById("thumb_basket_result").style.display='block';
	document.getElementById("empty_basket").style.display='none';
	
	tbody = null;
	
	
	return true;
}

function Display_thumb_basket(contentID) {
	if (!contentID) contentID = "thumb_basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=thumb_basket", contentID , "innerHTML");
}

function Display_main_basket(contentID) {
	if (!contentID) contentID = "basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=main_basket", contentID, "innerHTML");
}


function Display_tracking_basket(contentID) {
	if (!contentID) contentID = "basket";
	ajaxRead("modules.php?module=shopping_basket&op=tracking_basket", contentID, "innerHTML");
}

document.write("<script language=\"JavaScript\" src=\"" + ROOT_URL + "js/fly-to-basket.js\" type='text/javascript'></script>");
