/******************************************************************************
* gshpBasketManager.js
*******************************************************************************
Gestionnaire d panier (et son complement)
*******************************************************************************
Les fonctions de complement du "basket manager complement" sont :
- runAnalysis							Parametres : manager
- onSubmit								Parametres : manager, formManager, onSubmitResult		Return : boolean (Flag of check)

Les fonctions de remplacement du "basket manager complement" sont :
-

*******************************************************************************
*                                                                             *
* Copyright 2008									                          *
*                                                                             *
******************************************************************************/

//
// ------------------------------------- class GshpBasketManager
//
function GshpBasketManager()
{
	// Global init
	this._oComplement = null;

	this._basketType = "B2C";
	this._step = 1;
	this._cmd = "";

	// Map to find comand type
	this._nbCommandType = 0;
	this._commandTypeMap = new Object();

	// Current objects
	this.currentPersistentBasket = null;
}

//	=================== CONSTANTS
//

//	-------------------------------------------------------------------------
//	Global init
//	-------------------------------------------------------------------------
GshpBasketManager.prototype.setBasketType = function(basketType)
{
	this._basketType = basketType;
}

GshpBasketManager.prototype.setBasketState = function(step, cmd)
{
	this._step = step;
	this._cmd = cmd;
}

//	-------------------------------------------------------------------------
//	Persistent basket init
//	-------------------------------------------------------------------------
GshpBasketManager.prototype.setPersistentBasket = function(oid, typeOid, label)
{
	this.currentPersistentBasket = new GshpPersistentBasket(oid, typeOid, label);
}

GshpBasketManager.prototype.setPersistentBasketItems = function(nb, quantity, amount, vatAmount, list)
{
	if (this.currentPersistentBasket != null) {
		this.currentPersistentBasket.setNbItems(nb, quantity);
		this.currentPersistentBasket.setAmount(amount, vatAmount);
		this.currentPersistentBasket.setItems(list);
	}
}

//	-------------------------------------------------------------------------
//	Addition of command types
//	-------------------------------------------------------------------------
GshpBasketManager.prototype.addCommandTypes = function(list)
{
	for (var i=0; i<list.length; i++) {
		var record = list[i].split("|");
		var commandType = new GshpCommandType();
		if (commandType != null) {
			commandType.init(record);
			this._commandTypeMap[commandType.oid] = commandType;
			this._nbCommandType++;
		}
	}
}

//	-------------------------------------------------------------------------
//	Analysis of step
//	-------------------------------------------------------------------------
GshpBasketManager.prototype.runAnalysis = function()
{
	// Analysis of step context
	// - Basket B to B
	if (this._basketType == 'B2B') {
		if (this._step == 1) {
			// Check about command type
			if ((this.currentPersistentBasket != null) && (this.currentPersistentBasket.typeOid != "")) {
				var warningMsg = "";
				var commandType = this._commandTypeMap[this.currentPersistentBasket.typeOid];
				if (commandType == null)
					warningMsg = objThesaurus.translate("gshpUndefinedClientCommandType");
				else
				if (commandType.userHasVisibilityRights == false) 
					warningMsg = objThesaurus.translate("gshpClientCommandTypeInvalidRights");
				else
				if (commandType.isValidDate == false) 
					warningMsg = objThesaurus.translate("gshpClientCommandTypeInvalidDate");
				else
				if (commandType.isValid == false) 
					warningMsg = objThesaurus.translate("gshpInvalidClientCommandType");
				else
				if ( !(((commandType.thresholdMin == null) || (commandType.thresholdMin <= this.currentPersistentBasket.amount)) &&
					   ((commandType.thresholdMax == null) || (commandType.thresholdMax >= this.currentPersistentBasket.amount))))
					warningMsg = objThesaurus.translate("gshpClientCommandTypeInvalidThreshold");

				// Display of warninf message and disabling of button to go to next step
				if (warningMsg != "") {
					// Warning message
					var warningMsgSpan = document.getElementById("gshpWarningCommandTypeMsg");
					if (warningMsgSpan != null) {
						warningMsgSpan.style.display = "block";
						warningMsgSpan.innerHTML = warningMsg;
					}

					// Div with button
					var orderDiv = document.getElementById("gshpBasketButtonDiv_command");
					if (orderDiv != null)
						orderDiv.style.display = "none";
				}
			}
		}
	}

	// Add-on by manager complement
	if ((this._oComplement != null) && (typeof(this._oComplement.runAnalysis) == "function"))
		this._oComplement.runAnalysis(this);
}

//	-------------------------------------------------------------------------
//	OnSubmit of form
//	-------------------------------------------------------------------------
GshpBasketManager.prototype.onSubmit = function(formManager)
{
	var onSubmitResult = true;

	// Add-on by manager complement
	if ((this._oComplement != null) && (typeof(this._oComplement.onSubmit) == "function"))
		onSubmitResult = this._oComplement.onSubmit(this, formManager, onSubmitResult);

	return onSubmitResult;
}

//	-------------------------------------------------------------------------
//	getManagerComplement/setManagerComplement
//	Get/Set of basket manager complement
//	-------------------------------------------------------------------------
GshpBasketManager.prototype.getManagerComplement = function()
{
	return this._oComplement;
}

GshpBasketManager.prototype.setManagerComplement = function(managerComplement)
{
	this._oComplement = managerComplement;
}

//
// ------------------------------------- class GshpCommandType
//
function GshpCommandType()
{
	// Data
	this.oid 					= "";
	this.code 					= "";
	this.label 					= "";
	this.startDate				= null;
	this.endDate				= null;
	this.thresholdMin 			= null;
	this.thresholdMax 			= null;
	this.isValid				= false;
	this.userHasVisibilityRights= false;
	this.isValidDate			= false;
}

//	-------------------------------------------------------------------------
//	Init
//	-------------------------------------------------------------------------
GshpCommandType.prototype.init = function(record)
{
	// Reading of record
	this.oid 			= record[0];
	this.code 			= record[1];
	this.label 			= record[2];

	this.startDate				= (record[3] == "") ? null : parseFloat(record[3]);
	this.endDate				= (record[4] == "") ? null : parseFloat(record[4]);
	this.thresholdMin			= (record[5] == "") ? null : parseFloat(record[5]);
	this.thresholdMax			= (record[6] == "") ? null : parseFloat(record[6]);
	this.isValid				= (record[7] == "true");
	this.userHasVisibilityRights= (record[8] == "true");
	this.isValidDate			= (record[9] == "true");
}


// ------------------------------------- Global object
//
var objGshpBasketManagerComplement;	// May be overriden by decor
var objGshpBasketManager = new GshpBasketManager();

// Recovery of overriden of default manager complement
if (objGshpBasketManagerComplement != null)
	objGshpBasketManager.setManagerComplement( objGshpBasketManagerComplement );
else
	objGshpBasketManager.setManagerComplement( new GshpBasketManagerComplement() );