var platesMMixPerRxn = 10;
function addTAP() {
	
	if(getExperimentInfo('nooftargets') > 96)
	{
		Ext.get('emessage').removeCls('hidden');
		updateContMessage()
		return;
	}
	updateExperimentInfo("isPlate", "plate");
	Ext.get('emessage').addCls('hidden');
	//var title = "TaqMan<sup>&reg;</sup>&nbsp;Array Plates";
	var title = "Custom TaqMan<sup>&reg;</sup>&nbsp;Array 96-well Plates";
	if(getExperimentInfo('tubeSel') == 'TAPFast'){
		title = title + ' - Fast';
	}
	var isOtherFormat = getExperimentInfo('isOtherFormat');
	var spno = 86;
	if( (getExperimentInfo('tubeSel') != 'TAPFast') && (getExperimentInfo('tubeSel') != 'TAPStandard')){
	//if(getExperimentInfo('tubeSel') == 'All'){
		spno = 112;
	}
	if( isOtherFormat === "YES" ){
		spno = getExperimentInfo('otherFmPano');
		var selOtherForVal = getExperimentInfo('selOtherFormatValue');
		var formatObj = findAppTapRecForOtherFormat(tacTapHelper, 'tapformat', selOtherForVal);
	}else{
		updateExperimentInfo("currFmPno", spno);
		var formatObj = findApproximateTapRecord('nooftargets',	getExperimentInfo('nooftargets') - 1);
	}
	
	var formatNo = formatObj.get('tapformat');
	var partNumber = "";
	var pinRec = platesCardsModel.findRecord('description', formatObj.get('tapname'));
	var pinNo = "";
	/*
	//Commented on 31/10/2014 - Magesh
	if (pinRec) {
		if (getExperimentInfo('method') == 'Fast' && formatObj.get('tapname').indexOf('Format 8') != -1) {
			partNumber = 4413263;
		} else if (getExperimentInfo('method') == 'Fast' && formatObj.get('tapname').indexOf('Format 16') != -1) {
			partNumber = 4413261;
		} else if (getExperimentInfo('method') == 'Fast' && formatObj.get('tapname').indexOf('Format 32') != -1) {
			partNumber = 4413259;
		} else if (getExperimentInfo('method') == 'Fast' && formatObj.get('tapname').indexOf('Format 48') != -1) {
			partNumber = 4413257;
		} else if (getExperimentInfo('method') == 'Fast' && formatObj.get('tapname').indexOf('Format 96') != -1) {
			partNumber = 4413255;
		} else {
			partNumber = pinRec.get('partnumber'); 
		}
		var noofassay = pinRec.get('assay');
	}*/
	if (pinRec) {
		if (getExperimentInfo('tubeSel') == 'TAPFast' && formatObj.get('tapname').indexOf('Format 8') != -1) {
			partNumber = 4413263;
		} else if (getExperimentInfo('tubeSel') == 'TAPFast' && formatObj.get('tapname').indexOf('Format 16') != -1) {
			partNumber = 4413261;
		} else if (getExperimentInfo('tubeSel') == 'TAPFast' && formatObj.get('tapname').indexOf('Format 32') != -1) {
			partNumber = 4413259;
		} else if (getExperimentInfo('tubeSel') == 'TAPFast' && formatObj.get('tapname').indexOf('Format 48') != -1) {
			partNumber = 4413257;
		} else if (getExperimentInfo('tubeSel') == 'TAPFast' && formatObj.get('tapname').indexOf('Format 96') != -1) {
			partNumber = 4413255;
		} else {
			partNumber = pinRec.get('partnumber'); 
		}
		var noofassay = pinRec.get('assay');
	}
	if(getExperimentInfo('nooftargets') > 96)
	{
		noofassay = getExperimentInfo('nooftargets');
	}
	var description = formatObj.get('tapname');
	var format = description;
	var noofreplicates = formatObj.get('numberoftapreplicates');
	var noofsamples = formatObj.get('numberofsamplesperplate');
	var reqnoofunits = Math.ceil((getExperimentInfo('nooftargets') - 1) / 96)
			* Math.ceil(getExperimentInfo('noofreplicates') / noofreplicates)
			* Math.ceil(getExperimentInfo('noofsamples') / noofsamples);
	/*var reqnoofunits =  Math.ceil(getExperimentInfo('nooftargets') - 1 / 96
			* getExperimentInfo('noofreplicates') / noofreplicates	* getExperimentInfo('noofsamples') / noofsamples);
			console.log(reqnoofunits);*/
	
	var isMin = reqnoofunits < 6 ? 0 : 1;
	var noofunits = reqnoofunits < 6 ? 6 : reqnoofunits; 
	
	var noofreactions = noofassay * getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples');
	
			
	var mvolume = 0;
	if (getExperimentInfo('method') == 'Fast') {
		mvolume = (platesMMixPerRxn * 96 * noofunits * 0.5)/1000;
	} else {
		mvolume = (platesMMixPerRxn * 96 * noofunits)/1000;
	}
	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 < 12) {
		discountno = 6;
	} else if (noofunits < 18) {
		discountno = 12;
	} else {
		discountno = 18;
	}
	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));

	if (getExperimentInfo('method') == 'Fast'){		
			var hours = Math.ceil((40 * 1.2 * noofunits)/60);
	}else{
			var hours = Math.ceil((75 * 1.2 * noofunits)/60);
	}
	
	var totalReactions = getExperimentInfo('nooftargets') * getExperimentInfo('noofreplicates') * getExperimentInfo('noofsamples');
	var totalCost = totalAssayCost + totalMasterMixCost;
	var assayCostPerReaction = totalAssayCost/totalReactions;
	var masterMixCostPerReaction = totalMasterMixCost/totalReactions;
			
	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-arrayplates.html";
		productUrl = "http://www.lifetechnologies.com/us/en/home/life-science/pcr/real-time-pcr/real-time-pcr-assays/taqman-gene-expression/96-well-taqman-gene-expression-assays/custom-taqman-arrayplates.html";
	} else {
		productUrl = "#";
	}
	
	
	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);
	
	//updateExperimentInfo("isOtherFormat", "NO");
	//updateExperimentInfo("isOtherMMFormat", "NO");
	
	outputList.add({
		pno : spno,
		title : title,
		partnumber : partNumber,
		format : format.replace(/�/gi, '<sup>&reg;</sup>&nbsp;'),
		noofunits : noofunits,
		noofreactions : totalReactions,
		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),
		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,
		mastermixpartnumber : mastermixPartNumber,
		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 : 'plate-custom',
		min : 6,
		ismin : isMin,
		fnName:'addTAP'
	});
}

function findApproximateTapRecord(name, value) {
	var record = null;
	var prevRecord = null;
	if (value >= 96)
		return tacTapHelper.findRecord('tapname', 'TaqMan® Array Plate Format 96',0);
	tacTapHelper.each(function(e) {
		if (e.get(name) == value) {
			record = e;
			return false;
		} else if (e.get(name) > value) {
			if (prevRecord != null)
				record = prevRecord;
			else
				record = e;
			return false;
		}
		prevRecord = e;
	});
	return record;
}