/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var flyingSpeed = 3;
var url_addProductToBasket = 'addProduct.php';
var url_removeProductFromBasket = 'removeProduct.php';
var txt_totalPrice = 'Total: ';


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;

var ajaxObjects = new Array();

// BOI, followed by one or more digits, followed by EOI.
 var reInteger = /^\d+$/

//Validation for integer
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}


// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
//
//function getHTTPObject(){
//		var ro;
//		if (window.XMLHttpRequest)     // Object of the current windows
//{ 
//       	ro = new XMLHttpRequest();     // Firefox, Safari, ...
//} 
//else 
//if (window.ActiveXObject)   // ActiveX version
//{
//       ro = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer 
//} 
//return ro;
//}
function myXMLHttpRequest()
{
  var xmlhttplocal;
  try {
  	xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP");
  }
  catch (e) {
    try {
      xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP");
    }
    catch (E) {
      xmlhttplocal = false;
    }
  }
  if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
    try {
      var xmlhttplocal = new XMLHttpRequest ();
    }
    catch (e) {
      var xmlhttplocal = false;
      alert ('Sorry! I failed.');
    }
  }
  return (xmlhttplocal);
}
//var http = GetXmlHttpObject(); // We create the HTTP Object

//var http = getHTTPObject(); // We create the HTTP Object

var isWorking = 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,g_path_root)
{
	
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	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);
	addToShoopingBasket(productId,g_path_root);	
	
}


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 ajaxAddProduct(productId);	
}

function removeProductFromBasket(int_product_id,g_path_root)
{
	url = g_path_root+"addProduct.php?action=removeQuatity&no="+Math.random() +"&int_product_id=" +int_product_id;
	newobj = new myXMLHttpRequest();
	newobj.open ("GET", url, true);
	newobj.onreadystatechange = function ()
	{
		if (newobj.readyState == 4)
		{
			responsestring = newobj.responseText;
			if(document.getElementById("qty_"+int_product_id).innerHTML !='Qty : <strong>1</strong>')
			{
				NLBfadeBg('fadeDiv'+int_product_id,'#FDFB95','#FFFFFF','3000');
			}
			document.getElementById("cart").innerHTML = responsestring;
		}
	}
	
	newobj.send(null);
	return false;

} 

function addToShoopingBasket(int_product_id,g_path_root)
{
	
	int_quantity=document.getElementById('int_product_quantity_'+int_product_id).value;
	if (!isInteger(int_quantity))
	{
		alert('Please enter valid quantity');
	}
	else
	{
		url = g_path_root+"addProduct.php?action=addtocart&no="+Math.random() +"&int_quantity=" +int_quantity +"&int_product_id=" +int_product_id;
		
		newobj = new myXMLHttpRequest();
		newobj.open ("GET", url, true);
		newobj.onreadystatechange = function (){
		if (newobj.readyState == 4){
		responsestring = newobj.responseText;
		 document.getElementById("cart").innerHTML = responsestring;
		 NLBfadeBg('fadeDiv'+int_product_id,'#FDFB95','#FFFFFF','3000');
		}
		}
		newobj.send(null);
	}
	return false;
}
