/*
Author: Sloan Valve Company
Type: Simple-JavaScript
Copyright: ©2009
*/
var none_spec = "/images/watersavingscalculator/file_icon.jpg"; 
var het_manual_spec = "/Specifications/WETS_20001001_128.pdf";
var het_solis_spec = "/Specifications/WETS_20201201_128_SOLIS.pdf";
var het_battery_spec = "/Specifications/WEUS_10001001_013.pdf";
var het_ac_spec = "/Specifications/WETS_20001301_128_ES_S.pdf"
var heu_waterfree_spec = "/Specifications/Waterfree_WES-1000_Urinal.pdf";
var ac_25_spec = "/Specifications/ac11.pdf";
var ac_20_spec = "/Specifications/AC-11_HE.pdf";
var heu_manual_spec = "/Specifications/WEUS_10001001_013.pdf";
var heu_battery_spec = "/Specifications/WEUS_10021401_025_G2.pdf";
var heu_hardwired_spec = "/Specifications/WEUS_10021302_025_HWS.pdf";
var heu_solis_spec = "/Specifications/WETS_20201201_128_SOLIS.pdf";
var eaf275_spec = "/Specifications/Optima_Solis_EAF-275.pdf";
var eaf150_spec = "/Specifications/Optima_iq_EAF-150.pdf";
var ebf85_spec = "/Specifications/Optima_Plus_EBF-85.pdf";
var ebf650_spec = "/Specifications/Optima_Plus_EBF-650.pdf";
var ebf750_spec = "/Specifications/Optima_Plus_EBF-750.pdf";
var etf700_spec = "/Specifications/Optima_ETF-700.pdf";
var etf600_spec = "/Specifications/Optima_ETF-600.pdf";
var step3 = new Array("step3_residential","step3_commercial","step3_hospitality");
var step4 = new Array("step4_residential","step4_commercial","step4_hospitality");
var step5 = new Array("step5_residential","step5_commercial","step5_hospitality");
var results = new Array("results_residential","results_commercial","results_hospitality");
var projectType = "";
var buildingType = "";
var toilet_usage = 0;
var urinal_usage = 0;
var shower_usage = 0;
var faucet_usage = 0;
var people = 0;
var shower_time = 0;
var faucet_freq = 0;
var faucet_time = 0;
var toilet_freq = 0;
var male_employees = 0;
var female_employees = 0;
var days_open = 0;
var rooms = 0;
var occupancy_rate = 0;
var guest_type = 0;
var SloanToilets = "";
var SloanUrinals = "";
var SloanShowerhead = "";
var SloanFaucet = "";
var water_cost = 0;
var new_urinal_usage = 0;
var new_toilet_usage = 0;
var new_shower_usage = 0;
var new_faucet_usage = 0;
var toilet_cost = 0;
var urinal_cost = 0;
var faucet_cost = 0;
var shower_cost = 0;
var new_toilet_cost = 0;
var new_urinal_cost = 0;
var new_faucet_cost = 0;
var new_shower_cost = 0;
var new_total_cost = 0;
var new_total_savings = 0;
var total_cost = 0;
var zero_usage_urinal = false;
// BACK LINKS ____________________________________
function gotoStep1(){
    hideDiv("step2");
    prepareSeptNav(2,'Replace Existing', 'New Construction');
    showDiv("step1");
}
function gotoStep2(){
    hideDivs(step3);
    prepareSeptNav(0,'Replace Existing', 'New Construction');
    showDiv("step2");
}
function gotoStep3(){
    hideDivs(step4);
    if(projectType == "Replace Existing"){
        prepareStep3();
    }else{
        // New Construction does not see step 3
        hideDivs(step4);
        prepareStep2("New Construction");
    }
}
function gotoStep4(){
    hideDivs(step5);
    prepareStep4();
}
function gotoStep5(){
    hideDiv("step6");
    prepareStep5();
}
function gotoStep6(){
    hideDivs(results);
    prepareStep6();
}
function startOver(){
    //console.log("startOver()...");
    prepareSeptNav(2,'Replace Existing', 'New Construction');
    showDiv("step1");
    hideDivs(results);
}
// STEPS ____________________________________
// AL Block  
function checkIfBuildingTypeSelected(){
  var checkedFlag = false;
  var buildingType = document.getElementsByName("b_type");
  var count = 0;
  while(count < buildingType.length){
     if(buildingType[count].checked){
         checkedFlag = true;
         break;
     }
     count++;
  }
  return checkedFlag; 
}
function prepareSeptNav(nextStepNum, caption1, caption2){
   var back = findObj("navBack");
   clearElement(back);
   
   var forward = findObj("navForward");
   clearElement(forward);    
   
   var content; 
   if( nextStepNum != 2 && nextStepNum !=7 && nextStepNum !=8 && nextStepNum !=0 ){
      content = document.createElement('a');
       content.setAttribute('href', 'javascript:gotoStep'+(nextStepNum-2)+'();');
       content.appendChild(document.createTextNode(caption1));
       back.appendChild(content);
       
       content = document.createElement('a');
       content.setAttribute('href', 'javascript:prepareStep'+nextStepNum+'();');
       content.appendChild(document.createTextNode(caption2));
       forward.appendChild(content);
   }else if( nextStepNum == 7 ){
      content = document.createElement('a');
       content.setAttribute('href', 'javascript:gotoStep5();');
       content.appendChild(document.createTextNode(caption1));
       back.appendChild(content);
       
       content = document.createElement('a');
       content.setAttribute('href', 'javascript:calculate();');
       content.appendChild(document.createTextNode(caption2));
       forward.appendChild(content);   
   }else if ( nextStepNum == 2 || nextStepNum == 0 ){
      content = document.createElement('a');
       content.setAttribute('href', 'javascript:prepareStep2("Replace Existing");');
       content.appendChild(document.createTextNode(caption1));
       back.appendChild(content);
       
       content = document.createElement('a');
       content.setAttribute('href', 'javascript:prepareStep2("New Construction");');
       content.appendChild(document.createTextNode(caption2));
       forward.appendChild(content);   
   }else{
      content = document.createElement('a');
       content.setAttribute('href', 'javascript:gotoStep6();');
       content.appendChild(document.createTextNode(caption1));
       back.appendChild(content);
       
       content = document.createElement('a');
       content.setAttribute('href', 'javascript:startOver();');
       content.appendChild(document.createTextNode(caption2));
       forward.appendChild(content);
   }
}
function clearElement(element){
   while(element.hasChildNodes()){
     element.removeChild(element.lastChild);
   }
}
// End Block 
function prepareStep2(pType){ // Replace Existing OR New Construction
    //console.log("prepareStep2('%s')...",pType);
    prepareSeptNav(3,'Back', 'Continue');
    projectType = pType;
    hideDiv("step1");
    showDiv("step2");
}
function prepareStep3(){
    //console.log("prepareStep3()...");
    //AL - had to remove default selection to make waterdrop work, now checking if they selected anything 
    if(checkIfBuildingTypeSelected()){    
    prepareSeptNav(4,'Back', 'Continue');
    var tmpObj;
    hideDiv("step2");
    /* Dk modification: re-number Step labels so to user doesn't make it seem like
       a step is skipped, even though that is what we do in JS for New Construction */
    reNumberSteps(projectType);
    /* --end Dk modification */
    switch(projectType) {
        case "Replace Existing":
            tmpObj = findObj("b_type_residential");
            if(tmpObj.checked == true){
                showDiv("step3_residential");
                buildingType = "Residential";
            }
            tmpObj = findObj("b_type_commercial");
            if(tmpObj.checked == true){
                showDiv("step3_commercial");
                buildingType = "Commercial";
            }
            tmpObj = findObj("b_type_hospitality");
            if(tmpObj.checked == true){
                showDiv("step3_hospitality");
                buildingType = "Hospitality";
            }
            break;
        case "New Construction":
            tmpObj = findObj("b_type_residential");
            if(tmpObj.checked == true){
                buildingType = "Residential";
            }
            tmpObj = findObj("b_type_commercial");
            if(tmpObj.checked == true){
                buildingType = "Commercial";
            }
            tmpObj = findObj("b_type_hospitality");
            if(tmpObj.checked == true){
                buildingType = "Hospitality";
            }
            prepareStep4(buildingType);
            break;
    } 
     }else{
         alert("Please select the type of building are you calculating.")
     }
}
function prepareStep4(){
    //console.log("prepareStep4()...");
    prepareSeptNav(5,'Back', 'Continue');
    hideDivs(step3);
    switch(projectType){
        case "Replace Existing":
            switch(buildingType){
                case "Residential":
                    tmpObj = findObj("t_usage_3_residential");
                    toilet_usage = tmpObj.value;
                    tmpObj = findObj("s_usage_3_residential");
                    shower_usage = tmpObj.value;
                    tmpObj = findObj("f_usage_3_residential");
                    faucet_usage = tmpObj.value;
                    showDiv("step4_residential");
                    break;
                case "Commercial":
                    tmpObj = findObj("t_usage_3_commercial");
                    toilet_usage = tmpObj.value;
                    tmpObj = findObj("u_usage_3_commercial");
                    urinal_usage = tmpObj.value;
                    tmpObj = findObj("f_usage_3_commercial");
                    faucet_usage = tmpObj.value;
                    showDiv("step4_commercial");
                    break;
                case "Hospitality":
                    tmpObj = findObj("t_usage_3_hospitality");
                    toilet_usage = tmpObj.value;
                    tmpObj = findObj("s_usage_3_hospitality");
                    shower_usage = tmpObj.value;
                    tmpObj = findObj("f_usage_3_hospitality");
                    faucet_usage = tmpObj.value;
                    showDiv("step4_hospitality");
                    break;
            }
            break;
        case "New Construction":
            // Step 3 skipped for new construction, usage values are set automatically
            toilet_usage = 1.6;
            urinal_usage = 1;
            shower_usage = 2.5;
            faucet_usage = .5;
            switch(buildingType){
                case "Residential":
                    showDiv("step4_residential");
                    break;
                case "Commercial":
                    showDiv("step4_commercial");
                    break;
                case "Hospitality":
                    showDiv("step4_hospitality");
                    break;
            }
            break;
    }
}
function prepareStep5(){
    //console.log("prepareStep5()...\nbuildingType '%s'",buildingType);
    prepareSeptNav(6,'Back', 'Continue');
    hideDivs(step4);
    switch(buildingType){
        case "Residential":
            tmpObj = findObj("people");
            people = tmpObj.value;
            tmpObj = findObj("shower_time");
            shower_time = tmpObj.value;
            tmpObj = findObj("faucet_freq");
            faucet_freq = tmpObj.value;
            tmpObj = findObj("faucet_time");
            faucet_time = tmpObj.value;
            tmpObj = findObj("toilet_freq");
            toilet_freq = tmpObj.value;
            // Adjust display
            setupDisplay(findObj("SloanToilets_residential").value, 'step5_img_toilet_res');
            setupDisplay(findObj("SloanShowerhead_residential").value, 'step5_img_showerhead_res');
            setupDisplay(findObj("SloanFaucet_residential").value, 'step5_img_faucet_res');
            validate("SloanToilets_residential");
            showDiv("step5_residential");
            break;
        case "Commercial":
            tmpObj = findObj("male_employees");
            /* Dk modification: handle unset, empty string values, same as validation, force to 0 */
            //male_employees = tmpObj.value;
            male_employees = (tmpObj.value === "") ? 0 : tmpObj.value;
            tmpObj.value = male_employees;
            tmpObj = findObj("female_employees");
            //female_employees = tmpObj.value;
            female_employees = (tmpObj.value === "") ? 0 : tmpObj.value;
            tmpObj.value = female_employees;
            tmpObj.value = female_employees;
            /* --end Dk modification */
            tmpObj = findObj("days_open");
            days_open = tmpObj.value;
            setupDisplay(findObj("SloanToilets_residential").value, 'step5_img_toilet_res');
            validate("SloanToilets_residential");
            showDiv("step5_commercial");
            break;
        case "Hospitality":
            /* Dk modification: handle unset, empty string values, same as validation, force to 0 */
            tmpObj = findObj("rooms");
            //rooms = tmpObj.value;
            rooms = (tmpObj.value === "") ? 0 : tmpObj.value;
            tmpObj.value = rooms;
            tmpObj = findObj("occupancy_rate");
            //occupancy_rate = tmpObj.value;
            occupancy_rate = (tmpObj.value === "") ? 0 : tmpObj.value;
            tmpObj.value = occupancy_rate;
            /* --end Dk modification */
            tmpObj = findObj("guest_type");
            guest_type = tmpObj.value;
            showDiv("step5_hospitality");
            break;
    }
    /* Dk modification: do display setup and validation now to catch form
       states as retained by the web client after page re-load */
    initStep5Form();
}
/* Dk modification: setup display for form states and perform validation
   after page has reloaded but web client has retained user selections */
function initStep5Form() {
    //console.log("initStep5Form: \nbuildingType: '%s'",buildingType);
    var formEls = Array();
    /* make context-sensitive assigns */
    switch(buildingType){
        case "Residential":
            formEls.push({ selectEl: "SloanToilets_residential", imgId: "step5_img_toilet_res" });
            formEls.push({ selectEl: "SloanShowerhead_residential", imgId: "step5_img_showerhead_res" });
            formEls.push({ selectEl: "SloanFaucet_residential", imgId: "step5_img_faucet_res" });
            break;
        case "Commercial":
            formEls.push({ selectEl: "SloanToilets_commercial", imgId: "step5_img_toilet_comm" });
            formEls.push({ selectEl: "SloanUrinals_commercial", imgId: "step5_img_urinal_comm" });
            formEls.push({ selectEl: "SloanFaucet_commercial", imgId: "step5_img_faucet_comm" });
            break;
        case "Hospitality":
            formEls.push({ selectEl: "SloanToilets_hospitality", imgId: "step5_img_toilet_hosp" });
            formEls.push({ selectEl: "SloanShowerhead_hospitality", imgId: "step5_img_showerhead_hosp" });
            formEls.push({ selectEl: "SloanFaucet_hospitality", imgId: "step5_img_faucet_hosp" });
            break;
    }
    /* do the work, call util functions setupDisplay() and validate() */
    for (i=0, j=formEls.length; i<j; i++) {
        var tmpObj = findObj(formEls[i].selectEl);
        setupDisplay(tmpObj.value, formEls[i].imgId);
        validate(formEls[i].selectEl);
    }
}
/* --end Dk modifications */
function prepareStep6(){
    //console.log("prepareStep6()...");
    prepareSeptNav(7,'Back', 'Calculate');
    hideDivs(step5);
    switch(buildingType){
        case "Residential":
            tmpObj = findObj("SloanToilets_residential");
            SloanToilets = tmpObj.value;
            tmpObj = findObj("SloanShowerhead_residential");
            SloanShowerhead = tmpObj.value;
            tmpObj = findObj("SloanFaucet_residential");
            SloanFaucet = tmpObj.value;
               break;
        case "Commercial":
            tmpObj = findObj("SloanToilets_commercial");
            SloanToilets = tmpObj.value;
            tmpObj = findObj("SloanUrinals_commercial");
            SloanUrinals = tmpObj.value;
            tmpObj = findObj("SloanFaucet_commercial");
            SloanFaucet = tmpObj.value;
                break;
        case "Hospitality":
            tmpObj = findObj("SloanToilets_hospitality");
            SloanToilets = tmpObj.value;
            tmpObj = findObj("SloanShowerhead_hospitality");
            SloanShowerhead = tmpObj.value;
            tmpObj = findObj("SloanFaucet_hospitality");
            SloanFaucet = tmpObj.value;
               break;
    }
    showDiv("step6");
}
// CALCULATIONS __________________________________
function calculate(){
    //console.log("calculate()...");
    /* Dk modification: declare object */
    prepareSeptNav(8,'Back', 'Start Over');
    var SavingsPotential = { "toilet": { "savings": 0.00, "optimal_cost": 0.00 },
                             "urinal": { "savings": 0.00, "optimal_cost": 0.00 },
                             "faucet": { "savings": 0.00, "optimal_cost": 0.00 },
                             "shower": { "savings": 0.00, "optimal_cost": 0.00 }
                           }
    /* --end Dk modification */
    hideDiv("step6");
    tmpObj = findObj("water_cost");
    water_cost = tmpObj.value;
    tmpObj = findObj("toilet_freq");
    toilet_freq = tmpObj.value;
/*
console.log("calculate() with GLOBAL contexts as:\n"+
        "buildingType '%s'\n"+
        "toilet_usage '%s'\n"+
        "toilet_freq '%s'\n"+
        "faucet_usage '%s'\n"+
        "faucet_freq '%s'\n"+
        "shower_usage '%s'\n"+
        "shower_time '%s'\n"+
        "water_cost '%s'\n"+
        "",buildingType,toilet_usage,toilet_freq,faucet_usage,faucet_freq,shower_usage,shower_time,water_cost);
*/
  
        /* Dk modification: replace prev hardcoded new_toilet_usage with val based on
           form selection, so as to allow no Sloan toilet to be chosen, where new_toilet_usage will be 0, instead of 1.28 */
        //new_toilet_usage = 1.28;
        /* --end Dk modification */
    /* Dk modification: calc optimal efficiency values for comparison
       to user choices */
    optimal_toilet_usage = getToiletValue("optimal");
    optimal_urinal_usage = getUrinalValue("optimal");
    optimal_shower_usage = getShowerValue("optimal");
    optimal_faucet_usage = getFaucetValue("optimal");
    //console.log("have Optimal Usage values as:\noptimal_toilet_usage:\t'%s'\noptimal_urinal_usage:\t'%s'\noptimal_shower_usage:\t'%s'\noptimal_faucet_usage:\t'%s'",optimal_toilet_usage,optimal_urinal_usage,optimal_shower_usage,optimal_faucet_usage);
    /* --end Dk modification */
    switch(buildingType){
    case "Residential":
        /* Dk modification: replace prev hardcoded new_toilet_usage with val based on
           form selection, so as to allow no Sloan toilet to be chosen, where new_toilet_usage will be 0, instead of 1.28 */
        new_toilet_usage = getToiletValue("SloanToilets_residential");
        /* --end Dk modification */
        new_shower_usage = getShowerValue("SloanShowerhead_residential"); //($SloanShowerhead == 'Act-O-Matic')?2:0;
        new_faucet_usage = getFaucetValue("SloanFaucet_residential"); //($SloanFaucet == 'SolisFaucet')?0.5:0;
    //console.log("calculate() with Residential contexts as:toilet_freq '%s'\nnew_toilet_usage '%s'\nnew_shower_usage '%s'\nnew_faucet_usage '%s'",toilet_freq,new_toilet_usage,new_shower_usage,new_faucet_usage);
        /* Dk modification: override new_ values with old_ if user has
           not selected Sloan equivalents as new products */
        handleUnselectedProducts();
        /* --end Dk modification */
        var tmpObj;
         // Toilet Usage:
         toilet_cost = formatNumber(pf(people) * pf(toilet_usage) * pf(toilet_freq) * pf(water_cost) * 0.365);
    //console.log("calculate().... toilet_cost '%s'",toilet_cost);
         tmpObj = findObj("residential_t_cost");
         tmpObj.innerHTML = "$" + addCommas(toilet_cost);
         // Faucets Usage:
         faucet_cost = formatNumber(pf(people) * pf(faucet_usage) * pf(faucet_freq) * pf(faucet_time) * pf(water_cost) * 0.365);
    //console.log("calculate().... faucet_cost '%s'",faucet_cost);
        tmpObj = findObj("residential_f_cost");
        tmpObj.innerHTML = "$" + addCommas(faucet_cost);
        // Shower Usage:
        shower_cost = formatNumber(pf(people) * pf(shower_usage) * pf(shower_time) * pf(water_cost) * 0.365);
    //console.log("calculate().... shower_cost '%s'",shower_cost);
        tmpObj = findObj("residential_s_cost");
        tmpObj.innerHTML = "$" + addCommas(shower_cost);
        /* Dk modification: also calculate optimal cost based on user profile */
        /* Optimal Cost */
        SavingsPotential.toilet["optimal_cost"] = formatNumber(pf(people) * pf(optimal_toilet_usage) * pf(toilet_freq) * pf(water_cost) * 0.365);
        SavingsPotential.shower["optimal_cost"] = formatNumber(pf(people) * pf(optimal_shower_usage) * pf(shower_time) * pf(water_cost) * 0.365);
         SavingsPotential.faucet["optimal_cost"] = formatNumber(pf(people) * pf(optimal_faucet_usage) * pf(faucet_freq) * pf(faucet_time) * pf(water_cost) * 0.365);
    //console.log("have Optimal Cost values as: %o",SavingsPotential);
        /* --end Dk modification */
        // Estimated Energy Policy Act Standard Cost:
        total_cost = formatNumber(pf(toilet_cost) + pf(faucet_cost) + pf(shower_cost));
    //console.log("calculate().... total_cost '%s'",total_cost);
        tmpObj = findObj("residential_total_cost");
        tmpObj.innerHTML = "$" + addCommas(total_cost);
    //console.log("have Optimal Cost values as:\noptimal_toilet_usage:\t'%s'\noptimal_urinal_usage:\t'%s'\noptimal_shower_usage:\t'%s'\noptimal_faucet_usage:\t'%s'",optimal_toilet_usage,optimal_urinal_usage,optimal_shower_usage,optimal_faucet_usage);
        // New Toilet Usage:
        new_toilet_cost = formatNumber(pf(people) * pf(new_toilet_usage) * pf(toilet_freq) * pf(water_cost) * 0.365);
    //console.log("calculate().... new_toilet_cost '%s'",new_toilet_cost);
        tmpObj = findObj("residential_new_t_cost");
        tmpObj.innerHTML = "$" + addCommas(new_toilet_cost);
        // New Faucets Usage:
        new_faucet_cost = formatNumber(pf(people) * pf(new_faucet_usage) * pf(faucet_freq) * pf(faucet_time) * pf(water_cost) * 0.365);
    //console.log("calculate().... new_faucet_cost '%s'",new_faucet_cost);
        tmpObj = findObj("residential_new_f_cost");
        tmpObj.innerHTML = "$" + addCommas(new_faucet_cost);
        // New Shower Usage:
        new_shower_cost = formatNumber(pf(people) * pf(new_shower_usage) * pf(shower_time) * pf(water_cost) * 0.365);
    //console.log("calculate().... new_shower_cost '%s'",new_shower_cost);
        tmpObj = findObj("residential_new_s_cost");
        tmpObj.innerHTML = "$" + addCommas(new_shower_cost);
        // Estimated Revised Cost:
        new_total_cost = formatNumber(pf(new_toilet_cost) + pf(new_faucet_cost) + pf(new_shower_cost));
    //console.log("calculate().... new_total_cost '%s'",new_total_cost);
        tmpObj = findObj("residential_new_total_cost");
        tmpObj.innerHTML = "$" + addCommas(new_total_cost);
        // Total Savings with Sloan products
        new_total_savings = formatNumber(pf(total_cost) - pf(new_total_cost));
    //console.log("calculate().... new_total_savings '%s'",new_total_savings);
        tmpObj = findObj("residential_total_savings");
        tmpObj.innerHTML = "$" + addCommas(new_total_savings);
        /* Dk modification: calculate and display efficiency */
        SavingsPotential.toilet["savings"] = formatNumber(pf(new_toilet_cost) - pf(SavingsPotential.toilet["optimal_cost"]));
        SavingsPotential.faucet["savings"] = formatNumber(pf(new_faucet_cost) - pf(SavingsPotential.faucet["optimal_cost"]));
        SavingsPotential.shower["savings"] = formatNumber(pf(new_shower_cost) - pf(SavingsPotential.shower["optimal_cost"]));
        doOptimalEfficiency("residential",SavingsPotential);
        /* --end Dk modification */
         showDiv("results_residential");
        break;
        /*
        if ($b_type == 'Residential') { //Residential
        $t_cost = number_format($people * $t_usage * $t_freq * $w_cost * 0.365, 2, '.', '');
        $f_cost = number_format($people * $f_usage * $f_freq * $f_time * $w_cost * 0.365, 2, '.', '');
        $s_cost = number_format($people * $s_usage * $s_time * $w_cost * 0.365, 2, '.', '');
        $total_cost = number_format($t_cost + $f_cost + $s_cost, 2, '.', '');
        $new_t_cost = number_format($people * $new_t_usage * $t_freq * $w_cost * 0.365, 2, '.', '');
        $new_f_cost = number_format($people * $new_f_usage * $f_freq * $f_time * $w_cost * 0.365, 2, '.', '');
        $new_s_cost = number_format($people * $new_s_usage * $s_time * $w_cost * 0.365, 2, '.', '');
        $new_total_cost = number_format($new_t_cost + $new_f_cost + $new_s_cost, 2, '.', '');
        $total_savings = number_format($total_cost - $new_total_cost, 2, '.', '');
        include('results-a.htm');
    }
    */
    case "Commercial":
        var tmpObj;
         // Toilet Usage:
         toilet_cost = formatNumber(pf(toilet_usage) * (pf(male_employees) + 3 * pf(female_employees)) * pf(water_cost));
    //console.log("calculate().... toilet_cost '%s'",toilet_cost);
         tmpObj = findObj("commercial_t_cost");
         tmpObj.innerHTML = "$" + addCommas(toilet_cost);
         // Urinal Usage:
         urinal_cost = formatNumber(pf(urinal_usage) * pf(male_employees) * 2 * pf(water_cost));
    //console.log("calculate().... urinal_cost '%s'",urinal_cost);
        tmpObj = findObj("commercial_u_cost");
        tmpObj.innerHTML = "$" + addCommas(urinal_cost);
        // Faucet Usage:
        faucet_cost = formatNumber((parseInt(male_employees) + pf(female_employees)) * pf(faucet_usage) * 3 * 2 * pf(water_cost) * 0.365);
    //console.log("calculate().... faucet_cost '%s'",faucet_cost);
        tmpObj = findObj("commercial_f_cost");
        tmpObj.innerHTML = "$" + addCommas(faucet_cost);
        /* Dk modification: also calculate optimal cost based on user profile */
        /* Optimal Cost */
        SavingsPotential.toilet["optimal_cost"] = formatNumber(pf(optimal_toilet_usage) * (pf(male_employees) + 3 * pf(female_employees)) * pf(water_cost));
        SavingsPotential.urinal["optimal_cost"] = formatNumber(pf(optimal_urinal_usage) * pf(male_employees) * 2 * pf(water_cost));
        SavingsPotential.faucet["optimal_cost"] = formatNumber((parseInt(male_employees) + pf(female_employees)) * pf(optimal_faucet_usage) * 3 * 2 * pf(water_cost) * 0.365);
        //console.log("have Optimal Cost values as: %o",SavingsPotential);
        /* --end Dk modification */
        // Estimated Energy Policy Act Standard Cost:
        total_cost = formatNumber(pf(toilet_cost) + pf(urinal_cost) + pf(faucet_cost));
    //console.log("calculate().... total_cost '%s'",total_cost);
        tmpObj = findObj("commercial_total_cost");
        tmpObj.innerHTML = "$" + addCommas(total_cost);
        // New Usages:
        /* Dk modification: replace prev hardcoded new_toilet_usage with val based on
           form selection, so as to allow no Sloan toilet to be chosen, where new_toilet_usage will be 0, instead of 1.28 */
        new_toilet_usage = getToiletValue("SloanToilets_commercial");
        /* --end Dk modification */
        /* Dk modification: moved these assigns together so util handleUnselectedProducts()
           can be run at once before calculating New Cost for any product */
        new_urinal_usage = getUrinalValue("SloanUrinals_commercial");
        new_faucet_usage = getFaucetValue("SloanFaucet_commercial");
        handleUnselectedProducts();
        /* --end Dk modification */
    //console.log("calculate() with Commercial contexts as:\nnew_toilet_usage '%s'\nnew_urinal_usage '%s'\nnew_faucet_usage '%s'",new_toilet_usage,new_urinal_usage,new_faucet_usage);
        // New Toilet Cost:
        new_toilet_cost = formatNumber(pf(new_toilet_usage) * (pf(male_employees) + 3 * pf(female_employees)) * pf(water_cost));
    //console.log("calculate().... new_toilet_cost '%s'",new_toilet_cost);
        tmpObj = findObj("commercial_new_t_cost");
        tmpObj.innerHTML = "$" + addCommas(new_toilet_cost);
        // New Urinal Cost:
    //console.log("calculate().... new_urinal_usage '%s'",new_urinal_usage);
        /* Dk modification: moved this assign with others above so util handleUnselectedProducts()
           can be run at once before calculating New Cost for any product */
        //new_urinal_usage = getUrinalValue("SloanUrinals_commercial"); // for commerial
        /* --end Dk modification */
        new_urinal_cost = formatNumber(pf(new_urinal_usage) * pf(male_employees) * 2 * pf(water_cost));
    //console.log("calculate().... new_urinal_cost '%s'",new_urinal_cost);
        tmpObj = findObj("commercial_new_u_cost");
        tmpObj.innerHTML = "$" + addCommas(new_urinal_cost);
        // New Faucet Cost:
    //console.log("calculate().... new_faucet_usage '%s'",new_faucet_usage);
        /* Dk modification: moved this assign with others above so util handleUnselectedProducts()
           can be run at once before calculating New Cost for any product */
        //new_faucet_usage = getFaucetValue("SloanFaucet_commercial"); //($SloanFaucet == 'SolisFaucet')?0.5:0;
        /* --end Dk modification */
        new_faucet_cost = formatNumber((pf(male_employees) + pf(female_employees)) * pf(new_faucet_usage) * 3 * 2 * pf(water_cost) * 0.365);
    //console.log("calculate().... new_faucet_cost '%s'",new_faucet_cost);
        tmpObj = findObj("commercial_new_f_cost");
        tmpObj.innerHTML = "$" + addCommas(new_faucet_cost);
        // Estimated Revised Cost:
        new_total_cost = formatNumber(pf(new_toilet_cost) + pf(new_urinal_cost) + pf(new_faucet_cost));
    //console.log("calculate().... new_total_cost '%s'",new_total_cost);
        tmpObj = findObj("commercial_new_total_cost");
        tmpObj.innerHTML = "$" + addCommas(new_total_cost);
        // Total Savings with Sloan products
        new_total_savings = formatNumber(pf(total_cost) - pf(new_total_cost));
    //console.log("calculate().... new_total_savings '%s'",new_total_savings);
        tmpObj = findObj("commercial_total_savings");
        tmpObj.innerHTML = "$" + addCommas(new_total_savings);
        /* Dk modification: calculate and display efficiency */
        SavingsPotential.toilet["savings"] = formatNumber(pf(new_toilet_cost) - pf(SavingsPotential.toilet["optimal_cost"]));
        SavingsPotential.urinal["savings"] = formatNumber(pf(new_urinal_cost) - pf(SavingsPotential.urinal["optimal_cost"]));
        SavingsPotential.faucet["savings"] = formatNumber(pf(new_faucet_cost) - pf(SavingsPotential.faucet["optimal_cost"]));
        doOptimalEfficiency("commercial",SavingsPotential);
        /* --end Dk modification */
        showDiv("results_commercial");
        break;
/*         t_cost = formatNumber(t_usage * (m_employ + 3 * f_employ) * w_cost);
        u_cost = formatNumber(u_usage * m_employ * 2 * w_cost);
        f_cost = formatNumber((m_employ + f_employ) * f_usage * 3 * 2 * w_cost * 0.365);
        total_cost = formatNumber(t_cost + u_cost + f_cost);
        new_t_cost = formatNumber(new_t_usage * (m_employ + 3 * f_employ) * w_cost);
        new_u_cost = formatNumber(new_u_usage * m_employ * 2 * w_cost);
        new_f_cost = formatNumber((m_employ + f_employ) * new_f_usage * 3 * 2 * w_cost * 0.365);
        new_total_cost = formatNumber(new_t_cost + new_u_cost + new_f_cost);
        total_savings = formatNumber(total_cost - new_total_cost);
        showDiv("results_commercial");
        break;
*/
    case "Hospitality":
        var tmpObj;
        /* Dk modification: these vars are not retreived from form as currently they no
           questionnaire elements in the Hospitality case. Thus, hardcode: */
        // GLOBALS
        faucet_freq = 3;
        faucet_time = 2;
        shower_time = 10;
        // Local, new vars
        var toilet_freq = 5; //The average guest flushes 3 times per visit. Housekeeping also averages 2 flushes per day
        //var avg_flush_per_guest = 3;
        //var avg_flush_per_housekeep = 2;
        /* --end Dk modification */
         // Toilet Cost:
        //console.log("calculate()... doing toilet_cost based on:\nrooms '%s'\noccupancy_rate '%s'\nguest_type '%s'\ntoilet_usage '%s'\nwater_cost '%s'",rooms,occupancy_rate,guest_type,toilet_freq,toilet_usage,water_cost);
        toilet_cost = formatNumber(parseInt(rooms) * (parseInt(occupancy_rate)/100) * pf(guest_type) * pf(toilet_usage) * pf(toilet_freq) * pf(water_cost) * 0.365);
         //toilet_cost = formatNumber(parseInt(rooms) * parseInt(occupancy_rate)/100 * (pf(guest_type) * avg_flush_per_guest + avg_flush_per_housekeep) * pf(toilet_usage) * pf(water_cost) * 0.365);
    //console.log("calculate().... toilet_cost '%s'",toilet_cost);
         tmpObj = findObj("hospitality_t_cost");
         tmpObj.innerHTML = "$" + addCommas(toilet_cost);
         // Faucets Cost:
         //faucet_time = findObj("faucet_time").value;
         //faucet_freq = findObj("faucet_freq").value;
         //faucet_cost = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(faucet_usage) * pf(faucet_freq) * pf(faucet_time) * pf(water_cost) * 0.365);
         faucet_cost = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(faucet_usage) * pf(faucet_freq) * pf(faucet_time) * pf(water_cost) * 0.365);
    //console.log("calculate().... faucet_cost '%s'",faucet_cost);
        tmpObj = findObj("hospitality_f_cost");
        tmpObj.innerHTML = "$" + addCommas(faucet_cost);
        // Shower Cost:
        //shower_time = findObj("shower_time").value;
        //shower_cost = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(shower_usage) * pf(shower_time) * pf(water_cost) * 0.365);
        shower_cost = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(shower_usage) * pf(shower_time) * pf(water_cost) * 0.365);
    //console.log("calculate().... shower_cost '%s'",shower_cost);
        tmpObj = findObj("hospitality_s_cost");
        tmpObj.innerHTML = "$" + addCommas(shower_cost);
        // Estimated Energy Policy Act Standard Cost:
        total_cost = formatNumber(pf(toilet_cost) + pf(faucet_cost) + pf(shower_cost));
    //console.log("calculate().... total_cost '%s'",total_cost);
        tmpObj = findObj("hospitality_total_cost");
        tmpObj.innerHTML = "$" + addCommas(total_cost);
        // New Usages:
        /* Dk modification: replace prev hardcoded new_toilet_usage with val based on
           form selection, so as to allow no Sloan toilet to be chosen, where new_toilet_usage will be 0, instead of 1.28 */
        new_toilet_usage = getToiletValue("SloanToilets_hospitality");
        /* --end Dk modification */
        /* Dk modification: moved these assigns together so util handleUnselectedProducts()
           can be run at once before calculating New Cost for any product */
        new_shower_usage = getShowerValue("SloanShowerhead_hospitality");
        new_faucet_usage = getFaucetValue("SloanFaucet_hospitality");
    //console.log("calculate() with Hospitality contexts as:\nnew_toilet_usage '%s'\nnew_shower_usage '%s'\nnew_faucet_usage '%s'",new_toilet_usage,new_shower_usage,new_faucet_usage);
        handleUnselectedProducts();
        /* --end Dk modification */
        // New Toilet Cost:
        //console.log("calculate()... doing new_toilet_cost based on:\nrooms '%s'\noccupancy_rate '%s'\nguest_type '%s'\nnew_toilet_usage '%s'\ntoilet_freq '%s'\nwater_cost '%s'",rooms,occupancy_rate,guest_type,new_toilet_usage,toilet_freq,water_cost);
        new_toilet_cost = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(new_toilet_usage) * pf(toilet_freq) * pf(water_cost) * 0.365);
    //console.log("calculate().... new_toilet_cost '%s'",new_toilet_cost);
        tmpObj = findObj("hospitality_new_t_cost");
        tmpObj.innerHTML = "$" + addCommas(new_toilet_cost);
        // New Faucets Cost:
        /* Dk modification: moved this assign with others above so util handleUnselectedProducts()
           can be run at once before calculating New Cost for any product */
        //new_faucet_usage = getFaucetValue("SloanFaucet_hospitality"); //($SloanFaucet == 'SolisFaucet')?0.5:0;
        /* --end Dk modification */
    //console.log("calculate().... new_faucet_usage '%s'",new_faucet_usage);
        new_faucet_cost = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(new_faucet_usage) * pf(faucet_freq) * pf(faucet_time) * pf(water_cost) * 0.365);
    //console.log("calculate().... new_faucet_cost '%s'",new_faucet_cost);
        tmpObj = findObj("hospitality_new_f_cost");
        tmpObj.innerHTML = "$" + addCommas(new_faucet_cost);
        // New Shower Usage:
        /* Dk modification: moved this assign with others above so util handleUnselectedProducts()
           can be run at once before calculating New Cost for any product */
        //new_shower_usage = getShowerValue("SloanShowerhead_hospitality"); //($SloanShowerhead == 'Act-O-Matic')?2:0;
        /* --end Dk modification */
    //console.log("calculate().... new_shower_usage '%s'",new_shower_usage);
        new_shower_cost = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(new_shower_usage) * pf(shower_time) * pf(water_cost) * 0.365);
        /* Dk modification: also calculate optimal cost based on user profile */
        /* Optimal Cost */
        SavingsPotential.toilet["optimal_cost"] = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(toilet_freq) * pf(optimal_toilet_usage) * pf(water_cost) * 0.365);
        SavingsPotential.faucet["optimal_cost"] = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(optimal_faucet_usage) * pf(faucet_freq) * pf(faucet_time) * pf(water_cost) * 0.365);
        SavingsPotential.shower["optimal_cost"] = formatNumber(parseInt(rooms) *  parseInt(occupancy_rate)/100 * pf(guest_type) * pf(optimal_shower_usage) * pf(shower_time) * pf(water_cost) * 0.365);
        //console.log("have Optimal Cost values as: %o",SavingsPotential);
        /* --end Dk modification */
    //console.log("calculate().... new_shower_cost '%s'",new_shower_cost);
        tmpObj = findObj("hospitality_new_s_cost");
        tmpObj.innerHTML = "$" + addCommas(new_shower_cost);
        // Estimated Revised Cost:
        new_total_cost = formatNumber(pf(new_toilet_cost) + pf(new_faucet_cost) + pf(new_shower_cost));
    //console.log("calculate().... new_total_cost '%s'",new_total_cost);
        tmpObj = findObj("hospitality_new_total_cost");
        tmpObj.innerHTML = "$" + addCommas(new_total_cost);
        // Total Savings with Sloan products
        tmpObj = findObj("hospitality_total_savings");
        new_total_savings = formatNumber(pf(total_cost) - pf(new_total_cost));
        tmpObj.innerHTML = "$" + addCommas(new_total_savings);
        /* Dk modification: calculate and display efficiency */
        SavingsPotential.toilet["savings"] = formatNumber(pf(new_toilet_cost) - pf(SavingsPotential.toilet["optimal_cost"]));
        SavingsPotential.faucet["savings"] = formatNumber(pf(new_faucet_cost) - pf(SavingsPotential.faucet["optimal_cost"]));
        SavingsPotential.shower["savings"] = formatNumber(pf(new_shower_cost) - pf(SavingsPotential.shower["optimal_cost"]));
        doOptimalEfficiency("hospitality",SavingsPotential);
        /* --end Dk modification */
        showDiv("results_hospitality");
        break;
/*         t_cost = formatNumber(rooms * occupancy_rate/100 * guest_type * t_usage * t_freq * w_cost * 0.365);
        f_cost = formatNumber(rooms * occupancy_rate/100 * guest_type * f_usage * f_freq * f_time * w_cost * 0.365);
        s_cost = formatNumber(rooms * occupancy_rate/100 * guest_type * s_usage * s_time * w_cost * 0.365);
        total_cost = formatNumber(t_cost + f_cost + s_cost);
        new_t_cost = formatNumber(rooms * occupancy_rate/100 * guest_type * new_t_usage * t_freq * w_cost * 0.365);
        new_f_cost = formatNumber(rooms * occupancy_rate/100 * guest_type * new_f_usage * f_freq * f_time * w_cost * 0.365);
        new_s_cost = formatNumber(rooms * occupancy_rate/100 * guest_type * new_s_usage * s_time * w_cost * 0.365);
        new_total_cost = formatNumber(new_t_cost + new_f_cost + new_s_cost);
        total_savings = formatNumber(total_cost - new_total_cost);
        showDiv("results_hospitality");
        break;
*/
    }
    /* Dk modification: provide comparison between Savings achieved by calculator
       and the max possible savings and do display update for the results */
    function doOptimalEfficiency(buildingType,savingsObj) {
        //console.log("doOptimalEfficiency('%s','%o')...",buildingType,savingsObj);
        /* FIXME: need urinal savings too */
        var fixtureQueue = Array();
        var totalSavingsPotential = 0.00;
        switch(buildingType) {
            case "residential":
                fixtureQueue.push("toilet","faucet","shower");
                break;
            case "commercial":
                fixtureQueue.push("toilet","urinal","faucet");
                break;
            case "hospitality":
                fixtureQueue.push("toilet","faucet","shower");
                break;
        }
        for (i=0, j=fixtureQueue.length; i<j; i++) {
            var type = fixtureQueue[i];
            //var savingsPotential = type + "_savings_potential";
            //console.log("have savingsPotential as '%s'",savingsPotential);
            var DOMRef = buildingType + "_lowest_" + type.substr(0,1) + "_cost";
            //console.log("doOptimalEfficiency('%s'): have DOMRef as '%s'",buildingType,DOMRef);
            tmpObj = findObj(DOMRef);
            //console.log("doOptimalEfficiency(): have tmpObj as %o",tmpObj);
            tmpObj.innerHTML = "";
            var savingsPotential = savingsObj[type].savings;
            totalSavingsPotential = pf(totalSavingsPotential) + pf(savingsPotential);
            //console.log("doOptimalEfficiency(): have savings as '%s', totalSavingsPotential as '%s'",savingsPotential,totalSavingsPotential);
            if (savingsPotential > 0) {
                tmpObj.innerHTML = "&#8212;Save up to $" + addCommas(savingsPotential) +
                           " with a more efficient choice";
            }
        }
        var optimalTotalCost = 0.00;
        for (fType in savingsObj) {
            var optCost = savingsObj[fType].optimal_cost;
            //console.log("have optimal cost value as '%s', based on fType '%s'",optCost,fType);
            optimalTotalCost = pf(optimalTotalCost) + pf(optCost);
        }
        DOMRef = buildingType + "_lowest_total_cost";
        tmpObj = findObj(DOMRef);
        tmpObj.innerHTML = "$" + addCommas(formatNumber(optimalTotalCost));
        var efficiencyMsg = "";
        if (pf(new_total_cost) == pf(optimalTotalCost)) {
            efficiencyMsg = "Congratulations! You have chosen the most efficient options for your new project.";
        } else {
            efficiencyMsg = "Improve your efficiency: by selecting more water-efficient Sloan products, "+
                            "you could save an additional $" + addCommas(formatNumber(totalSavingsPotential));
        }
        DOMRef = buildingType + "_efficiency_msg";
        tmpObj = findObj(DOMRef);
        tmpObj.innerHTML = efficiencyMsg;
    }
    /* --end Dk modification */
    /* --Dk modification: handle GLOBAL representing product water usage.
         If values are zero, then a product was not selected. Therefore, set it
         to value associated to current fixture as it will not be replaced */
    function handleUnselectedProducts() {
        if (pf(new_toilet_usage) == 0) {
            //console.log('no toilet product selected? overriding zero new_toilet_usage "%s" with toilet_usage as "%s"',new_toilet_usage,toilet_usage);
            new_toilet_usage = toilet_usage;
        }
        if (pf(new_urinal_usage) == 0) {
            //console.log('no urinal product selected? overriding zero new_urinal_usage "%s" with urinal_usage as "%s"',new_urinal_usage,urinal_usage);
            if(zero_usage_urinal){
                new_urinal_usage = 0;
            }else{
                new_urinal_usage = urinal_usage;
            }
        }
        if (pf(new_shower_usage) == 0) {
            //console.log('no shower product selected? overriding zero new_shower_usage "%s" with shower_usage as "%s"',new_shower_usage,shower_usage);
            new_shower_usage = shower_usage;
        }
        if (pf(new_faucet_usage) === 0) {
            //console.log('no faucet product selected? overriding zero new_faucet_usage "%s", with faucet_usage as "%s"',new_faucet_usage,faucet_usage);
            new_faucet_usage = faucet_usage;
        }
    }
    /* --end Dk modification */
    function getUrinalValue(strObj){
        //console.log("getUrinalValue('%s')...",strObj);
        /* Dk modification: add input handling for "optimal" efficiency value
           so it can be set here with related vals and returned by same function */
        zero_usage_urinal = false;
        var switchStr = "";
        if (strObj === "optimal") {
            switchStr = strObj;
        } else {
            var tmpObj = findObj(strObj);
            var tmpval = 0.000;
            switchStr = tmpObj.value;
        }
        //switch (tmpObj.value) {
        switch (switchStr) {
            case 'optimal':
                //tmpval = 0.125;
                tmpval = 0;
                break;
        /* --end Dk modification */
            /*case 'Waterfree':
                tmpval = 0;
                break; */
            case 'Pint-SMOOTH':
                tmpval = 0.13;
                break;
            case 'heu_solis':
                tmpval = 0.125;
                break;
            case 'heu_manual':
                tmpval = 0.25;
                break;
            case 'heu_battery':
                tmpval = 0.25;
                break;
            case 'heu_hardwired':
                tmpval = 0.25;
                break;
            case 'heu_waterfree':
                tmpval = 0;
                zero_usage_urinal = true;
                break;
            case '':
                tempval = 0;
                break;
        }
        return pf(tmpval);
    }
    function getToiletValue(strObj){
        //console.log("getToiletValue('%s')...",strObj);
        /* Dk modification: add input handling for "optimal" efficiency value
           so it can be set here with related vals and returned by same function */
        var switchStr = "";
        if (strObj === "optimal") {
            switchStr = strObj;
        } else {
            var tmpObj = findObj(strObj);
            var tmpval = 0.000;
            switchStr = tmpObj.value;
        }
        //console.log("getToiletValue: switching based on string '%s'",switchStr);
        //switch (tmpObj.value) {
        switch (switchStr) {
            case 'optimal':
                tmpval = 1.28;
                break;
        /* --end Dk modification */
            case '':
                tmpval = 0;
                break;
            default:
                tmpval = 1.28;
                break;
        }
        return pf(tmpval);
    }
    function getShowerValue(strObj){
        //console.log("getShowerValue('%s')...",strObj);
        /* Dk modification: add input handling for "optimal" efficiency value
           so it can be set here with related vals and returned by same function */
        var switchStr = "";
        if (strObj === "optimal") {
            switchStr = strObj;
        } else {
            var tmpObj = findObj(strObj);
            var tmpval = 0.000;
            switchStr = tmpObj.value;
        }
        //console.log("getShowerValue: switching based on string '%s'",switchStr);
        //switch (tmpObj.value) {
        switch (switchStr) {
            case 'optimal':
                tmpval = 2;
                break;
        /* --end Dk modification */
            case 'ac_25':
                tmpval = 2.5;
                break;
            case 'ac_20':
                tmpval = 2;
                break;
            case '':
                tmpval = 0;
                break;
            default:
                tmpval = 0;
                break;
        }
        return pf(tmpval);
    }
    function getFaucetValue(strObj){
        //console.log("getFaucetValue('%s')...",strObj);
        /* Dk modification: add input handling for "optimal" efficiency value
           so it can be set here with related vals and returned by same function */
        var switchStr = "";
        if (strObj === "optimal") {
            switchStr = strObj;
        } else {
            var tmpObj = findObj(strObj);
            var tmpval = 0.000;
            switchStr = tmpObj.value;
        }
        //console.log("getFaucetValue: switching based on string '%s'",switchStr);
        //switch (tmpObj.value) {
        switch (switchStr) {
            case 'optimal':
                tmpval = 0.5;
                break;
        /* --end Dk modification */
            case 'eaf275':
                tmpval = 0.5;
                break;
            case 'eaf150':
                tmpval = 0.5;
                break;
            case 'ebf85':
                tmpval = 0.5;
                break;
            case 'ebf650':
                tmpval = 0.5;
                break;
            case 'ebf750':
                tmpval = 0.5;
                break;
            case 'etf700':
                tmpval = 0.5;
                break;
            case 'etf600':
                tmpval = 0.5;
                break;
            case '':
                tmpval = 0;
                break;
            default:
                tmpval = 0;
                break;
        }
        return pf(tmpval);
    }
}
// UTILS ___________________________________
function reNumberSteps(projectType) {
    //console.log('reNumberSteps(%o)...',projectType);
    var labels_step_4;
    var labels_step_5;
    var labels_step_6;
    if (document.getElementsByClassName) {
    // native DOM implementation for browsers that support it, for speed.
        labels_step_4 = document.getElementsByClassName("label_step_4");
        labels_step_5 = document.getElementsByClassName("label_step_5");
        labels_step_6 = document.getElementsByClassName("label_step_6");
    } else {
    // mainly IE, but also older other browsers
        labels_step_4 = getElementsByClass("label_step_4");
        labels_step_5 = getElementsByClass("label_step_5");
        labels_step_6 = getElementsByClass("label_step_6");
    }
    var label_groups = [ labels_step_4, labels_step_5, labels_step_6 ];
    var newLabels = [ ];
    switch(projectType) {
       case "New Construction":
        newLabels = ["STEP 3","STEP 4","STEP 5"];
        break;
       case "Replace Existing":
        newLabels = ["STEP 4","STEP 5","STEP 6"];
        break;
    }
    for (var i=0, j=label_groups.length; i<j; i++) {
        var labelEls = label_groups[i];
        var label = newLabels[i];
        newTextContent(labelEls,label);
    }
}
function newTextContent(nodeCollection,newLabel) {
    for (var i=0, j=nodeCollection.length; i<j; i++) {
        var label = nodeCollection[i];
        //console.log(label);
        //console.log("replacing '%s' with '%s'",label.innerHTML,newLabel);
        label.innerHTML = newLabel;
    }
}
function validate(strId){
    //console.log("validate('%s')...",strId);
    var strComp = strId.split("_");
    var tmpObj;
    var set = 0;
    switch (strComp[1]) {
        case 'residential':
            tmpObj = findObj("SloanToilets_residential")
            if(tmpObj.value.length != 0) set++;
            tmpObj = findObj("SloanShowerhead_residential")
            if(tmpObj.value.length != 0) set++;
            tmpObj = findObj("SloanFaucet_residential")
            if(tmpObj.value.length != 0) set++;
            //tmpObj = document.getElementById("step5_res");
            break;
        case 'commercial':
            tmpObj = findObj("SloanToilets_commercial")
            if(tmpObj.value.length != 0) set++;
            tmpObj = findObj("SloanUrinals_commercial")
            if(tmpObj.value.length != 0) set++;
            tmpObj = findObj("SloanFaucet_commercial")
            if(tmpObj.value.length != 0) set++;
            //tmpObj = document.getElementById("step5_com");
            break;
        case 'hospitality':
            tmpObj = findObj("SloanToilets_hospitality")
            if(tmpObj.value.length != 0) set++;
            tmpObj = findObj("SloanShowerhead_hospitality")
            if(tmpObj.value.length != 0) set++;
            tmpObj = findObj("SloanFaucet_hospitality")
            if(tmpObj.value.length != 0) set++;
            //tmpObj = document.getElementById("step5_hosp");
            break;
    }
    //if(set == 3){
    /* Dk modification only require one of 3 Sloan product selections
       Part 1/2 as the calculations are adjusted to use defaults if not user-selected */
    /*
    if(set >= 1){
        tmpObj.className = "";
        tmpObj.innerHTML = "<a id='reqstep5' href='javascript:prepareStep6();'>Continue</a>"
        tmpObj.title = "";
    }else{
        tmpObj.innerHTML = "<u>Continue</u>";
        tmpObj.className = "disabled"
        tmpObj.title = "Please choose Sloan products";
    }
    */
}
function setupDisplay(value, strType){
    //console.log("setupDisplay('%s','%s')...",value,strType);
    var tempObj;
    if(value != ''){
        //setup image
        tempObj= document.getElementById(strType);
        tempObj.src = '/images/watersavingscalculator/' + value + '.jpg';
        //setup links
        tempObj = document.getElementById(strType + "_spec");
        //if(eval(value + '_spec') == ""){
            //tempObj.innerHTML = "<a href='" + eval(value + '_spec') + "' target='_blank'><img src='/images/watersavingscalculator/file_icon.jpg' border='0' title='View Specification'></a>";
        //}else{
            tempObj.innerHTML = "<a href='" + eval(value + '_spec') + "' target='_blank'><img src='/images/watersavingscalculator/file_icon.jpg' border='0' title='View Specification'></a>";
        //}
    }else{
        clearImageAndLink(strType);
    }
}
function clearImageAndLink(strType){
    //console.log("clearImageAndLink('%s')...",strType);
    var tempObj = document.getElementById(strType);
    tempObj.src = '/images/watersavingscalculator/blank.jpg';
    tempObj = document.getElementById(strType + "_spec");
    tempObj.innerHTML = "&nbsp;";
}
function pf(obj){
    return(parseFloat(obj));
}
function formatNumber(numObj){
    debug_buffer = numObj;
    return numObj.toFixed(2);
}
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;
}
function findObj(str){
    return document.getElementById(str);
}
function hideDivs(divArray){
    for (i=0; i < divArray.length; i++){
        hideDiv(divArray[i]);
    }
}
function showDivs(divArray){
    for (i=0; i < divArray.length; i++){
        showDiv(divArray[i]);
    }
}
function hideDiv(divName){
    tmpObj = document.getElementById(divName);
    tmpObj.style.display = "none";
    tmpObj.style.visibility = "hidden";
    return true;
}
function showDiv(divName){
    tmpObj = document.getElementById(divName);
    tmpObj.style.display = "";
    tmpObj.style.visibility = "";
    return true;
}
function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}
 
/*
CMS image publication
/images/watersavingscalculator/ac_20.jpg
/images/watersavingscalculator/ac_25.jpg
/images/watersavingscalculator/ebf85.jpg
/images/watersavingscalculator/eaf150.jpg
/images/watersavingscalculator/eaf275.jpg
/images/watersavingscalculator/ebf750.jpg
/images/watersavingscalculator/ebf650.jpg
/images/watersavingscalculator/etf80.jpg
/images/watersavingscalculator/etf600.jpg
/images/watersavingscalculator/etf700.jpg
/images/watersavingscalculator/het_ac.jpg
/images/watersavingscalculator/het_battery.jpg
/images/watersavingscalculator/het_manual.jpg
/images/watersavingscalculator/het_solis.jpg
/images/watersavingscalculator/heu_battery.jpg
/images/watersavingscalculator/heu_hardwired.jpg
/images/watersavingscalculator/heu_manual.jpg
/images/watersavingscalculator/heu_solis.jpg
/images/watersavingscalculator/heu_waterfree.jpg
/images/watersavingscalculator/fmpa_1.jpg
/images/watersavingscalculator/fmpa_16.jpg
/images/watersavingscalculator/dft.jpg
/images/watersavingscalculator/stt16.jpg
/images/watersavingscalculator/stt128.jpg
/images/watersavingscalculator/stt16_11.jpg
/images/watersavingscalculator/none.jpg

*/
