

/* <a-zone id="history"> */
/* </a-zone> */


/**
 * This field is used to know if AJAX has already been initialized
 */
 var initDone = false;

/* <a-zone id="fields"> */
/* </a-zone> */

 function initAJAX() {jsonrpc = new JSONRpcClient("/rdc/remoteBasketManager.jsp?ajax-call=true");	
	initDone = true;
	/* <a-zone id="initAJAX"> */
	/* </a-zone> */
}

 function addProductRPC( cug,  qte) {
 
	if (initDone == false) {
		initAJAX();
	}
	
	jsonrpc.remoteBasketManager.addProductRPC(treatAddProductRPC, cug, qte);
}

 function treatAddProductRPC( result,  exception) {
	/* <a-zone id="treatAddProductRPC"> */
	if (onAddProductRPCException != null && exception != null) {
		onAddProductRPCException(exception);
	}
	if (onAddProductRPCSuccess != null && result != null) {
		onAddProductRPCSuccess(result);
	}
	/* </a-zone> */
}

 function onAddProductRPCException( exception) {
	/* <a-zone id="onAddProductRPCException"> */
	if (exception) {
		//alert(exception.message);
		//alert(exception.code);
		//alert(exception.name);
	}
	/* </a-zone> */
}

 function onAddProductRPCSuccess( result) {
	/* <a-zone id="onAddProductRPCSuccess"> */
	if (result != null) {
		//alert(result);
	}
	/* </a-zone> */
}

 function removeAllItemsRPC() {
	if (initDone == false) {
		initAJAX();
	}
	jsonrpc.remoteBasketManager.removeAllItemsRPC(treatRemoveAllItemsRPC);
}

 function treatRemoveAllItemsRPC( result,  exception) {
	/* <a-zone id="treatRemoveAllItemsRPC"> */
	if (onRemoveAllItemsRPCException != null && exception != null) {
		onRemoveAllItemsRPCException(exception);
	}
	//~ if (onRemoveAllItemsRPCSuccess != null && result != null) {
	if (onRemoveAllItemsRPCSuccess != null) {
		onRemoveAllItemsRPCSuccess(result);
	}
	/* </a-zone> */
}

 function onRemoveAllItemsRPCException( exception) {
	/* <a-zone id="onRemoveAllItemsRPCException"> */
	if (exception) {
	//	alert(exception.message);
		//alert(exception.code);
		//alert(exception.name);
	}
	/* </a-zone> */
}

 function onRemoveAllItemsRPCSuccess( result) {
	/* <a-zone id="onRemoveAllItemsRPCSuccess"> */
	basket.shoppingCart = new Array();
	graphicBasket.drawBasket();
	/* </a-zone> */
}

 function setProductQuantityRPC( cug,  qte) {
	if (initDone == false) {
		initAJAX();
	}
	//jsonrpc.remoteBasketManager.setProductQuantityRPC(treatSetProductQuantityRPC, cug, qte);
	jsonrpc.remoteBasketManager.setProductQuantityRPC(cug, qte);
}

 function treatSetProductQuantityRPC( result,  exception) {
	/* <a-zone id="treatSetProductQuantityRPC"> */
	if (onSetProductQuantityRPCException != null && exception != null) {
		onSetProductQuantityRPCException(exception);
	}
	if (onSetProductQuantityRPCSuccess != null && result != null) {
		onSetProductQuantityRPCSuccess(result);
	}
	/* </a-zone> */
}

 function onSetProductQuantityRPCException( exception) {
	/* <a-zone id="onSetProductQuantityRPCException"> */
	if (exception) {
		//alert(exception.message);
		//alert(exception.code);
		//alert(exception.name);
	}
	/* </a-zone> */
}

 function onSetProductQuantityRPCSuccess( result) {
	/* <a-zone id="onSetProductQuantityRPCSuccess"> */
	if (result != null) {
		//alert(result);
	}
	/* </a-zone> */
}
/* <a-zone id="applicative-code"> */

function RPCcall() {
	// Initialize the "connection" immedialty to have a more responsive call after.
	try {	
		initAJAX();
	} catch(e) {
	}

	this.setProductQuantity = function(cug, quantity) {
		try {
			setProductQuantityRPC(cug, quantity);
		} catch(e) {
			// exception code==0 when the XMLHttpRequest object can not be created.
			if(e.code==0) {
				var controlForm = frameNoAjax.remoteBasketManagerForm;
				controlForm.remoteMethod.value = "setProductQuantityRPC";
				controlForm.cug.value = cug;
				controlForm.quantity.value = quantity;
				controlForm.submit();
			}
		}
	}
	
	this.removeAllItems = function() {
		try {
			removeAllItemsRPC();
		} catch(e) {
			// exception code==0 when the XMLHttpRequest object can not be created.
			if(e.code==0) {
				var controlForm = frameNoAjax.remoteBasketManagerForm;
				controlForm.remoteMethod.value = "removeAllItemsRPC";
				controlForm.submit();
			}
		}
	}
	
	this.addProduct = function(cug, qte) {
		try {
			addProductRPC(cug,qte);
		} catch(e) {
			// exception code==0 when the XMLHttpRequest object can not be created.
			if(e.code==0) {
				var controlForm = frameNoAjax.remoteBasketManagerForm;
				controlForm.remoteMethod.value = "addProductRPC";
				controlForm.cug.value = cug;
				controlForm.quantity.value = qte;
				controlForm.submit();
			}
		}
	}

}

function exceptionTreatement(exception) {

	if (exception) {
	alert("exceptionTreatement RPCcall() remoteBasketManager.js ="+exception);
		if(exception.code==550) {
			// session expired
			top.location = "/index.jsp";
		}
	}
}

/* </a-zone> */

