

isNs4=(navigator.appName.indexOf("Netscape")>=0 && !document.getElementById)? 1 : 0;
isIe4=(document.all && !document.getElementById)? 1 : 0;
isIe5=(document.getElementById && document.all)? 1 : 0;
isNs6=(document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? 1: 0;
isW3c=(document.getElementById)? 1 : 0;

//wid=(isIe4||isIe5)?window.document.body.clientWidth-20:window.innerWidth-36

document.onmousemove = watchMouse;
if(isNs4){document.captureEvents( Event.MOUSEMOVE );}	

divY=divX=0		

function getById(id)
{
	if(isW3c) return document.getElementById(id);
	if(isIe4) return document.all[id];
	if(isNs4) return document.layers[id];		
}

function getEtVis(id)
{
	visId = id + '_DivVis';
	return getById(visId);					
}

function getEtDiv(id)
{
	divId = id + '_Panel';
	return getById(divId);	
}	

function getEtDivX(id)
{
	divXId = id + '_DivX';
	return getById(divXId);			
}	

function getEtDivY(id)
{
	divYId = id + '_DivY';	
	return getById(divYId);		
}

function hideEt(id)
{
	div = getEtDiv(id);
	setEtVisible(id, 'hidden');		
}

function setEtVisible(id, visible)
{
	div = getEtDiv(id);
	vis = getEtVis(id);
	
	div.style.visibility = visible;
	vis.value = visible;		
	
	if (visible == 'visible')
		div.style.zIndex = 10;
	else
		div.style.zIndex = -1;
}

function setEtControls(id)
{
	div = getEtDiv(id);
	vis = getEtVis(id);

	setEtVisible(id, vis.value);
	
	
	if (vis.value == 'visible')
	{
		
		divX = getEtDivX(id).value * 1;
		divY = getEtDivY(id).value * 1;			
		
		placeDiv(div, divX, divY);		
	}
}


function watchMouse(evt)
{
	if(isIe4||isIe5)
	{
		divX=window.event.clientX+document.body.scrollLeft; 
		divY=window.event.clientY+document.body.scrollTop;
	}
	else 
	{
		divX=evt.pageX; 
		divY=evt.pageY; 
	}
	if (typeof(INARRAY) != 'undefined')
		mouseMove(evt);

}


	
function displayEditScreen(id, dW, dH) 
{			
	minX = 165;
	minY = 145;
			
	div1 = getEtDiv(id);
	vis = getEtVis(id);
	
	if (vis.value != 'visible')
	{			
		setEtVisible(id, 'visible');
		
		div1.style.width = dW;
		div1.style.height = dH;
		
		w = dW * 1;
		h = dH * 1;
		
		dX = ((divX - w) < minX) ? minX : divX - w;
		dY = ((divY - h) < minY) ? minY : divY - h;						 
		
		placeDiv(div1, dX, dY);
		
		divX = getEtDivX(id);
		divY = getEtDivY(id);
		
		divX.value = dX;
		divY.value = dY;
	}
}

function placeDiv(div1,divX,divY)
{
	if (isNaN(divX+divY))return
	if(isNs4){div1.moveTo(divX,divY)}else{div1.style.left=divX+'px';div1.style.top=divY+'px';}
}	

function moneyToNumber(money) 
{
	//alert(money);
	var n = money.toString().replace(/\$|\,/g,'');
	var num;
	if (n.indexOf('(') >= 0)
	{
		n = n.replace(/\(|\)/g,'');
		num = n * -1;
	} else {
		num = n * 1;
	}
	return num;
}	

function confirmChangeMonthlySales()
{
	return confirm('Changing the starting month and number of months can delete some of the values you have previously entered.');
}



function formatCurrency(num) 
{
	
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {
		alert('nun is NaN');
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
			num.substring(num.length-(4*i+3));
	
	var money = '$' + num + '.' + cents;
	
	if (!sign)		
		money = '(' + money + ')';
		
	return money;
	//return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function calculateIncomeStatement()
{

	grossProfit		= document.getElementById('GrossProfitTotal');
	operating			= document.getElementById('OperatingExpensesTotal');
	otherExpenses = document.getElementById('OtherExpensesTotal');

	g		= moneyToNumber(grossProfit.innerHTML);
	op	= moneyToNumber(operating.innerHTML);
	oe	= moneyToNumber(otherExpenses.value);

	n	= g - op + oe;
	
	netIncomeTotal									= document.getElementById('NetIncomeTotal');
	netIncomeTotal.innerHTML				= formatCurrency(n);	
	netIncomeTotal.style.fontWeight = 'bold';
}


function calculateCategoryTotal(valId, prevId, totalId, isAdd, isUseAbs)
{
	val = document.getElementById(valId);
	prev = document.getElementById(prevId);
	total = document.getElementById(totalId);
	
	//alert(typeof(total).toString());
	
	if (val)
	{
		v = moneyToNumber(val.value);
		p = moneyToNumber(prev.value);
		
		if (total.innerHTML != '')		
			totalVal = total.innerHTML;
		else
			totalVal = total.value;
		
		t = moneyToNumber(totalVal); 
		
		//alert('isUseAbs: ' + isUseAbs);
		if (isAdd) {		
			x = v - p + t;
		} else {
			if(isUseAbs){
			x = p - v + t;
			}
			else{
			x = Math.abs(p) - Math.abs(v) + t;
			}
			//x = p - v + t;
		} 
		
		//alert(foo);
		
		if (!isNaN(x.toString()))
		{
			var formattedCurrency = formatCurrency(x);
		
			
			if (total.innerHTML != '')
			{
				total.innerHTML = formattedCurrency;
				total.style.fontWeight = 'bold';
			} else {
				total.value = formattedCurrency;
			}
			prev.value = v;
		}		
		
			
	}		
}		

function calculateAdditionToPriceTotals(valId, prevId, totalId, addId, addPrevId)
{
	val = document.getElementById(valId);
	prev = document.getElementById(prevId);
	add = document.getElementById(addId);
	addPrev = document.getElementById(addPrevId);

	total = document.getElementById(totalId);
	
	if (val)
	{
		v = moneyToNumber(val.value);
		p = moneyToNumber(prev.value);
		t = moneyToNumber(total.innerHTML);
		if (add.selectedIndex == 0) {
			
				x = v - p + t;
			
		}else{
			x = p - v + t;
		}
			
		
		
		if (!isNaN(x.toString()))
		{
			total.innerHTML = formatCurrency(x);
			total.style.fontWeight = 'bold';
			prev.value = v;
			
		}				
	}		

}

function calculateAssetSheetTotals()
{
	assets = document.getElementById("CurrentAssetsTotal");
	longTerm = document.getElementById("LongTermTotal");
	assetsTotal = document.getElementById("AssetsTotal");
	
	if (assets) 
	{
		a = moneyToNumber(assets.innerHTML);
		l = moneyToNumber(longTerm.innerHTML);
		
		t = a + l;
		
		if (!isNaN(t.toString()))
		{
			assetsTotal.innerHTML = formatCurrency(t);
			assetsTotal.style.fontWeight = 'bold';				
		}			
	}	
}

function calculateLiabilitiesTotals() 
{
	if (document.getElementById)
	{
		current = document.getElementById("CurrentLiabilitiesTotal");
		longTerm = document.getElementById("LongTermLiabilitiesTotal");
		owner = document.getElementById("OwnerEquityTotal");
		total = document.getElementById("LiabilityAndEquityTotal");
		offBy = document.getElementById("BalanceSheetOffByTotal");
		assetsTotal = document.getElementById("AssetsTotal");
		diffExplain = document.getElementById("BalanceSheetDifferentExplain");
		editAssets = document.getElementById("EditAssets");
		
		c = moneyToNumber(current.innerHTML);
		l = moneyToNumber(longTerm.innerHTML);
		o = moneyToNumber(owner.innerHTML);
		
		t = c + l + o;		
		
		if (!isNaN(t.toString())) 
		{		
			total.innerHTML = formatCurrency(t);
			
			a = moneyToNumber(assetsTotal.value);
			
			o = t - a;
			o = Math.abs(o);
			
			offBy.innerHTML = formatCurrency(o);
			offBy.style.fontWeight = 'bold';
			
			if (o > 0) 
			{
				offBy.style.color = 'firebrick';
				var explain;
				if (t > a)
					explain = '(Liabilities > Assets)';
				else
					explain = '(Assets > Liabilities)';
				
				diffExplain.innerHTML = explain;
				diffExplain.style.color = 'firebrick';
				
				editAssets.innerHTML = '<br>Click to edit Assets';
				
			} else {
				//alert('not 0');
				offBy.style.color = 'black';
				diffExplain.innerHTML = '';
				editAssets.innerHTML = '';
			}
		}		
	}
}

function confirmRemoveBalanceSheet()
{
	return confirm('Deleting this balance sheet will permanently remove all Liabilities and Asset data.');
}

function confirmDeleteFacility()
{
	return confirm('Deleting this facility will permanently remove all data related to this location.');	
}

function calculateNonOwners(fullId, partId, totalId)
{
	fullTime = document.getElementById(fullId);
	partTime = document.getElementById(partId);
	total = document.getElementById(totalId);
	
	f = fullTime.value * 1;
	p = partTime.value * 1;
	
	t = f + p;
	
	if (!isNaN(t)) 
	{
		total.innerHTML = t.toString();
		total.style.fontWeight = 'bold';
	}
}

function calculateOtherEmployees(fullId, partId, seasonalId, totalId) 
{
	fullTime = document.getElementById(fullId);
	partTime = document.getElementById(partId);
	seasonal = document.getElementById(seasonalId);
	total = document.getElementById(totalId);
	
	f = fullTime.value * 1;
	p = partTime.value * 1;
	s = seasonal.value * 1;
	
	t = f + p + s;
	
	if (!isNaN(t)) 
	{
		total.innerHTML = t.toString();
		total.style.fontWeight = 'bold';
	}
}  


