var flyingSpeed = 50;

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('shopping_chart');
	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);	
	
}

function makeRequest(url) {
   var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            return "Cannot create an XMLHTTP instance";
            //return false;
        }

        http_request.onreadystatechange = function() { alertContents(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function alertContents(http_request) {
		var msg;
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                //alert(http_request.responseText);
				msg = http_request.responseText;
            } else {
                msg = 'There was a problem with the request.';
            }
        }
		if (!msg) msg = "<div align=center><img src=\"/images/loading.gif\" border=0></div>"; 
		document.getElementById('shopping_chart').innerHTML = msg;
    }
/*
function add_to_chart (field_id)
{
	var qty = document.getElementById(field_id+'_qty').value;
	var url = "/ajax/build-to-chart.php?action=add&pid="+field_id+"&qty="+qty; 
		//alert(url);
	makeRequest(url);
}
*/
function add_to_chart (field_id,type)
{
	var type,qty,c_qty;
	qty = c_qty = 0;
	if(type) {
		if (document.getElementById(field_id+'_a_qty')) {
			qty = document.getElementById(field_id+'_a_qty').value;
		}
		if (document.getElementById(field_id+'_c_qty')) {
			c_qty = document.getElementById(field_id+'_c_qty').value;
		}
		qty = parseInt(qty) + parseInt(c_qty);
		var url = "/ajax/build-to-chart.php?action=add&pid="+field_id+"&type="+type+"&qty="+qty+"&c_qty="+c_qty; 
	} else {
		var qty = document.getElementById(field_id+'_qty').value;
		var url = "/ajax/build-to-chart.php?action=add&pid="+field_id+"&qty="+qty; 
	}
	if (qty < 1)
	{
		alert('Quantity feild should be a more than 0');
		return false;
	}
		//alert(url);
		
		makeRequest(url);
		addToBasket(field_id);
		//alert('Item(s) has been added to the basket (on the top right hand side).\nTo proceed with payment, just click the \"Checkout\" button.'); 
}
function remove_from_chart (field_id)
{
	//var qty = document.getElementById(field_id+'_qty').value;
	var url = "/ajax/build-to-chart.php?action=remove&pid="+field_id; 
		//alert(url);
	makeRequest(url);
}
function display_chart (val)
{
	var url = "/ajax/build-to-chart.php?action=display"; 
		//alert(url);
	makeRequest(url);
}
function CalcOptn(field_id,price,type)
{
	var type;
	if (type) var qty = document.getElementById(field_id+'_'+type+'_qty').value;
	else var qty = document.getElementById(field_id+'_qty').value;
	var new_price = qty * price;
	var result = new_price.toFixed(2);
	if (type) document.getElementById(field_id+'_'+type+'_total').value = 'RM '+result; 
	else document.getElementById(field_id+'_total').value = 'RM '+result;
}
