var flyingSpeed = 25;
/*var url_addProductToBasket = 'http://www.raiseo.com/addProduct.php';
var url_removeProductFromBasket = 'http://www.raiseo.com/removeProduct.php';*/

var url_addProductToBasket = 'addProduct.php';
var url_removeProductFromBasket = 'removeProduct.php';


//var txt_totalPrice = 'Total: ';
var txt_totalPrice = '';

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)
{
	//alert(inputObj)
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}
	

function addToBasket(productId,groupId)
{

	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,groupId);
	
}


function flyToBasket(productId,groupId)
{

	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 + '","' + groupId + '")',10); else ajaxAddProduct(productId,groupId);	
}

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
	
	//alert(productItems )
	
	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[0].innerHTML /1;
		//alert(row.rows[1].cells[1].innerHTML )
		items = row.rows[1].cells[1].innerHTML /1;
		items = items + 1;
		row.rows[1].cells[1].innerHTML = items;
	}else{	// Product isn't allready in the basket - add a new row
		
		CreateTable('4', '2', 'shopping_cart', ajaxIndex)
		

		/*var tr = itemBox.insertRow(-1);
		tr.id = 'shopping_cart_items_product' + productItems[0]
		
		var td = tr.insertCell(-1);
		td.innerHTML = '1'; 	// Number of items
		
		var td = tr.insertCell(-1);
		td.innerHTML = productItems[1]; 	// Description
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'right';
		td.innerHTML = productItems[2]; 	// Price	
		
		var td = tr.insertCell(-1);
		var a = document.createElement('A');
		td.appendChild(a);
		a.href = '#';
		a.onclick = function(){ removeProductFromBasket(productItems[0]); };
		var img = document.createElement('IMG');
		img.src = 'images/remove.gif';
		a.appendChild(img);*/
		
		
		
		//td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';	
	} 


	updateTotalPrice();
	
	ajaxObjects[ajaxIndex] = false;		
	
}

function showAjaxBasketContentFromDB(cartrow)
{
	
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
	var productItems = cartrow.split('|||');	// Breaking response from Ajax into tokens
	CreateTableFromDB('4', '2', 'shopping_cart', cartrow)
	updateTotalPrice();
}


		var DEFAULT_WIDTH = 140;
		var DEFAULT_HEIGHT = 100;

		function CreateTable(rowCount, colCount, srcHolder,ajaxIndex)
		{
			var itemBox = document.getElementById('shopping_cart_items');
			var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
			if(IsValidNumber(rowCount) && IsValidNumber(colCount) && (srcHolder != null) )
			{
				srcHolder.innerHTML = "";
				var srcTable = document.createElement("table");
				//alert(srcTable )
				srcTable.border = 0;
				srcTable.borderColor = "Black";
				//srcTable.height = DEFAULT_HEIGHT;
				srcTable.width = DEFAULT_WIDTH;
				srcTable.cellPadding="1";
				srcTable.cellSpacing="1";
				srcTable.id = 'shopping_cart_items_product' + productItems[0];
				var tmpRow = null;
				var tmpCell = null;
				
				document.getElementById(srcHolder).appendChild(srcTable);
				//alert(document.getElementById(srcHolder).innerHTML)
				
				for(i=0; i<rowCount; i++)
				{
					tmpRow = srcTable.insertRow(i);
					for(j=0; j<colCount; j++)
					{
						tmpCell = tmpRow.insertCell(j);
						tmpCell.innerHTML = j;
						tmpCell.id = 'shopping_cart_items_productDetails' + i+ '_'+j ;
						
							if(tmpCell.id == "shopping_cart_items_productDetails0_0"){
								tmpCell.innerHTML = '<strong>'+productItems[1]+'</stong>';
								tmpCell.align = 'left';
								
							}
							if(tmpCell.id == "shopping_cart_items_productDetails0_1"){
								abc = '<a href="#" onclick=removeProductFromBasket(' + productItems[0] + ');return false;><img height="15" width="16" border="0" src="http://www.raiseo.com/affiliate/images/icon_cart.gif"/></a>';
								tmpCell.innerHTML = abc;
								tmpCell.align = 'right';
							}
						
							if(tmpCell.id == "shopping_cart_items_productDetails2_0"){
								tmpCell.innerHTML = '<strong>Price:</strong>';
								tmpCell.align = 'left';
							}
						
							if(tmpCell.id == "shopping_cart_items_productDetails2_1"){
								tmpCell.innerHTML = "$"+productItems[2];
								tmpCell.align = 'right';
							}
							
							if(tmpCell.id == "shopping_cart_items_productDetails1_0"){
								tmpCell.innerHTML = '<strong># of Item</strong>';
								tmpCell.align = 'left';
							}
						
							if(tmpCell.id == "shopping_cart_items_productDetails1_1"){
								tmpCell.innerHTML = '1';
								tmpCell.align = 'right';
							}							

							if(tmpCell.id == "shopping_cart_items_productDetails3_0"){
								//alert(tmpCell)
								tmpCell.colSpan = '2';
								tmpCell.innerHTML = '<hr size="1" width="100%" />';
								break;

							}							

						tmpCell = null;
					}
					tmpRow = null;
				}
			}
		}
		
		
	function CreateTableFromDB(rowCount, colCount, srcHolder,cartrow)
		{
			var itemBox = document.getElementById('shopping_cart_items');
			var productItems = cartrow.split('|||');	// Breaking  into tokens
			if(IsValidNumber(rowCount) && IsValidNumber(colCount) && (srcHolder != null) )
			{
				srcHolder.innerHTML = "";
				var srcTable = document.createElement("table");
				//alert(srcTable )
				srcTable.border = 0;
				srcTable.borderColor = "Black";
				//srcTable.height = DEFAULT_HEIGHT;
				srcTable.width = DEFAULT_WIDTH;
				srcTable.cellPadding="1";
				srcTable.cellSpacing="1";
				srcTable.id = 'shopping_cart_items_product' + productItems[0];
				var tmpRow = null;
				var tmpCell = null;
				
				document.getElementById(srcHolder).appendChild(srcTable);
				//alert(document.getElementById(srcHolder).innerHTML)
				
				for(i=0; i<rowCount; i++)
				{
					tmpRow = srcTable.insertRow(i);
					for(j=0; j<colCount; j++)
					{
						tmpCell = tmpRow.insertCell(j);
						tmpCell.innerHTML = j;
						tmpCell.id = 'shopping_cart_items_productDetails' + i+ '_'+j ;
						
							if(tmpCell.id == "shopping_cart_items_productDetails0_0"){
								tmpCell.innerHTML = '<strong>'+productItems[1]+'</stong>';
								tmpCell.align = 'left';
								
							}
							if(tmpCell.id == "shopping_cart_items_productDetails0_1"){
								abc = '<a href="#" onclick=removeProductFromBasket(' + productItems[0] + ');return false;><img height="15" width="16" border="0" src="http://www.raiseo.com/affiliate/images/icon_cart.gif"/></a>';
								tmpCell.innerHTML = abc;
								tmpCell.align = 'right';
							}
						
							if(tmpCell.id == "shopping_cart_items_productDetails2_0"){
								tmpCell.innerHTML = '<strong>Price:</strong>';
								tmpCell.align = 'left';
							}
						
							if(tmpCell.id == "shopping_cart_items_productDetails2_1"){
								tmpCell.innerHTML = "$"+productItems[2];
								tmpCell.align = 'right';
							}
							
							if(tmpCell.id == "shopping_cart_items_productDetails1_0"){
								tmpCell.innerHTML = '<strong># of Item</strong>';
								tmpCell.align = 'left';
							}
						
							if(tmpCell.id == "shopping_cart_items_productDetails1_1"){
								tmpCell.innerHTML = productItems[3];
								tmpCell.align = 'right';
							}							

							if(tmpCell.id == "shopping_cart_items_productDetails3_0"){
								//alert(tmpCell)
								tmpCell.colSpan = '2';
								tmpCell.innerHTML = '<hr size="1" width="100%" />';
								break;

							}							

						tmpCell = null;
					}
					tmpRow = null;
				}
			}
		}



		function AppendRow(srcTable)
		{
			if(srcTable != null)
			{
				return srcTable.insertRow();
			}
			else
			{
				alert("Error while creating table. Cause: Container Table is null!");
			}
		}

		function AppendCell(srcRow)
		{
			if(srcRow != null)
			{
				return srcRow.insertCell();
			}
			else
			{
				alert("Error while creating table. Cause: Container row is null!");
			}
		}

		function IsValidNumber(ipNum)
		{
			if(isNaN(ipNum))
			{
				alert("Invalid Number!");
				return false;
			}
			else if(ipNum < 1)
			{
				alert("Number should be greater than 0!");
				return false;
			}
			else
			{
			return true;
			}
		}

function numberOfChild(parent1)
{
	var num = 0;
	var thisChild = parent1.firstChild;
	while ( thisChild != parent1.lastChild )
	{
/*		if ( thisChild.nodeType == 1 )
		{
			num = num +1 /1;
		}
*/		thisChild = thisChild.nextSibling;
		num = num +1 /1;
		
	}
	return num
}





function updateTotalPrice()
{
	//alert(document.getElementById('shopping_cart_items'))
	var itemBox = document.getElementById('shopping_cart');
//	alert(numberOfChild(itemBox))
//	var count = numberOfChild(itemBox))
	var count = itemBox.childNodes.length;
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	if(document.getElementById('shopping_cart_totalprice')){
		for(var n=0;n<count;n++){
			//totalPrice = totalPrice + (itemBox.childNodes[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].innerHTML);
			//totalPrice = totalPrice + (itemBox.childNodes[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].innerHTML);
			totalPrice = totalPrice + itemBox.childNodes[n].rows[1].cells[1].innerHTML.replace(/[^0-9]/g) *  itemBox.childNodes[n].rows[2].cells[1].innerHTML.replace("$","")
			//alert(totalPrice)
		}	
		document.getElementById('shopping_cart_totalprice').innerHTML = '$'+ (txt_totalPrice + totalPrice.toFixed(2));
        document.getElementById('shopping_cart_togroup_price').innerHTML = '$'+(totalPrice * 0.4).toFixed(2);
		
	}	
	
}

function removeProductFromBasket(productId)
{
	var productRow = document.getElementById('shopping_cart_items_product' + productId);
	var numberOfItemCell = productRow.rows[1].cells[1] ;
	if(numberOfItemCell.innerHTML == '1'){
		productRow.parentNode.removeChild(productRow);	
	}else{
		//alert(numberOfItemCell.innerHTML)
		numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;
	}
	updateTotalPrice();
	ajaxRemoveProduct(productId);	
}

function ajaxValidateRemovedProduct(ajaxIndex)
{
	var rem_response=ajaxObjects[ajaxIndex].response;
	if(rem_response*1 == 1){
	}else{
		alert('Error while removing product from the database');
	}
		
	
}

function ajaxRemoveProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	var urlString = "http://"+window.location.hostname+"/"+url_removeProductFromBasket + '?productIdToRemove=' + productId;
	ajaxObjects[ajaxIndex].requestFile = urlString;	// Saving product in this file
//	ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(productId,groupId)
{
	
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	var urlString = "http://"+window.location.hostname+"/"+url_addProductToBasket + '?productId=' + productId +'&groupId=' + groupId;
	
	ajaxObjects[ajaxIndex].requestFile = urlString;	// Saving product in this file
	//ajaxObjects[ajaxIndex].setVar('productId',productId);
	
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}


/* Added by Jayanta on 230909*/

function ajaxListFilterProduct(productName, groupId, groupName,queryorder,mainfilter)
{
	
	
	var ajaxIndex1 = ajaxObjects.length;
	ajaxObjects[ajaxIndex1] = new sack();
	var urlString1 = 'http://'+window.location.hostname+'/filter-product.php?productName=' + productName+ '&groupId='+groupId+'&groupName='+groupName+'&queryorder='+queryorder+'&mainfilter='+mainfilter;
	
	ajaxObjects[ajaxIndex1].requestFile = urlString1;	// Saving product in this file
	//ajaxObjects[ajaxIndex].setVar('productId',productId);
	
	ajaxObjects[ajaxIndex1].onCompletion = function(){ showAjaxFilterContent(ajaxIndex1); };	// Specify function that will be executed after file has been found
	
	ajaxObjects[ajaxIndex1].runAJAX();		// Execute AJAX function
}

function showAjaxFilterContent(ajaxIndex1)
{
	
	
	var productFilters = ajaxObjects[ajaxIndex1].response;
	document.getElementById("filter_product").innerHTML	= productFilters;
	ajaxObjects[ajaxIndex1] = false;		
	
}

function updateRenewal(fldId)
{
	
	var fldRenew = document.getElementById("renewal").value;
	
	var ajaxIndex2 = ajaxObjects.length;
	ajaxObjects[ajaxIndex2] = new sack();
	var urlString2 = 'http://'+window.location.hostname+'/store/update_renewal.php?fldId=' + fldId+ '&fldRenew='+fldRenew;
	ajaxObjects[ajaxIndex2].requestFile = urlString2;	// Saving product in this file
	//ajaxObjects[ajaxIndex].setVar('productId',productId);
	
	ajaxObjects[ajaxIndex2].onCompletion = function(){ showAjaxUpdaterenewal(ajaxIndex2); };	// Specify function that will be executed after file has been found
	
	ajaxObjects[ajaxIndex2].runAJAX();		// Execute AJAX function
		
}

function showAjaxUpdaterenewal(ajaxIndex2)
{
	
	
	var updateRenewal = ajaxObjects[ajaxIndex2].response;
	alert("Renewal has been Updated Successfully!");
	ajaxObjects[ajaxIndex2] = false;		
	
}