function sn(num) {

   num=num.toString();


   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;

   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }

   for(i = 0; i <= len; i++) {

      b = num.substring(i,i+1);

      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == 

"9" || b == ".") {
         rnum = rnum + "" + b;

      }

   }

   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }

   rnum = Number(rnum);

   return rnum;

}



function fn(num, places, comma) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    var myDecFact = 1;
    var myPlaces = 0;
    var myZeros = "";
    while(myPlaces < places) {
       myDecFact = myDecFact * 10;
       myPlaces = eval(myPlaces) + eval(1);
       myZeros = myZeros + "0";
    }
    
	onum=Math.round(num*myDecFact)/myDecFact;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal=myZeros;
	} else{
		decimal=Math.round((onum-integer)* myDecFact)
	}
	decimal=decimal.toString();
	if (decimal.length<places) {
        fillZeroes = places - decimal.length;
	   for (z=0;z<fillZeroes;z++) {
        decimal="0"+decimal;
        }
     }

   if(places > 0) {
      decimal = "." + decimal;
   }

   if(comma == 1) {
	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}


	finNum=tmpinteger+""+decimal;
   } else {
      finNum=integer+""+decimal;
   }

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}



function compute(form) {

   var v_jobs = sn(document.calc.jobs.value);
   var v_printcost = sn(document.calc.printcost.value);
   var v_hours = sn(document.calc.hours.value);
   var v_rate = sn(document.calc.rate.value);
   var v_storage = sn(document.calc.storage.value);

   if(v_jobs == 0) {
      alert("Please enter an amount.");
      document.calc.jobs.focus();
   } else
	if(v_printcost == 0) {
      alert("Please enter a cost.");
      document.calc.printcost.focus();
   } else
   if(v_hours == 0) {
      alert("Please enter an amount.");
      document.calc.hours.focus();
   } else
   if(v_rate == 0) {
      alert("Please enter a rate.");
      document.calc.rate.focus();
   } else
   if(v_storage == 0) {
      alert("Please enter the annual cost.");
      document.calc.storage.focus();
   } else {

      var v_savings = (v_jobs*v_printcost) + ((v_hours * v_rate * v_jobs) *.5) + v_storage;
      document.calc.savings.value = fn(v_savings,2,1);
   }

}

function cr(form) {

   document.calc.savings.value = "";

}
