
vxTicks = new Array();
vxTicks[1] = '1<sup>st</sup>';
vxTicks[2] = '2<sup>nd</sup>';
vxTicks[3] = '3<sup>rd</sup>';
vxTicks[4] = '4<sup>th</sup>';
vxTicks[5] = '5<sup>th</sup>';
vxTicks[6] = '6<sup>th</sup>';
vxTicks[7] = '7<sup>th</sup>';
vxTicks[8] = '8<sup>th</sup>';
vxTicks[9] = '9<sup>th</sup>';
vxTicks[10] = '10<sup>th</sup>';
vxTicks[11] = '11<sup>th</sup>';
vxTicks[12] = '12<sup>th</sup>';


vGraph = 0;

function draw(){;
	if(vGraph) vGraph.clear();
	vGraph = new SAWGraph(document.getElementById('graph01'), document.getElementById('graph01frame'));
	
	vFrame = vGraph.getFrame();
	vFrame.setAxisTitles('<strong>Months from opening</strong>', '<strong>[$]</strong>')
	vFrame.getXAxis().setTicks(vxTicks);

	var i;
	for(i=1; i<=12; i++){
		if(i==1) sMonth = '1<sup>st</sup> month'; else if(i==2) sMonth = '2<sup>nd</sup> month'; else if(i==3) sMonth = '3<sup>rd</sup> month'; else sMonth = i+'<sup>th</sup> month';
	
		vCash = cash[i-1];
		sCashColor = '#FF7070';
		vBar = vGraph.addBar(i, 0.2, 0, vCash, sCashColor);
		vBar.forcePixelWidth(11);
		vBar.setPixelMove(-11, 0);
		vBar.setEvent('mouseover', function(vAmount, sMonth, vBar, sColor, bLeft){
			return function(){popupInfo("&nbsp;"+sMonth+" cash: $"+Math.round(vAmount)+"&nbsp;", vBar, sColor, bLeft);}
		}(vCash, sMonth, vBar, sCashColor, i>=10));
		
		vCashFlow = cashFlow[i-1];
		sCashFlowColor = '#A0FFC0';
		vBar = vGraph.addBar(i, 0.2, 0, vCashFlow, sCashFlowColor);
		vBar.forcePixelWidth(11);
		vBar.setEvent('mouseover', function(vAmount, sMonth, vBar, sColor, bLeft){
			return function(){popupInfo("&nbsp;"+sMonth+" cashflow: $"+Math.round(vAmount)+"&nbsp;", vBar, sColor, bLeft);}
		}(vCashFlow, sMonth, vBar, sCashFlowColor, i>=10));
		
		vProfit = profits[i-1];
		vBar = vGraph.addBar(i, 0.2, 0, vProfit, '#7090E0');
		vBar.forcePixelWidth(11);
		vBar.setPixelMove(11, 0);
		vBar.setEvent('mouseover', function(vAmount, sMonth, vBar, sColor, bLeft){
			return function(){popupInfo("&nbsp;"+sMonth+" profits: $"+Math.round(vAmount)+"&nbsp;", vBar, sColor, bLeft)}
		}(vProfit, sMonth, vBar, '#70B0D0', i>=10));
	}
	
	vGraph.addLegendBar(document.getElementById('legend01_01'),'#FF7070');

	vGraph.addLegendBar(document.getElementById('legend01_02'),'#A0FFC0');

	vGraph.addLegendBar(document.getElementById('legend01_03'),'#7090E0');

	vGraph.draw();
}

function popupInfo(sText, vBar, sColor, bLeft){
	var vSpan = document.createElement('div');
	vSpan.className ='infoPopupFlyHidden';
	vSpan.innerHTML = sText;
	vSpan.style.left = "0px";
	vSpan.style.top = "0px";
	if(bLeft)ix = vBar.getLeftPos();
	else ix = vBar.getRightPos();

	iy = vBar.getTopPos()-12;
	vSpan.style.position = 'absolute';
	document.getElementById('graph01').appendChild(vSpan);
	vSpan.style.width = vSpan.offsetWidth+"px";
	vSpan.style.height = vSpan.offsetHeight+"px";
	if(bLeft) vSpan.style.left = (ix-vSpan.offsetWidth)+"px";
	else vSpan.style.left = ix+"px";
	vSpan.style.top = iy+"px";
	//vSpan.style.backgroundColor = sColor;
	vSpan.className ='infoPopupFly';
	
	setTimeout(function(vEl){return function(){document.getElementById('graph01').removeChild(vEl)}}(vSpan), 2000);
}

sliderCOGS = new SAWSlider(document.getElementById('sliderDivCOGS'));
sliderCOGS.setBounds(0, 90);
sliderCOGS.onchange='getSliderValue100("COGS")';
sliderCOGS.onfinalchange='getSliderValue100("COGS");SAWUtils.unblurElement(document.getElementById("graph01frame"));setTimeout("calculator_compute(); adjust_tips()", 10)';
sliderCOGS.onslidestart = function(){setTimeout("SAWUtils.blurElement(document.getElementById('graph01frame'));", 10); }


sliderMSG = new SAWSlider(document.getElementById('sliderDivMSG'));
sliderMSG.setResolution(0.5);
sliderMSG.setBounds(0,15);
sliderMSG.onchange="getSliderValue100('MSG', true)";
sliderMSG.onfinalchange='getSliderValue100("MSG", true); SAWUtils.unblurElement(document.getElementById("graph01frame"));setTimeout("calculator_compute();adjust_tips()", 10)';
sliderMSG.onslidestart = function(){setTimeout("SAWUtils.blurElement(document.getElementById('graph01frame'));", 10); }

sliderCD = new SAWSlider(document.getElementById('sliderDivCD'));
sliderCD.setBounds(0, 120);
sliderCD.onchange='getSliderValue("CD")';
sliderCD.onfinalchange='getSliderValue("CD");SAWUtils.unblurElement(document.getElementById("graph01frame"));setTimeout("calculator_compute(); adjust_tips();", 10)';
sliderCD.onslidestart = function(){setTimeout("SAWUtils.blurElement(document.getElementById('graph01frame'));", 10); }

sliderCP = new SAWSlider(document.getElementById('sliderDivCP'));
sliderCP.setBounds(0, 100);
sliderCP.onchange='getSliderValue100("CP")';
sliderCP.onfinalchange='getSliderValue100("CP");SAWUtils.unblurElement(document.getElementById("graph01frame"));setTimeout("calculator_compute(); adjust_tips();", 10)';
sliderCP.onslidestart = function(){setTimeout("SAWUtils.blurElement(document.getElementById('graph01frame'));", 10); }

//sliderPOS = new SAWSlider(document.getElementById('sliderDivPOS'));
//sliderPOS.setBounds(0, 90);
//sliderPOS.onchange='getSliderValue100("POS")';
//sliderPOS.onfinalchange='getSliderValue100("POS");SAWUtils.unblurElement(document.getElementById("graph01frame"));setTimeout("calculator_compute();adjust_tips()", 10)';
//sliderPOS.onslidestart = function(){setTimeout("SAWUtils.blurElement(document.getElementById('graph01frame'));", 10); }

sliderMIK = new SAWSlider(document.getElementById('sliderDivMIK'));
sliderMIK.setBounds(0, 12);
sliderMIK.onchange='getSliderValue("MIK")';
sliderMIK.onfinalchange='getSliderValue("MIK");SAWUtils.unblurElement(document.getElementById("graph01frame"));setTimeout("calculator_compute(); adjust_tips()", 10)';
sliderMIK.onslidestart = function(){setTimeout("SAWUtils.blurElement(document.getElementById('graph01frame'));", 10); }


sliderPD = new SAWSlider(document.getElementById('sliderDivPD'));
sliderPD.setBounds(0, 90);
sliderPD.onchange='getSliderValue("PD")';
sliderPD.onfinalchange='getSliderValue("PD");SAWUtils.unblurElement(document.getElementById("graph01frame"));setTimeout("calculator_compute();adjust_tips();", 10)';
sliderPD.onslidestart = function(){setTimeout("SAWUtils.blurElement(document.getElementById('graph01frame'));", 10); }

document.getElementById("valueMSG").innerHTML = "<strong>"+sliderMSG.getValue()+" %</strong>";
document.getElementById("valueCOGS").innerHTML = "<strong>"+sliderCOGS.getValue()+" %</strong>";


var vDate = new Date();
var iMonth = vDate.getMonth();
var iYear = vDate.getFullYear();
var iDay = vDate.getDay();

var sMonth, sDay;
switch(iMonth){
case(0): sMonth = "January"; break;
case(1): sMonth = "February"; break;
case(2): sMonth = "March"; break;
case(3): sMonth = "April"; break;
case(4): sMonth = "May"; break;
case(5): sMonth = "June"; break;
case(6): sMonth = "July"; break;
case(7): sMonth = "August"; break;
case(8): sMonth = "September"; break;
case(9): sMonth = "October"; break;
case(10): sMonth = "November"; break;
case(11): sMonth = "December"; break;
}

iDay++;
if(iDay==1 || iDay==21 || iDay==31) sDay = iDay+"st";
else if(iDay==2) sDay = iDay+"nd";
else if(iDay==3) sDay = iDay+"rd";
else sDay = iDay+"th";

document.getElementById('date').value = sMonth+" "+sDay+", "+iYear;



function setInputValue(sVar){
	document.getElementById('i'+sVar).value=Math.round(eval(sVar)*10000)/10000;
}

function setInputValue100(sVar){
	document.getElementById('i'+sVar).value=Math.round(eval(sVar)*10000)/100;
}

function setSliderValue(sVar){
	var vv=Math.round(eval(sVar)*10000)/10000;
	eval("slider"+sVar+".setValue("+vv+");");
	document.getElementById('value'+sVar).innerHTML="<strong>"+vv+"</strong>";
}

function setSliderValue100(sVar, bOneDigit){
	var vv=Math.round(eval(sVar)*10000)/100;
	if(bOneDigit && Math.abs(Math.round(vv)-vv)<0.01) vv=vv+".0";
	eval("slider"+sVar+".setValue("+vv+");");
	document.getElementById('value'+sVar).innerHTML="<strong>"+vv+" %</strong>";
}

function getSliderValue(sVar, bOneDigit){
	eval(sVar+" = slider"+sVar+".getValue()");
	var vVar = Math.round(eval(sVar)*100)/100; 
	if(bOneDigit && Math.abs(Math.round(vVar)-vVar)<0.01) vVar=vVar+".0"; 
	document.getElementById("value"+sVar).innerHTML = "<strong>"+vVar+" </strong>";
}


function getSliderValue100(sVar, bOneDigit){
	eval(sVar+" = slider"+sVar+".getValue()/100");
	var vVar = Math.round(eval(sVar)*10000)/100;
	if(bOneDigit && Math.abs(Math.round(vVar)-vVar)<0.01) vVar=vVar+".0"; 
	document.getElementById("value"+sVar).innerHTML = "<strong>"+vVar+" %</strong>";
}

function getInputValue(sVar, vmin, vmax){
	if(vmin==null) vmin='';
	if(vmax==null) vmax='';
	sEval=
		"bRet=true;"+sVar+"=eval(document.getElementById('i'+sVar).value);";
	if(vmin!='') sEval+="if("+sVar+"<"+vmin+") {alert('Variable "+sVar+" should be greater than "+vmin+"!'); bRet=false;}";
	if(vmax!='') sEval+="if(bRet && "+sVar+">"+vmax+") {alert('Variable "+sVar+" should not exceed "+vmax+"!'); bRet=false;}";
	eval(sEval);
	return bRet;
}

function setOutputValue(sVar){
	document.getElementById('i'+sVar).innerHTML=Math.round(eval(sVar));
}

function calculator_compute(){
	cashFlow=new Array();
	cash=new Array();
	profits=new Array();

	imCalc();
	setPDFData();
	draw();
}

function setPDFData(){
	sCash = ''; sCashFlow=''; sProfits='';
	for(i=0;i<12;i++) {sCash+=Math.round(cash[i]); if(i<11) sCash+=',';}
	for(i=0;i<12;i++) {sCashFlow+=Math.round(cashFlow[i]); if(i<11) sCashFlow+=',';}
	for(i=0;i<12;i++) {sProfits+=Math.round(profits[i]); if(i<11) sProfits+=',';}
	document.getElementById('profitsPDF').value=sProfits;
	document.getElementById('cashPDF').value=sCash;
	document.getElementById('cashFlowPDF').value=sCashFlow;

	document.getElementById('SCPDF').value=Math.round(SC);
	document.getElementById('MS1PDF').value=Math.round(MS1);
	document.getElementById('COGSPDF').value=Math.round(100*COGS);
	document.getElementById('MSGPDF').value=Math.round(100*MSG);
	document.getElementById('CPPDF').value=Math.round(100*CP);
	document.getElementById('CDPDF').value=Math.round(CD);
	document.getElementById('COSTS1PDF').value=Math.round(COSTS1);
	document.getElementById('COSTS2PDF').value=Math.round(COSTS2);
	document.getElementById('COSTS3PDF').value=Math.round(COSTS3);
	document.getElementById('IEPDF').value=Math.round(IE);
	document.getElementById('MIKPDF').value=Math.round(MIK);
	document.getElementById('SRCVPDF').value=Math.round(SRCV);
	document.getElementById('SPAYPDF').value=Math.round(SPAY);
	document.getElementById('PDPDF').value=Math.round(PD);

	document.getElementById('namePDF').value=document.getElementById('name').value;
	document.getElementById('sba_namePDF').value=document.getElementById('sba_name').value;
	document.getElementById('sba_phonePDF').value=document.getElementById('sba_phone').value;
	document.getElementById('datePDF').value=document.getElementById('date').value;

}



// DEFAULT INPUT data
SC = 10000; //"Starting Cash:","Starting Cash: Money you can spend, not necessarily coins and bills.  It\'s the money available in your business checking account balance when you start.    "
MS1 = 15000; //"First Month\'s Sales:", "First Month\'s Sales: Enter the amount of sales you will have in your first month."
COGS = 0.3;   //"Cost of Goods Sold (% of Sales):\n", "Cost of Goods Sold (COGS): This is the direct cost of purchasing what you sell. For purpose of illustration, in this model, COGS is directly related to inventory and dictates inventory purchase.  However, you can still use this calculator for service businesses that don\'t carry inventory. Just set your inventory to zero and use this for direct cost of sales, such as labor and fuel. "
MSG = 0.005; //"Monthly Sales Growth:\n", "Monthly Sales Growth: Use the slider to change the estimated Monthly sales growth. Notice what higher growth does to cash flow under most assumptions sets."
CD = 60;    //"Collection Days:", "Collection Days: This is the average number of days it takes for you to collect on an invoice. Typically, businesses invoice Net 30, but in some industries it may take as long as 120 days to collect payment on an invoice."
CP = 0.75;  // "Sales on Credit:", "Sales on Credit: This is the percentage of your customers that you sell to on credit terms. Sales on credit are not sales related to credit cards, despite the name, but sales made on terms such as net 30. Most business-to-business sales are made on credit, and the most common terms are net 30. This important estimator helps determine your cash flow."
MIK = 1;     // "Months of Inventory Kept on Hand:", "Months of Inventory Kept on Hand: This is the number of months of inventory that you have in stock at all times. Keeping large amounts of inventory on hand will adversely effect your cash flow."
COSTS1 = 1000;  //"Personnel costs"
COSTS2 = 1000;  //"Fixed costs"
COSTS3 = 1000;  //"Other costs"
IE = 5000;  // "Initial Inventory Balance:", "Initial Inventory Expense: This is the amount of inventory that you will purchase before you start selling anything."
SRCV = 0;     //"Starting Receivables:", "Starting Receivables: When you start your business, this is the amount of money currently owed to you. This money will be collected based on your Collection Days entry."
SPAY = 3000; //"Starting Payables:", "Starting Payables: When you start your business, this is the amount of money that you currently owe to vendors, contractors, and suppliers."
PD = 30;   //"Payment Days:", "Payment days measures how long a business takes to pay its bills.  30 days is normal, but remember that the average is affected by bills you pay immediately, such as payroll.  Watch how cash flow changes as you increase the waiting period"

getInputValue("SC");
getInputValue("MS1");
getInputValue("IE");
getInputValue("SRCV");
getInputValue("SPAY");
getInputValue("COSTS1");
getInputValue("COSTS2");
getInputValue("COSTS3");

//Further parameters:
V11 = 0.01;
V64='undefined';
V645 = 1000;
V648 = 0;
V733 = 0;

sliderCOGS.show();
sliderMSG.show();
sliderCD.show();
sliderCP.show();
sliderMIK.show();
sliderPD.show();


setInputValue("SC");
setInputValue("MS1");
setSliderValue100("COGS");
setSliderValue100("MSG", true);
setSliderValue("CD");
setSliderValue100("CP");
setSliderValue("MIK");
setInputValue("IE");
setInputValue("SRCV");
setInputValue("SPAY");
setSliderValue("PD");


calculator_compute();
adjust_tips();
