// Get the HTTP Object

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 ajaxObjects = new Array();


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 getHTTPObject(){
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}

function option_add(productId) {
		var pid='#p_id'+productId;
		//alert(pid);
		var dataString = jQuery(pid).serialize();//'name='+ name + '&email=' + email + '&phone=' + phone;
		//alert(dataString);
		//alert (dataString);return false;
	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);
	
		jQuery.ajax({
      type: "POST",
      url: "http://www.esmale.com/websites/esmale/html/callAjax.php",
      data: dataString,
      success: function(reponse) {
		 // alert(reponse);
		  var html = reponse.split("[BRK]");
				//var html = httpObject.responseText;
				//alert(html);
				document.getElementById('items').innerHTML = html[0];
				document.getElementById('cart_total').innerHTML = html[1];
				document.getElementById('cart_block_list_inner'+productId).innerHTML = html[2];
				//alert('This item has been added to your Quote Basket');
      			//document.getElementById('toggleEditItemOption'+p_id).innerHTML=reponse;
      }
     });
    return false;
	
}
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 updateItemTrolley(product_id,qty)
{
	httpObject = getHTTPObject();
	
	if(httpObject != null) {
	  if(qty<0)
	  {
		  qty=0;
		  }
		var querystring = "&product_id=" + product_id + "&qty="+qty;
		httpObject.open("GET", "http://www.esmale.com/websites/esmale/html/callAjax.php?showcase=showTrolley" + querystring, true);
		httpObject.onreadystatechange=function() {
			if(httpObject.readyState == 4) {
				var html = httpObject.responseText;
				//alert(html);
				document.getElementById('cart_block_list_inner').innerHTML = html;
			}
		}
		httpObject.send(null);
	}
	
}



function roundNumber(num,dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

	
function charDisplay(domain, nt_siteid, product_id) {
	var selectimgid = "select"+product_id; // Select Icon Id
	var downimgid = "down"+product_id; // Down Icon Id
	var charmenuid = "char"+product_id;  // characteristics DIV Id to show/hide
	var styleval = document.getElementById(charmenuid).style.display;
	if(styleval == 'none') {
		document.getElementById(selectimgid).style.display = 'none';
		document.getElementById(charmenuid).style.display = '';
		document.getElementById(downimgid).style.display = '';
	} else {
		document.getElementById(downimgid).style.display = 'none';
		document.getElementById(charmenuid).style.display = 'none';
		document.getElementById(selectimgid).style.display = '';
	}
}

function option_addCombo(productId) {
		var pid='#p_id'+productId;
		//alert(pid);
		var dataString = jQuery(pid).serialize();//'name='+ name + '&email=' + email + '&phone=' + phone;
		//alert(dataString);
		//alert (dataString);return false;
	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);
	
		jQuery.ajax({
      type: "POST",
      url: "http://www.esmale.com/websites/esmale/html/callAjax.php",
      data: dataString,
      success: function(reponse) {
		 // alert(reponse);
		  var html = reponse.split("[BRK]");
				//var html = httpObject.responseText;
				//alert(html);
				document.getElementById('items').innerHTML = html[0];
				document.getElementById('cart_total').innerHTML = html[1];
				document.getElementById('cart_block_list_inner'+productId).innerHTML = html[2];
				//alert('This item has been added to your Quote Basket');
      			//document.getElementById('toggleEditItemOption'+p_id).innerHTML=reponse;
      }
     });
    return false;
	
}

function option_addCombo3for2(productId) {
		var pid='#cp_id'+productId;
		//alert(pid);
		var dataString = jQuery(pid).serialize();//'name='+ name + '&email=' + email + '&phone=' + phone;
		//alert(dataString);
		//alert (dataString);return false;
	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('slidingProduct3for2' + 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);
	
		jQuery.ajax({
      type: "POST",
      url: "http://www.esmale.com/websites/esmale/html/callAjax.php",
      data: dataString,
      success: function(reponse) {
		 // alert(reponse);
		  var html = reponse.split("[BRK]");
				//var html = httpObject.responseText;
				//alert(html);
				document.getElementById('items').innerHTML = html[0];
				document.getElementById('cart_total').innerHTML = html[1];
				document.getElementById('block_list_inner_3for2combo'+productId).innerHTML = html[2];
				//alert('This item has been added to your Quote Basket');
      			//document.getElementById('toggleEditItemOption'+p_id).innerHTML=reponse;
      }
     });
    return false;
	
}
