// 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 calculateBE(form){
	
  var wBreakEven = 0;
  var wTotalRevenue = 0;
  var wTotalCost = 0;
  var wProfit = 0;
  var wSP1 = 0;
  var wFixedCost = 0;
  var wVC1 = 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;
  
  var wPriceRBselect = form.PriceUnitsOption.value;
  var wCostRBselect = form.VcuTvcOption.value;
  
  if (wPriceRBselect == 1)
  	{
		if (wCostRBselect == 1)
			{
  wBreakEven = Math.round((wFixedCost/(wSP1-wVC1))*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*(wFixedCost/(wSP1-wVC1)))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1*(wFixedCost/(wSP1-wVC1)))*100)/100 ; 
  wProfit = Math.round(((wSP1*(wFixedCost/(wSP1-wVC1)))-(wFixedCost+wVC1*(wFixedCost/(wSP1-wVC1))))*100)/100 ; 
			}
		else if (wCostRBselect == 2)
			{
  wBreakEven = Math.round(((wFixedCost+wVC1)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wVC1)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1)*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wVC1)/wSP1)) - (wFixedCost+wVC1))*100)/100 ; 
			}
	}
  else if (wPriceRBselect == 2)
  	{
		if (wCostRBselect == 1)
			{
  wBreakEven = Math.round((wVC1+wFixedCost/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*(wVC1+wFixedCost/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1*wSP1)*100)/100 ; 
  wProfit = Math.round(((wSP1*(wVC1+wFixedCost/wSP1)) - (wFixedCost+wVC1*wSP1))*100)/100 ; 
			}
		else if (wCostRBselect == 2)
			{
  wBreakEven = Math.round(((wFixedCost+wVC1)/wSP1)*100)/100 ; 
  wTotalRevenue = Math.round((wSP1*((wFixedCost+wVC1)/wSP1))*100)/100 ; 
  wTotalCost = Math.round((wFixedCost+wVC1)*100)/100 ; 
  wProfit = Math.round(((wSP1*((wFixedCost+wVC1)/wSP1)) - (wFixedCost+wVC1))*100)/100 ; 
			}
	}
	

  if ((wSP1 > 0) && (wFixedCost > 0) && (wVC1 > 0))
    {
		if ((wBreakEven < 0) || ((wPriceRBselect == 1) && (wSP1 == wVC1)))
		  {
			  alert ("This case CAN NOT break-even!!! The revenue is less then the cost.");
	  
			  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 = "";
  
  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 = "Break-even 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 = "Break-even units:";
			}
		else if (wUnitsRB)
			{
				document.getElementById('PriceUnitsOption').value=2;
				document.getElementById('PriceUnitsLabel').innerHTML = "Selling Units:";
				document.getElementById('BEUnitsPriceLabel').innerHTML = "Break-even 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;}


