// JavaScript Document

nextfield = "SellingPrice"; // name of first box on page
netscape = "";
ver = navigator.appVersion; len = ver.length;
for(iln = 0; iln < len; iln++) if (ver.charAt(iln) == "(") break;
netscape = (ver.charAt(iln+1).toUpperCase() != "C");

function keyDown(DnEvents) { // handles keypress
// determines whether Netscape or Internet Explorer
k = (netscape) ? DnEvents.which : window.event.keyCode;
if (k == 13) { // enter key pressed
if (nextfield == 'done') return true; // submit, we finished all fields
else { // we're not done yet, send focus to next box
eval('document.BreakEvenForm.' + nextfield + '.focus()');
return false;
      }
   }
}
document.onkeydown = keyDown; // work together to analyze keystrokes
if (netscape) document.captureEvents(Event.KEYDOWN|Event.KEYUP);


function SelectFieldOnFocus(thisfield) { 
	eval('document.BreakEvenForm.' + thisfield + '.select()');
	return false;
}

function showdetails() {
	
  var wBreakEven = 0;
  var wTotalRevenue = 0;
  var wTotalCost = 0;
  var wProfit = 0;
  var wSP1 = 0;
  var wFixedCost = 0;
  var wVC1 = 0;
  var wTP = 0;  
  var wTotalVariableCost = 0;
  var wTotalCostperUnit = 0;
  var wProfitMargin = 0;
  var wMarkupPercent = 0;
  var content = "";

  wSP1 = (document.getElementById('SellingPrice').value != '') ? eval(document.getElementById('SellingPrice').value) : 0;
  wFixedCost = (document.getElementById('FixedCost').value != '') ? eval(document.getElementById('FixedCost').value) : 0;
  wVC1 = (document.getElementById('VarCostPerUnit').value != '') ? eval(document.getElementById('VarCostPerUnit').value) : 0;
  wTP = (document.getElementById('TargetProfit').value != '') ? eval(document.getElementById('TargetProfit').value) : 0;  
  
  var wPriceRBselect = document.getElementById('PriceUnitsOption').value;
  var wCostRBselect = document.getElementById('VcuTvcOption').value;
  
  wInput2 = "Fixed Cost: ";
  wInput4 = "Target Profit: ";
  wResults2 = "Total Revenue: ";
  wResults3 = "Fixed Cost (FC): ";
  wResults4 = "Total Variable Cost (VC): ";
  wResults5 = "Total Cost (TC): ";
  wResults6 = "Profit: ";
  wResults7 = "Total Cost per Unit: ";
  wResults8 = "Profit margin: ";
  wResults9 = "Markup percent: ";
  
  
  if (wPriceRBselect == 1)
  	{
		if (wCostRBselect == 1)
			{
  wBreakEven = Math.round(((wFixedCost+wTP)/(wSP1-wVC1))*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wTP)/(wSP1-wVC1)))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1*((wFixedCost+wTP)/(wSP1-wVC1)))*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wTP)/(wSP1-wVC1)))-(wFixedCost+wVC1*((wFixedCost+wTP)/(wSP1-wVC1))))*100)/100 ; 
  wInput1 = "Selling Price per Unit: ";
  wInput3 = "Variable Cost per Unit: ";
  wResults1 = "Target units: ";
  
			}
		else if (wCostRBselect == 2)
			{
  wBreakEven = Math.round(((wFixedCost+wVC1+wTP)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wVC1+wTP)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1)*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wVC1+wTP)/wSP1)) - (wFixedCost+wVC1))*100)/100 ; 
  wInput1 = "Selling Price per Unit: ";
  wInput3 = "Total Variable Cost: ";
  wResults1 = "Target units: ";
  
			}
	}
  else if (wPriceRBselect == 2)
  	{
		if (wCostRBselect == 1)
			{
  wBreakEven = Math.round((wVC1+(wFixedCost+wTP)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*(wVC1+(wFixedCost+wTP)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1*wSP1)*100)/100 ; 
  wProfit = Math.round(((wSP1*(wVC1+(wFixedCost+wTP)/wSP1)) - (wFixedCost+wVC1*wSP1))*100)/100 ; 
  wInput1 = "Selling Units: ";
  wInput3 = "Variable Cost per Unit: ";
  wResults1 = "Target price: ";
  
			}
		else if (wCostRBselect == 2)
			{
  wBreakEven = Math.round(((wFixedCost+wVC1+wTP)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wVC1+wTP)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1)*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wVC1+wTP)/wSP1)) - (wFixedCost+wVC1))*100)/100 ; 
  wInput1 = "Selling Units: ";
  wInput3 = "Total Variable Cost: ";
  wResults1 = "Target price: ";
  
			}
	}
	
	wTotalVariableCost = wTotalCost - wFixedCost;
	wProfitMargin = Math.round(((wProfit*100)/wTotalRevenue)*100)/100;
	wMarkupPercent = Math.round(((wProfit*100)/wTotalCost)*100)/100;
	
  if (wPriceRBselect == 1)
  	{
	wTotalCostperUnit = Math.round((wTotalCost/wBreakEven)*100)/100;
	}
  else if (wPriceRBselect == 2)
  	{
	wTotalCostperUnit = Math.round((wTotalCost/wSP1)*100)/100;
	}
	
	
	content = "<strong> Profit Analysis - details report: </strong>";
	content = content + "<br /><br />";
	
	content = content + "<strong><u>Input</u></strong>";
	content = content + "<br /><br />";
	
	content = content + wInput1;
	
  if (wPriceRBselect == 1)
  	{
	content = content + "$ " + formatNumber(wSP1,2,',','.','','','-','') ;
	}
  else if (wPriceRBselect == 2)
  	{
	content = content + formatNumber(wSP1,2,',','.','','','-','') ;
	}
	content = content + "<br /><br />";
	
	content = content + wInput2;
	content = content + "$ " + formatNumber(wFixedCost,2,',','.','','','-','');
	content = content + "<br /><br />";
	content = content + wInput3;
	content = content + "$ " + formatNumber(wVC1,2,',','.','','','-','');
	content = content + "<br /><br />";
	content = content + wInput4;
	content = content + "$ " + formatNumber(wTP,2,',','.','','','-','');
	content = content + "<br /><br /><br />";
	
	content = content + "<strong><u>Results</u></strong>";
	content = content + "<br /><br />";
	
	content = content + "<strong>" + wResults1 + "</strong>";
	
  if (wPriceRBselect == 1)
  	{
	content = content + "<strong>" + formatNumber(wBreakEven,2,',','.','','','-','') + "</strong>";
	}
  else if (wPriceRBselect == 2)
  	{
	content = content + "<strong>" + "$ " + formatNumber(wBreakEven,2,',','.','','','-','') + "</strong>";
	}
	content = content + "<br /><br />";

	content = content + "<table cellpadding='5' cellspacing='0' border='1'>";

	content = content + "<tr>";
	content = content + "<td>&nbsp;</td>";
	content = content + "<td>&nbsp;</td>";
	content = content + "<td>" + wResults3 + "</td>";
	content = content + "<td>" + "$ " + formatNumber(wFixedCost,2,',','.','','','-','') + "</td>";
	content = content + "</tr>";

	content = content + "<tr>";
	content = content + "<td>&nbsp;</td>";
	content = content + "<td>&nbsp;</td>";
	content = content + "<td>" + wResults4+ "</td>";
	content = content + "<td>" + "$ " + formatNumber(wTotalVariableCost,2,',','.','','','-','') + "</td>";
	content = content + "</tr>";

	content = content + "<tr>";
	content = content + "<td><strong>" + wResults2 + "</strong></td>";
	content = content + "<td><strong>" + "$ " + formatNumber(wTotalRevenue,2,',','.','','','-','') + "</strong></td>";
	content = content + "<td><strong>" + wResults5 + "</strong></td>";
	content = content + "<td><strong>" + "$ " + formatNumber(wTotalCost,2,',','.','','','-','') + "</strong></td>";
	content = content + "</tr>";

	content = content + "</table>";

	content = content + "<br />";
	
	content = content + "<strong>" + wResults6 + "</strong>";
	content = content + "<strong>" + "$ " + formatNumber(wProfit,2,',','.','','','-','') + "</strong>";
	content = content + "<br /><br />";
	
	content = content + wResults7;
	content = content + "$ " + formatNumber(wTotalCostperUnit,2,',','.','','','-','');
	content = content + "<br /><br />";
	
	content = content + wResults8;
	content = content + wProfitMargin + " %";
	content = content + "<br /><br />";
	
	content = content + wResults9;
	content = content + wMarkupPercent + " %";

	
	contentfinal = content + "<br /><br />";
	
  if ((wSP1 > 0) && (wFixedCost > 0) && (wVC1 > 0))
    {
	writeNewPage(contentfinal);
	}
  else
  	{
	writeNewPage("In order to see the detail report, all the fields " + "<strong>" + "MUST" + "</strong>" + " be filled in.");
	}
	
}

function writeNewPage(content) {
 top.pageRef=window.open('','detailspage',
  'width=750,height=500'
   +',menubar=0'
   +',toolbar=1'
   +',status=0'
   +',scrollbars=1'
   +',resizable=1')
 top.pageRef.document.writeln(
  '<html><head><title>Profit Analysis - details report</title></head>'
   +'<body bgcolor="white">'
   +content
   +'</body></html>'
 )
 top.pageRef.document.close()
}

function calculateBE(form){
	
  var wBreakEven = 0;
  var wTotalRevenue = 0;
  var wTotalCost = 0;
  var wProfit = 0;
  var wSP1 = 0;
  var wFixedCost = 0;
  var wVC1 = 0;
  var wTP = 0;  
  
  wSP1 = (form.SellingPrice.value != '') ? eval(form.SellingPrice.value) : 0;
  wFixedCost = (form.FixedCost.value != '') ? eval(form.FixedCost.value) : 0;
  wVC1 = (form.VarCostPerUnit.value != '') ? eval(form.VarCostPerUnit.value) : 0;
  wTP = (form.TargetProfit.value != '') ? eval(form.TargetProfit.value) : 0;  
  
  var wPriceRBselect = form.PriceUnitsOption.value;
  var wCostRBselect = form.VcuTvcOption.value;
  
  if (wPriceRBselect == 1)
  	{
		if (wCostRBselect == 1)
			{
  wBreakEven = Math.round(((wFixedCost+wTP)/(wSP1-wVC1))*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wTP)/(wSP1-wVC1)))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1*((wFixedCost+wTP)/(wSP1-wVC1)))*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wTP)/(wSP1-wVC1)))-(wFixedCost+wVC1*((wFixedCost+wTP)/(wSP1-wVC1))))*100)/100 ; 
			}
		else if (wCostRBselect == 2)
			{
  wBreakEven = Math.round(((wFixedCost+wVC1+wTP)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wVC1+wTP)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1)*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wVC1+wTP)/wSP1)) - (wFixedCost+wVC1))*100)/100 ; 
			}
	}
  else if (wPriceRBselect == 2)
  	{
		if (wCostRBselect == 1)
			{
  wBreakEven = Math.round((wVC1+(wFixedCost+wTP)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*(wVC1+(wFixedCost+wTP)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1*wSP1)*100)/100 ; 
  wProfit = Math.round(((wSP1*(wVC1+(wFixedCost+wTP)/wSP1)) - (wFixedCost+wVC1*wSP1))*100)/100 ; 
			}
		else if (wCostRBselect == 2)
			{
  wBreakEven = Math.round(((wFixedCost+wVC1+wTP)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wVC1+wTP)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1)*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wVC1+wTP)/wSP1)) - (wFixedCost+wVC1))*100)/100 ; 
			}
	}
	

  if ((wSP1 > 0) && (wFixedCost > 0) && (wVC1 > 0))
    {
		if ((wBreakEven < 0) || ((wPriceRBselect == 1) && (wSP1 == wVC1)))
		  {
			  alert ("This case CAN NOT reach the target profit!!!");
	  
			  if (wPriceRBselect == 2)
				{
			  document.getElementById('BreakEvenPoint').innerHTML = "$ 0.00";
				}
			  else
				{
			  document.getElementById('BreakEvenPoint').innerHTML = "0.00";
				}
			  
			  document.getElementById('TotalRevenue').innerHTML = "$ 0.00";  
			  document.getElementById('TotalCost').innerHTML = "$ 0.00";
			  document.getElementById('Profit').innerHTML = "$ 0.00";
	  
		  } 
		else
		  {
			  if (wPriceRBselect == 2)
				{
			  document.getElementById('BreakEvenPoint').innerHTML = "$ " + formatNumber(wBreakEven,2,',','.','','','-','');
				}
			  else
				{
			  document.getElementById('BreakEvenPoint').innerHTML = formatNumber(wBreakEven,2,',','.','','','-','');
				}
			  document.getElementById('TotalRevenue').innerHTML = "$ " + formatNumber(wTotalRevenue,2,',','.','','','-','');  
			  document.getElementById('TotalCost').innerHTML = "$ " + formatNumber(wTotalCost,2,',','.','','','-','');
			  document.getElementById('Profit').innerHTML = "$ " + formatNumber(wProfit,2,',','.','','','-','');
		  } 
	
	}
	else
	{
	  if (wPriceRBselect == 2)
		{
	  document.getElementById('BreakEvenPoint').innerHTML = "$ 0.00";
		}
	  else
		{
	  document.getElementById('BreakEvenPoint').innerHTML = "0.00";
		}
	  
	  document.getElementById('TotalRevenue').innerHTML = "$ 0.00";  
	  document.getElementById('TotalCost').innerHTML = "$ 0.00";
	  document.getElementById('Profit').innerHTML = "$ 0.00";
	}
		
}


function resetBE(form){

  var image = document.getElementById('imageHU');
  var title = document.getElementById('TitleSwitch');
  var element = document.getElementById('elementToShow');
  
  form.SellingPrice.value = "";
  form.FixedCost.value = "";
  form.VarCostPerUnit.value = "";
  form.TargetProfit.value = "";
  
  document.getElementById('BreakEvenPoint').innerHTML = "0.00";
  document.getElementById('TotalRevenue').innerHTML = "$ 0.00";  
  document.getElementById('TotalCost').innerHTML = "$ 0.00";
  document.getElementById('Profit').innerHTML = "$ 0.00";
  
  document.getElementById('PriceRB').checked = "true";
  document.getElementById('VcuRB').checked = "true";
  
  document.getElementById('PriceUnitsOption').value=1;
  document.getElementById('VcuTvcOption').value=1;
  
  document.getElementById('PriceUnitsLabel').innerHTML = "Selling Price per Unit:";
  document.getElementById('BEUnitsPriceLabel').innerHTML = "Target units:";
  document.getElementById('CpuVcuLabel').innerHTML = "Variable Cost per Unit:";
  
  image.src = "../images/unhide.png";
  element.style.display = "none";

}


function HideUnhide(form) {
		
		var image = document.getElementById('imageHU');
		var title = document.getElementById('TitleSwitch');
		var element = document.getElementById('elementToShow');
		
		if (element.style.display == "none"){
			image.src = "../images/hide.png";
			document.getElementById('TitleSwitch').innerHTML = "Change variables";
			element.style.display = "block";
		}
		else
		{
			image.src = "../images/unhide.png";
			document.getElementById('TitleSwitch').innerHTML = "Change variables";
			element.style.display = "none";
		}
		
	}


function SwitchPriceUnitsVcuTvc() {
		
		var wPriceRB = document.getElementById('PriceRB').checked;
		var wUnitsRB = document.getElementById('UnitsRB').checked;
		var wVcuRB = document.getElementById('VcuRB').checked;
		var wTvcRB = document.getElementById('TvcRB').checked;
		
		if (wPriceRB)
			{
				document.getElementById('PriceUnitsOption').value=1;
				document.getElementById('PriceUnitsLabel').innerHTML = "Selling Price per Unit:";
				document.getElementById('BEUnitsPriceLabel').innerHTML = "Target units:";
			}
		else if (wUnitsRB)
			{
				document.getElementById('PriceUnitsOption').value=2;
				document.getElementById('PriceUnitsLabel').innerHTML = "Selling Units:";
				document.getElementById('BEUnitsPriceLabel').innerHTML = "Target price:";
			}
		
		if (wVcuRB)
			{
				document.getElementById('VcuTvcOption').value=1;
				document.getElementById('CpuVcuLabel').innerHTML = "Variable Cost per Unit:";
			}
		else if (wTvcRB)
			{
				document.getElementById('VcuTvcOption').value=2;
				document.getElementById('CpuVcuLabel').innerHTML = "Total variable cost:";
			}
		
	}

// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
// num=number; dec=nr of decimals; thou=thousand separator; pnt=decimal separator; curr=currency; n1,n2=the symbols to place around the number when the value is negative.

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) n1=n2='';
	var y = (''+Math.abs(x)).split('');
	var z = y.length - dec; 
	
	if (z<0) z--; 
	for(var i = z; i < 0; i++) y.unshift('0'); 
	if (z<0) z = 1; 
	y.splice(z, 0, pnt); 
	if(y[0] == pnt) y.unshift('0'); 
	while (z > 3) {z-=3; y.splice(z,0,thou);}
	var r = curr1+n1+y.join('')+n2+curr2;return r;}


