// JavaScript Document
/* 
 * author Vaclav Rak
 * vytvoreno Tuesday, November 27, 2007 20:35
 * kontakt rak@avast.com 
 * pro ALWIL Software
 *
 * $Log$
 *
 */
 
 function suiteObject(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, InternalId){
	this.inheritFrom = productObject;	
	this.inheritFrom('suiteObject', maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, InternalId);
	this.name = "suiteObject";
	this.requestedLicence = new Array();		// contain requested number of licenses
	this.configuration = new Array(); 			// contain best price offer
	this.bestConfiguration = new Array();		// best configuration set in getBestOffer
	this.pricePerLicense = new Array();			// price per licese by internalId
	this.originalPrice = new Array();			// original price without any discount
	this.renewalDiscountPrice = new Array();	// renewal discount in money
	this.renewalDiscount = new Array();			// renewal in percent
	this.finalPrice = new Array();				// final price of current suite product
	this.productDiscountPrice = new Array();	// discount amount in money
	this.productDiscount = new Array();			// product discount in percent
	this.productDiscountDescription = new Array();// product discount description
	this.suiteProducts = new Array();			// product internalId what contain suite 
	this.reachMaximumLicNo = 0;
	this.unlimitedFromList = new Array();		// copy of unlimited list from calculator
	this.minLic = NaN; 
	this.maxLic = NaN; 
	this.lastCalculation = NaN;					// tell me whether is trend down or up
	this.previousCalculation = NaN;				// how much cost previous configuration? trend question 
	this.startCountAt = NaN;					// we had started with this number of licenses
	this.idealSuiteCount = NaN; 				// ideal configuration
	/*
	 * parametr products get an associative array InternalId => numberOfLicences
	 */
	this.acceptRequest = function(products){
		this.requestedLicence = products;
	}
	this.writeDebug = function(msg){
		var debug = document.getElementById('debug');
		debug.innerHTML += msg;
		debug.innerHTML += "<hr>";
	}
	this.setBestConfiguration = function(){
		this.bestConfiguration = new Array();
		for(internaId in this.configuration) {
			this.bestConfiguration[internaId] = new Array();
			var tmpArr = new Object();
			tmpArr.configuration  = this.configuration[internaId];
			tmpArr.pricePerLicense  = this.pricePerLicense[internaId];
			tmpArr.originalPrice = this.originalPrice[internaId];
			tmpArr.renewalDiscountPrice  = this.renewalDiscountPrice[internaId];
			tmpArr.renewalDiscount  = this.renewalDiscount[internaId];
			tmpArr.finalPrice  = this.finalPrice[internaId];
			tmpArr.productDiscountPrice  = parseFloat(this.productDiscountPrice[internaId]);
			tmpArr.productDiscount  = this.productDiscount;// alert( this.productDiscount);
			tmpArr.productDiscountDescription  = this.productDiscountDescription;
			this.bestConfiguration[internaId] = tmpArr;
		}
	}
	
	this.setMaxLicenseToCheck = function(){
		if (((this.requestedLicence['SE'] * 10) +1) > (this.requestedLicence['PE']+this.requestedLicence['SE'])){
			this.reachMaximumLicNo 	= (parseInt(this.requestedLicence['SE']) * 10) +parseInt(this.requestedLicence['SE']);
		} else {
			// this.reachMaximumLicNo = this.pricelistObject.getNextLevel(this.InternalId, (parseInt(this.currentLicences/10)) * 10); // old version commented 2008-07-17
			this.reachMaximumLicNo = this.pricelistObject.getNextLevel(this.InternalId, this.requestedLicence['PE']+this.requestedLicence['SE']);
			
		}
		if (this.reachMaximumLicNo > this.maxLic) this.reachMaximumLicNo = this.maxLic;
	}
	
	this.getBestOffer = function(){
		var bestPrice = this.coverRequirements();
		this.setBestConfiguration();
		var price = 0;
		this.setMaxLicenseToCheck();
		while(this.reachMaximumLicNo > this.currentLicences) {
			price = this.nextCover();
			if (price < bestPrice) {
				bestPrice = price;
				this.setBestConfiguration();
			} 
		}
		return bestPrice;
	}
	
	this.getLicenceNumber = function(){
		return this.currentLicences;
	}
	
	this.calculate = function (){
		var finalSuitePrice = 0;
		for(internalId in this.configuration){
			if (typeof(this.unlimitedFromList[internalId]!= 'undefined')){
				var licNo = this.configuration[internalId];
				if (licNo > this.unlimitedFromList[internalId]) licNo = this.unlimitedFromList[internalId];
				this.pricePerLicense[internalId] = this.pricelistObject.getPricePerLicense(internalId, this.getCurrency(), licNo, this.getMaintenanceContract());
				this.originalPrice[internalId] =  this.pricePerLicense[internalId]  * licNo;
			}else{
				this.pricePerLicense[internalId] = this.pricelistObject.getPricePerLicense(internalId, this.getCurrency(), this.configuration[internalId], this.getMaintenanceContract());
				this.originalPrice[internalId] =  this.pricePerLicense[internalId]  * this.configuration[internalId];
			}
			this.renewalDiscountPrice[internalId] = (this.getRenewalByInternalId(internalId)/100) * this.originalPrice[internalId];
			this.renewalDiscount[internalId] = this.getRenewalByInternalId(internalId);
			this.finalPrice[internalId] = this.originalPrice[internalId] - this.renewalDiscountPrice[internalId];
			this.productDiscountPrice[internalId] = (this.getDiscount()/100) * (this.finalPrice[internalId]);
			this.productDiscount[internalId] = this.getDiscount(); 
			this.productDiscountDescription[internalId] = this.productDiscountDescription;
			this.finalPrice[internalId] = this.finalPrice[internalId] - this.productDiscountPrice[internalId];
			finalSuitePrice += this.finalPrice[internalId];
		}
		if (this.InternalId == 'STDS' || this.InternalId == 'ENTS'){
			var lines = "<b>" + this.InternalId+"</b><br>";
			for(var ix in this.configuration){
				lines += ix + ": " + this.configuration[ix] + "\n<br>";
			}
			//this.writeDebug(lines);
		}
		this.lastCalculation = finalSuitePrice;
		return finalSuitePrice;
	}
	this.getLicenceNumber = function(){
		return this.currentLicences
	}
	
	this.addLicence = function(internalId, licNumber){
		if (typeof(this.requestedLicence[internalId]) != 'undefined'){
			if (licNumber > this.configuration[this.InternalId]) {
				this.configuration[internalId] = licNumber - this.configuration[this.InternalId];
			}
			this.minLic = this.pricelistObject.getMinimalLicenses(internalId);
			if (this.configuration[internalId] < this.minLic) this.configuration[internalId] = this.minLic;
		}
	}

	this.fullFillRequest = function(){
		for(internalId in this.suiteProducts){
			this.addLicence(this.suiteProducts[internalId], this.requestedLicence[this.suiteProducts[internalId]]);
		}
	}
	this.addRestLicences = function(){
		for(internalId in this.requestedLicence){
			if (internalId != "SE" && internalId != "PE" && !this.inA(this.suiteProducts, internalId)){
				this.configuration[internalId] =  this.requestedLicence[internalId];
				this.minLic = this.pricelistObject.getMinimalLicenses(internalId);
				if (this.configuration[internalId] < this.minLic) this.configuration[internalId] = this.minLic;
				if (this.configuration[internalId] > this.maxLic) this.configuration[internalId] = NaN;

			}
		}
	}

	this.getBaseSuiteLicenceNo = function(){
		var licNumber = 0;
		var addServer = 0;
		if (isNaN(this.minLic)) this.minLic = parseInt(this.pricelistObject.getMinimalLicenses(this.InternalId));
		if (isNaN(this.maxLic)) this.maxLic = parseInt(this.pricelistObject.getMaximumLicenses(this.InternalId));

		this.configuration = new Array();
		//var lic = parseInt(this.requestedLicence['PE']);
		//minLi = this.getMinLicenceNumber(lic);
		//if (lic < this.minLic) lic = this.minLic;
		var se =  0;
		var pe =  0;
		if (!isNaN(this.requestedLicence['SE']))  se = this.requestedLicence['SE'];
		if (!isNaN(this.requestedLicence['PE']))  pe = this.requestedLicence['PE'];
		if (se > 0){
			if (((se-1)*10) < (pe + se)) {
				this.currentLicences = (se-1)*10;	
			} else {
				this.currentLicences = pe + se;
			}
		} else if (pe > 0) {
			this.currentLicences = pe;
		} else {
			this.currentLicences = 0;
		} 
		if (this.currentLicences < this.minLic) this.currentLicences = this.minLic;
		
		this.configuration[this.InternalId] = this.currentLicences;
		if ((se > 0) && ((se * 10 +1) > this.configuration[this.InternalId])) {
			if (se - (parseInt(this.configuration[this.InternalId]/10) +1)  >0 ) this.configuration['SE'] = se - (parseInt(this.configuration[this.InternalId]/10) +1);
		}
		if ((se > 0) && (pe > this.configuration[this.InternalId] -se)) {
			this.configuration['PE'] = pe - (this.configuration[this.InternalId] -se) ;
		}
		if (isNaN(this.startCountAt) || this.startCountAt == 0) this.startCountAt = this.configuration[this.InternalId];
		if (isNaN(this.idealSuiteCount) || this.idealSuiteCount == 0) this.idealSuiteCount = ((se -1) * 10) +  ((pe - (((se -1) * 10) - se)) > 0 ? (pe - (((se -1) * 10) - se)) : 0);
		if (this.startCountAt > this.configuration[this.InternalId]) this.startCountAt = this.configuration[this.InternalId];
	}
	
	this.getNextBaseLicenceNo = function(){
		var pe = parseInt(this.requestedLicence['PE']);
		var se = parseInt(this.requestedLicence['SE']);
		if (isNaN(pe)) pe = 0;
		if (isNaN(se)) se = 0;
		var divPart = parseInt(this.currentLicences / 10);
		if (this.currentLicences  > ((parseInt(this.startCountAt/10)+1)*10)) { 			// we reach first decade, next jump will be by 10th
			if (this.lastCalculation <= this.previousCalculation) {			// previous calculation has higher price, lets try next decade
				this.currentLicences = (parseInt(this.currentLicences/10)+1) * 10;
			} else {														// previous calculation has lower price, lets jump to next license level
				this.currentLicences = this.pricelistObject.getNextLevel(this.InternalId, this.currentLicences );
			}
		} else {
			this.currentLicences = this.currentLicences +1 ;//;
		}
		if (this.currentLicences > this.idealSuiteCount) { 
			this.currentLicences = this.idealSuiteCount;
			this.idealSuiteCount = this.reachMaximumLicNo +1; // disable ideal count
		}
		
		this.previousCalculation =  this.lastCalculation;

		var maxServer = parseInt((this.currentLicences)/ 10) +1;
		this.configuration = new Array();
		this.configuration[this.InternalId] = this.currentLicences ;
		if (maxServer < se) {	
			this.configuration['SE'] = se - maxServer;
		} 
		if ((se > 0) && (pe > this.configuration[this.InternalId] -se)) {
			this.configuration['PE'] = pe - (this.configuration[this.InternalId] -se) ;
		}
		/*
		if ((this.configuration[this.InternalId] - maxServer) > this.configuration['PE']){
			this.configuration['PE'] = this.configuration[this.InternalId] - maxServer - this.configuration['PE'];
		}
		*/
		
	}

}

function pfpSuiteObject(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl){
	this.inheritFrom = suiteObject;
	this.inheritFrom(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, "PFP");
	this.name = "pfpSuiteObject";
	
	this.setMaxLicenseToCheck = function(){
		this.reachMaximumLicNo = 1;
		if (this.reachMaximumLicNo > this.maxLic) this.reachMaximumLicNo = this.maxLic;

	}
	
	this.getBaseSuiteLicenceNo = function(){
		this.configuration = new Array();
		if (this.getCustomerType() == "home"){
			this.configuration[this.InternalId] = 1;
			this.currentLicences = 1 ;
		} else {
			this.configuration[this.InternalId] = NaN;
			this.currentLicences = NaN;
		}
	}
	this.coverRequirements = function(){
		if (isNaN(this.minLic)) this.minLic = parseInt(this.pricelistObject.getMinimalLicenses(this.InternalId));
		if (isNaN(this.maxLic)) this.maxLic = parseInt(this.pricelistObject.getMaximumLicenses(this.InternalId));
		this.getBaseSuiteLicenceNo();
		//this.configuration['SE'] = 0;
		if (parseInt(this.requestedLicence['WHSE']) > 1) this.configuration['WHSE'] =  parseInt(this.requestedLicence['WHSE']) -1;
		if (this.requestedLicence['PE'] > 10) this.configuration['PE'] =  this.requestedLicence['PE'] -10;
		for(internalId in this.requestedLicence){
			if (internalId != "WHSE" && internalId != "PE" && !this.inA(this.suiteProducts, internalId)){
				this.configuration[internalId] =  this.requestedLicence[internalId];
				this.minLic = this.pricelistObject.getMinimalLicenses(internalId);
				if (this.configuration[internalId] < this.minLic) this.configuration[internalId] = this.minLic;
			}
		}
		var calc = this.calculate();	
/*		if (this.InternalId == "STDS") {
			var line = "calc: " + calc;
			line += "<br>\n<b>Request</b>";
			for(var ii in this.requestedLicence){
				line += "<br>\n"+ii+" = "+this.requestedLicence[ii];
			}
			line += "<br>\n<b>Config</b>";
			for(var ii in this.configuration){
				line += "<br>\n"+ii+" = "+this.configuration[ii];
			}
			this.writeDebug(line);
		}
*/		return calc;
	}
	
	this.nextCover = function(){
		return this.coverRequirements();	
	}

}


function standardSuiteObject(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl){
	this.inheritFrom = suiteObject;
	this.inheritFrom(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, "STDS");
	this.name = "standardSuiteObject";
	this.coverRequirements = function(){
		this.getBaseSuiteLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}
	
	this.nextCover = function(){
		this.getNextBaseLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}
}

function advaceddSuiteObject(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl){
	this.inheritFrom = suiteObject;
	this.inheritFrom(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, "ADVS");
	this.name = "advacedSuiteObject";
	this.suiteProducts = new Array('EE', 'LIN');

	this.coverRequirements = function(){
		this.getBaseSuiteLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}
	
	this.nextCover = function(){
		this.getNextBaseLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}
}

function enterpriseSuiteObject(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl){
	this.inheritFrom = suiteObject;
	this.inheritFrom(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, "ENTS");
	this.name = "enterpriseSuiteObject";
	this.suiteProducts = new Array('EE', 'IE', 'SMTP', 'DOM', 'SP', 'LIN', 'AFK', 'PDA');

	this.coverRequirements = function(){
		this.getBaseSuiteLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}

	this.nextCover = function(){
		this.getNextBaseLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}
}

function sbsSSuiteObject(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl){
	this.inheritFrom = suiteObject;
	this.inheritFrom(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, "SBSSS");
	this.name = "sbsSSuiteObject";
	
	this.setMaxLicenseToCheck = function(){	
		if (this.requestedLicence['SBSS'] > this.requestedLicence['PE']){
			this.reachMaximumLicNo = this.requestedLicence['SBSS']+1;
		}
		if (this.requestedLicence['SBSS'] <= this.requestedLicence['PE']){
			this.reachMaximumLicNo = this.requestedLicence['PE']+1;
		}
		if (this.reachMaximumLicNo > this.maxLic) this.reachMaximumLicNo = this.maxLic;
		//alert(this.reachMaximumLicNo);
	}
	this.addRestLicences = function(){
		for(internalId in this.requestedLicence){
			if (internalId != "SBSS" && internalId != "PE" && !this.inA(this.suiteProducts, internalId)){
				this.configuration[internalId] =  this.requestedLicence[internalId];
				if (this.configuration[internalId] > this.maxLic) this.configuration[internalId] = NaN;
			}
		}
	}

	this.getBaseSuiteLicenceNo = function(){
		this.configuration = new Array();
		this.configuration[this.InternalId] = NaN;
		if (this.requestedLicence['SBSS'] > this.requestedLicence['PE'])			this.configuration[this.InternalId] = this.requestedLicence['PE'];
		if (this.requestedLicence['SBSS'] < this.requestedLicence['PE']) 			this.configuration[this.InternalId] = this.requestedLicence['SBSS'];
		if (this.requestedLicence['SBSS'] == this.requestedLicence['PE']) 			this.configuration[this.InternalId] = this.requestedLicence['SBSS'];
		if (this.minLic > this.configuration[this.InternalId]) 							this.configuration[this.InternalId] = this.minLic;
		if (this.maxLic < this.configuration[this.InternalId]) 							this.configuration[this.InternalId] = this.maxLix;
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['PE'])) 		this.configuration['PE'] = parseInt(this.requestedLicence['PE']) - parseInt(this.configuration[this.InternalId]) ;
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['SBSS'])) 	this.configuration['SBSS'] = parseInt(this.requestedLicence['SBSS']) - parseInt(this.configuration[this.InternalId]) ;
		//alert("configuration["+this.InternalId+"]: "+this.configuration[this.InternalId]+",\n configuration['PE']: "+this.configuration['PE'] +",\n requestedLicence['PE']: "+this.requestedLicence['PE'] +",\n configuration['SBSS']: "+this.configuration['SBSS']+",\n requestedLicence['SBSS']: "+ this.requestedLicence['SBSS']+",\n configuration['SBSS']: "+ this.configuration['SBSS']);
		this.currentLicences = this.configuration[this.InternalId];
	}

	this.getNextBaseLicenceNo = function(){
		this.configuration = new Array();
		
		this.minLic = this.pricelistObject.getMinimalLicenses("SBSS");
		if (this.minLic >= (this.requestedLicence['SBSS']  - (this.currentLicences +0))) {
			this.currentLicences = parseInt(this.currentLicences) + parseInt(this.minLic);
		} else {
			this.currentLicences = this.currentLicences +1 ;//;
		}
		if (this.maxLic < this.currentLicences) this.currentLicences = this.maxLic;
		this.configuration[this.InternalId] = this.currentLicences;
		//alert(this.configuration[this.InternalId]);
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['PE'])) 		this.configuration['PE'] = parseInt(this.requestedLicence['PE']) - parseInt(this.configuration[this.InternalId]) ;
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['SBSS'])) 	this.configuration['SBSS'] = parseInt(this.requestedLicence['SBSS']) - parseInt(this.configuration[this.InternalId]) ;

	}
	this.coverRequirements = function(){
		if (isNaN(this.minLic)) this.minLic = parseInt(this.pricelistObject.getMinimalLicenses(this.InternalId));
		if (isNaN(this.maxLic)) this.maxLic = parseInt(this.pricelistObject.getMaximumLicenses(this.InternalId));
		this.getBaseSuiteLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		var calc = this.calculate();	
/*		if (this.InternalId == "STDS") {
			var line = "calc: " + calc;
			line += "<br>\n<b>Request</b>";
			for(var ii in this.requestedLicence){
				line += "<br>\n"+ii+" = "+this.requestedLicence[ii];
			}
			line += "<br>\n<b>Config</b>";
			for(var ii in this.configuration){
				line += "<br>\n"+ii+" = "+this.configuration[ii];
			}
			this.writeDebug(line);
		}
*/		return calc;
	}

	this.nextCover = function(){
		this.getNextBaseLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}
}


function sbsPSuiteObject(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl){
	this.inheritFrom = suiteObject;
	this.inheritFrom(maintenanceCtrl, currencyCtrl, customerTypeCtrl, renewalCtrl, discountCtrl, "SBSPS");
	this.name = "sbsPSuiteObject";

	this.setMaxLicenseToCheck = function(){	
		if (this.requestedLicence['SBSP'] > this.requestedLicence['PE']){
			this.reachMaximumLicNo = this.requestedLicence['SBSP']+1;
		}
		if (this.requestedLicence['SBSP'] <= this.requestedLicence['PE']){
			this.reachMaximumLicNo = this.requestedLicence['PE']+1;
		}
		if (this.reachMaximumLicNo > this.maxLic) this.reachMaximumLicNo = this.maxLic;
	}
	this.addRestLicences = function(){
		for(internalId in this.requestedLicence){
			if (internalId != "SBSP" && internalId != "PE" && !this.inA(this.suiteProducts, internalId)){
				this.configuration[internalId] =  this.requestedLicence[internalId];
			}
		}
	}

	this.getBaseSuiteLicenceNo = function(){
		this.configuration = new Array();
		this.configuration[this.InternalId] = NaN;
		if (this.requestedLicence['SBSP'] > this.requestedLicence['PE'])			this.configuration[this.InternalId] = this.requestedLicence['PE'];
		if (this.requestedLicence['SBSP'] < this.requestedLicence['PE']) 			this.configuration[this.InternalId] = this.requestedLicence['SBSP'];
		if (this.requestedLicence['SBSP'] == this.requestedLicence['PE']) 			this.configuration[this.InternalId] = this.requestedLicence['SBSP'];
		if (this.minLic > this.configuration[this.InternalId]) 							this.configuration[this.InternalId] = this.minLic;
		if (this.maxLic < this.configuration[this.InternalId]) 							this.configuration[this.InternalId] = this.maxLic;
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['PE'])) 		this.configuration['PE'] = parseInt(this.requestedLicence['PE']) - parseInt(this.configuration[this.InternalId]) ;
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['SBSP'])) 	this.configuration['SBSP'] = parseInt(this.requestedLicence['SBSP']) - parseInt(this.configuration[this.InternalId]) ;
		//alert("configuration["+this.InternalId+"]: "+this.configuration[this.InternalId]+",\n configuration['PE']: "+this.configuration['PE'] +",\n requestedLicence['PE']: "+this.requestedLicence['PE'] +",\n configuration['SBSS']: "+this.configuration['SBSS']+",\n requestedLicence['SBSS']: "+ this.requestedLicence['SBSS']+",\n configuration['SBSS']: "+ this.configuration['SBSS']);
		this.currentLicences = this.configuration[this.InternalId];
	}

	this.getNextBaseLicenceNo = function(){
		this.configuration = new Array();
		
		this.minLic = this.pricelistObject.getMinimalLicenses("SBSP");
		if (this.minLic >= (this.requestedLicence['SBSP']  - (this.currentLicences +0))) {
			this.currentLicences = parseInt(this.currentLicences) + parseInt(this.minLic);
		} else {
			this.currentLicences = this.currentLicences +1 ;//;
		}
		if (this.maxLic < this.currentLicences) this.currentLicences = this.maxLic;
		this.configuration[this.InternalId] = this.currentLicences;
		
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['PE'])) 		this.configuration['PE'] = parseInt(this.requestedLicence['PE']) - parseInt(this.configuration[this.InternalId]) ;
		if (parseInt(this.configuration[this.InternalId]) < parseInt(this.requestedLicence['SBSP'])) 	this.configuration['SBSP'] = parseInt(this.requestedLicence['SBSP']) - parseInt(this.configuration[this.InternalId]) ;

	}
	this.coverRequirements = function(){
		if (isNaN(this.minLic)) this.minLic = parseInt(this.pricelistObject.getMinimalLicenses(this.InternalId));
		if (isNaN(this.maxLic)) this.maxLic = parseInt(this.pricelistObject.getMaximumLicenses(this.InternalId));
		this.getBaseSuiteLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}

	this.nextCover = function(){
		this.getNextBaseLicenceNo();
		this.fullFillRequest();
		this.addRestLicences();
		return this.calculate();	
	}
}

function suiteCollection(calculator){
	this.inheritFrom = baseObject;	
	this.inheritFrom();
	this.name = "suiteCollection";
	this.suites = new Array();			// suite collection
	this.bestSuite = null;				// object reference to best suite
	this.bestPrice = 0;					// best suite price
	this.otherSolutions = new Array();	// all other (not best) solutions
	this.request = new Array();			// requested configuration
	this.calculator = calculator;		// object  reference to parent calculator
	this.maintenanceCtrl = this.calculator.maintenanceCtrl; // text id of maintenance contract
	this.currencyCtrl = this.calculator.currencyCtrl;		// text id of currency selector
	this.ctrlCustomerType = this.calculator.ctrlCustomerType;
	this.renewalCtrl = this.calculator.renewalCtrl;
	this.discountCtrl = this.calculator.discountCtrl;
	//alert(calculator.name);
	
	this.registerSuite = function(suite){
		suite.setPricelist(this.calculator.pricelist);
		suite.unlimitedFromList = this.calculator.unlimitedFromList;		// copy of unlimited list from calculator
		suite.customerTypeCtrl = this.calculator.customerTypeCtrl;
		suite.calculator = this.calculator;
		this.suites.push(suite);
	}
	this.registerSuite(new  standardSuiteObject(this.maintenanceCtrl, this.currencyCtrl, this.ctrlCustomerType, this.renewalCtrl, this.discountCtrl));
	this.registerSuite(new  advaceddSuiteObject(this.maintenanceCtrl, this.currencyCtrl, this.ctrlCustomerType, this.renewalCtrl, this.discountCtrl));
	this.registerSuite(new  enterpriseSuiteObject(this.maintenanceCtrl, this.currencyCtrl, this.ctrlCustomerType, this.renewalCtrl, this.discountCtrl));
	this.registerSuite(new  sbsSSuiteObject(this.maintenanceCtrl, this.currencyCtrl, this.ctrlCustomerType, this.renewalCtrl, this.discountCtrl));
	this.registerSuite(new  sbsPSuiteObject(this.maintenanceCtrl, this.currencyCtrl, this.ctrlCustomerType, this.renewalCtrl, this.discountCtrl));
	this.registerSuite(new  pfpSuiteObject(this.maintenanceCtrl, this.currencyCtrl, this.ctrlCustomerType, this.renewalCtrl, this.discountCtrl));
	
	this.acceptRequest = function(request){
		this.request = request;
	}
	this.cleanUp = function (){
	
	}
	this.init = function (){
	
	}
	this.addOtherSolution = function(solution, suiteId){
		var writeSolution = new Array();
		for(var internalId in solution){
			var tmpArr = new Object();
			tmpArr.configuration  = solution[internalId].configuration;
			tmpArr.pricePerLicense  = solution[internalId].pricePerLicense;
			tmpArr.originalPrice = solution[internalId].originalPrice;
			tmpArr.renewalDiscountPrice  = solution[internalId].renewalDiscountPrice;
			tmpArr.renewalDiscount  = solution[internalId].renewalDiscount;
			tmpArr.finalPrice  = solution[internalId].finalPrice;
			tmpArr.productDiscountPrice  = solution[internalId].productDiscountPrice;
			tmpArr.productDiscount  = solution[internalId].productDiscount;
			tmpArr.productDiscountDescription  = solution[internalId].productDiscountDescription;
			writeSolution[internalId] =	tmpArr;
		}
		if (solution) {
			this.otherSolutions[suiteId] = writeSolution;
			//alert(writeSolution.length +", bestConf length: "+ solution.length);
		}
	}

	this.getBestSolution = function(){
		this.bestSuite = null;
		this.otherSolutions = new Array();
		this.bestPrice = 0;
		for(var suiteIx in this.suites ){
			var suite = this.suites[suiteIx];
			suite.bestConfiguration = new Array();
			suite.lastCalculation = NaN;
			suite.minLic = NaN;
			suite.maxLic = NaN;
			suite.startCountAt = NaN;
			suite.idealSuiteCount = NaN;
			suite.acceptRequest(this.request);
			var currentPrice = suite.getBestOffer();
			if (!isNaN(currentPrice)) {
				if (this.bestSuite == null) {
					this.bestPrice = currentPrice;
					if (this.bestSuite) this.addOtherSolution(this.bestSuite, this.bestSuite.InternalId);		// move old best solution to sand box
					this.bestSuite = suite.bestConfiguration;
				}
				if (currentPrice  < this.bestPrice){
					this.bestPrice = currentPrice;
					this.bestSuite = suite.bestConfiguration;
					//alert("this.bestSuite: (current price lower) "+this.bestSuite);
				} else {
					this.addOtherSolution(suite.bestConfiguration, suite.InternalId);
				}
			}
			
		}
		return this.bestPrice;
	}
	
}

