var cartmsgXPos = 300;
var cartmsgYPos = 300;

var siteurl = document.frmsiteurl.siteurl.value;
var filename = document.frmsiteurl.filename.value;

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;
var categoryid = 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_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);
	
}


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 getHTTPObject()
{
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest()
  	}
// code for IE
	else if (window.ActiveXObject)
  	{
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  	}
	return xmlhttp;
}
var http = getHTTPObject();

function AddProduct(mode, productid, qty, price, name,categoryid, get_citycountry)
{
	//	alert(name);
	//check product quantity
	//addToBasket(productid);
	prod_sel_att = new Array();
	if(filename == "productdetail")
	{
		var tot = document.getElementById('tot_att').value;
		if( tot > 0 )
		{
			for(var i = 0; i < tot ; i++)
			{
				var att_sel = 'iProAttrId_'+i;
				prod_sel_att[i] = document.getElementById(att_sel).value;
			}
		}
		if(document.frmproductdetail.quantity.value == "")
			prod_qty = "1";
		else
		{
			if(document.frmproductdetail.quantity.value == "0")
				prod_qty = "1";
			else
				prod_qty = document.frmproductdetail.quantity.value;
		}
	}
	else
		prod_qty = "1";
	
	//	get product attribute
	//	iProAttrId = document.getElementById('iProAttrId').value;
	
	//get product price w/o "$" sign
	productprice = price.split("Rs. ");
	//get url of ajax file
	var url = siteurl+"ajax_shoppingcart.php";
	url = url + "?productid="+ escape(productid)+"&prod_selatt="+escape(prod_sel_att)+"&qty="+ escape(prod_qty)+"&price="+escape(productprice[1])+"&name="+escape(name)+"&mode="+escape(mode)+"&categoryid="+categoryid;
	http.open("GET", url, true);
	http.onreadystatechange = getCartDataAdd;
    http.send(null);
}

function RemoveProduct(mode,productid)
{
	//get url of ajax file
	var url = siteurl+"ajax_shoppingcart.php";
	url = url + "?productid="+ escape(productid)+"&mode="+escape(mode);
	http.open("GET", url, true);
	http.onreadystatechange = getCartDataRemove;
    http.send(null);
}

function getCartDataAdd()
{
	if (http.readyState == 4)
	{
   		var xmlDocument = http.responseXML; 
		var cart_data  = xmlDocument.getElementsByTagName('cart_data').item(0).firstChild.data;
		document.getElementById("shopping_cart").innerHTML = decode64(cart_data);
/*
		document.getElementById("cart_message").style.width = '500px';
		document.getElementById("cart_message").style.position = 'absolute';
		document.getElementById("cart_message").style.display = '';
		document.getElementById("cart_message").style.left = cartmsgXPos + 'px';
		document.getElementById("cart_message").style.top = cartmsgYPos + 'px';
*/
	}
}

function getCartDataRemove()
{
	if (http.readyState == 4)
	{
   		var xmlDocument = http.responseXML; 
		var cart_data  = xmlDocument.getElementsByTagName('cart_data').item(0).firstChild.data;
		document.getElementById("shopping_cart").innerHTML = decode64(cart_data);
	}
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/=";
function decode64(inp)
{
var out = ""; //This is the output
var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
var i = 0; //Position counter

// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
var base64test = /[^A-Za-z0-9\+\/\=]/g;

if (base64test.exec(inp)) { //Do some error checking
alert("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
"Expect errors in decoding.");
}
inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

do { //Here’s the decode loop.

//Grab 4 bytes of encoded content.
enc1 = keyStr.indexOf(inp.charAt(i++));
enc2 = keyStr.indexOf(inp.charAt(i++));
enc3 = keyStr.indexOf(inp.charAt(i++));
enc4 = keyStr.indexOf(inp.charAt(i++));

//Heres the decode part. There’s really only one way to do it.
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

//Start to output decoded content
out = out + String.fromCharCode(chr1);

if (enc3 != 64) {
out = out + String.fromCharCode(chr2);
}
if (enc4 != 64) {
out = out + String.fromCharCode(chr3);
}

//now clean out the variables used
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";

} while (i < inp.length); //finish off the loop
//Now return the decoded values.
return out;
}
function continue_shopping()
{
	document.getElementById("cart_message").style.display = 'none';
	return false;
}
function view_cart()
{
	window.location = siteurl+'shoppingcart';
	return false;
}
