Command = Class.create();

Command.prototype.update = function(state, parameters)
{
	if(Object.isUndefined(parameters))
		parameters = [];
	workflow_mgr.update("frontoffice/ajax/command/renderWorkflow.php",{"state" : state, "parameters" : Object.toJSON(parameters)});
}

Command.prototype.showAddressForm = function(address_id,element,event)
{
	Event.stop(event);
	popupAddress.options.parameters.address_id = address_id;
	popupAddress.options.parameters.element = element;
	popupAddress.show();
	//this.updateAddressDetails(element,address_id)
}

Command.prototype.updateAddressDetails = function(element, address_id)
{
	var divToUpdate = $(element);
	$(element).update('<img src="backoffice/img/loading.gif" />');
	new Ajax.Updater(divToUpdate,"frontoffice/ajax/address/getDetail.php",{
						 method : "post",
						 asynchronous:false,
						 parameters : {
						 	"addressId" : address_id
					 		},
					 	 onSuccess : function(xhr){
					 			divToUpdate.next().down().writeAttribute({"onclick":"command_mgr.showAddressForm("+address_id+",'"+element+"',event);"});
					 		}
		});
}

Command.prototype.addDiscount = function(discount_id, shipmentAmount)
{ 
	var symbole = "";
	var discount_amount = "";
	new Ajax.Request(
			"frontoffice/ajax/cart/addDiscount.php",
			{
				"method" : "post",
				"parameters" : {
					"ms" : new Date(),
					"discount_id" : discount_id
				},
				onSuccess : function (xhr) {
					if(xhr.responseJSON.discount_is_percent == 0){
						symbole = " €";
						discount_amount = xhr.responseJSON.discount_amount.toFixed(2);
					}
					else{
						symbole = " %";
						discount_amount = xhr.responseJSON.discount_amount * 100;
					}
					$("cartDiscountPrice").innerHTML = " - " + discount_amount + symbole + "<br /><span style='font-size:10px; font-weight:normal; color:#000000'>(<a href='' onclick='command_mgr.removeDiscount("+ discount_id +"); Event.stop(event);'>enlever</a>)</span>";
					$("cartTotalTtcPrice").innerHTML = xhr.responseJSON.total_cart_amount_ttc.toFixed(2) + symbole;
					if(xhr.responseJSON.total_command_amount_ttc !== -1 && $("commandTotalTtcPrice") !== null){
						$("commandTotalTtcPrice").innerHTML = xhr.responseJSON.total_command_amount_ttc.toFixed(2) + " €";
					}
				}
			}
			);
	showDiscountPopup.hide();
}

Command.prototype.removeDiscount = function(discount_id)
{ 
	new Ajax.Request(
			"frontoffice/ajax/cart/removeDiscount.php",
			{
				"method" : "post",
				"parameters" : {
					"ms" : new Date(),
					"discount_id" : discount_id
				},
				onSuccess : function (xhr) {
					$("cartDiscountPrice").innerHTML = "0";
					$("cartTotalTtcPrice").innerHTML = xhr.responseJSON.total_cart_amount_ttc.toFixed(2) + " €";
					if(xhr.responseJSON.total_command_amount_ttc !== -1 && $("commandTotalTtcPrice") !== null){
						$("commandTotalTtcPrice").innerHTML = xhr.responseJSON.total_command_amount_ttc.toFixed(2) + " €";
					}
				}
			}
			);
	showDiscountPopup.hide();
}

Command.prototype.processCommand = function(meanPayment_id)
{
//	var parameters = {};
//	parameters.meanPayment = meanPayment_id ;
//	parameters.meanShipment = window.parseInt($("inpt_meanShipment").value);
//	parameters.addressShipment = window.parseInt($("inpt_addressShipment").value);
//	parameters.addressBilling = window.parseInt($("inpt_addressBilling").value);
//	
//	new Ajax.Request("frontoffice/ajax/cart/createCommand.php",{
//		method : "post",
//		asynchronous : false,
//		parameters : parameters,
//		onSuccess : function(xhr){
//			alert(xhr.responseText);
//			$("buttonPaiement").hide();
//			$("paiementCardArea").show();
//		}
//	});
}

Command.prototype.saveCommandSuccess = function(json)
{
	var parameters = {};
	parameters.commandId = json.commandId;
	parameters.commandAmount = json.commandAmount;
	parameters.commandReference = json.commandReference;
	parameters.clientId = json.clientId;
	parameters.userIpAddress = json.userIpAddress;
	parameters.userEmail = json.userEmail;
	
	new Ajax.Updater(
		$("paiementApiArea"),
		"paiement/Phase1Aller.php",
		{
			"method" : "post",
			"evalScripts" : true,
			"parameters" : parameters
		});
	
	$("paiementButtonArea").hide();
	$("paiementCardArea").show();
}

Command.prototype.addAddressSubmit = function(form,event)
{
	Event.stop(event);
	form.request({
		onSuccess : this.addressSubmitSuccess.bind(this)
	});
}

Command.prototype.addressSubmitSuccess = function()
{
//	popupAddress.hide();
//	this.update(3);
	myAccount_mgr.update("frontoffice/ajax/account/address_book.php");
}

Command.prototype.show = function()	
{
//	this.menu_container.hide();
//	this.cross_container.hide();
	this.update(1);
}

Command.prototype.askPayment = function()
{
	alert("payment");
	//here is the code to enhance
	//payment.
}

Command.prototype.onSubmitShipment = function(form,event)
{
	if(form.serialize().indexOf("shipment_mean_id") == -1)
	{
		alert("Veuillez choisir un moyen de livraison.");
		return;
	}
	Event.stop(event);
	form.request({
		onComplete : this.onSubmitShipmentSuccess.bind(this)
	});
}

Command.prototype.onSubmitShipmentSuccess = function(xhr)
{
	this.update(4,[xhr.responseJSON.billingAddr, xhr.responseJSON.shipmentAddr, xhr.responseJSON.shipmentObj]);
}

Command.prototype.onSubmitShipmentFailure = function(json)
{
	alert("shipment failure");
}

Command.prototype.showInscriptionForm = function()
{
	$("inscriptionTxt").hide();
	$("inscriptionForm").show();
	$("inscriptionFormButton").show();
}