var digitalPcrMMxs = [ {
	partnumber : "4458080",
	product : "TaqMan® OpenArray® Digital PCR Master Mix, 5 mL",
	volume : 5000,
	price : 3
}, {
	partnumber : "4458086",
	product : "TaqMan® OpenArray® Digital PCR Master Mix, 1.5mL",
	volume : 1500,
	price : 1
} ];

function addODIG() {
	var title = "TaqMan<sup>&reg;</sup>&nbsp;OpenArray<sup>&reg;</sup>&nbsp;Digital PCR Kit";
	var noofreactions = getExperimentInfo('noofreplicates')
			* getExperimentInfo('noofsamples')
			* getExperimentInfo('nooftargets');

	var isQuant = getExperimentInfo('openarrayinstrument') == "QuantStudio" ? true
			: false;

	var formatObj = findApproximateOpenDigPcrRec(getExperimentInfo('noofsamples')
			* getExperimentInfo('noofsubarrays') / 48);
	var format = formatObj.get('concatenate');

	var quantObj = format.indexOf("10-pack") != -1 ? quantstudio.findRecord(
			'concatenate', "QuantStudioTM Digital PCR Kit, 10-Pack")
			: quantstudio.findRecord('concatenate',
					"QuantStudioTM Digital PCR Kit, 4-Pack");
	var formatObj1 = isQuant ? quantObj : formatObj;

	format = formatObj1.get('concatenate');
	var partNo = formatObj1.get('partnumber');
	var reqnoofunits = getExperimentInfo('noofsamples')
			* getExperimentInfo('noofsubarrays')
			/ (48 * ((isQuant && (quantObj.get('Min Order') == '1 x 4-Pack')) ? 4
					: formatObj.get('effectivesamplesperunit')));
	var noofunits = reqnoofunits < formatObj.get('min') ? formatObj.get('min')
			: Math.ceil(reqnoofunits);

	var min = formatObj.get('min');
	var isMin = (min == 1 ? 1 : (Math.ceil(reqnoofunits) < min ? 0 : 1));

	var mvolume = (getExperimentInfo('noofsamples')
			* getExperimentInfo('noofsubarrays') / 48) * 150;
	var mastermixRec = findDigMastermix(mvolume);
	var mastermix = mastermixRec.product;
	var mmxPartNumber = mastermixRec.partnumber;
	var masterperc = (Ext.get('masterperc').dom.value).split('%')[0];
	var accessperc = (Ext.get('accessoriesperc').dom.value).split('%')[0];
	var volumeDiscount = 0;

	var discObj = tsstabledigitalpcr.findRecord('product', Ext.util.Format
			.trim(formatObj.get('concatenate')));

	var disnoofunits = noofunits;
	if (Ext.util.Format.trim(formatObj.get('concatenate')) == "TaqMan® OpenArray® Digital PCR Kit, 3-pack") {
		if (noofunits > 50) {
			disnoofunits = 50;
		}
	} else {
		var nunits = noofunits > 20 ? noofunits - (noofunits % 10) : noofunits;
		disnoofunits = nunits;
		if (nunits > 900) {
			disnoofunits = 900;
		}
	}

	var volobj = discObj.discounts().findRecord('volume', disnoofunits, 0,
			false, false, true);
	volumeDiscount = volobj.get('value');

	//var pricePerUnit = (isQuant ? formatObj1.get('Pricing or Min Order')/ formatObj1.get('min') : (pricelist.findRecord('partnumber',partNo).regions().findRecord('region', region + "-" + currency).get('price')));
	var pricePerUnit = (pricelist.findRecord('partnumber',partNo).regions().findRecord('region', region + "-" + currency).get('price'));
	var price = (pricelist.findRecord('partnumber',partNo).regions().findRecord('region', region + "-" + currency).get('price'))* noofunits;

	var openarrayperc = parseFloat((Ext.get('openarrayperc').dom.value)
			.split('%')[0]);
	var assaysCostWithVolDisc = price
			- ((volumeDiscount + openarrayperc) / 100 * price);

	var openArray384Obj = accessories.findRecord('product',
			'OpenArray® 384-well Sample Plates');
	var acc1PartNumber = openArray384Obj.get('partnumber');

	var openArray384WellSamplePlatesUnits = 0;
	if (isQuant && (format.indexOf("10-pack") == -1)) {
		openArray384WellSamplePlatesUnits = Math.ceil(0.05 * reqnoofunits) < openArray384Obj
				.get('min') ? openArray384Obj.get('min') : Math.ceil(formatObj
				.get('sampleplates')
				* reqnoofunits);

	} else {
		openArray384WellSamplePlatesUnits = Math.ceil(formatObj
				.get('sampleplates')
				* reqnoofunits) < openArray384Obj.get('min') ? openArray384Obj
				.get('min') : Math.ceil(formatObj.get('sampleplates')
				* reqnoofunits);
	}

	var openArray384WellSamplePlatesCost = pricelist.findRecord('partnumber',
			acc1PartNumber).regions().findRecord('region',
			region + "-" + currency).get('price')
			* openArray384WellSamplePlatesUnits;

	var opeArrayAccuObj = accessories.findRecord('product',
			'OpenArray® AccuFill® System Tips');
	var acc2PartNumber = opeArrayAccuObj.get('partnumber');

	var openArrayAccuFillSystemTipsUnits = 0;
	if (isQuant
			&& (format.indexOf("10-pack") == -1 && format.indexOf("10-Pack") == -1)) {
		openArrayAccuFillSystemTipsUnits = Math.ceil((0.5 * reqnoofunits) % 10) < opeArrayAccuObj
				.get('min') ? opeArrayAccuObj.get('min') : Math
				.ceil((0.5 * reqnoofunits) % 10);
	} else {
		openArrayAccuFillSystemTipsUnits = Math.ceil((formatObj
				.get('tipsperunit') * reqnoofunits) % 10) < opeArrayAccuObj
				.get('min') ? opeArrayAccuObj.get('min') : Math.ceil((formatObj
				.get('tipsperunit') * reqnoofunits) % 10);
	}

	var openArrayAccuFillSystemTipsCost = pricelist.findRecord('partnumber',
			acc2PartNumber).regions().findRecord('region',
			region + "-" + currency).get('price')
			* openArrayAccuFillSystemTipsUnits ;

	var openArrayAccu10Obj = accessories.findRecord('product',
			'OpenArray® AccuFill® System Tips, 10 pack');
	var acc3PartNumber = openArrayAccu10Obj.get('partnumber');

	var openArrayAccuFillSystemTips10PackUnits = Math.ceil(Math
			.floor((formatObj.get('tipsperunit') * reqnoofunits) / 10)) < openArrayAccu10Obj
			.get('min') ? openArrayAccu10Obj.get('min') : Math.ceil(Math
			.floor((formatObj.get('tipsperunit') * reqnoofunits) / 10));
	var openArrayAccuFillSystemTips10PackCost = pricelist.findRecord(
			'partnumber', acc3PartNumber).regions().findRecord('region',
			region + "-" + currency).get('price')
			* openArrayAccuFillSystemTips10PackUnits ;

	var masterMixUnits = Math.ceil(mvolume / mastermixRec.volume);

	var masterMixCostPerUnit = mastermixRec.price;

	var masterMixCost = masterMixCostPerUnit * masterMixUnits;

	var masterMixCostWithDisc = masterMixCostPerUnit * masterMixUnits
			* (1 - (masterperc / 100));

	var masterMixCostPerReaction = masterMixCostWithDisc / noofreactions;

	

	var openArray384WellSamplePlatesCostWithDisc = openArray384WellSamplePlatesCost * (1 - (accessperc / 100));
	var openArrayAccuFillSystemTips10CostWithDisc = openArrayAccuFillSystemTips10PackCost * (1 - (accessperc / 100));
	var openArrayAccuFillSystemTipsCostWithDisc	  = openArrayAccuFillSystemTipsCost * (1 - (accessperc / 100));	
	var accessoriescost = openArrayAccuFillSystemTipsCostWithDisc + openArrayAccuFillSystemTips10CostWithDisc
			+ openArray384WellSamplePlatesCostWithDisc;
	
	var totalAccessoriescost = accessoriescost;

	var accessoriesCostPerReaction = totalAccessoriescost / noofreactions;

	var assayCostPerReaction = assaysCostWithVolDisc / noofreactions;

	var totalCostPerReaction = masterMixCostPerReaction + assayCostPerReaction
			+ accessoriesCostPerReaction;

	var totalCostPreDisc = accessoriescost + masterMixCost + price;
	var partNumber = formatObj.get('partnumber');
	// var openarraycost = formatObj.

	var totalCost = roundNumber(totalAccessoriescost,2) + roundNumber(masterMixCostWithDisc,2) + roundNumber(assaysCostWithVolDisc,2);
	
	
	var totalDiscount = 100 - (totalCost * 100 / totalCostPreDisc);

	var assayCostPerSample = assaysCostWithVolDisc
			/ getExperimentInfo('noofsamples');
	var acc1CostPerSample = openArray384WellSamplePlatesCost
			/ getExperimentInfo('noofsamples');
	var acc2CostPerSample = openArrayAccuFillSystemTipsCost
			/ getExperimentInfo('noofsamples');
	var acc3CostPerSample = openArrayAccuFillSystemTips10PackCost
			/ getExperimentInfo('noofsamples');
	var accessoriesCostPerSample = acc1CostPerSample + acc2CostPerSample
			+ acc3CostPerSample;
	var mmixCostPerSample = masterMixCostWithDisc
			/ getExperimentInfo('noofsamples');
	
			
	var totalCostPerSample = assayCostPerSample + acc1CostPerSample
			+ acc2CostPerSample + acc3CostPerSample + roundNumber(mmixCostPerSample,2);

	var productUrl = producturls.findRecord('partNumber', partNumber).get(
			'productURL');

	var noOfHours = 0;
	var oType = getExperimentInfo('openarrayinstrument') == "QuantStudio" ? "DigitalQS"
			: "DigitalNT";
	var tRec = openarrayruntimes.findRecord('type', oType);
	noOfHours = Math.ceil(Math.ceil((reqnoofunits * ((isQuant && (quantObj
			.get('Min Order') == '1 x 4-Pack')) ? 4 : formatObj
			.get('effectivesamplesperunit')))
			/ tRec.get('noofplatesperrun'))
			* tRec.get('totalmin') / 60);

	outputList.add({
				pno : 19,
				title : title,
				partnumber : partNo,
				format : format.replace(/�/gi, '<sup>&reg;</sup>&nbsp;'),
				noofunits : noofunits,
				noofreactions : noofreactions,
				mastermix : mastermix
						.replace(/�/gi, '<sup>&reg;</sup>&nbsp;'),
				mnoofunits : masterMixUnits,
				totalcost : roundNumber(totalCost, 2),
				volumediscount : volumeDiscount,
				mvolumediscount : 0,
				mastermixdisc : masterperc,
				accessoriesdisc : accessperc,
				assayscost : roundNumber(price, 2),
				assayscostperunit : roundNumber(pricePerUnit, 2),
				assayscostwithvoldisc : roundNumber(assaysCostWithVolDisc, 2),
				mastermixvol : 0,
				mastermixcostperunit : masterMixCostPerUnit,
				mastermixcostwithvoldisc : masterMixCost,
				mastermixcostwithdisc : roundNumber(masterMixCostWithDisc, 2),
				mastermixcost : masterMixCost,
				noofhours : noOfHours,
				assaycostperreaction : roundNumber(assayCostPerReaction, 2),
				mastermixcostperreaction : roundNumber(
						masterMixCostPerReaction, 2),
				accessoriescostperreaction : roundNumber(
						accessoriesCostPerReaction, 2),
				optimizationcostperreaction : 0,
				totalcostperreaction : roundNumber(totalCostPerReaction, 2),
				producturl : '',
				openarray384wellsampleplatescost : openArray384WellSamplePlatesCost,
				openarrayaccufillsystemtipscost : openArrayAccuFillSystemTipsCost,
				openarrayaccufillsystemtips10packcost : openArrayAccuFillSystemTips10PackCost,
				openarray384wellsampleplatesunits : openArray384WellSamplePlatesUnits,
				openarrayaccufillsystemtipsunits : openArrayAccuFillSystemTipsUnits,
				openarrayaccufillsystemtips10packunits : openArrayAccuFillSystemTips10PackUnits,
				mastermixunits : masterMixUnits,
				totalcostpersample : roundNumber(totalCostPerSample, 2),
				accessoriescost : roundNumber(accessoriescost, 2),
				accessoriescostwithdisc : roundNumber(totalAccessoriescost, 2),
				assaycostpersample : roundNumber(assayCostPerSample, 2),
				accessoriescostpersample : roundNumber(
						accessoriesCostPerSample, 2),
				mmxcostpersample : roundNumber(mmixCostPerSample, 2),
				totalcostpredisc : roundNumber(totalCostPreDisc, 2),
				totaldiscount : roundNumber(totalDiscount, 2),
				accessory1partnumber : acc1PartNumber,
				accessory2partnumber : acc2PartNumber,
				accessory3partnumber : acc3PartNumber,
				mastermixpartnumber : mmxPartNumber,
				producturl : productUrl,
				reqnoofunits : reqnoofunits,
				type : 'openarray-custom',
				min : min,
				ismin : isMin,
				fnName:'addODIG'
			});

}

function findDigMastermix(mvolume) {
	var recordIndex = 0;
	var price = 0;
	var count = 0;
	for ( var i = 0; i < digitalPcrMMxs.length; i++) {
		var e = digitalPcrMMxs[i];
		var mnoofunits = Math.ceil(mvolume / e.volume);
		var mprice = mnoofunits * e.price;

		if (price == 0) {
			price = mprice;
			recordIndex = count;
		}
		if (mprice < price) {
			price = mprice;
			recordIndex = count;
		}

		count++;

	}
	return digitalPcrMMxs[recordIndex];
}

function findApproximateOpenDigPcrRec(value) {
	var record = null;
	var name = "TaqMan® OpenArray® Digital PCR Kit";
	var recordIndex = 0;
	var count = 0;
	var price = 0;

	openArray
			.each(function(e) {
				if (e.get('concatenate').indexOf(name) != -1) {
					var noofunits = Math.ceil(value
							/ e.get('effectivesamplesperunit'));
					var partNo = e.get('partnumber');
					var mprice = noofunits
							* pricelist.findRecord('partnumber', partNo)
									.regions().findRecord('region',
											region + "-" + currency).get(
											'price');
					if (price == 0) {
						price = mprice;
						recordIndex = count;
					}
					if (mprice < price) {
						price = mprice;
						recordIndex = count;
					}

				}
				count++;
			});
	return openArray.getAt(recordIndex);

}