/* REMOVES COMMAS, DOLLAR SIGNS & PERCENT SIGNS FROM INPUTS */
function filterNum(str) {
re = /^\$|,|%/g;
// remove "$" and ","
return str.replace(re, "");
}

/* ADDS COMMAS INTO OUTPUTTED TOTALS */
function Comma(number) {
number = '' + number;
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0))
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
else
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
return (output);
}
else return number;
}



function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}


/* FORM VALIDATION - CHECKS TO SEE IF FIELDS ARE FILLED IN  */

function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.revIncForm.MonVis.value=="") {
themessage = themessage + " - Monthly Visitors";
}
if (document.revIncForm.ConvRate.value=="") {
themessage = themessage + " - Conversion Rate";
}
if (document.revIncForm.AvgPurch.value=="") {
themessage = themessage + " - Average Purchase";
}
if (document.revIncForm.ProfMarg.value=="") {
themessage = themessage + " - Profit Margin";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {

}
else {
alert(themessage);
return false;
   }
}

function verify2() {
var themessage = "You are required to complete the following fields: ";
if (document.CustServ.MonInq.value=="") {
themessage = themessage + " - Inquiries per month";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {

}
else {
alert(themessage);
return false;
   }
}

function verify3() {
var themessage = "You are required to complete the following fields: ";
if (document.DevCostForm.ProjManCost.value=="") {
themessage = themessage + " - Proj Mgmt Costs";
}
if (document.DevCostForm.DesProgCost.value=="") {
themessage = themessage + " - Design-Programming Costs";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {

}
else {
alert(themessage);
return false;
   }
}
	
/* CALCULATION FOR FIRST FORM */
	
		function CalculatePayments(visitors, conversions, purchases, profitMargin, increase)
		{
			var x = (visitors * conversions  *  purchases * profitMargin * increase);
			return Math.floor(x * 100) / 100
		}
		
		function ShowPayments()
		{
			var x = CalculatePayments(document.revIncForm.MonVis.value, document.revIncForm.ConvRate.value, document.revIncForm.AvgPurch.value / 100, document.revIncForm.ProfMarg.value / 100,document.revIncForm.ConvIncr.value / 100);
			if (isNaN(x))
				document.revIncForm.IncreaseResult.value = 'Invalid Entry';
			else
				x = addSeparatorsNF(x.toFixed(2), '.', '.', ',');
				document.revIncForm.IncreaseResult.value = x;
				
			var z = (parseInt(filterNum(document.revIncForm.IncreaseResult.value)) * 12);
			
			if (isNaN(z))
				document.revIncForm.YearIncResult.value = 'Invalid Entry';
			else
				z = addSeparatorsNF(z.toFixed(2), '.', '.', ',');
				document.revIncForm.YearIncResult.value = z;
		}
		
/* CALCULATION FOR SECOND FORM */
		
		function CalculateSavings(inquiries, cost, reduction)
		{
			var x = (inquiries * cost  *  reduction);
			return Math.floor(x * 100) / 100
		}
		
		function ShowSavings()
		{
			var x = CalculateSavings(document.CustServ.MonInq.value, document.CustServ.CostInq.value,  document.CustServ.RedInq.value / 100);
			if (isNaN(x))
				document.CustServ.SavingsResult.value = 'Invalid Entry';
			else
				x = addSeparatorsNF(x.toFixed(2), '.', '.', ',');
				document.CustServ.SavingsResult.value = x;
				
			
			var z = (parseInt(filterNum(document.CustServ.SavingsResult.value)) * 12);
			
			if (isNaN(z))
				document.CustServ.YearSavResult.value = 'Invalid Entry';
			else
				z = addSeparatorsNF(z.toFixed(2), '.', '.', ',');
				document.CustServ.YearSavResult.value = z;
		}

/* CALCULATION FOR THIRD FORM */

		function CalculateDevelopment(principal, down_payment, interest)
		{
			var x = (principal + down_payment + interest);
			return Math.floor(x * 100) / 100
		}
		
		function ShowDevelopment()
		{
			var x = CalculateDevelopment(parseInt(document.DevCostForm.IntChkCost.value), parseInt(document.DevCostForm.ProjManCost.value), parseInt(document.DevCostForm.DesProgCost.value));
			if (isNaN(x))
				document.DevCostForm.DevCostResult.value = 'Invalid Entry';
			else
				x = addSeparatorsNF(x.toFixed(2), '.', '.', ',');
				document.DevCostForm.DevCostResult.value = x;
				
			var y = ((parseInt(filterNum(document.revIncForm.YearIncResult.value)) - parseInt(filterNum(document.DevCostForm.DevCostResult.value))) / parseInt(filterNum(document.DevCostForm.DevCostResult.value)) * 100);


			
			if (isNaN(y))
				document.DevCostForm.ROIresult.value = 'Invalid Entry';
			else
				document.DevCostForm.ROIresult.value  = y.toFixed(1);
				
		}
		
/* CALCULATION FOR FOURTH FORM */

		function CalculateDevelopment2(principal, down_payment, interest)
		{
			var x = (principal + down_payment + interest);
			return Math.floor(x * 100) / 100
		}
		
		function ShowDevelopment2()
		{
			var x = CalculateDevelopment2(parseInt(document.DevCostForm.IntChkCost.value), parseInt(document.DevCostForm.ProjManCost.value), parseInt(document.DevCostForm.DesProgCost.value));
			if (isNaN(x))
				document.DevCostForm.DevCostResult.value = 'Invalid Entry';
			else
				x = addSeparatorsNF(x.toFixed(2), '.', '.', ',');
				document.DevCostForm.DevCostResult.value = x;
				
			var y = ((parseInt(filterNum(document.CustServ.YearSavResult.value)) - parseInt(filterNum(document.DevCostForm.DevCostResult.value))) / parseInt(filterNum(document.DevCostForm.DevCostResult.value)) * 100);
			
			if (isNaN(y))
				document.DevCostForm.ROIresult.value = 'Invalid Entry';
			else
				document.DevCostForm.ROIresult.value  = y.toFixed(1);
		}