/*****************************************
*      The Earth's Outlet ScapeCalc      *
*                                        *
*  Copyright 2009 Earth's Outlet, Inc.   *
*                                        *
*  All rights reserved. No part of this  *
*  file may be used, copied, nor distri- *
*  buted without prior express written   *
*  permission from Earth's Outlet, Inc.  *
*****************************************/

function toggleDisplay (divid) {
    var thisdiv = document.getElementById(divid);
    if (thisdiv.style.display == 'none') { thisdiv.style.display = 'inline'; }
    else { thisdiv.style.display = 'none'; }
}
function doDisplay (divids) {
    var divid_array = divids.split(',');
    for (var i in divid_array) {
        document.getElementById(divid_array[i]).style.display = 'inline';
    }
}
function dontDisplay (divids) {
    var divid_array = divids.split(',');
    for (var i in divid_array) {
        document.getElementById(divid_array[i]).style.display = 'none';
    }
}
function onStyle (divids) {
    var divid_array = divids.split(',');
    for (var i in divid_array) {
        document.getElementById(divid_array[i]).style.color = 'black';
        document.getElementById(divid_array[i]).style.fontStyle = 'normal';
    }
}
function offStyle (divids) {
    var divid_array = divids.split(',');
    for (var i in divid_array) {
        document.getElementById(divid_array[i]).style.color = 'grey';
        document.getElementById(divid_array[i]).style.fontStyle = 'oblique';
    }
}
function changeHTML (divid,html,add) {
    var thisdiv = document.getElementById(divid);
    if (add==true) { thisdiv.innerHTML = thisdiv.innerHTML+html; }
    else { thisdiv.innerHTML = html; }
}
function calculate (calc) {
    var BClbs = 0;
    var VPgal = 0;
    var d = new Date();
    var month = d.getMonth()+1;
    if (month>7) { if (calc.season.value=='spring') { month = 3; } }
    var VPmonths = 10-month;
    var la = calc.lawnarea.value;
    var lan = la * calc.lawnareanew.value / 100;
    var lae = la - lan;
    var laeBC = lae * .05;
    var laeVPoz = Math.round(lae/170);
    var laeVPgal = Math.round(laeVPoz/1.28)/100;
    var lanBC = lan * .1;
    var lanVPoz = Math.round(lan/85);
    var lanVPozhalf = Math.round(lanVPoz/2);
    var lanVPgal = Math.round(lanVPoz/1.28)/100;
    var lanVPgalhalf = Math.round(lanVPozhalf/1.28)/100;
    if (calc.seedsod.value=='sod') { lanBC = lanBC * 2; }
    var addhtml = false;
    if (lae!=0 || lan!=0) {
        changeHTML('results','<br /><strong>Lawn</strong><br />');
        addhtml = true;
    }
    if (lae!=0) {
        var stuff = 'the ';
        if (lan==0) { stuff = 'all '; }
        var VPstuff = laeVPgal+' gal.';
        if (laeVPgal<.2) { VPstuff = laeVPoz+' oz.'; }
        changeHTML('results','&bull; Apply '+laeBC+' lbs. Black Castings&trade; on '+stuff+lae+' ft<sup>2</sup> of established lawn. Drench with '+VPstuff+' VermaPlex&reg; diluted in water',true);
        BClbs = BClbs+laeBC;
        VPgal = VPgal+laeVPgal;
        if (lan==0) {
            if (VPmonths>0) {
                var monthps = 's';
                if (VPmonths==1) { monthps = ''; }
                changeHTML('results',' and again each month through the fall (est. '+VPmonths+' more month'+monthps+').<br />',true);
                VPgal = VPgal+(laeVPgal*VPmonths);
            }
            else { changeHTML('results','.<br />',true); }
        }
        else { changeHTML('results','.<br />',true); }
    }
    if (lan!=0) {
        var stuff = 'the ';
        if (lae==0) { stuff = 'all '; }
        changeHTML('results','&bull; Apply '+lanBC+' lbs. Black Castings&trade; on '+stuff+lan+' ft<sup>2</sup> of ',true);
        BClbs = BClbs+lanBC;
        if (calc.seedsod.value=='seed') { changeHTML('results','area to be seeded, then spread seed.',true); }
        else { changeHTML('results','prepared sod bed, then lay sod.',true); }
        var VPstuff = lanVPgal+' gal.';
        if (lanVPgal<.2) { VPstuff = lanVPoz+' oz.'; }
        changeHTML('results',' Drench with '+VPstuff+' VermaPlex&reg; diluted in water',true);
        VPgal = VPgal+lanVPgal;
        if (lae==0) {
            if (VPmonths>0) {
                VPstuff = lanVPgalhalf+' gal.';
                if (lanVPgalhalf<.2) { VPstuff = lanVPozhalf+' oz.'; }
                changeHTML('results',', and with '+VPstuff+' VermaPlex&reg; each month thereafter through the fall.<br />',true);
                VPgal = VPgal+(lanVPgalhalf*VPmonths);
            }
            else { changeHTML('results','.<br />',true); }
        }
        else { changeHTML('results','.<br />',true); }
    }
    if (lae!=0) {
        if (lan!=0) {
            if (VPmonths>0) {
                var VPstuff = (laeVPgal+lanVPgalhalf)+' gal.';
                if ((laeVPgal+lanVPgalhalf)<.2) { VPstuff = (laeVPoz+lanVPozhalf)+' oz.'; }
                changeHTML('results','&bull; Drench entire '+la+' ft<sup>2</sup> with '+VPstuff+' VermaPlex&reg; diluted in water each month thereafter through the fall.<br />',true);
                VPgal = VPgal+((laeVPgal+lanVPgalhalf)*VPmonths);
            }
        }
    }
    var et = calc.esttrees.value;
    var etd = calc.esttreesdiameter.value;
    var eth = Math.round(etd/6)+4;
    var etVPoz = etd*3;
    var etVP = Math.round(etVPoz*et*100/128)/100;
    var nt = calc.newtrees.value;
    var ntd = calc.newtreesdiameter.value;
    var ntVPoz = ntd*3;
    var ntVP = Math.round(ntVPoz*nt*100/128)/100;
    if (et!=0 || nt!=0) {
        changeHTML('results','<br /><strong>Trees</strong><br />',addhtml);
        addhtml = true;
    }
    if (et!=0) {
        if (et==1) { changeHTML('results','&bull; Apply '+etd+' lbs. Black Castings&trade; around the drip line of the established tree.',true); }
        else { changeHTML('results','&bull; Apply Black Castings&trade; around the drip line of each established tree, 1 lb. per inch of trunk diameter (avg. '+etd+' lbs. per tree).',true); }
        BClbs = BClbs+(etd*et);
        changeHTML('results', ' For best results, evenly distribute the Black Castings&trade; into '+eth+' holes spaced around the drip line, leaving enough space to replace original sod.',true);
        var stuff1 = '';
        var stuff2 = '';
        var stuff3 = '';
        if (et!=1) { stuff1 = ' around each tree'; }
        if (etVP!=0) {
            if (et==1) { stuff2 = ' ('+etVP+' gal.)'; }
            else { stuff3 = ' ('+etVP+' gal. VermaPlex&reg; total for all '+et+' trees)'; }
        }
        changeHTML('results',' Drench the Black Castings&trade;'+stuff1+' with '+etVPoz+' oz.'+stuff2+' VermaPlex&reg; diluted in water'+stuff3+'.<br />',true);
        VPgal = VPgal+etVP;
    }
    if (nt!=0) {
        var stuff1 = 's';
        var stuff2 = 'each';
        var stuff3 = '';
        var stuff4 = '';
        if (nt==1) {
            stuff1 = '';
            stuff2 = 'the';
        }
        if (ntVP!=0) {
            if (nt==1) { stuff3 = ' ('+ntVP+' gal.)'; }
            else { stuff4 = ' ('+ntVP+' gal. VermaPlex&reg; total for all '+nt+' transplants)'; }
        }
        changeHTML('results','&bull; For the transplant'+stuff1+', place '+ntd+' lbs. Black Castings&trade; into '+stuff2+' hole.  Set the root ball'+stuff1+' in '+stuff2+' hole, then backfill with soil from the hole mixed with another '+ntd+' lbs. Black Castings&trade;. Drench '+stuff2+' filled hole with '+ntVPoz+' oz.'+stuff3+' VermaPlex&reg;'+stuff4+'.<br />',true);
        BClbs = BClbs+(ntd*nt*2);
        VPgal = VPgal+ntVP;
    }
    if (VPgal>0) { changeHTML('results','<br /><strong>VermaPlex&reg; dilution ratio</strong> for all applications should be about 1:25, or 5 oz. VermaPlex&reg; per gallon of water. If you find you are over watering or not watering enough for good coverage, the dilution ratio can be adjusted from 1:15 (8 oz. VermaPlex&reg; per gallon of water) to 1:40 (3 oz. VermaPlex&reg; per gallon of water). A hose-end sprayer or other siphoning system is recommended for best results.<br />',true); }
    if (addhtml==true) {
        changeHTML('results','<br /><strong>Project Totals</strong><br />',true);
        if (BClbs>0) {
            var BCsize = '40';
            var BCbags = Math.ceil(BClbs/40);
            var BCmsrp = 39.95;
            if (BClbs<=20) {
                BCsize = '20';
                BCbags = 1;
                BCmsrp = 23.95;
            }
            if (BClbs <=5) {
                BCsize = '5';
                BCbags = 1;
                BCmsrp = 7.95;
            }
            var BCmsrpstring = '';
            var BCps = '';
            if (BCbags>1) {
                BCps = 's';
                BCmsrpstring = '$'+BCmsrp+' = $'+(BCmsrp*BCbags).toFixed(2)+' total';
            }
            else { BCmsrpstring = '$'+BCmsrp; }
            changeHTML('results','&bull; <strong>Black Castings&trade;</strong> &mdash; '+BCbags+' &times; '+BCsize+' lb. bag'+BCps+' (MSRP of '+BCmsrpstring+')<br />',true);
        }
        if (VPgal>0) {
            var VPsize = '5 gal. cube';
            var VPjugs = Math.ceil(VPgal/5);
            var VPmsrp = 129.95;
            if (VPjugs>1) { VPps = 's'; }
            if (VPgal<=2.5) {
                VPsize = '2.5 gal. jug';
                VPjugs = 1;
                VPmsrp = 79.95;
            }
            if (VPgal<=1) {
                VPsize = '1 gal. jug';
                VPjugs = 1;
                VPmsrp = 39.95;
            }
            if (VPgal<=.25) {
                VPsize = '1 quart bottle';
                VPjugs = 1;
                VPmsrp = 15.95;
            }
            var VPmsrpstring = '';
            var VPps = '';
            if (VPjugs>1) {
                VPps = 's';
                VPmsrpstring = '$'+VPmsrp+' = $'+(VPmsrp*VPjugs).toFixed(2)+' total';
            }
            else { VPmsrpstring = '$'+VPmsrp; }
            changeHTML('results','&bull; <strong>VermaPlex&reg;</strong> &mdash; '+VPjugs+' &times; '+VPsize+VPps+' (MSRP of '+VPmsrpstring+')<br />',true);
        }
    }
    else { changeHTML('results',''); }
}
function buildProject (url) {
    url = url.substring(1);
    var nvs = url.split("&");
    var calc = document.getElementById('calc');
    for (var i=0; i<nvs.length; i++) {
        var nv = nvs[i].split("=");
        var n = nv[0];
        var v = nv[1];
        var field = document.createElement('input');
        field.setAttribute('type', 'hidden');
        field.setAttribute('name', n);
        field.setAttribute('value', v);
        calc.appendChild(field);
    }
}
function printProject (calc) {
    var url = '/scripts/EO-ScapeCalc.html?';
    var and = '';
    for (var i=0; i<calc.length; i++) {
        if (calc.elements[i].name!='') {
            url = url+and+calc.elements[i].name+'='+calc.elements[i].value;
            and = '&';
        }
    }

    window.open(url,'ScapeCalc','width=800,height=400,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=yes,copyhistory=no');
}
function insertDate (id) {
    var d = new Date();
    var month = d.getMonth()+3;
    var year = d.getFullYear();
    if (month>12) {
        month = month-12;
        year = year+1;
    }
    changeHTML(id,month+'/1/'+year);
}


