/**
 * update basket - Version 1.0
 * (c) 2009 Nguyen Hoang
 * Update : 6/03/2009
 *
 * All basket functions 
 */

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&file=basket_actions&op=basket_add_good_action&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();
							}
						};
	
	//document.write("module=shopping_basket&file=basket_actions&op=basket_update_quantity_action&" + str);					
	aj.run("module=shopping_basket&file=basket_actions&op=basket_update_quantity_action&" + 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);
	//alert(id);
	var aj = new AJAX ("modules.php");
	aj.element = "result";
	aj.execute = true;
	aj.onCompletion = function() {
							Display_thumb_basket();
						};
	aj.run("module=shopping_basket&file=basket_actions&op=remove_good_action&good=" + id);
}

function Update_basket(id, 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);
	
	try {
		if (typeof ele_fly_to_basket_loaded == 'undefined') {
			if (document.body )	{
				
				ele_fly_to_basket = document.createElement("SCRIPT");
				ele_fly_to_basket.type = 'text/javascript';
				ele_fly_to_basket.src = ROOT_URL + "js/fly-to-basket.js";		
									
				document.body.appendChild(ele_fly_to_basket);
			}else {
				document.write("<script language=\"JavaScript\" src=\"" + ROOT_URL + "js/fly-to-basket.js\" type='text/javascript'></script>");
			}
			
			ele_fly_to_basket_loaded = 1;
		}
		
		BeginAddToBasket(sid);
	}catch(ex) {
		//alert(ex.message);
	}
		
	//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_action", contentID , "innerHTML");
}
function Display_order_basket(contentID) {
	if (!contentID) contentID = "basket_info";
	
	ajaxRead("modules.php?module=shopping_basket&op=order_basket_action", contentID , "innerHTML");
}

function Display_main_basket(contentID) {
	if (!contentID) contentID = "basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=main_basket_action", contentID, "innerHTML");
}


function Display_tracking_basket(contentID) {
	if (!contentID) contentID = "basket";
		ajaxRead("modules.php?module=shopping_basket&op=main_basket_action&basket_type=tracking_basket", contentID, "innerHTML");
}

function Display_basket_index(contentID) {
	if (!contentID) contentID = "sliderContent";
		ajaxRead("modules.php?module=shopping_basket&op=thumb_basket_index_action", contentID, "innerHTML");	
}
	
function Display_thumb_item_basket(contentID) {
	if (!contentID) contentID = "thumb_item_basket";

	ajaxRead("modules.php?module=shopping_basket&op=thumb_item_basket_action", contentID , "innerHTML");
}
function Display_thumb_price_basket(contentID) {
	if (!contentID) contentID = "thumb_price_basket";
	
	ajaxRead("modules.php?module=shopping_basket&op=thumb_price_basket_action", contentID , "innerHTML");
}

function Display_basket(id) {				
	/*if(document.getElementById(id).style.display == "block") {
			document.getElementById(id).style.display = "none";
	}else 
			document.getElementById(id).style.display = "block";
	*/
	if ($("#" + id).is(":hidden"))
	 jQuery("#" + id).slideDown("slow");
	else  jQuery("#" + id).hide("slow");
}