/************************************************************************************************************
(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 = 13;
var url_addProductToBasket = 'addProductPick.php';
var url_removeProductFromBasket = 'removeProductPick.php';
var txt_totalPrice = 'Modifier: ';


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 AlreadyExists(productId, type)
{
	//alert(productId);
	if(document.getElementById('shopping_cart_items_product' + productId))
	{
		
		row=document.getElementById('shopping_cart_items_product' + productId);
		var items = row.cells[1].innerHTML;
		//alert(items);
		var findspace = items.match(' ');
		if(findspace)
			items = items.split( " " )[ 0 ];
		if(items=='Over' || items=='Under')
		{
			if(type==5 || type==6)
				return false;
			else
				return true;
		}
		else
		{
			if(type==5 || type==6)
				return true;
			else
				return false;
		}
	}
	else
		return true;
}
function AlreadyExists2(productId, type)
{
//	alert(productId);
	if(document.getElementById('shopping_cart_items_product' + productId + 'a'))
	{
		row=document.getElementById('shopping_cart_items_product' + productId + 'a');
		var items = row.cells[1].innerHTML;
		//alert(items);
		var findspace = items.match(' ');
		if(findspace)
			items = items.split( " " )[ 0 ];
		if(items=='Over' || items=='Under')
		{
			if(type==5 || type==6)
				return false;
			else
				return true;
		}
		else
		{
			if(type==5 || type==6)
				return true;
			else
				return false;
		}
	}
	else
		return true;
}
function addToBasket(productId, type)
{
	if(AlreadyExists(productId, type))
	{
		if(AlreadyExists2(productId, type))
		{
			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);
			
			if(type==5 || type==6)
				currentProductDiv = document.getElementById('slidingProductb' + productId);
			else if(type==3 || type==4)
				currentProductDiv = document.getElementById('slidingProductc' + productId);
			else
				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, type);
		}
	}
	
}


function flyToBasket(productId, type)
{
	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';		
	}
	var something=',';
	if(flyingDiv.style.display=='block')setTimeout('flyToBasket("' + productId + '","' + type +'")',10); else ajaxAddProduct(productId, type);	
}

function showAjaxBasketContent(ajaxIndex)
{
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
		
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
	if(document.getElementById("ErrorRow"))
	{
		var productRow = document.getElementById("ErrorRow");
		productRow.parentNode.removeChild(productRow);
	}
	if(productItems[0]=="error")
	{
		var tr = itemBox.insertRow(-1);
		tr.id= "ErrorRow";
		var td = tr.insertCell(-1);
		td.colSpan=3;
		td.innerHTML= '<b style="color:#900000;">' + productItems[1] + '</b>';
	}
	else
	{
		if(document.getElementById('shopping_cart_items_product' + productItems[0])){	// A product with this id is allready in the basket - just add number items
			var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
			var items = row.cells[1].innerHTML;
			
			if(!document.getElementById('shopping_cart_items_product' + productItems[0] + 'a'))
			{
			//	var findspace = items.match(' ');
				//if(findspace)
					//items = items.split( " " )[ 1 ];
				
				//if(items != productItems[1])
				//{
					var tr = itemBox.insertRow(-1);
					tr.id = 'shopping_cart_items_product' + productItems[0] + 'a';
					
						// Number of items
						
					var td = tr.insertCell(-1);
					if(productItems[5]==1 || productItems[5]==3)
						td.innerHTML = '<font color="green"><b>' + productItems[3] + '</b></font><br>' + productItems[4];
					else if(productItems[5]==2 || productItems[5]==4)
						td.innerHTML = productItems[3] + '<br><font color="green"><b>' + productItems[4] + '</b></font>';
					else
						td.innerHTML = productItems[3] + '<br>' + productItems[4];
					
					var td = tr.insertCell(-1);
					td.style.textAlign = 'right';
			
					if(productItems[5]==5)
					td.innerHTML = 'Under ' + productItems[1]; 	// Price	
					else if(productItems[5]==6)
					td.innerHTML = 'Over ' + productItems[1]; 	// Price
					else
					td.innerHTML = productItems[1];
					
					var myInt=parseFloat(productItems[2]); 
					var second=parseFloat(document.getElementById('Modifier').value);
					
					if(second<=0)
					second=1;
					//var send=myInt+second;
					var allTRs = itemBox.getElementsByTagName("tr");
					var NumGames=allTRs.length;
					if(NumGames > 2)
						second+=1;
					//alert("1:" + second);
					var price=myInt;
					//alert("price:" + price);
					if(price < 0)
					{
						price*=-1;
						price=100/price;
						price+=1;
						second*=price;
					}
					else
					{
						price=price/100;
						price+=1;
						second*=price;
					}
					var tester=second.toFixed(2);
					second=tester;
					second-=1;
					//alert(second);
					document.getElementById('Modifier').value=second;
					var td = tr.insertCell(-1);
					var a = document.createElement('A');
					td.appendChild(a);
					a.href = '#';
	
					a.onclick = function(){ removeProductFromBasket2(productItems[0], productItems[2], productItems[5]); };
					var img = document.createElement('IMG');
					img.src = 'images/remove.jpg';
					a.appendChild(img);
					document.getElementById('ConfirmBetButton').style.display='block';
					var rowCount = document.getElementById("shopping_cart_items").getElementsByTagName("TR").length;
					
					if(rowCount > 1)
					{
						document.getElementById('shopping_cart_totalprice').style.display='block';
						document.getElementById('showWager').style.display='block';
				
					}
					else
					{
						document.getElementById('showWager').style.display='none';
						//document.getElementById('HelpDiv').style.display='block';
					}
				//}
			}
			//items = items + 1;
			//row.cells[0].innerHTML = items;
		}else{	// Product isn't allready in the basket - add a new row
			var tr = itemBox.insertRow(-1);
			tr.id = 'shopping_cart_items_product' + productItems[0];
			
				// Number of items
			var td = tr.insertCell(-1);
			if(productItems[5]==1 || productItems[5]==3)
				td.innerHTML = '<font color="green"><b>' + productItems[3] + '</b></font><br>' + productItems[4];
			else if(productItems[5]==2 || productItems[5]==4)
				td.innerHTML = productItems[3] + '<br><font color="green"><b>' + productItems[4] + '</b></font>';
			else
				td.innerHTML = productItems[3] + '<br>' + productItems[4];
			
			var td = tr.insertCell(-1);
			td.style.textAlign = 'right';
	
			if(productItems[5]==5)
			td.innerHTML = 'Under ' + productItems[1]; 	// Price	
			else if(productItems[5]==6)
			td.innerHTML = 'Over ' + productItems[1]; 	// Price
			else
			td.innerHTML = productItems[1];
			
			var myInt=parseFloat(productItems[2]); 
			var second=parseFloat(document.getElementById('Modifier').value);
			
			if(second<=0)
			second=1;
			//var send=myInt+second;
			var allTRs = itemBox.getElementsByTagName("tr");
			var NumGames=allTRs.length;
			if(NumGames > 2)
				second+=1;
			//alert("2:" + second);
			var price=myInt;
			//alert(price);
			if(price < 0)
			{
				price*=-1;
				price=100/price;
				price+=1;
				second*=price;
			}
			else
			{
				price=price/100;
				price+=1;
				second*=price;
			}
			//alert(second);
			second=second-1;
			var tester=second.toFixed(2);
			//alert(second);
			document.getElementById('Modifier').value=tester;
			var td = tr.insertCell(-1);
			var a = document.createElement('A');
			td.appendChild(a);
			a.href = '#';
			a.onclick = function(){ removeProductFromBasket(productItems[0], productItems[2], productItems[5]); };
			var img = document.createElement('IMG');
			img.src = 'images/remove.jpg';
			a.appendChild(img);
			//td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';
			document.getElementById('ConfirmBetButton').style.display='block';
			var rowCount = document.getElementById("shopping_cart_items").getElementsByTagName("TR").length;
			if(rowCount > 0)
			{
				document.getElementById('shopping_cart_totalprice').style.display='block';
				document.getElementById('showWager').style.display='block';
				//document.getElementById('HelpDiv').style.display='none';
			}
			else
			{
				document.getElementById('showWager').style.display='none';
				//document.getElementById('HelpDiv').style.display='block';
			}
		} 
		updateTotalPrice();
		UpdateAmount();
	}
	ajaxObjects[ajaxIndex] = false;		
	
}

function updateTotalPrice()
{
	/*var itemBox = document.getElementById('shopping_cart_items');
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	if(document.getElementById('shopping_cart_totalprice')){
		for(var no=1;no<itemBox.rows.length;no++){
			totalPrice = totalPrice + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].innerHTML);
			
		}	*/	
		//alert(document.getElementById('Modifier').value);
		//alert(document.getElementById("Modifier").value);
		document.getElementById('shopping_cart_totalprice').innerHTML = '<br>' + txt_totalPrice + document.getElementById('Modifier').value;
		//document.getElementById('PlaceAmount').innerHTML = document.getElementById('amount').value * document.getElementById('Modifier').value;

		
//	}	
	
}
function UpdateAmount()
{
	var initial=document.getElementById('amount').value * document.getElementById('Modifier').value;
	var finished=initial.toFixed(2);
	document.getElementById('PlaceAmount').innerHTML = finished;
	var bdone=document.getElementById("PlayerBalance").value-document.getElementById('amount').value;
	var bal=bdone.toFixed(2);
	if(bal < 0)
		document.getElementById("BalAmount").style.color="#900000";
	else
		document.getElementById("BalAmount").style.color="black";
	document.getElementById("BalAmount").innerHTML=bal;
	
	
}

function removeProductFromBasket(productId, price, type)
{

	var productRow = document.getElementById('shopping_cart_items_product' + productId);
	var myInt=parseFloat(price); 
	var second=parseFloat(document.getElementById('Modifier').value);
	var rowCount = document.getElementById("shopping_cart_items").getElementsByTagName("TR").length;
	if(rowCount > 2)
		second+=1;
	var price=myInt;
		if(price < 0)
		{
			price*=-1;
			price=100/price;
			price+=1;
			second/=price;
		}
		else
		{
			price=price/100;
			price+=1;
			second/=price;
		}
		second=second-1;
		var tester=second.toFixed(2);
		document.getElementById('Modifier').value=tester;
	var numberOfItemCell = productRow.cells[0];

		productRow.parentNode.removeChild(productRow);	
	
	updateTotalPrice();
	UpdateAmount();
	var rowCount2 = document.getElementById("shopping_cart_items").getElementsByTagName("TR").length;
	if(rowCount2 > 1)
	{
		document.getElementById('showWager').style.display='block';
		//document.getElementById('HelpDiv').style.display='none';
	}
	else
	{
		document.getElementById('showWager').style.display='none';
		//document.getElementById('HelpDiv').style.display='block';
		document.getElementById('shopping_cart_totalprice').style.display='none';
	}
	if(rowCount2 > 12)
	{
		var productRow = document.getElementById("ErrorRow");
		productRow.parentNode.removeChild(productRow);
	}
	ajaxRemoveProduct(productId, type);	
}
function removeProductFromBasket2(productId, price, type)
{
	var productRow = document.getElementById('shopping_cart_items_product' + productId + 'a');
	var myInt=parseFloat(price); 
	var rowCount = document.getElementById("shopping_cart_items").getElementsByTagName("TR").length;
	var second=parseFloat(document.getElementById('Modifier').value);
	if(rowCount > 2)
		second+=1;
	var price=myInt;
		if(price < 0)
		{
			price*=-1;
			price=100/price;
			price+=1;
			second/=price;
		}
		else
		{
			price=price/100;
			price+=1;
			second/=price;
		}
		second=second-1;
		var tester=second.toFixed(2);
		document.getElementById('Modifier').value=tester;
	var numberOfItemCell = productRow.cells[0];

		productRow.parentNode.removeChild(productRow);	
	
	updateTotalPrice();
	UpdateAmount();
	
	var rowCount2 = document.getElementById("shopping_cart_items").getElementsByTagName("TR").length;
	if(rowCount2 > 1)
	{
		document.getElementById('showWager').style.display='block';
		//document.getElementById('HelpDiv').style.display='none';
	}
	else
	{
		document.getElementById('showWager').style.display='none';
		//document.getElementById('HelpDiv').style.display='block';
		document.getElementById('shopping_cart_totalprice').style.display='none';
	}
	if(rowCount2 > 12)
	{
		var productRow = document.getElementById("ErrorRow");
		productRow.parentNode.removeChild(productRow);
	}
	ajaxRemoveProduct(productId, type);	
}


function ajaxRemoveProduct(productId, type)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket + '?productIdToRemove=' + productId + '&Type=' + type;	// Saving product in this file
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(productId, type)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket + '?productId=' + productId + '&Type=' + type;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}