var programcost, numberofpieces, costofpieces, responds, responsecost;
var responserate, conversion, buyersnumber, totalrevenue, averagespent;
var costofbuyer, returnoninvestment;
var respsround, respcostround, buyround, totrevround, costbuyround, roiround;

function calcroi(roi) {
  if ((document.roi.pieces.value == "") ||
      (document.roi.progcost.value == "") ||
	  (document.roi.resprate.value == "") ||
	  (document.roi.convrate.value == "") ||
	  (document.roi.avespend.value == "") ) {
     alert ("All input fields must have a value");
	 } else {
	 numberofpieces = document.roi.pieces.value;
	 programcost = document.roi.progcost.value;
     responserate = document.roi.resprate.value;
	 conversion = document.roi.convrate.value;
	 averagespent = document.roi.avespend.value;
	 } 

costofpieces = programcost/numberofpieces;
costpieceround = (parseInt(costofpieces*100))/100;
responds = numberofpieces * (responserate/100);
respsround = Math.round(responds);
responsecost = programcost/respsround;
respcostround = (parseInt(responsecost*100))/100;
buyersnumber = (respsround * (conversion/100));
buyround = Math.round(buyersnumber);
totalrevenue = averagespent * buyround;
totrevround = (parseInt(totalrevenue*100))/100;
costofbuyer = programcost/buyround;
costbuyround = (parseInt(costofbuyer))/100;
returnoninvestment = ((totrevround - programcost)/programcost)*100;
roiround = (parseInt(returnoninvestment*100))/100;
document.roi.piececost.value = costpieceround;
document.roi.responders.value = respsround;
document.roi.respcost.value = respcostround;
document.roi.buyers.value = buyround;
document.roi.totrev.value = totrevround;
document.roi.costbuyer.value = costbuyround;
document.roi.roi.value = roiround;
}

function reset(roi) {
document.roi.pieces.value = "";
document.roi.piececost.value = "";
document.roi.resprate.value = "";
document.roi.convrate.value = "";
document.roi.avespend.value = "";
document.roi.progcost.value = "";
document.roi.responders.value = "";
document.roi.respcost.value = "";
document.roi.buyers.value = "";
document.roi.totrev.value = "";
document.roi.costbuyer.value = "";
document.roi.roi.value = "";
}

