var cardsMMixPerRxn = 1;
function addTAC() {

	if(getExperimentInfo('nooftargets') > 384)
	{
		Ext.get('emessage').removeCls('hidden');
		updateContMessage()
		return;
	}
	Ext.get('emessage').addCls('hidden');
	//var title = "TaqMan<sup>&reg;</sup>&nbsp;Array Cards";
	
	//Added on 31/10/2014 - By Magesh.
	updateExperimentInfo("isPlate", "None");
	
	var title = "Custom TaqMan<sup>&reg;</sup>&nbsp;Array 384-well Cards";
	
	var isOtherFormat = getExperimentInfo('isOtherFormat');
	var spno = 15;
	if( isOtherFormat === "YES" ){
		spno = getExperimentInfo('otherFmPano');
		var selOtherForVal = getExperimentInfo('selOtherFormatValue');
		var formatObj = findAppTapRecForOtherFormat(tacTapHelper, 'tacname', selOtherForVal);
	}else{
		updateExperimentInfo("currFmPno", spno);
		var formatObj = findApproximateRecord('nooftargets', getExperimentInfo('nooftargets') - 1);
	}
	
	//var formatObj = findApproximateRecord('nooftargets', getExperimentInfo('nooftargets') - 1); 
	var partNumber = "";
	// 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');
	}
	if(getExperimentInfo('nooftargets') > 384)
	{
		noofassay = getExperimentInfo('nooftargets');
	}
	
	var description = formatObj.get('tacname');
	var format = description;
	// formatObj = findApproximateRecord('tacformat', formatNo);
	var noofreplicates = formatObj.get('numberoftacreplicates');
	//console.log(noofreplicates);
	var noofsamples = formatObj.get('numberofsamplespercard');
	var reqnoofunits = Math.ceil((getExperimentInfo('nooftargets') - 1) / 384)
			* Math.ceil(getExperimentInfo('noofreplicates') / noofreplicates)
			* Math.ceil(getExperimentInfo('noofsamples') / noofsamples);
	/*var reqnoofunits =  Math.ceil((getExperimentInfo('nooftargets') - 1) / 384
			* getExperimentInfo('noofreplicates') / noofreplicates * getExperimentInfo('noofsamples') / noofsamples);*/
	
	var isMin = reqnoofunits < 10 ? 0 : 1;
	var noofunits = reqnoofunits < 10 ? 10 : reqnoofunits;
	
	var noofreactions = noofassay * getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples');

	var mvolume = cardsMMixPerRxn
			* platesCardsModel.findRecord('description', description)
					.get('rxn') / 1000 * noofunits;
	var isOtherMMFormat = getExperimentInfo('isOtherMMFormat');
	if( isOtherMMFormat === "YES" ){
		spno = getExperimentInfo('otherFmPano');
		var selOtherForVal = getExperimentInfo('selOtherFormatMMValue');
		var selOtherForPd = getExperimentInfo('selOtherFormatMMProd');
		var mastermixObj = findAppTapRecForOtherMMFormat(masterMixModel, 'product', selOtherForPd);
	}else{
		var mastermixObj = findOptimumGeneMicroMMXTubeRecord(mvolume);
	}
	
	var mastermix = mastermixObj.get('product');
	var mastermixPartNumber = mastermixObj.get('partnumber');

	var mnoofunits = Math.ceil(mvolume / mastermixObj.get('volume'));
	var assayCostPerUnit = pricelist.findRecord('partnumber', partNumber)
			.regions().findRecord('region', region + "-" + currency).get(
					'price');
	
	var discountno = 0;
	if (noofunits < 20) {
		discountno = 10;
	} else if (noofunits < 50) {
		discountno = 20;
	} else if (noofunits < 100) {
		discountno = 50;
	} else {
		discountno = 100;
	}
	var volumeDiscount = platesCardsModel.findRecord('description', description).regions().findRecord('region', region).discounts().findRecord('volume',discountno).get('value');
	
	var costWithVolDisc = noofunits * assayCostPerUnit	* (1 - (volumeDiscount / 100));
	
	var plateperc = (Ext.get('plateperc').dom.value).split('%')[0];
		
	var totalAssayCost = costWithVolDisc * (1 - (plateperc / 100));
	
	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 hours = Math.ceil((130 * reqnoofunits)/60);
	

	var totalCost = totalAssayCost + totalMasterMixCost;

	var assayCostPerReaction = totalAssayCost/noofreactions;
			
	var masterMixCostPerReaction = totalMasterMixCost / noofreactions;
	var optimizationCostPerReaction = 0;
	var totalCostPerReaction = assayCostPerReaction + masterMixCostPerReaction
			+ optimizationCostPerReaction;
	var productUrl = "";
	if (getExperimentInfo('type') == "Gene Expression") {
		productUrl = "http://www.invitrogen.com/site/us/en/home/Products-and-Services/Applications/PCR/real-time-pcr/real-time-pcr-assays/taqman-gene-expression/custom-taqman-arraycards.html";
	} else {
		productUrl = "http://www.invitrogen.com/site/us/en/home/Products-and-Services/Applications/PCR/real-time-pcr/real-time-pcr-assays/mirna-ncrna-taqman-assays/taqman-microrna-profiling-using-384-well-array-cards.html";
	}
	
	var mmixCostPerSample = totalMasterMixCost
			/ getExperimentInfo('noofsamples');
	var assayCostPerSample = totalAssayCost / getExperimentInfo('noofsamples');
	var totalCostPerSample = assayCostPerSample + mmixCostPerSample;

	var totalCostPreDisc = costWithVolDisc
			+ (masterMixCostPerUnit * mnoofunits);
	var totalDiscount = 100 - (totalCost * 100 / totalCostPreDisc);
	//console.log(isMin);
	outputList.add({
		pno : spno,
		title : title,
		partnumber : partNumber,
		format : format.replace(/�/gi, '<sup>&reg;</sup>&nbsp;'),
		noofunits : noofunits,
		noofreactions : noofreactions,
		mastermix : mastermix.replace(/�/gi, '<sup>&reg;</sup>&nbsp;'),
		mnoofunits : mnoofunits,
		totalcost : roundNumber(totalCost, 2),
		volumediscount : volumeDiscount,
		mvolumediscount : masterperc,
		assayscost : roundNumber(costWithVolDisc, 2),
		assayscostperunit : roundNumber(assayCostPerUnit, 2),
		assayscostwithvoldisc : roundNumber(totalAssayCost, 2),
		mastermixpartnumber : mastermixPartNumber,
		mastermixvol : roundNumber(mvolume, 2),
		mastermixcostperunit : roundNumber(masterMixCostPerUnit, 2),
		mastermixdisc : masterperc,
		mastermixcostwithdisc : roundNumber(totalMasterMixCost, 2),
		mastermixcostpredisc : roundNumber((masterMixCostPerUnit * mnoofunits),	2),
		mastermixcost : roundNumber(masterMixCostPerUnit * mnoofunits, 2),
		mmxcostpersample : roundNumber(mmixCostPerSample, 2),
		assaycostpersample : roundNumber(assayCostPerSample, 2),
		totalcostpersample : roundNumber(totalCostPerSample, 2),
		totalcostpredisc : roundNumber(totalCostPreDisc, 2),
		totaldiscount : roundNumber(totalDiscount, 2),
		noofhours : hours,
		assaycostperreaction : roundNumber(assayCostPerReaction, 2),
		mastermixcostperreaction : roundNumber(masterMixCostPerReaction, 2),
		optimizationcostperreaction : roundNumber(optimizationCostPerReaction,	2),
		totalcostperreaction : roundNumber(totalCostPerReaction, 2),
		totalcostpersample : roundNumber(totalCostPerSample, 2),
		producturl : productUrl,
		reqnoofunits : reqnoofunits,
		type : 'card-custom',
		min : 10,
		ismin : isMin,
		fnName:'addTAC'
		
		/*totalrtcost : roundNumber(130, 2),
		rtproduct : ('TaqMan® Gene Expression Cells-to-CT™Kit').replace(/\u00AE/gi, '&#174;&nbsp;'),
		rtnoofunits : 4,		
		rtpartnumber : '1728',
		rtcostperunit : roundNumber(2345, 2),
		rtcostpersample : 0.23,
		rtcostperreaction : 0.2,
		
		preampproduct : ('High Capacity cDNA Reverse Transcription Kit').replace(/\u00AE/gi,'&#174;&nbsp;'),
		preamppartnumber : '4368814',
		preampnoofunits : 5,		
		preampcostperunit :roundNumber( 300, 2),
		preampcostpersample : 1.30,
		preampcostperreaction : 0.32,
		totalpreampcost : roundNumber(1500, 2),
		
		preampprimerpools : ('Hign Capacity RNA-to-cDNA Kit').replace(/\u00AE/gi,'&#174;&nbsp;'),
		preampprimerpoolsnoofunits : 4,
		preprimerpoolpartnumber : '4387406',
		preprimerpoolspriceperunit : 100,
		totalpreampprimerpoolspricecost : 400,
		preampprimerpoolsreaction: 0.23,
		preampprimerpoolspricepersample: 0.30*/
		
	});
}

function findApproximateRecord(name, value) {

	var count = 0;
	var record = null;
	var prevRecord = null;
	tacTapHelper.each(function(e) {
		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 (tacTapHelper.count() - 1 == count) {
			record = e;
			return false;
		}
		count++;
		prevRecord = e;
	});
	if( record.get("tacname").indexOf("96a") != -1 || record.get("tacname").indexOf("96b") != -1){
		if( getExperimentInfo('noofreplicates') > 1 ){
			record = tacTapHelper.findRecord("tacname","TaqMan® Array Card, Format 96b");
		}else{
			record = tacTapHelper.findRecord("tacname","TaqMan® Array Card, Format 96a");
		}
	}
	return record;
}
