var totalQty = [];

var genoTypingMasterMix = [1,10,20,50,100];
var geneExpressionMasterMix = [1,5,10,25,50];
var CopyMasterMix = [10,20,50,100];
var aQuantityArray = [];
var subTotal = [];
var cType = 0;
var gRemainingQty = 0;
var subRemainQty = 0;
var nCob =0;
var nCob2 = 0;
var nCob3 = 0;
var remainingQty = 0;
function pbRatio(vType, totQty, mVolume, genoMaster){
	remainingQty = totQty %  genoMaster[vType];
	var qty = totQty /  genoMaster[vType];
	var firstQty =  parseInt(qty) * genoMaster[vType];
	if(remainingQty === totQty){
		remainingQty = 0;
	}
	if( remainingQty === 0 ){
		if( nCob2 > 0 && aQuantityArray.length != 0 ){
			if(qty > 0 && qty < 1){
				qty = 1;
			}
			var temp1 = "qtyType_" + genoMaster[vType];
			var temp2 = "qty_" + parseInt(qty);
			subTotal.push({ temp1 : genoMaster[vType], temp2 : parseInt(qty)});
			
			totalQty.push({"aQtyType":aQuantityArray[0]["aQtyType"],"mQty":aQuantityArray[0]["mQty"],"sub":subTotal});
			subTotal = [];
			if(nCob3 < (genoMaster.length-1)){
				//nCob = nCob+1;
				pbRatio( ++nCob3, gRemainingQty, mVolume, genoMaster );
			}else{
				nCob = 0;
				aQuantityArray = [];
				if(cType < (genoMaster.length-1)){
					var total = Math.ceil( mVolume / 1000 );
					nCob2 = 0;
					pbRatio( ++cType, total, mVolume, genoMaster );
				}else{
					//alert(totalQty.toSource());
					//toGetCombinationVal = totalQty;
					//alert(toGetCombinationVal);
					//return toGetOptimumPrice(totalQty);
				}
			}
			
		} else {
			if(qty > 0 && qty < 1){
				qty = 1;
			}
			totalQty.push({"aQtyType":genoMaster[vType],"mQty":parseInt(qty),"sub":subTotal});
			subTotal = [];
			cType = vType;
			nCob = 0;
			if(vType < (genoMaster.length-1)){
				nCob2 = 0;
				var total = Math.ceil( mVolume / 1000 );
				pbRatio( ++cType, total, mVolume, genoMaster );
			}else{
				//toGetOptimumPrice(totalQty);
			}
		}  
	} else {
		if( nCob2 === 0 ){
			gRemainingQty = remainingQty;
			aQuantityArray = [];
			aQuantityArray.push({"aQtyType":genoMaster[vType],"mQty":parseInt(qty)});
			nCob2 = 1;
			nCob3 = 0;
		} else {
			var temp1 = "qtyType_" + genoMaster[vType];
			var temp2 = "qty_" +  parseInt(qty);
			subTotal.push({ temp1 : genoMaster[vType], temp2 :  parseInt(qty)});
		}
		pbRatio( nCob, remainingQty, mVolume, genoMaster );
	}
	return totalQty;
}

var combination = [];
var allCombination = [];
var qty2Price = 0;
var priceArray = [];
var val = 1;
function toGetOptimumPrice(totalQty, masterMixObjects){
	//alert(JSON.stringify(totalQty));
	for(var vi = 0; vi < totalQty.length; vi++){
		var obj = {};
		val = 1;
		combination = [];
		var rec1 = masterMixObjects.findRecord('volume', (totalQty[vi]['aQtyType'] * 1000) );
		var qty1Price = totalQty[vi]['mQty'] * pricelist.findRecord('partnumber',rec1.get('partnumber')).regions().findRecord('region',region + "-" + currency).get('price');
		//var qty1Price = totalQty[vi]['mQty'] * rec1.get('price');
		var subComb = totalQty[vi]['sub'];
		qty2Price = qty1Price;
		obj["Part Number" + val] 	= rec1.get('partnumber');
		obj["Product Name" + val]   = rec1.get('productname');
		obj["volume" + val]		 	= rec1.get('volume');
		obj["qty" + val]			= totalQty[vi]['mQty'];
		obj["qty1Rate" + val]	 	= qty1Price;
		val ++;
		
		if(subComb.length > 0){
			for(var si = 0; si < subComb.length; si++){
				var rec2 = masterMixObjects.findRecord('volume', (subComb[si]['temp1'] * 1000) );
				qty2Price = qty2Price + ( subComb[si]['temp2'] * pricelist.findRecord('partnumber',rec2.get('partnumber')).regions().findRecord('region',region + "-" + currency).get('price') );
				
				obj["Part Number" + val] 	 = rec2.get('partnumber');
				obj["Product Name" + val] 	 = rec2.get('productname');
				obj["volume" + val]		 	 = rec2.get('volume');
				obj["qty" + val]			 = subComb[si]['temp2'];
				obj["qty1Rate" + val]	 	 = subComb[si]['temp2'] * pricelist.findRecord('partnumber',rec2.get('partnumber')).regions().findRecord('region',region + "-" + currency).get('price');
				val ++;
				if(si === (subComb.length -1)){
					obj["total Price"]	 = qty2Price;
					allCombination.push(obj);
				}
			}
		}else{
			obj["total Price"]	 = qty2Price;
			allCombination.push(obj);
		}
	}
	var price =0;
	var recordIndex = 0;
	var mprice = 0;
	var finalArray = [];
	for(var ai = 0; ai < allCombination.length; ai++){
		mprice = allCombination[ai]['total Price'];
		if (price == 0) {
			price = mprice;
			finalArray = allCombination[ai];
		}
		if (mprice < price) {
			price = mprice;
			finalArray = [];
			finalArray = allCombination[ai];
		}
		
		if(ai === allCombination.length - 1){
			var aa = [];
			var ab = {};
			$.each( finalArray, function( key, value ) {
			  var arrayVal = key.split("");
			  if(key.indexOf('Part Number') != -1){
				if($.inArray(value, aa) == -1){
				   aa.push(value);
				   ab[key] = value;
				   ab["Product Name"+arrayVal[key.length-1]] =  finalArray["Product Name"+arrayVal[key.length-1]]
				   ab["volume"+arrayVal[key.length-1]] = finalArray["volume"+arrayVal[key.length-1]];
				   ab["qty"+arrayVal[key.length-1]] = finalArray["qty"+arrayVal[key.length-1]];
				   ab["qty1Rate"+arrayVal[key.length-1]] = finalArray["qty1Rate"+arrayVal[key.length-1]];
				}else{
				  $.each( ab, function( key1, value1 ) {
					 //console .log(value +" === "+ value1);
					if(value === value1){
						var arrayVal1 = key1.split("");
						var abi = ab["qty"+arrayVal1[key1.length-1]] + finalArray["qty"+arrayVal[key.length-1]];
						var abi1 = ab["qty1Rate"+arrayVal1[key1.length-1]] + finalArray["qty1Rate"+arrayVal[key.length-1]];
						ab["qty"+arrayVal1[key1.length-1]] = abi;
						ab["qty1Rate"+arrayVal1[key1.length-1]] = abi1;
					 }
				  });
				}
			  }
			});
			//alert(JSON.stringify(ab));
			return ab;
		}
	}
	//alert("finalArray : "+finalArray);
}