
function showHideCartBox() {
	var box = document.getElementById('cart_box');
	if (box.style.visibility == "visible") {
		box.style.visibility = "hidden";
	}
	else {
		box.style.visibility = "visible";
	}
}
function submitCartForm() {
	document.cartForm.submit();
	timerID=setTimeout('listCart()',500);
}

var ajax = new sack();
var ajax1 = new sack();
var ajax2 = new sack();

function whenLoading(){
	var e = document.getElementById('cart_box_item'); 
	e.innerHTML = "<p>Sending Data...</p>";
}

function whenLoaded(){
	var e = document.getElementById('cart_box_item'); 
	e.innerHTML = "<p>Data Sent...</p>";
}

function whenInteractive(){
	var e = document.getElementById('cart_box_item'); 
	e.innerHTML = "<p>getting data...</p>";
}

function whenCompleted(){
	var e = document.getElementById('cart_box_item'); 
	if (ajax.responseStatus){
		var string = ajax.response;
	} else {
		var string = "<p>URLString Sent: " + ajax.URLString + "</p>";
	}
	e.innerHTML = string;
	getCartItemCount();
	ajax.reset;
}

function listCompleted(){
	var e2 = document.getElementById('cart_list_item'); 
	if (ajax2.responseStatus){
		var string2 = ajax2.response;
	}
	e2.innerHTML = string2;
	getCartItemCount();
	listCartBox();
	ajax2.reset;
}

function countCompleted(){
	var e1 = document.getElementById('cart_item_count'); 
	if (ajax1.responseStatus){
		var string1 = ajax1.response;
	}
	e1.innerHTML = string1;	
	ajax1.reset;
}

function add2Cart(id){
	ajax.requestFile = "./?mod=shop&_com=order&task=add2Cart&productID="+id;
	ajax.element = 'cart_box_item';
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}

function emptyCartBox() {
	var flag = confirm('你確定要清空購物籃嗎？');
	if (flag) {
		ajax.requestFile = "./?mod=shop&_com=order&task=emptyCartBox";
		ajax.element = 'cart_box_item';
		ajax.onCompletion = whenCompleted;
		ajax.runAJAX();
	}
}

function removeCartBoxItem(id) {
	var flag = confirm('你確定要移除此項目嗎？');
	if (flag) {
		ajax.requestFile = "./?mod=shop&_com=order&task=removeCartBoxItem&cartID="+id;
		ajax.element = 'cart_box_item';
		ajax.onCompletion = whenCompleted;
		ajax.runAJAX();
	}
}

function emptyCart() {
	var flag = confirm('你確定要清空購物籃嗎？');
	if (flag) {
		ajax2.requestFile = "./?mod=shop&_com=order&task=emptyCart";
		ajax2.element = 'cart_list_item';
		ajax2.onCompletion = listCompleted;
		ajax2.runAJAX();
	}
}

function removeCartItem(id) {
	var flag = confirm('你確定要移除此項目嗎？');
	if (flag) {
		ajax2.requestFile = "./?mod=shop&_com=order&task=removeCartItem&cartID="+id;
		ajax2.element = 'cart_list_item';
		ajax2.onCompletion = listCompleted;
		ajax2.runAJAX();
	}
}

function listCartBox(){
	ajax.requestFile = "./?mod=shop&_com=order&task=listCartBox";
	ajax.element = 'cart_box_item';
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}

function listCart(){
	ajax2.requestFile = "./?mod=shop&_com=order&task=listCart";
	ajax2.element = 'cart_list_item';
	ajax2.onCompletion = listCompleted;
	ajax2.runAJAX();
}

function getCartItemCount(){
	ajax1.requestFile = "./?mod=shop&_com=order&task=getCartItemCount";
	ajax1.element = 'cart_item_count';
	ajax1.onCompletion = countCompleted;
	ajax1.runAJAX();
}


/* scripts for flying to cart */
var flyingSpeed = 25;

var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x = false;
var shopping_cart_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;

function shoppingCart_getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}

function addToBasket(productId)
{
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('cart_box');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	
	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);

	currentProductDiv = document.getElementById('slidingProduct' + productId);
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='';
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId);
}

function flyToBasket(productId)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';	
	
	if(moveX>0 && currentXPos > shopping_cart_x){
		flyingDiv.style.display='none';		
	}
	if(moveX<0 && currentXPos < shopping_cart_x){
		flyingDiv.style.display='none';		
	}
		
	if(flyingDiv.style.display=='block')setTimeout('flyToBasket("' + productId + '")',10); else add2Cart(productId);	
}

function goCheckout() {
	var user_id = getCookie('userID');
	if (user_id == false) {
		var url = "./?mod=shop&_com=user&task=login&refer=checkout";
		location.replace(url);
	}
	else {
		var url = "./?mod=shop&_com=order&task=checkout";
		location.replace(url);
	}
}

