// START Top Nav Rollover Scripts

plugOn = new Image();
plugOn.src = "images/plug_on.gif"
plugOff = new Image();
plugOff.src = "images/plug_off.gif"

cflOn = new Image();
cflOn.src = "images/cfl_on.gif"
cflOff = new Image();
cflOff.src = "images/cfl_off.gif"

coldwaterOn = new Image();
coldwaterOn.src = "images/coldwater_on.gif"
coldwaterOff = new Image();
coldwaterOff.src = "images/coldwater_off.gif"

linedryOn = new Image();
linedryOn.src = "images/linedry_on.gif"
linedryOff = new Image();
linedryOff.src = "images/linedry_off.gif"

thermOn = new Image();
thermOn.src = "images/therm_on.gif"
thermOff = new Image();
thermOff.src = "images/therm_off.gif"

showerheadOn = new Image();
showerheadOn.src = "images/showerhead_on.gif"
showerheadOff = new Image();
showerheadOff.src = "images/showerhead_off.gif"

waterheaterOn = new Image();
waterheaterOn.src = "images/waterheater_on.gif"
waterheaterOff = new Image();
waterheaterOff.src = "images/waterheater_off.gif"

// Hide the instructions on the Calculator Screen, once 
function calc_hide_instructions() {
    if (document.getElementById("calc_national_average_box")) { 
        var element = document.getElementById("calc_national_average_box");
        element.style.display = "none";
    }
}

// The Icon Rollover Script on the Results Page
function rollover(whichAction,overimg) {
	if (document.getElementById("results_" + whichAction + "_icon")) { 
		var element = document.getElementById("results_" + whichAction + "_icon");
		if (element.className == "unchecked") {
			element.src = eval(overimg + '.src');
		}
	}
}

// Show the Tips Box for each Energy Saving Action on the Results Page
function show_tip(whichAction) {
    if (document.getElementById("results_" + whichAction + "_rollover")) {
        var element = document.getElementById("results_" + whichAction + "_rollover");
        element.style.display = "block"; 
    }
}

// Hide the Tips Box for each Energy Saving Action on the Results Page
function hide_tip(whichAction) {
    if (document.getElementById("results_" + whichAction + "_rollover")) { 
        var element = document.getElementById("results_" + whichAction + "_rollover");
        element.style.display = "none"; 
    }
}

// Toggle the checkmark for each Energy Saving Action on the Results Page
function toggle_action(whichAction) {
    if (document.getElementById("results_" + whichAction + "_icon")) { 
        var element = document.getElementById("results_" + whichAction + "_icon");
        element.className = element.className == "checked" ? "unchecked" : "checked";
        element.src = element.className == "unchecked" ? "images/" + whichAction + "_off.gif"  : "images/" + whichAction + "_checked.gif" ;
    }
}

// Grab the Deatils for the Energy Saving Action Icons
function results_get_icons() {
	this.icon_plug = document.getElementById("results_plug_icon") ; // 'Unplug Idle Appliances' Icon
	this.icon_cfl = document.getElementById("results_cfl_icon") ;  // 'Use CFLs' Icon
	this.icon_coldwater = document.getElementById("results_coldwater_icon") ;  // 'Wash in Cold Water' Icon
	this.icon_linedry = document.getElementById("results_linedry_icon") ;  // 'Line-Dry Your Clothes' Icon
	this.icon_therm = document.getElementById("results_therm_icon") ; // 'Adjust Your ThermoStat' Icon
	this.icon_showerhead = document.getElementById("results_showerhead_icon") ; //'Get a new Shower Head' Icon
	this.icon_waterheater = document.getElementById("results_waterheater_icon") ; // 'Get a new Water Heater' Icon
}

// Reset each Energy Saving Action on the Results Page
function reset_results() {
	// Setup the Energy Saving Icons
	results_get_icons();

	// Reset the 'Unplug Idle Appliances' Icon
	this.icon_plug.className = "unchecked" ;
	this.icon_plug.src =  "images/plug_off.gif" ;

	// Reset the 'Use CFLs' Icon
	this.icon_cfl.className = "unchecked" ;
	this.icon_cfl.src =  "images/cfl_off.gif" ;

	// Reset the 'Wash in Cold Water' Icon
	this.icon_coldwater.className = "unchecked" ;
	this.icon_coldwater.src =  "images/coldwater_off.gif" ;

	// Reset the 'Line-Dry Your Clothes' Icon
	this.icon_linedry.className = "unchecked" ;
	this.icon_linedry.src =  "images/linedry_off.gif" ;

	// Reset the 'Adjust Your ThermoStat' Icon
	this.icon_therm.className = "unchecked" ;
	this.icon_therm.src =  "images/therm_off.gif" ;

	// Reset the 'Get a new Shower Head' Icon
	this.icon_showerhead.className = "unchecked" ;
	this.icon_showerhead.src =  "images/showerhead_off.gif" ;

	// Reset the 'Get a new Water Heater' Icon
	this.icon_waterheater.className = "unchecked" ;
	this.icon_waterheater.src =  "images/waterheater_off.gif" ;

	// Display the Annual Lbs of CO2 Saved
	var element = document.getElementById("lbs_of_co2") ;
	element.innerHTML = '0' ;

	// Display the Annual Savings Per Yer 
	var element = document.getElementById("you_saver_per_year_value") ;
	element.innerHTML = "$0" ;

	// Hide the What Can You Buy Box
	var element = document.getElementById("what_can_you_buy_container") ;
	element.style.display = "none" ;

	// Hide the What Can You Buy Image
	var element = document.getElementById("results_what_can_this_buy") ;
	element.style.display = "none" ;

}

// Debugging on the Results Page
function results_calculate() {
	// Setup the Energy Saving Icons
	results_get_icons();

    // Setup the default values
    this.default_money = 1900;
    this.default_kwh = 1.24;

	// Grab the Variables from the Calc Form
	// Remove any $ and , symbols
	
	this.kwh = queryString("kwh") ;
	this.kwh = this.kwh.replace("$","") ;
	this.kwh = this.kwh.replace(",","") ;
    this.kwh = this.kwh.replace("..",".") ;
	if (this.kwh == "") {
	    this.kwh = this.default_kwh ;
	}
    
	this.money = queryString("money") ;
	this.money = this.money.replace("$","") ;
	this.money = this.money.replace(",","") ;
    this.money = this.money.replace("..",".") ;
	if (this.money == "") {
	    this.money = this.default_money ;
	}
    
	this.therm = queryString("therm") ;
	this.therm = this.therm.replace("$","") ;
	this.therm = this.therm.replace(",","") ;


	// Calculate the Cost of kwH and Therms
	if (this.therm > 0) {
		this.cost_kwh = (this.money * .53) / this.kwh ;
 		this.cost_therm = (this.money * .47) / this.therm ; 
	} else {
		this.cost_kwh = this.money / this.kwh ;
 		this.cost_therm = 0 ; 
	}

	// Add up the Total Usage of Energy
	this.usage_total = this.cost_kwh + (this.cost_therm * 0.03413) ;

	// Determine the total pounds of CO2 generated
	this.pounds_of_co2_created = this.usage_total * 0.134 ;

	// Percentages for Specific Items
	this.perc_heating_cooling = .49 ;
	this.perc_waterheater = .13 ;
	this.perc_lighting = .10 ;
	this.perc_appliances = .13 ;
	this.perc_electronics = .07 ;
	this.perc_other = .08 ;

	// kwH Usage for General areas of energy usage
	this.kwh_heating_cooling = this.usage_total * this.perc_heating_cooling ;
	this.kwh_waterheater = this.usage_total * this.perc_waterheater ;
	this.kwh_lighting = this.usage_total * this.perc_lighting ;
	this.kwh_appliances = this.usage_total * this.perc_appliances ;
	this.kwh_electronics = this.usage_total * this.perc_electronics ;
	this.kwh_other = this.usage_total * this.perc_other ;

	// Cost for General areas of energy usage
	this.cost_heating_cooling = this.money * this.perc_heating_cooling ;
	this.cost_waterheater = this.money * this.perc_waterheater ;
	this.cost_lighting = this.money * this.perc_lighting ;
	this.cost_appliances = this.money * this.perc_appliances ;
	this.cost_electronics = this.money * this.perc_electronics ;
	this.cost_other = this.money * this.perc_other ;

	// Calculate Energy Savings for 'Unplug Idle Appliances'
	if (this.icon_plug.className == "checked") {
		this.cost_savings_plug = .05 * this.cost_kwh * this.kwh ;
		this.energy_savings_plug = .07 * this.kwh_electronics ;
	} else {
		this.cost_savings_plug = 0 ;
		this.energy_savings_plug = 0 ;
	}

	// Calculate Energy Savings for 'Use CFLs'
	if (this.icon_cfl.className == "checked") {
		this.cost_savings_cfl = .38 * this.cost_lighting ;
		this.energy_savings_cfl = .38 * this.kwh_lighting ;
	} else {
		this.cost_savings_cfl = 0 ;
		this.energy_savings_cfl = 0 ;
	}

	// Calculate Energy Savings for 'Wash in Cold Water'
	if (this.icon_coldwater.className == "checked") {
		this.cost_savings_coldwater = .27 * this.cost_appliances ;
		this.energy_savings_coldwater = .27 * this.kwh_appliances ;
	} else {
		this.cost_savings_coldwater = 0 ;
		this.energy_savings_coldwater = 0 ;
	}

	// Calculate Energy Savings for 'Line-Dry Your Clothes'
	if (this.icon_linedry.className == "checked") {
		this.cost_savings_linedry = .05 * this.money ;
		this.energy_savings_linedry = .48 * this.kwh_appliances ;
	} else {
		this.cost_savings_linedry = 0 ;
		this.energy_savings_linedry = 0 ;
	}

	// Calculate Energy Savings for 'Adjust Your ThermoStat'
	if (this.icon_therm.className == "checked") {
		this.cost_savings_therm = .1 * this.cost_heating_cooling ;
		this.energy_savings_therm = .1 * this.kwh_heating_cooling ;
	} else {
		this.cost_savings_therm = 0 ;
		this.energy_savings_therm = 0 ;
	}

	// Calculate Energy Savings for 'Get a new Shower Head'
	if (this.icon_showerhead.className == "checked") {
		this.cost_savings_showerhead = .15 * this.cost_waterheater ;
		this.energy_savings_showerhead = .15 * this.kwh_waterheater ;
	} else {
		this.cost_savings_showerhead = 0 ;
		this.energy_savings_showerhead = 0 ;
	}

	// Calculate Energy Savings for 'Turn Down Your Water Heater'
	if (this.icon_waterheater.className == "checked") {
		this.cost_savings_waterheater = .05 * this.money ;
		this.energy_savings_waterheater = .05 * this.usage_total ;
	} else {
		this.cost_savings_waterheater = 0 ;
		this.energy_savings_waterheater = 0 ;
	}

	// Determine the Annual Lbs of CO2 Saved
	this.lbs_of_co2 = 1.34 * ( this.energy_savings_waterheater + this.energy_savings_showerhead ) ;
	this.lbs_of_co2 += 1.34 * ( this.energy_savings_therm + this.energy_savings_linedry );
	this.lbs_of_co2 += 1.34 * ( this.energy_savings_coldwater + this.energy_savings_cfl + this.energy_savings_plug );

	// Display the Annual Lbs of CO2 Saved
	var element = document.getElementById("lbs_of_co2") ;
	element.innerHTML = '' ;
	element.innerHTML += Math.round(this.lbs_of_co2) ;

	// Determine the Auto Equivalent Saved (in days)
	this.automobiles_saved = ( this.energy_savings_waterheater + this.energy_savings_showerhead ) / 31.37 ;
	this.automobiles_saved += ( this.energy_savings_therm + this.energy_savings_linedry ) / 31.37 ;
	this.automobiles_saved += ( this.energy_savings_coldwater + this.energy_savings_cfl + this.energy_savings_plug ) / 31.37 ;

	// Determine the Annual Savings Per Yer
	this.annual_savings = this.cost_savings_waterheater + this.cost_savings_showerhead  ;
	this.annual_savings  += this.cost_savings_therm + this.cost_savings_linedry ;
	this.annual_savings  += this.cost_savings_coldwater + this.cost_savings_cfl + this.cost_savings_plug ;

	// Display the Annual Savings Per Yer 
	var element = document.getElementById("you_saver_per_year_value") ;
	element.innerHTML = "$" + Math.round(this.annual_savings) ;

	// Calculate the potential purchases with money saved
	what_this_can_buy(this.annual_savings) ;

	// Spit out some debug information
	// results_debug() ;
}

// Debugging on the Results Page
function results_debug() {
	// Find the element that we will insert the debug info into
	var element = document.getElementById("results_debug") ;

	element.innerHTML += "Money: " + this.money + "<BR>" ;
	element.innerHTML += "KWH: " + this.kwh + "<BR>" ;
	element.innerHTML += "Therm: " + this.therm + "<BR>" ;
	element.innerHTML += "Cost in kwH: " + Math.round(this.cost_kwh) + "<BR>" ;
	element.innerHTML += "Cost in Therm: " + Math.round(this.cost_therm) + "<BR>" ;
	element.innerHTML += "Total Usage: " + Math.round(this.usage_total) + "<BR><BR>" ;

	element.innerHTML += "Pounds of CO2 generated: " + Math.round(this.pounds_of_co2_created) + "<BR><BR>" ;

	element.innerHTML += "kwH Usage for Heating & Cooling: " + Math.round(this.kwh_heating_cooling) + "<BR>" ;
	element.innerHTML += "kwH Usage for Water Heating: " + Math.round(this.kwh_waterheater) + "<BR>" ;
	element.innerHTML += "kwH Usage for Lighting: " + Math.round(this.kwh_lighting) + "<BR>" ;
	element.innerHTML += "kwH Usage for Major Appliances: " + Math.round(this.kwh_appliances) + "<BR>" ;
	element.innerHTML += "kwH Usage for Electronics: " + Math.round(this.kwh_electronics) + "<BR>" ;
	element.innerHTML += "kwH Usage for Other: " + Math.round(this.kwh_other) + "<BR><BR>" ;

	element.innerHTML += "Cost for Heating & Cooling: " + Math.round(this.cost_heating_cooling) + "<BR>" ;
	element.innerHTML += "Cost for Water Heating: " + Math.round(this.cost_waterheater) + "<BR>" ;
	element.innerHTML += "Cost for Lighting: " + Math.round(this.cost_lighting) + "<BR>" ;
	element.innerHTML += "Cost for Major Appliances: " + Math.round(this.cost_appliances) + "<BR>" ;
	element.innerHTML += "Cost for Electronics: " + Math.round(this.cost_electronics) + "<BR>" ;
	element.innerHTML += "Cost for Other: " + Math.round(this.cost_other) + "<BR><BR>" ;

	element.innerHTML += "Cost Savings for Unplugging: " + Math.round(this.cost_savings_plug) + "<BR>" ;
	element.innerHTML += "Cost Savings for CFLs: " + Math.round(this.cost_savings_cfl) + "<BR>" ;
	element.innerHTML += "Cost Savings for Cold Water: " + Math.round(this.cost_savings_coldwater) + "<BR>" ;
	element.innerHTML += "Cost Savings for Line Dry: " + Math.round(this.cost_savings_linedry) + "<BR>" ;
	element.innerHTML += "Cost Savings for Therm: " + Math.round(this.cost_savings_therm) + "<BR>" ;
	element.innerHTML += "Cost Savings for Shower Head: " + Math.round(this.cost_savings_showerhead) + "<BR>" ;
	element.innerHTML += "Cost Savings for Water Heater: " + Math.round(this.cost_savings_waterheater) + "<BR><BR>" ;

	element.innerHTML += "Energy Savings for Unplugging: " + Math.round(this.energy_savings_plug) + "<BR>" ;
	element.innerHTML += "Energy Savings for CFLs: " + Math.round(this.energy_savings_cfl) + "<BR>" ;
	element.innerHTML += "Energy Savings for Cold Water: " + Math.round(this.energy_savings_coldwater) + "<BR>" ;
	element.innerHTML += "Energy Savings for Line Dry: " + Math.round(this.energy_savings_linedry) + "<BR>" ;
	element.innerHTML += "Energy Savings for Therm: " + Math.round(this.energy_savings_therm) + "<BR>" ;
	element.innerHTML += "Energy Savings for Shower Head: " + Math.round(this.energy_savings_showerhead) + "<BR>" ;
	element.innerHTML += "Energy Savings for Water Heater: " + Math.round(this.energy_savings_waterheater) + "<BR><BR>" ;

	element.innerHTML += "Annual Savings: " + Math.round(this.annual_savings) + "<BR>" ;
	element.innerHTML += "Annual lbs of CO2 Saved: " + Math.round(this.lbs_of_co2) + "<BR>" ;
	element.innerHTML += "Auto Equivalent Saved (in days): " + Math.round(this.automobiles_saved) + "<BR>" ;
}

// Convenient Function for Grabbing Get Variables from a Form Submission
function queryString(key){
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

// Function that will parse out the Get Variables from a Form Submission
function PageQuery(q) {
	// Validate that a Get variable has been requested
	if (q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;

	// Cycle through all the variables in the Address Bar 
	// And put them into an array
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}

	// Obtain the value for a specific parameter
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s) return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
}

function what_this_can_buy(annual_savings) {
	annual_savings = Math.round(annual_savings) ;

	this.items_to_buy = new Array();
	this.items_to_buy[1] = " a pack of gum" ;
	this.items_to_buy[2] = " a hot chocolate" ;
	this.items_to_buy[3] = " a fruit smoothie" ;
	this.items_to_buy[4] = " an ice cream sundae" ;
	this.items_to_buy[5] = " a personal pizza" ;
	this.items_to_buy[6] = " one movie theater ticket" ;
	this.items_to_buy[7] = " one roller rink admission" ;
	this.items_to_buy[8] = " a nice T-shirt" ;
	this.items_to_buy[9] = " a set of markers" ;
	this.items_to_buy[10] = " two paperback chapter books" ;
	this.items_to_buy[20] = " a wildlife magazine subscription" ;
	this.items_to_buy[30] = " a rolling backpack" ;
	this.items_to_buy[40] = " in-line skates" ;
	this.items_to_buy[50] = " two pairs of jeans" ;
	this.items_to_buy[60] = " fancy athletic shoes" ;
	this.items_to_buy[70] = " a skateboard" ;
	this.items_to_buy[80] = " four pro baseball bleacher seats" ;
	this.items_to_buy[90] = " a scooter" ;
	this.items_to_buy[100] = " an electric scooter" ;
	this.items_to_buy[200] = " a mountain bike" ;
	this.items_to_buy[300] = " 20 new music CDs" ;
	this.items_to_buy[400] = " 20 new DVDs" ;
	this.items_to_buy[500] = " a laptop computer" ;
	this.items_to_buy[600] = " a ping-pong table" ;
	this.items_to_buy[700] = " a bumper pool table" ;
	this.items_to_buy[800] = " 40 new DVDs" ;
	this.items_to_buy[900] = " 60 new music CDs" ;
	this.items_to_buy[1000] = " a theme park + hotel weekend for four people" ;

	// Display the What Can You Buy Box
	var element = document.getElementById("what_can_you_buy_container") ;
	element.style.display = "block" ;

	// Display the What Can You Buy Image
	var element = document.getElementById("results_what_can_this_buy") ;
	element.style.display = "block" ;

	// Find the element that we will insert the debug info into
	var element = document.getElementById("what_can_you_buy_box") ;
	if (annual_savings > 0) {
    	element.innerHTML = "<b>With the money you have saved,</b> you could buy " ;
    } else {
	    // No savings hide the savings results
	    var element = document.getElementById("what_can_you_buy_container") ;
	    element.style.display = "none" ;
	    var element = document.getElementById("results_what_can_this_buy") ;
	    element.style.display = "none" ;
    }
    
    // Variable that tracks if a comma should be displayed for the next set of results
    var items_displayed = 0 ;

    // Display the items to buy for the Thousands
	increment = Math.floor((annual_savings % 10000)*.001)   ;
	if (annual_savings >= 1000 && increment > 0) {
		element.innerHTML += this.items_to_buy[1000] ;
		items_displayed++ ;
	} 
	
    // Display the items to buy for the Hundreds
	increment = Math.floor((annual_savings % 1000)*.01)   ;
	if (annual_savings >= 100 && increment > 0) {
        if (items_displayed >= 1) {
            element.innerHTML += ", ";
        }
		element.innerHTML += this.items_to_buy[increment*100] ;
		items_displayed++ ;
	} 
	
    // Display the items to buy for the Tens
	increment = Math.floor((annual_savings % 100) * .1) ;
	if (annual_savings >= 10 && increment > 0) {
        if (items_displayed >= 1) {
            element.innerHTML += ", ";
        }
		element.innerHTML += this.items_to_buy[increment*10] ;
		items_displayed++ ;
	} 

    // Display the items to buy for the Ones
	var increment = Math.floor(annual_savings % 10);
	if (items_displayed >= 2) element.innerHTML += ", ";
	if (items_displayed >= 1) element.innerHTML += " and ";
	if (annual_savings >= 1 && increment > 0) { 
		element.innerHTML += this.items_to_buy[increment]+"!" ;
    } 
    else {
    	element.innerHTML += "a gel pen!";
    }
}

