// JavaScript Document
//This file contains the functions for the Shelf Plus Calculator
// created 19, 2008 tcn
var sections = new Array('partOne', 'partTwo');
function init() {
    show('partOne');
    document.forms['roiform'].elements['ceilingheight'].focus();
}
function show(section) {
    for (var i = 0; i < sections.length; i++) {
        if (sections[i] == section) {
            document.getElementById(sections[i]).style.display = 'block';
        } else {
            document.getElementById(sections[i]).style.display = 'none';
        }
    }
	calculate();
}
function showend(section) {

    for (var i = 0; i < sections.length; i++) {
        if (sections[i] == section) {
            document.getElementById(sections[i]).style.display = 'block';
        } else {
            document.getElementById(sections[i]).style.display = 'none';
        }
		
    }
calculate();	

}
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false;}
else {return true}
}
}
/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/
function calculatept1()
{
//first validate partOne
if (validate_required(document.roiform.ceilingheight,"Please enter your ceiling height")==false)
  {document.roiform.ceilingheight.focus();return false;}
if (validate_required(document.roiform.numbershelves,"Please enter the number of existing shelves")==false)
  {document.roiform.numbershelves.focus();return false;}
if (validate_required(document.roiform.shelfwidth,"Please enter your shelf width")==false)
  {document.roiform.shelfwidth.focus();return false;}
if (validate_required(document.roiform.shelfdepth,"Please enter your shelf depth")==false)
  {document.roiform.shelfdepth.focus();return false;}
  
//Equivalent shelving - first use the width/depth of existing shelving to get equivalent number of 36x18 shelf sections
equivshelves = ((document.roiform.shelfwidth.value * document.roiform.shelfdepth.value)/(36*18)) * document.roiform.numbershelves.value
// use equivshelves to get vertical units needed (round up to nearest integer
unitsneeded = Math.ceil(equivshelves / (document.roiform.ceilingheight.value * 3.5))
mysqftsaved = equivshelves * (9.37)
//populate the fields
document.roiform.sqftsaved.value = mysqftsaved.toFixed(0)
document.roiform.units.value = unitsneeded
//then show part 2
show('partTwo');
}


function addCommas(nStr)
 {
   nStr += '';
   x = nStr.split('.');
   x1 = x[0];
   x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
 }
