//document.domain = "factory-express.com";
function xmlhttpPostLoginInfo(url) 
{
	try
	{
		var xmlHttpReqLoginInfo = false;
		var self = this;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
				self.xmlHttpReqLoginInfo = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
				self.xmlHttpReqLoginInfo = new ActiveXObject("Microsoft.XMLHTTP");
		}
		//document.getElementById("spanLogin").innerHTML = "<a href=\"http://www.factory-express.com/SignIn.aspx\" class=\"utilityLinksBlue\">login</a>";
		
		self.xmlHttpReqLoginInfo.open('POST', url + "LoginInfo.aspx", true);
		self.xmlHttpReqLoginInfo.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReqLoginInfo.onreadystatechange = function() {
				if (self.xmlHttpReqLoginInfo.readyState == 4 && document.getElementById("spanLogin") != null) {
						document.getElementById("spanLogin").innerHTML = self.xmlHttpReqLoginInfo.responseText;
				}
		}
		self.xmlHttpReqLoginInfo.send(url);
  }
  catch(err)
  {
		//document.getElementById("spanLogin").innerHTML = "<a href=\"http://www.factory-express.com/SignIn.aspx\" class=\"utilityLinksBlue\">login-</a>";
		//alert(err.message);
  }
} 

function xmlhttpPostLogout(url) 
{
	try
	{
		var xmlHttpReqLogout = false;
		var self = this;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
				self.xmlHttpReqLogout = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
				self.xmlHttpReqLogout = new ActiveXObject("Microsoft.XMLHTTP");
		}
		self.xmlHttpReqLogout.open('POST', url + "Logout.aspx", true);
		self.xmlHttpReqLogout.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReqLogout.onreadystatechange = function() {
				if (self.xmlHttpReqLogout.readyState == 4) {
				}
		}
		self.xmlHttpReqLogout.send(url);
  }
  catch(err)
  {
  }
} 

function xmlhttpPostCartInfo(url) 
{
	try
	{
		var xmlHttpReqCartInfo = false;
		var self = this;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
				self.xmlHttpReqCartInfo = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
				self.xmlHttpReqCartInfo = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		//if (url.indexOf("factory-express.com") > -1)
			//document.domain = "factory-express.com";
		
		self.xmlHttpReqCartInfo.open('POST', url + "CartInfo.aspx", true);
		self.xmlHttpReqCartInfo.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReqCartInfo.onreadystatechange = function() {
				if (self.xmlHttpReqCartInfo.readyState == 4 && document.getElementById("spanLogin") != null) {
						document.getElementById("spanCart").innerHTML = self.xmlHttpReqCartInfo.responseText;
				}
		}
		self.xmlHttpReqCartInfo.send(url);
  }
  catch(err)
  {
  }
} 

function xmlhttpPostCartUpdateGroup(url,DivID,frm,Quantity) 
{
	StartFlyToBasket(DivID);
	
	try
	{
		var params = "AddpkProduct="+frm.AddpkProduct.value+"&productQuantity="+Quantity;
		var xmlHttpReqCartUpdateGroup = false;
		var self = this;
		
		//Add to cart
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
				self.xmlHttpReqCartUpdateGroup = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
				self.xmlHttpReqCartUpdateGroup = new ActiveXObject("Microsoft.XMLHTTP");
		}
	  
		self.xmlHttpReqCartUpdateGroup.open('POST', url + "CartInfo.aspx", true);
		self.xmlHttpReqCartUpdateGroup.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReqCartUpdateGroup.onreadystatechange = function() {
			if (self.xmlHttpReqCartUpdateGroup.readyState == 4) {
				DisplaySpanCart(self.xmlHttpReqCartUpdateGroup.responseText);
			}
		}
		self.xmlHttpReqCartUpdateGroup.send(params);
  /*
		try
		{
			var img = document.getElementById("imgAddToCart" + DivID);
			if (img != null)
				img.src = url + "images/viewCartBtn.gif";
				
			var frm = document.getElementById("frmAddToCart" + DivID);
			if (frm != null)
				frm.onsubmit = "";
				
			var AddPk = document.getElementById("AddPk" + DivID);
			if (AddPk != null)
			{
				AddPk.style.visibility = "hidden";
				AddPk.value = "";
			}
				
		}
		catch(err)
		{
		}
		*/
		
		StartFlyingCart(DivID, url)
	}
	catch(err)
	{
		return;
	}
}

function xmlhttpPostCartUpdate(url,ProductID,Quantity) 
{
	try
	{
		StartFlyToBasket(ProductID);
		
		var params = "AddpkProduct="+ProductID+"&productQuantity="+Quantity;
		var xmlHttpReqCartUpdate = false;
		var self = this;
		
		//Get Product Options
		var productOptions = "";
		
		if (document.getElementById("ProductOptions") != null)
		{
			if (document.forms["frmProductDetail"].ProductOptions.value > 0)
			{
				productOptions = "ProductOptions=" + document.forms["frmProductDetail"].ProductOptions.value;
			}
			else
			{
				for (var i = 0; i < document.forms["frmProductDetail"].ProductOptions.length; i++)
				{
					if (document.forms["frmProductDetail"].ProductOptions[i].value > 0)
					{
						productOptions = document.forms["frmProductDetail"].ProductOptions.value + ",";
					}
				}
				
				if (productOptions.length > 0)
					productOptions = "ProductOptions=" + productOptions;
			}
		}
		
		if (productOptions.length > 0)
			params = params + "&" + productOptions;
		
		//Get Required Product Options
		var productOptionsRequired = "";
		
		if (document.getElementById("ProductOptionsRequired") != null)
		{
			if (document.forms["frmProductDetail"].ProductOptionsRequired.value > 0)
			{
				productOptionsRequired = "ProductOptionsRequired=" + document.forms["frmProductDetail"].ProductOptionsRequired.value;
			}
			else
			{
				for (var i = 0; i < document.forms["frmProductDetail"].ProductOptionsRequired.length; i++)
				{
					if (document.forms["frmProductDetail"].ProductOptionsRequired[i].value > 0)
					{
						productOptionsRequired = document.forms["frmProductDetail"].ProductOptionsRequired.value + ",";
					}
				}
				
				if (productOptionsRequired.length > 0)
					productOptionsRequired = "ProductOptionsRequired=" + productOptions;
			}
		}
		
		if (productOptionsRequired.length > 0)
			params = params + "&" + productOptionsRequired;
			
		//Add to cart
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
				self.xmlHttpReqCartUpdate = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
				self.xmlHttpReqCartUpdate = new ActiveXObject("Microsoft.XMLHTTP");
		}
	  
		self.xmlHttpReqCartUpdate.open('POST', url + "CartInfo.aspx", true);
		self.xmlHttpReqCartUpdate.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReqCartUpdate.onreadystatechange = function() {
			if (self.xmlHttpReqCartUpdate.readyState == 4) {
				//document.getElementById("spanCart").innerHTML = self.xmlHttpReq.responseText;
				//xmlhttpPostCartInfo(url);
				DisplaySpanCart(self.xmlHttpReqCartUpdate.responseText);
			}
		}
		self.xmlHttpReqCartUpdate.send(params);
		/*
		try
		{
			var link = document.getElementById("aAddToCart" + ProductID);
			if (link != null)
			{
				link.href = url + "shoppingcart.aspx";
				link.onclick = "";
			}
		  
			var img = document.getElementById("imgAddToCart" + ProductID);
			if (img != null)
				img.src = url + "images/viewCartBtn.gif";
				
			var frm = document.getElementById("frmProductDetail");
			if (frm != null)
				frm.onsubmit = "";
				
			var AddPk = document.getElementById("AddpkProduct");
			if (AddPk != null)
				AddPk.value = "";
				
		}
		catch(err)
		{
		}
		*/
		try
		{/*
			var link = document.getElementById("aAddToCart" + ProductID);
			if (link != null)
			{
				link.href = url + "shoppingcart.aspx";
				link.onclick = "";
			}
		  
			var img = document.getElementById("imgAddToCart" + ProductID);
			if (img != null)
				img.src = url + "images/viewCartBtn.gif";
				
			var frm = document.getElementById("frmProductDetail");
			if (frm != null)
				frm.onsubmit = "";
				
			var AddPk = document.getElementById("AddpkProduct");
			if (AddPk != null)
				AddPk.value = "";
				
				
				
				
			if(!shopping_cart_div)shopping_cart_div = document.getElementById('spanCart');
			if(!flyingDiv){
				flyingDiv = document.createElement('DIV');
				flyingDiv.style.position = 'absolute';
				document.body.appendChild(flyingDiv);
			}

			currentProductDiv = document.getElementById('div' + productId);
			
			//shopping_cart_FlyToCartx = shoppingCart_getLeftPos(shopping_cart_div);
			//shopping_cart_FlyToCarty = shoppingCart_getTopPos(shopping_cart_div);
			//shopping_cart_FlyToCartx = shoppingCart_getLeftPos(shopping_cart_div) - (currentProductDiv.style.width / 2);
			//shopping_cart_FlyToCarty = shoppingCart_getTopPos(shopping_cart_div) - currentProductDiv.style.height;
			shopping_cart_FlyToCartx = shoppingCart_getLeftPos(shopping_cart_div) - 100;
			shopping_cart_FlyToCarty = shoppingCart_getTopPos(shopping_cart_div) - 100;
			
			currentFlyToCartXPos = shoppingCart_getLeftPos(currentProductDiv);
			currentFlyToCartYPos = shoppingCart_getTopPos(currentProductDiv);
			
			diffFlyToCartX = shopping_cart_FlyToCartx - currentFlyToCartXPos;
			diffFlyToCartY = shopping_cart_FlyToCarty - currentFlyToCartYPos;
			

			
			var shoppingContentCopy = currentProductDiv.cloneNode(true);
			shoppingContentCopy.id='';
			flyingDiv.innerHTML = '';
			flyingDiv.style.left = currentFlyToCartXPos + 'px';
			flyingDiv.style.top = currentFlyToCartYPos + 'px';
			flyingDiv.appendChild(shoppingContentCopy);
			flyingDiv.style.display='block';
			flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
			flyToBasket(productId);
			*/
			/*
			if(!flyingViewCart){
			
				//shopping_cart_FlyToCartx = shoppingCart_getLeftPos(shopping_cart_div) - 100;
				//shopping_cart_FlyToCarty = shoppingCart_getTopPos(shopping_cart_div) - 100;
				
				
				//diffFlyToCartX = shopping_cart_FlyToCartx - currentFlyToCartXPos;
				//diffFlyToCartY = shopping_cart_FlyToCarty - currentFlyToCartYPos;
			
				//altert(flyingViewCart);
				flyingViewCart = document.createElement('img');
				flyingViewCart.style.position = 'absolute';
				//flyingViewCart.style.left = '200px';
				//flyingViewCart.style.top = '200px';
				flyingViewCart.src = url + "images/cartIcon.gif";
				//flyingViewCart.style.width = currentProductDiv.offsetWidth + 'px';
				document.body.appendChild(flyingViewCart);
			}
		
			prodDiv = document.getElementById('aAddToCart' + ProductID);
			xPos = shoppingCart_getLeftPos(prodDiv) + 120;
			yPos = shoppingCart_getTopPos(prodDiv);
			flyingViewCart.style.left = xPos + 'px';
			flyingViewCart.style.top = yPos + 'px';
			flyingViewCart.style.display='block';
			*/
		}
		catch(err)
		{
		}
		StartFlyingCart(ProductID, url)
  }
  catch(err)
  {
  }
}


function DisplaySpanCart(cartHTML)
{
	try
	{
		if(flyingDiv.style.display=='block')
			setTimeout('DisplaySpanCart("' + cartHTML + '")',10); 
		else 
			document.getElementById("spanCart").innerHTML = cartHTML;
  }
  catch(err)
  {
  }
}
        
/**************************************************/
var flyingSpeed = 25;
//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_FlyToCartx = false;
var shopping_cart_FlyToCarty = false;

var diffFlyToCartX = false;
var diffFlyToCartY = false;

var currentFlyToCartXPos = false;
var currentFlyToCartYPos = false;

//var ajaxObjects = new Array();


function shoppingCart_getTopPos(inputObj)
{		
	try
	{
		var returnValue = inputObj.offsetTop;
		while((inputObj = inputObj.offsetParent) != null){
  		if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
		}
		return returnValue;
  }
  catch(err)
  {
  }
}

function shoppingCart_getLeftPos(inputObj)
{
	try
	{
		var returnValue = inputObj.offsetLeft;
		while((inputObj = inputObj.offsetParent) != null){
  		if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
		}
		return returnValue;
  }
  catch(err)
  {
  }
}
	

function StartFlyToBasket(productId)
{
	try
	{
		if(!shopping_cart_div)shopping_cart_div = document.getElementById('spanCart');
		if(!flyingDiv){
			flyingDiv = document.createElement('DIV');
			flyingDiv.style.position = 'absolute';
			document.body.appendChild(flyingDiv);
		}

		currentProductDiv = document.getElementById('div' + productId);
		
		//shopping_cart_FlyToCartx = shoppingCart_getLeftPos(shopping_cart_div);
		//shopping_cart_FlyToCarty = shoppingCart_getTopPos(shopping_cart_div);
		//shopping_cart_FlyToCartx = shoppingCart_getLeftPos(shopping_cart_div) - (currentProductDiv.style.width / 2);
		//shopping_cart_FlyToCarty = shoppingCart_getTopPos(shopping_cart_div) - currentProductDiv.style.height;
		shopping_cart_FlyToCartx = shoppingCart_getLeftPos(shopping_cart_div) - 100;
		shopping_cart_FlyToCarty = shoppingCart_getTopPos(shopping_cart_div) - 100;
		
		currentFlyToCartXPos = shoppingCart_getLeftPos(currentProductDiv);
		currentFlyToCartYPos = shoppingCart_getTopPos(currentProductDiv);
		
		diffFlyToCartX = shopping_cart_FlyToCartx - currentFlyToCartXPos;
		diffFlyToCartY = shopping_cart_FlyToCarty - currentFlyToCartYPos;
		

		
		var shoppingContentCopy = currentProductDiv.cloneNode(true);
		shoppingContentCopy.id='';
		flyingDiv.innerHTML = '';
		flyingDiv.style.left = currentFlyToCartXPos + 'px';
		flyingDiv.style.top = currentFlyToCartYPos + 'px';
		flyingDiv.appendChild(shoppingContentCopy);
		flyingDiv.style.display='block';
		flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
		flyToBasket(productId);
  }
  catch(err)
  {
  }
	
}


function flyToBasket(productId)
{
	try
	{
		var maxDiff = Math.max(Math.abs(diffFlyToCartX),Math.abs(diffFlyToCartY));
		var moveX = (diffFlyToCartX / maxDiff) * flyingSpeed;;
		var moveY = (diffFlyToCartY / maxDiff) * flyingSpeed;	
		
		currentFlyToCartXPos = currentFlyToCartXPos + moveX;
		currentFlyToCartYPos = currentFlyToCartYPos + moveY;
		
		flyingDiv.style.left = Math.round(currentFlyToCartXPos) + 'px';
		flyingDiv.style.top = Math.round(currentFlyToCartYPos) + 'px';	
		
		
		if(moveX>0 && currentFlyToCartXPos > shopping_cart_FlyToCartx){
			flyingDiv.style.display='none';		
		}
		if(moveX<0 && currentFlyToCartXPos < shopping_cart_FlyToCartx){
			flyingDiv.style.display='none';		
		}
			
		if(flyingDiv.style.display=='block')
			setTimeout('flyToBasket("' + productId + '")',10); 
		//else 
			//ajaxAddProduct(productId);	
  }
  catch(err)
  {
  }
}



var flyingViewCart = false;
var currentAddToCartBtn = false;

var addToCartFlyingCartX = false;
var addToCartFlyingCartY = false;

var diffFlyingCartX = false;
var diffFlyingCartY = false;

var currentFlyingCartXPos = false;
var currentFlyingCartYPos = false;



function StartFlyingCart(ProductID, url)
{
	try
	{
		currentAddToCartBtn = document.getElementById('aAddToCart' + ProductID);
		
		if(!shopping_cart_div)
			shopping_cart_div = document.getElementById('spanCart');
		
		if(!flyingViewCart){
			flyingViewCart = document.createElement('a');
			flyingViewCart.href = url + "shoppingcart.aspx";
			flyingViewCart.style.position = 'absolute';
			
			var img = document.createElement('img');
			//flyingViewCart.style.left = '200px';
			//flyingViewCart.style.top = '200px';
			img.src = url + "images/viewCartButtonLarge.gif";
			img.style.border = "0";
			//flyingViewCart.style.width = currentProductDiv.offsetWidth + 'px';
			flyingViewCart.appendChild(img);
			
			/*
			flyingViewCart = document.createElement('img');
			flyingViewCart.style.position = 'absolute';
			//flyingViewCart.style.left = '200px';
			//flyingViewCart.style.top = '200px';
			flyingViewCart.src = url + "images/cartIcon.gif";
			*/
			
			document.body.appendChild(flyingViewCart);
			
			currentFlyingCartXPos = shoppingCart_getLeftPos(shopping_cart_div) - 20;
			currentFlyingCartYPos = shoppingCart_getTopPos(shopping_cart_div);
		}
		
		addToCartFlyingCartX = shoppingCart_getLeftPos(currentAddToCartBtn) + 100;
		addToCartFlyingCartY = shoppingCart_getTopPos(currentAddToCartBtn) - 40;
		
		diffFlyingCartX = addToCartFlyingCartX - currentFlyingCartXPos;
		diffFlyingCartY = addToCartFlyingCartY - currentFlyingCartYPos;
			
		flyingViewCart.style.left = currentFlyingCartXPos + 'px';
		flyingViewCart.style.top = currentFlyingCartYPos + 'px';
		flyingViewCart.style.display='block';
	  
		flyFlyingCart(ProductID);
  }
  catch(err)
  {
  }
	
}

function flyFlyingCart(ProductID)
{
	try
	{
		var maxDiff = Math.max(Math.abs(diffFlyingCartX),Math.abs(diffFlyingCartY));
			
		if (maxDiff > 0)
		{
			var moveX = (diffFlyingCartX / maxDiff) * flyingSpeed;
			var moveY = (diffFlyingCartY / maxDiff) * flyingSpeed;	
			
			currentFlyingCartXPos = currentFlyingCartXPos + moveX;
			currentFlyingCartYPos = currentFlyingCartYPos + moveY;
			
			if (Math.abs(Math.abs(currentFlyingCartXPos) - Math.abs(addToCartFlyingCartX)) <= flyingSpeed
						&& Math.abs(Math.abs(currentFlyingCartYPos) - Math.abs(addToCartFlyingCartY)) <= flyingSpeed)
			{
				currentFlyingCartXPos = addToCartFlyingCartX;
				currentFlyingCartYPos = addToCartFlyingCartY;
			}
			
			flyingViewCart.style.left = Math.round(currentFlyingCartXPos) + 'px';
			flyingViewCart.style.top = Math.round(currentFlyingCartYPos) + 'px';	
			//alert(moveX);
			
			if(moveX>0 && currentFlyingCartXPos > addToCartFlyingCartX){
				flyingViewCart.style.display='';		
			}
			if(moveX<0 && currentFlyingCartXPos < addToCartFlyingCartX){
				flyingViewCart.style.display='';		
			}
		}
		else
			flyingViewCart.style.display='';	
			
		if(flyingViewCart.style.display=='block')
			setTimeout('flyFlyingCart("' + ProductID + '")',10); 
		//else 
			//ajaxAddProduct(productId);	
  }
  catch(err)
  {
  }
}


