// round off numeber to 2 decimal points
/** UTILITY & HELPER FUNCTIONS */	

	function roundNumber(numberInput){
		//alert("hello from ROUND NUMBER function");
		var numberInput = (Math.round(numberInput*Math.pow(10,2))/Math.pow(10,2));
		return numberInput;
	}

/** BUSINESS LOGIN FUNCTIONS */	

	function initPriceCalc(){
		
	var itemCost = document.gpmFrm.itemCostTxt.value;
	var newPrice = document.gpmFrm.newPriceTxt.value;
	var profitMargPerc = document.gpmFrm.profitMargTxt.value;
	
	itemCost = parseFloat(itemCost);
	
	
	if(profitMargPerc != "" && newPrice!=""){
		alert("you can't fill in Desired Price and Desired Profit Margin at the same time");
	return;
	}
	
	// entered PROFIT MAGIN - CALCULATE NEW PRICE
	else if(newPrice == "" && profitMargPerc != ""){
		profitMargPerc = parseFloat(profitMargPerc);
		newPrice = (itemCost/(100 - profitMargPerc)) * 100;
		newPrice = roundNumber(newPrice);
		
		document.gpmFrm.newPriceTxt.value = newPrice.toFixed(2);
	
	}
	
	// entered NEW PRICE - CALCULATE PROFIT MARGIN
	else if(newPrice != "" && profitMargPerc == ""){
		newPrice = parseFloat(newPrice);
		profitMargPerc = ((newPrice-itemCost)/newPrice) *100;
		
		profitMargPerc = roundNumber(profitMargPerc);
		
		document.gpmFrm.profitMargTxt.value = profitMargPerc;
	
	}
	
	
	}
	
/** ----------------- Incorrect way to calculate new price ------------------------------------ */
	function IW_initPriceCalc(){
	
	var itemCost = document.gpmFrm.IW_itemCostTxt.value;
	var newPrice = document.gpmFrm.IW_newPriceTxt.value;
	var profitMargPerc = document.gpmFrm.IW_profitMargTxt.value;
	
	itemCost = parseFloat(itemCost);
	
	// entered PROFIT MAGIN - CALCULATE NEW PRICE
	if(profitMargPerc != ""){
		profitMargPerc = (parseFloat(profitMargPerc) / 100.00);
		newPrice = (itemCost + (itemCost * profitMargPerc));
		newPrice = roundNumber(newPrice);
		
		document.gpmFrm.IW_newPriceTxt.value = newPrice.toFixed(2);
	
	}
	
	}

	
	
/** ----------------- CALCULATE PRICE ROUNDOUP ------------------------------------ */
	function roundUpPrice(){
	var itemCost = document.gpmFrm.itemCostTxt.value;
	
	var subTotal = parseFloat(document.gpmFrm.newPriceTxt.value);

	var roundUpAmt;	 // as Dollars

	var roundUpPerc; // as Percents
	
	var priceDif;
	var profitMargDif;

	
		if(subTotal> 0 && subTotal <2){
			subTotal = subTotal + 0.24;
			//alert("1: > 0 subTotal: " + subTotal);
			subTotal = roundNumber(subTotal/0.1);
			//alert("2: > 0 subTotal: " + subTotal);
			subTotal = subTotal- (subTotal%1);
			//alert("3: > 0 subTotal: " + subTotal);
			subTotal = roundNumber((subTotal * 0.10) + 0.09);
			//alert("4: > 0 subTotal: " + subTotal);
			roundUpAmt = subTotal - (parseFloat(document.gpmFrm.newPriceTxt.value));
			roundUpAmt = roundNumber(roundUpAmt);
			
	/** #############################################################################
	 #
	 # PUT THE FOLLOWING CODE IN A FUNCTION - REWRITE THE finalCalc() AT THE BOTTOM
	 #
	 ################################################################################ */
			roundUpPerc = ((subTotal - itemCost) /subTotal) * 100;
			roundUpPerc = roundNumber(roundUpPerc);				
			
			document.gpmFrm.roundUpDolTxt.value = subTotal;
			document.gpmFrm.roundUpPercTxt.value = roundUpPerc;
		
			// calculate differenced between old and new price
			// old and new profit margins		
			priceDif = roundNumber(subTotal - parseFloat(document.gpmFrm.newPriceTxt.value));			
			profitMargDif = roundNumber(roundUpPerc - parseFloat(document.gpmFrm.profitMargTxt.value));

			// populate the price Difference & profit Difference fields on form
			document.gpmFrm.priceDifTxt.value = priceDif;
			document.gpmFrm.grossProfitPercDifTxt.value = profitMargDif;
		}
		
		
		if((subTotal >= 2.0) && (subTotal < 9.99)){

		
			if((subTotal%1) < 0.29){
					roundUpAmt = 0.29;
					subTotal = parseInt(subTotal);
					finalCalc(subTotal, roundUpAmt);
			
				
			}
			
			if( (subTotal%1 >= 0.29) && (subTotal%1 < 0.49) ){			
					roundUpAmt = 0.49;
					subTotal = parseInt(subTotal);
					//alert("sub total = " + subTotal);
					finalCalc(subTotal, roundUpAmt);

			}
			
			if( (subTotal%1 >= 0.49) && (subTotal%1 < 0.79) ){
					roundUpAmt = 0.79;
					subTotal = parseInt(subTotal);
					finalCalc(subTotal, roundUpAmt);
			}
			
			if( (subTotal%1 >= 0.79) && (subTotal%1) < 0.99){
					roundUpAmt = 0.99;
					subTotal = parseInt(subTotal);
					finalCalc(subTotal, roundUpAmt);
			}
			
		}
		
			if( (subTotal >= 9.99) && (subTotal < 20) ){
					roundUpAmt = 0.99;
					subTotal = parseInt(subTotal);
					finalCalc(subTotal, roundUpAmt);
		}
		
		
		// round up for items costing 20 - 50
			if( (subTotal > 20) && (subTotal <= 50) ){
			
				if( subTotal%10 > 0.0 && subTotal%10 < 1.99 ){
					roundUpAmt = 1.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 > 1.99 && subTotal%10 < 2.99 ){
					roundUpAmt = 2.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 > 2.99 && subTotal%10 < 4.99 ){
					roundUpAmt = 4.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 >= 4.99 && subTotal%10 < 6.99 ){
					roundUpAmt = 6.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 >= 6.99 && subTotal%10 < 7.99 ){
					roundUpAmt = 7.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 >= 7.99 && subTotal%10 < 9.99 ){
					roundUpAmt = 9.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
		}
		/** for items costing over $50 */
			if( subTotal > 50 ){							
			
				if( subTotal%10 >= 0.0 && subTotal%10 < 2.99 ){
					roundUpAmt = 2.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 >= 2.99 && subTotal%10 < 4.99 ){
					roundUpAmt = 4.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 >= 4.99 && subTotal%10 < 7.99 ){
					roundUpAmt = 7.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}
			
				if( subTotal%10 >= 7.99 && subTotal%10 < 9.99 ){
					roundUpAmt = 9.99;
					subTotal = subTotal - (subTotal%10);
					finalCalc(subTotal, roundUpAmt);
			}			
		}		
	}
	
	function finalCalc(subTotal, roundUpAmt){
		
		var subTotal = subTotal;
		var roundUpAmt = roundUpAmt;		 
		var itemCost = parseFloat(document.gpmFrm.itemCostTxt.value);
		var oldMargin = parseFloat(document.gpmFrm.profitMargTxt.value);
		
		roundedTotal = roundNumber(subTotal + roundUpAmt);
								
		roundUpPerc = ((roundedTotal - itemCost) /roundedTotal) * 100;
		roundUpPerc = roundNumber(roundUpPerc);				

		// populate form fields with results
		document.gpmFrm.roundUpDolTxt.value = roundedTotal;
		document.gpmFrm.roundUpPercTxt.value = roundUpPerc;
		
		// calculate differenced between old and new price
		// old and new profit margins		
		priceDif = roundNumber(roundedTotal - parseFloat(document.gpmFrm.newPriceTxt.value));
		
		profitMargDif = roundNumber(roundUpPerc - oldMargin);		
		
		// populate the price Difference & profit Difference fields on form
		document.gpmFrm.priceDifTxt.value = priceDif;
		document.gpmFrm.grossProfitPercDifTxt.value = profitMargDif;
	
	} // end finalCalc