var cardsMMixPerRxn = 1;
function addcustMRNA() {
	if(getExperimentInfo('nooftargets') > 384)
	{
		Ext.get('emessage').removeCls('hidden');
		updateContMessage()
		return;
	}
	
	Ext.get('emessage').addCls('hidden');
	var title = "Custom TaqMan<sup>&reg;</sup>&nbsp;Array miRNA Cards";
	var isOtherFormat = getExperimentInfo('isOtherFormat');
	var spno = 15;
	if( isOtherFormat === "YES" ){
		spno = getExperimentInfo('otherFmPano');
		var selOtherForVal = getExperimentInfo('selOtherFormatValue');
		var formatObj = findAppTapRecForOtherFormat(taccustmiRNA, 'tacformat', selOtherForVal);
	}else{
		updateExperimentInfo("currFmPno", spno);
		var formatObj = findApproximatemiRNARecord('nooftargets',getExperimentInfo('nooftargets') - 1);// - 1
	}
	var partNumber = formatObj.get('partnumber');
	var noofformat = formatObj.get('tacformat');
	// var formatNo = formatObj.get('tacformat');
	/*
	var pinRec = platesCardsModel.findRecord('description', formatObj
			.get('tacname'));
	var pinNo = "";
	if (pinRec) {
		partNumber = pinRec.get('partnumber');
		var noofassay = pinRec.get('assay');
	}*/
	
	/*Added by SS on Aug 30th*/
	var isPreamp = false;
	if (Ext.get('preampchk').dom.checked) {
		isPreamp = true;
	} else {
		isPreamp = false;
	}
	
	var specie = getExperimentInfo('species');
	
	//var totalpreampprimerpoolspricecost = 100;
    /*End - SS*/
	
	if(getExperimentInfo('nooftargets') > 384)
	{
		noofassay = getExperimentInfo('nooftargets');
	}
	
	var noofreactions = noofformat * getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples');	
	var description = formatObj.get('tacname');
	var format = description;
	var noofreplicates = formatObj.get('numberoftacreplicates');
	
	var noofsamples = formatObj.get('numberofsamplespercard');
	// commented on 16-06-2016
	//var reqnoofunits =  Math.ceil((getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples')) / noofsamples);
	var reqnoofunits = Math.ceil((getExperimentInfo('nooftargets') - 1) / 384)
			* Math.ceil(getExperimentInfo('noofreplicates') / noofreplicates)
			* Math.ceil(getExperimentInfo('noofsamples') / noofsamples);
	var min = formatObj.get('min');
	var isMin = reqnoofunits < min ? 0 : 1;
	var noofunits = reqnoofunits < min ? min : reqnoofunits; 
	
	var assayCostPerSample = totalCardCost / getExperimentInfo('noofsamples');
	var mvolume = noofunits * 400;
	var isOtherMMFormat = getExperimentInfo('isOtherMMFormat');
	if( isOtherMMFormat === "YES" ){
		spno = getExperimentInfo('otherFmPano');
		var selOtherForVal = getExperimentInfo('selOtherFormatMMValue');
		var selOtherForPd = getExperimentInfo('selOtherFormatMMProd');
		var mastermixObj = findAppTapRecForOtherMMFormat(mirnaMasterMix, 'productname', selOtherForPd);
		var mastermix = mastermixObj.get('productname');
		var mnoofunits = (Math.ceil(mvolume / mastermixObj.get('volume')));
		var mastermixPartNumber = mastermixObj.get('partnumber');
		var masterMixCostPerUnit = pricelist.findRecord('partnumber',mastermixPartNumber).regions().findRecord('region',region + "-" + currency).get('price');
		var masterperc = (Ext.get('masterperc').dom.value).split('%')[0];
		var totalMasterMixCost = masterMixCostPerUnit * mnoofunits
				* (1 - (masterperc / 100));
		var masterMixCostPerReaction = totalMasterMixCost
			/ (getExperimentInfo('nooftargets')
					* getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples'));
		var mmixCostPerSample = totalMasterMixCost
			/ getExperimentInfo('noofsamples');
		var masterMixCostPerReaction1 = 0;		
		var mmixCostPerSample1 = 0;		
		var totalMasterMixCost1 = 0;		
	} else {
		/* Master Mix*/
		
		var mastermixObj = findMicrnafixedMMXRecord(mvolume);
		var mastermix = mastermixObj.get('productname');
		var mastermixPartNumber = mastermixObj.get('partnumber');
		//var masterMixCostPerUnit = mastermixObj.get('price');
		var masterMixCostPerUnit = pricelist.findRecord('partnumber',mastermixPartNumber).regions().findRecord('region',region + "-" + currency).get('price');
		//pricelist.findRecord('partnumber', mastermixPartNumber).regions().findRecord('region', region + "-" + currency).get('price');
		var mmxPartVolume = mastermixObj.get('volume');
		var mnoofunits = Math.ceil(mvolume / mmxPartVolume);
		
		/*
			This logic was changed accorrding to the lini instructions on Nov 28.
			All secondary mastermix value is set 0
		*/
		var mastermixObj1 = "";
		var mastermix1 = "";
		var mastermixPartNumber1 = "";
		var mnoofunits1 = 0;
		var masterMixCostPerUnit1 = 0;
		var totalMasterMixCost1 = 0;
		var masterMixCostPerReaction1 = 0
		var optimizationCostPerReaction = 0;
		
		/* End of Master Mix*/
	}
	/*
	var mastermixObj = accessories.findRecord('partnumber',"4462159");
	var mastermix = "TaqMan?? Universal Master Mix II, no UNG 10-Pack (1.5mL)";//mastermixObj.get('product');
	var mastermixPartNumber = mastermixObj.get('partnumber');
	var Totmnoofunits = mvolume / 1000;
	var mnoofunits = Math.ceil(Totmnoofunits / 1.5);
	var masterMixCostPerUnit = pricelist.findRecord('partnumber', mastermixPartNumber)
	.regions().findRecord('region', region + "-" + currency).get('price');
	var mastermixObj1 = accessories.findRecord('partnumber',"4462164");
	var mastermix1 = "TaqMan?? Universal Master Mix II, no UNG 1-Pack (5 mL)";//mastermixObj1.get('product');
	var mastermixPartNumber1 = mastermixObj1.get('partnumber');
	var mnoofunits1 = Math.ceil(Totmnoofunits / 5);
	var masterMixCostPerUnit1 = pricelist.findRecord('partnumber', mastermixPartNumber1)
	.regions().findRecord('region', region + "-" + currency).get('price');
	*/
	
	/*Start volumeDiscount */
	var volumeDiscount = 0;
	if (noofunits >= 20 && noofformat < 192 ) {
		volumeDiscount = 20;
	} else if(noofunits >= 20 && noofformat >= 192){
		volumeDiscount = 5;
	}
	
	var oneunit = 1;
	
	var cardCostPerUnit = pricelist.findRecord('partnumber', partNumber)
			.regions().findRecord('region', region + "-" + currency).get('price');
	var costWithVolDisc = noofunits * cardCostPerUnit * (1 - (volumeDiscount / 100));
	var plateperc = parseFloat((Ext.get('plateperc').dom.value).split('%')[0]);
	var totalDiscTemp = volumeDiscount + plateperc;
	var totalCardCost = (cardCostPerUnit * noofunits) * (1 - (totalDiscTemp / 100));
	var masterperc = (Ext.get('masterperc').dom.value).split('%')[0];
	var assayCostPerSample = totalCardCost / getExperimentInfo('noofsamples');
	assayCostPerSample = roundNumber(assayCostPerSample,2);
	var cardCostPerReaction = totalCardCost/noofreactions;
	cardCostPerReaction = roundNumber(cardCostPerReaction, 2);
	var productUrl =  producturls.findRecord('partNumber', partNumber).get('productURL');
	//master mix - net price
	var totalMasterMixCost = masterMixCostPerUnit * mnoofunits * (1 - (masterperc / 100));
	
	
	
	/* workflow time calculation changes starts - 25-03-2014 */
	
	//var preamHours = isPreamp ? 120 : 0;
    //var rtHours = 140;
	//var noofunithours = min * 120;
	
	//var hours = Math.ceil((preamHours + rtHours + noofunithours)/60);
	
	var preamHours = isPreamp ? 75 : 0;
    var rtHours = 65;
	var RealTimePCR = 120;    
	var noOfHours = Math.ceil(preamHours + rtHours + (reqnoofunits * RealTimePCR))/60;
	
	/* workflow time calculation ends - 25-03-2014 */
	
	
	//master mix - price per sample and price per rxn
	var masterMixCostPerReaction = totalMasterMixCost / noofreactions;
	masterMixCostPerReaction =  roundNumber(masterMixCostPerReaction, 2);
	var mmixCostPerSample = totalMasterMixCost / getExperimentInfo('noofsamples');
	mmixCostPerSample =  roundNumber(mmixCostPerSample, 2);
	var mmixCostPerSample1 = 0;
	var newnoofreactions = (377 * getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples'));
	/*End volumeDiscount */
	
	/*RT Tool Started */
	var preamprtperc = (Ext.get('preamprtperc').dom.value).split('%')[0];
	var rtObj = findMicrnaFixedRTRecord(getExperimentInfo('noofsamples'),specie);
	var rtProduct = rtObj.get('productname');
	var rtPartNumber = rtObj.get('partnumber');
	var rtCostPerUnit = pricelist.findRecord('partnumber', rtObj.get('partnumber')).regions().findRecord('region', region + "-" + currency).get('price');
	var rtNoOfReactions = getExperimentInfo('noofsamples');
	var rtNoOfUnits = Math.ceil(rtNoOfReactions / rtObj.get('reactions'));
	var totalRTCost = rtCostPerUnit * rtNoOfUnits * (1 - (preamprtperc / 100));
	var rtCostPerReaction = totalRTCost / noofreactions;
	rtCostPerReaction = roundNumber(rtCostPerReaction, 2);
	var rtCostPerSample = totalRTCost / getExperimentInfo('noofsamples');
	rtCostPerSample = roundNumber(rtCostPerSample, 2);
	/*RT Tool End */
	
	/* Pre AMP start and Added by SS*/
	
	var preAmpObj = findMicrnaFixedPreAmpRecord(getExperimentInfo('noofsamples'), specie);
	//var preAmpCostPerUnit = preAmpObj.get('price');
	var preAmpPartNumber = preAmpObj.get('partnumber');
	var preAmpCostPerUnit = pricelist.findRecord('partnumber', preAmpPartNumber).regions().findRecord('region', region + "-" + currency).get('price');
	var preAmpNoOfReactions = getExperimentInfo('noofsamples');
	var preAmpNoOfUnits = isPreamp ?  Math.ceil(getExperimentInfo('noofsamples')/40) : 0;
	var totalPreAmpCost = preAmpCostPerUnit * preAmpNoOfUnits * (1 - (preamprtperc / 100));
	var preAmpCostPerReaction = totalPreAmpCost /noofreactions;
	preAmpCostPerReaction = roundNumber(preAmpCostPerReaction,2);
    var preAmpProduct = preAmpObj.get('productname');
    
    var preAmpCostPerSample = totalPreAmpCost/ getExperimentInfo('noofsamples');
	preAmpCostPerSample = roundNumber(preAmpCostPerSample,2);
   /*PreAmp Tool End by SS */
   
   /*RT PreAMP Primer Tool start*/
	
	var rtPrimerPoolsObj =  findMicrnaFixedCustRTRecord(noofformat, specie, 'Cust RT Primer');
	var rtPrimerPools = rtPrimerPoolsObj.get('productname');
	var rtPrimerPoolsNoOfUnits =  1;
	/*Commented by SS accorrding to Lini instruction on NOV-27-2013
		Math.ceil(getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples')/rtPrimerPoolsObj.get('reactions'));
	*/
    var rtPrimerPoolsPartNumber = rtPrimerPoolsObj.get('partnumber');
    //var rtPrimerPoolsPricePerUnit = rtPrimerPoolsObj.get('price');
    var rtPrimerPoolsPricePerUnit = pricelist.findRecord('partnumber', rtPrimerPoolsPartNumber).regions().findRecord('region', region + "-" + currency).get('price');
    var totalRtPrimerPoolsPriceCost = rtPrimerPoolsPricePerUnit * rtPrimerPoolsNoOfUnits * (1 - (preamprtperc / 100));
	var rtpreampprimerpersample = totalRtPrimerPoolsPriceCost/ getExperimentInfo('noofsamples');
	rtpreampprimerpersample  = roundNumber(rtpreampprimerpersample,2);
	var rtpreampprimerperreaction = totalRtPrimerPoolsPriceCost/noofreactions;
	rtpreampprimerperreaction = roundNumber(rtpreampprimerperreaction,2);
 	/*RT Primer Tool End*/

	/*Custom  PreAMP Tool End by SS */
	var preampprimerobj =  findMicrnaFixedCustPreAmpRecord(noofformat, specie);
	var preampprimerpools = preampprimerobj.get('productname');
	var preampprimerpoolsnoofunits =  isPreamp ?1:0;
	/*Commented by SS accorrding to Lini instruction on NOV-27-2013
	isPreamp ? Math.ceil(getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples')/preampprimerobj.get('preampformat')):0;
	*/
	var preprimerpoolpartnumber = preampprimerobj.get('partnumber');
    //var preprimerpoolspriceperunit = preampprimerobj.get('price');
    var preprimerpoolspriceperunit = pricelist.findRecord('partnumber', preprimerpoolpartnumber).regions().findRecord('region', region + "-" + currency).get('price');
	var totalpreampprimerpoolspricecost = preprimerpoolspriceperunit * preampprimerpoolsnoofunits * (1 - (preamprtperc / 100));
	var preampprimerpoolspricepersample = totalpreampprimerpoolspricecost / getExperimentInfo('noofsamples');
	preampprimerpoolspricepersample = roundNumber(preampprimerpoolspricepersample,2);
	var preampprimerpoolsreaction = totalpreampprimerpoolspricecost/ noofreactions;
	preampprimerpoolsreaction = roundNumber(preampprimerpoolsreaction,2);
	//convertCurrency(((rec.get('totalpreampprimerpoolspricecost')) / (rec.get('noofreactions'))), region + "-" + currency);
	
    
 	/*PreAMP Tool End by SS */
 	
	var totalCost = totalCardCost + totalMasterMixCost + totalMasterMixCost1 + totalRTCost +  totalpreampprimerpoolspricecost + totalRtPrimerPoolsPriceCost + totalPreAmpCost;
	var totalCostPerReaction = cardCostPerReaction + masterMixCostPerReaction + rtCostPerReaction + preAmpCostPerReaction + 0.01 + preampprimerpoolsreaction + optimizationCostPerReaction;
	var totalCostPerSample = assayCostPerSample + mmixCostPerSample + rtCostPerSample + preAmpCostPerSample + preampprimerpoolspricepersample +rtpreampprimerpersample;
	var totalCostPreDisc = costWithVolDisc + (masterMixCostPerUnit * mnoofunits);
	var totalDiscount = 100 - (totalCost * 100 / totalCostPreDisc);
	
	updateExperimentInfo("isOtherFormat", "NO");
	
	outputList.add({
		pno : spno,
		title : title,
		partnumber : partNumber,
		format : format.replace(/\u00AE/gi, '&#174;&nbsp;'),
		assayscost : roundNumber(costWithVolDisc, 2),
		assayscostperunit : roundNumber(cardCostPerUnit, 2),
		assayscostwithvoldisc : roundNumber(totalCardCost, 2),
		assaycostpersample : assayCostPerSample,
		assaycostperreaction : cardCostPerReaction,
		noofunits : noofunits,
		noofreactions : noofreactions,
		noofhours : noOfHours,
		
		totalrtcost : roundNumber(totalRTCost, 2),
		rtproduct : rtProduct.replace(/\u00AE/gi, '&#174;&nbsp;'),
		rtnoofunits : rtNoOfUnits,		
		rtpartnumber : rtPartNumber,
		rtcostperunit : roundNumber(rtCostPerUnit, 2),
		rtcostpersample : rtCostPerSample,
		rtcostperreaction : rtCostPerReaction,
		
		preampproduct : preAmpProduct.replace(/\u00AE/gi,'&#174;&nbsp;'),
		preamppartnumber : preAmpPartNumber,
		preampnoofunits : preAmpNoOfUnits,		
		preampcostperunit :roundNumber( preAmpCostPerUnit, 2),
		preampcostpersample : preAmpCostPerSample,
		preampcostperreaction : preAmpCostPerReaction,
		totalpreampcost : roundNumber(totalPreAmpCost, 2),
		
		
		totalcost : roundNumber(totalCost, 2),
		volumediscount : volumeDiscount,
		
		mastermix : mastermix,
		mnoofunits : mnoofunits,
		mvolumediscount : masterperc,
		mastermixdisc : masterperc,
		mastermixvol : roundNumber(mvolume, 2),
		mastermixcostperunit : roundNumber(masterMixCostPerUnit, 2),
		mastermixcostwithvoldisc : roundNumber((masterMixCostPerUnit * mnoofunits), 2),
		mastermixcostwithdisc : roundNumber(totalMasterMixCost, 2),
		mastermixcost : roundNumber(masterMixCostPerUnit * mnoofunits, 2),
		mastermixpartnumber : mastermixPartNumber,
		mastermixcostpredisc : roundNumber((masterMixCostPerUnit * mnoofunits),2),//newrec
		mmxcostpersample : mmixCostPerSample,
		mastermixcostperreaction : masterMixCostPerReaction,
		
		optimizationcostperreaction : roundNumber(optimizationCostPerReaction,2),
		producturl : productUrl,
		
		totalcostperreaction : roundNumber(totalCostPerReaction, 2),
		totalcostpersample : roundNumber(totalCostPerSample, 2),
		totaldiscount : roundNumber(totalDiscount, 2),
		
		rtprimerpools : rtPrimerPools,
		rtprimerpoolsnoofunits : rtPrimerPoolsNoOfUnits,
		rtprimerpoolpartnumber : rtPrimerPoolsPartNumber,
		rtprimerpoolspriceperunit : rtPrimerPoolsPricePerUnit,
		totalrtprimerpoolspricecost : totalRtPrimerPoolsPriceCost,
		
		preampprimerpools : preampprimerpools,
		preampprimerpoolsnoofunits : preampprimerpoolsnoofunits,
		preprimerpoolpartnumber : preprimerpoolpartnumber,
		preprimerpoolspriceperunit : preprimerpoolspriceperunit,
		totalpreampprimerpoolspricecost : totalpreampprimerpoolspricecost,
		preampprimerpoolsreaction: preampprimerpoolsreaction,
		preampprimerpoolspricepersample: preampprimerpoolspricepersample,
		reqnoofunits : reqnoofunits,
		type : 'card-custom',
		min : 10,
		ismin : isMin,
		oneunit : oneunit,
		mastermix1 : mastermix1,
		mnoofunits1 : mnoofunits1,
		mastermixcostperunit1 : roundNumber(masterMixCostPerUnit1, 2),
		mastermixcostwithvoldisc1 : roundNumber((masterMixCostPerUnit1 * mnoofunits1), 2),
		mastermixcostwithdisc1 : roundNumber(totalMasterMixCost1, 2),
		mastermixcost1 : roundNumber(masterMixCostPerUnit1 * mnoofunits1, 2),
		mastermixcostperreaction1 : roundNumber(masterMixCostPerReaction1, 2),
		mmxcostpersample1 : roundNumber(mmixCostPerSample1, 2),
		mastermixpartnumber1 : mastermixPartNumber1,
		fnName:'addcustMRNA'
	});
}
//custmiRIN
function findApproximatemiRNARecord(name, value) {
	var count = 0;
	var record = null;
	var prevRecord = null;
	taccustmiRNA.each(function(e) {
		/*if (e.get('tacformat') == value) {
			record = e;
			return false;
		}else if(e.get(name) < value && e.get('tacformat') >= value){
			record = e;
			return false;
		}*/
		if (e.get(name) == value) {
			record = e;
			return false;
		} else if (e.get(name) > value) {
			if (prevRecord)
				record = prevRecord;
			else
				record = e;
			return false;
		} else if (taccustmiRNA.count() - 1 == count) {
			record = e;
			return false;
		}
		count++;
		prevRecord = e;
	});
	return record;
}
