// JavaScript Document





var googleCheckout_obj = new Object();


// set up variables
	googleCheckout_obj.products_obj = new Object();
	googleCheckout_obj.originalBasketHTML_str = new String();

// add helper functions for fscommand from flash

	googleCheckout_obj.addProductFromString = function(args_str){
		var args_array = args_str.split("|");		
		this.addProduct(args_array[0],args_array[1],args_array[2],args_array[3],args_array[4],args_array[5],args_array[6],args_array[7]);		
	}//end args_str

	googleCheckout_obj.updateQuantityFromString = function(args_str){
		var args_array = args_str.split("|");		
		this.updateQuantity(args_array[0],args_array[1]);		
	}//end args_str

	


// set up functions to manipulate the form
	googleCheckout_obj.addProduct = function(name_str,description_str,quantity_int,price_num,currency_str,shipName_str,shipPrice_num,shipCurrency_str){
		

		// tidy up parameters not passed through
		var error_bln = false;
		var error_str = "Google Checkout Error:\n\n";
		
		// error mistakes
			if (name_str == undefined){
				error_bln = true;
				error_str +="The name of the product wasn't defined\n\n";
			}//endif
			if (price_num == undefined){
				error_bln = true;
				error_str +="The price of the product wasn't defined\n\n";
			}//endif


		// tidy up omissions
			if (quantity_int == undefined) quantity_int = 1;
			if (shipCurrency_str == undefined) shipCurrency_str = "GBP";
			if (currency_str == undefined) currency_str = "GBP";
			
		
		// debug if there is an error and stop
		if (error_bln == true){
			alert(error_str);
			//return false;
		}//endif
	
		
		
		
		// check to see if the product already exists (based on a unique product name)
		var productAlreadyExists_bln = false;
		if (typeof this.products_obj[name_str] == "object") productAlreadyExists_bln = true;
		
		if (productAlreadyExists_bln == false){
			// create new product
			var newProduct_obj = new Object();
			newProduct_obj.name = name_str;
			newProduct_obj.description = description_str;
			newProduct_obj.quantity = quantity_int;
			newProduct_obj.price = price_num;
			newProduct_obj.currency = currency_str;
			newProduct_obj.shipName = shipName_str;
			newProduct_obj.shipPrice = shipPrice_num;
			newProduct_obj.shipCurrency = shipCurrency_str;
			
			this.products_obj[name_str] = newProduct_obj;

			// add new product to the checkout
			this.generateHTML();
			
		}else{
			// product already exists
			alert("This product is already in your basket")
		}//endif
		
				
		
	}//end addProduct

	googleCheckout_obj.removeProduct = function(name_str){
		delete this.products_obj[name_str];
		this.generateHTML();
	}//end removeProduct
	
	
	googleCheckout_obj.updateQuantity = function(name_str,quantity){
		this.products_obj[name_str].quantity = quantity;
		this.generateHTML();
	}//end updateQuantity






	googleCheckout_obj.generateHTML = function(){
		
		var basketHTML_str = "";
		
		var loop_int = 0;
		for (var loopItem in this.products_obj){
			var thisProduct = this.products_obj[loopItem];
			
			basketHTML_str += '<input type="hidden" name="item_name_' + (loop_int +1) + '" value="' + thisProduct.name + '"/>';
			basketHTML_str += '<input type="hidden" name="item_description_' + (loop_int +1) + '" value="' + thisProduct.description + '"/>';
			basketHTML_str += '<input type="hidden" name="item_quantity_' + (loop_int +1) + '" value="' + thisProduct.quantity + '"/>';
			basketHTML_str += '<input type="hidden" name="item_price_' + (loop_int +1) + '" value="' + thisProduct.price + '"/>';
			basketHTML_str += '<input type="hidden" name="item_currency_' + (loop_int +1) + '" value="' + thisProduct.currency + '"/>';
			
			if (thisProduct.shipName != null && thisProduct.shipName != ""){
				basketHTML_str += '<input type="hidden" name="ship_method_name_' + (loop_int +1) + '" value="' + thisProduct.shipName + '"/>';
				basketHTML_str += '<input type="hidden" name="ship_method_price_' + (loop_int +1) + '" value="' + thisProduct.shipPrice + '"/>';
				basketHTML_str += '<input type="hidden" name="ship_method_currency_' + (loop_int +1) + '" value="' + thisProduct.shipCurrency + '"/>';
			}//endif
			
			loop_int++;
		}//endfor
		document.all.basket.innerHTML = basketHTML_str + this.originalBasketHTML_str;
		
		if (loop_int > 0){
			// enable checkout button
			document.all.GoogleCheckoutImage.disabled = false
		}else{
			// disable checkout button
			document.all.GoogleCheckoutImage.disabled = true
		}//endif
		
	}//end generateHTML



	googleCheckout_obj.checkout = function(){
		// call the checkout (post the form)
		alert("Thank you.\n\nNow proceeding to Google Checkout.")
		document.all.googleCheckout.submit();
	}//end checkout
	
	
	googleCheckout_obj.debug = function(){
		alert(document.all.basket.innerHTML);
	}//end debug
	
	
	// draw the initial empty basket and make the button non clicable
	//googleCheckout_obj.generateHTML();


	// do an initial debug to check the value of the initial basket
	googleCheckout_obj.originalBasketHTML_str = document.all.basket.innerHTML;
	
	
	
	

