﻿///// Implementation of the PFrame
if(typeof parkside == "undefined") var parkside = new Object();

// Constructor
parkside.ShoppingCart = function() {
}

/// Static variables
parkside.ShoppingCart.timeout = null;

/// Static methods
parkside.ShoppingCart.updateQuantity = function( idx ) {
	if (parkside.ShoppingCart.timeout != null)
		clearTimeout(parkside.ShoppingCart.timeout);
	parkside.ShoppingCart.timeout = setTimeout( function() { parkside.ShoppingCart.updateQuantityRequest( idx ); }, 1500 );
}

parkside.ShoppingCart.updateQuantityRequest = function ( idx ) {
	if( typeof http == "undefined" ) {
		alert("JSMX not included!");
		return;
	}	
		
	var element = document.getElementById("quantity_" + idx);
	if (!element) return;
	
	var quantity = element.value;
	
	try {
		quantity = parseFloat( quantity );
	} catch(ex) {
		element.value = 1;
		quantity = 1;
	}
	
	var params = new Object();
	params.position = idx;
	params.quantity = quantity;
	
	http("GET", "custom/modules/shopping_cart/shopping_cart.cfc?method=updateItemQuantity", parkside.ShoppingCart.updateQuantityResponse, params);
}

parkside.ShoppingCart.updateQuantityResponse = function ( cfc_result ) {

	var element = document.getElementById("quantity_" + cfc_result.idx);
	if (!element) return;
	
	if (cfc_result.error == "false") {
		document.getElementById("shoppingcart_total").innerHTML = cfc_result.total;

		for(i=0; i <  cfc_result.total_tax.length; i++)
		{
			/*document.getElementById("shoppingcart_total_tax_rate_"+i).innerHTML =  cfc_result.total_tax[i].tax_rate;*/
			var k = i+1;
			document.getElementById("shoppingcart_total_tax_"+k).innerHTML = cfc_result.total_tax[i].value;
		}

		document.getElementById("shoppingcart_value_"+cfc_result.idx).innerHTML = cfc_result.new_value;
	}
	
	window.location.reload( true );
}
	
/// Class members
parkside.ShoppingCart.prototype = {
	/// variables

	/// methods
}

