//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Tank Volume Calculator
//2009 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 7/2/2009
//Last Modified: 7/2/2009
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//*******************************************



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 = Number(myPlaces) + Number(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 calc_volume(form) {

   var v_length = sn(document.vol_calc.length.value);
   var v_width = sn(document.vol_calc.width.value);
   var v_height = sn(document.vol_calc.height.value);

   if(v_length == 0) {
      alert("Please enter the tank length in inches.");
      document.vol_calc.length.focus();
   } else
   if(v_width == 0) {
      alert("Please enter the tank width in inches.");
      document.vol_calc.width.focus();
   } else
   if(v_height == 0) {
      alert("Please enter the tank height in inches.");
      document.vol_calc.height.focus();
   } else {

      var v_volume = v_length * v_width * v_height / 231;
      document.vol_calc.volume.value = fn(v_volume,1,1);

      document.rock_calc.gallons.value = fn(v_volume,1,1);
      document.change_calc.gallons.value = fn(v_volume,1,1);
      document.fish_calc.gallons.value = fn(v_volume,1,1);

   }

 
}


function clear_vol_results(form) {

   document.vol_calc.volume.value = "";

}

function calc_change(form) {

   var v_gallons = sn(document.change_calc.gallons.value);

   if(v_gallons == 0) {
      alert("Please enter tank volume in gallons.");
      document.change_calc.gallons.focus();

   } else {

      var v_change = v_gallons * .15;
      document.change_calc.change.value = fn(v_change,1,1);

   }

 
}


function clear_change_results(form) {

   document.change_calc.change.value = "";

}

function calc_rock(form) {

   var v_gallons = sn(document.rock_calc.gallons.value);

   if(v_gallons == 0) {
      alert("Please enter tank volume in gallons.");
      document.rock_calc.gallons.focus();

   } else {

      var v_lbs_1 = v_gallons * 3;
      document.rock_calc.lbs_1.value = fn(v_lbs_1,1,1);

      var v_lbs_2 = v_gallons * 2;
      document.rock_calc.lbs_2.value = fn(v_lbs_2,1,1);

   }

 
}


function clear_rock_results(form) {

   document.rock_calc.lbs_1.value = "";
   document.rock_calc.lbs_2.value = "";

}


function calc_fish(form) {

   var v_gallons = sn(document.fish_calc.gallons.value);

   if(v_gallons == 0) {
      alert("Please enter tank volume in gallons.");
      document.fish_calc.gallons.focus();

   } else {

      var v_fish = v_gallons / 3;
      document.fish_calc.fish.value = fn(v_fish,0,1);

   }

 
}


function clear_rock_results(form) {

   document.fish_calc.fish.value = "";

}


function calc_sand(form) {

   var v_length = sn(document.sand_calc.length.value);
   var v_width = sn(document.sand_calc.width.value);
   var v_depth = sn(document.sand_calc.depth.value);
   var v_weight = sn(document.sand_calc.weight.value);

   if(v_length == 0) {
      alert("Please enter the tank length in inches.");
      document.sand_calc.length.focus();
   } else
   if(v_width == 0) {
      alert("Please enter the tank width in inches.");
      document.sand_calc.width.focus();
   } else
   if(v_depth == 0) {
      alert("Please enter the desired depth of sand in inches.");
      document.sand_calc.depth.focus();
   } else
   if(v_weight == 0) {
      alert("Please enter the pounds per square foot of the sand you are using.");
      document.sand_calc.weight.focus();
   } else {

      var v_sand = ((v_length / 12) * (v_width / 12) * (v_depth / 12)) * v_weight;
      document.sand_calc.sand.value = fn(v_sand,0,1);


   }

 
}


function clear_sand_results(form) {

   document.sand_calc.sand.value = "";

}
