/************************************************************************************************************
(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 = 25;
var url_addUpdateProduct = 'http://www.dandystopsoil.co.uk/components/com_virtuemart/themes/default';


var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var ajaxObjects = new Array();


function showAjaxBasketContent(ajaxIndex)
{

	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
	
	//var nextstep = document.getElementById('nextstep1');
	
	var justaddedRow = document.getElementById('added');
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
	var productItems2 = ajaxObjects[ajaxIndex].response;	// Breaking response from Ajax into tokens

var newRowindex;
//fix carraige return probelm when returning variable fomr ajax

	var package_id = Number(productItems[0]); //value of the package id
/*	alert(package_id);
	alert(productItems[2]);
	alert(productItems[3]);
	*/

//define elements
	var actual_price_element = "actual_price_"+package_id;
	var qty_button_less_element = "button_less_"+package_id;
	var qty_button_more_element = "button_more_"+package_id;

	var decrease_price_difference_element = "decrease_price_difference_"+package_id;
	var increase_price_difference_element = "increase_price_difference_"+package_id;
	
	var increase_qty_element = "increase_qty_"+package_id;
	var decrease_qty_element = "decrease_qty_"+package_id;
	var actual_qty_element = "actual_qty_"+package_id;

//define returned values
	var product_id = productItems[1];
	var postcode = productItems[2];
	var submittedamount = productItems[3];
	var more = Number(productItems[3]) +1;
	var less = Number(productItems[3]) - 1;

	var decrease_price_difference = productItems[6]; 
	var increase_price_difference = productItems[5];
	var increase_price_difference = productItems[5];

	var actualPrice = productItems[4];
    var morePrice = productItems[5]; //price for one more
    var lessPrice = productItems[6]; //price fore two more

	var DandysProductTypeid = productItems[7];
    var DandysDeliveryid = productItems[8];
    var DandysProductid = productItems[9];
    var DandysPackageid = productItems[10];
    var DandysUnlimited = productItems[11];
    var Dandysweight = productItems[12];
   	var DandysPackagesize = productItems[13];
    var DandysDeliverycapacity = productItems[14];
    var DandysPackageName = productItems[15];



	var DandysProductTypeid_element = "DandysProductTypeid-"+package_id;
	var DandysDeliveryid_element = "DandysDeliveryid-"+package_id;
	var DandysProductid_element = "DandysProductid-"+package_id;
	var DandysPackageid_element = "DandysPackageid-"+package_id;
	var DandysUnlimited_element = "DandysUnlimited-"+package_id;
	var Dandysweight_element = "Dandysweight-"+package_id;
	var DandysPackagesize_element = "DandysPackagesize-"+package_id;
	var DandysDeliverycapacity_element = "DandysDeliverycapacity-"+package_id;
	var DandysPackageName_element = "DandysPackageName-"+package_id;
	var amount_element = "amount-"+package_id;


	document.getElementById(DandysProductTypeid_element).value = DandysProductTypeid;
	document.getElementById(DandysDeliveryid_element).value = DandysDeliveryid;
	document.getElementById(DandysProductid_element).value = DandysProductid;
	document.getElementById(DandysPackageid_element).value = DandysPackageid;
	document.getElementById(DandysUnlimited_element).value = DandysUnlimited;
	document.getElementById(Dandysweight_element).value = Dandysweight;
	document.getElementById(DandysPackagesize_element).value = DandysPackagesize;
	document.getElementById(DandysDeliverycapacity_element).value = DandysDeliverycapacity;
	document.getElementById(DandysPackageName_element).value = DandysPackageName;

	//change prices
	document.getElementById(actual_price_element).innerHTML = actualPrice;
	document.getElementById(decrease_price_difference_element).innerHTML = decrease_price_difference;
	document.getElementById(increase_price_difference_element).innerHTML = increase_price_difference;
	
	//change qtys
	document.getElementById(actual_qty_element).innerHTML = submittedamount;
	document.getElementById(decrease_qty_element).innerHTML = less;
	document.getElementById(increase_qty_element).innerHTML = more;
	document.getElementById(amount_element).value = submittedamount;



	document.getElementById(qty_button_less_element).onclick = Function("ajaxAddProduct("+product_id+","+less+",\'"+postcode+"\',"+package_id+");");
	document.getElementById(qty_button_more_element).onclick = Function("ajaxAddProduct("+product_id+","+more+",\'"+postcode+"\',"+package_id+");");






//	psol1 = document.getElementById('shopping_cart_items_product' + productItemsFind);
//	psol2 = document.getElementById('shopping_cart_items_product' + productItems[0]);
	
/*	if(psol2){
		var productItems_0 = productItems[0];
	}else{
		var productItems_0 = Number(productItems[0]);

	}
*/
/*
	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 newRow = itemBox.insertRow(productrowIndex-1);
		newRow.id = 'added'
		
		var td = newRow.insertCell(0);
		td.colSpan = '5'
		td.innerHTML = 'Just Updated'; 	// Just added*/

/*			
		var items = row.cells[0].innerHTML /1;
		items = items + 1;
		row.cells[0].innerHTML = items;
		newRowindex = row.rowIndex;

		//row.cells[0].style.backgroundColor = "#000000";
		
		
		//remove the row with the added item text
		var productrowIndex = row.rowIndex;
		if(justaddedRow){
			itemBox.deleteRow(justaddedRow.rowIndex);
			justaddedRow.deleteCell(0);
		}
		
		//itemBox.moveRow(newRowindex, 1); 
		//nextstep.style.visibility='visible';

	}else{	// Product isn't allready in the basket - add a new row
	
		
	
	
		if(justaddedRow){ // if the row with just added text exists
		
		}else{
				
			var tr = itemBox.insertRow(1);
			tr.id = 'added'

			//tr.moveRow(-1, -1) //Alternate way to move first row to the end of the table

			var td = tr.insertCell(0);
			td.style.color = "#123456";
			td.colSpan = '5'
			td.innerHTML = 'Just Added to your Basket';
			
			// Just added
		}
		



		var tr = itemBox.insertRow(2);
		tr.id = 'shopping_cart_items_product' + productItems[0]
		
		var td = tr.insertCell(-1);
		td.style.width = "1px";
		td.innerHTML = '1'; 	// Number of items
		
		var td = tr.insertCell(-1);

		td.innerHTML = '<a style="font-size:10px;" href="'+productItems[3]+'">'+productItems[1]+'<a>'; 	// Description
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'right';
		td.innerHTML = "$"+productItems[2]; 	// Price	
		newRowindex = tr.rowIndex;

		// add an extra colum
		/*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);*/
		
		/*nextstep.style.visibility='visible';*/



		/*td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';	*/
/*	} 

	showCursorDiv('buypopup','visible','');
	updateTotalPrice();
	itemBox.rows[newRowindex].cells[0].style.backgroundColor = "#F3FB00";
	itemBox.rows[newRowindex].cells[1].style.backgroundColor = "#F3FB00";
	itemBox.rows[newRowindex].cells[2].style.backgroundColor = "#F3FB00";
	document.getElementById('shopping_cart_items_product' + productItems[0]).style.backgroundColor = "#000000";
*/
	
	ajaxObjects[ajaxIndex] = false;		
	
}



function ajaxAddProduct(productId,amount,postcode,package_id)
{
//	alert("product id posted to ajax"+productId);
	//alert("amount posted to ajax"+amount);
	if(amount <= 0 ){
			amount = 1;
	}
	var actual_price_element = "actual_price_"+package_id;

	document.getElementById(actual_price_element).innerHTML = "Getting Price";

	var url_addUpdateProduct = 'http://www.dandystopsoil.co.uk/modules/mod_dandys_ajax_price/dandys_get_price.php?product_id='+productId+'&postcode='+postcode+'&package_id='+package_id;
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addUpdateProduct;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('amount',amount);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

