/*
  Solar Calculators.  Author Joe McCormack.  www.virtualsecrets.com.
*/
/* Panel Calculation */
function solarpanel_calculate() {
var compiled = "";
var solarpanel_volts = document.getElementById("solarpanel_volts").value;
var solarpanel_watts = document.getElementById("solarpanel_watts").value;
var solarpanel_amps = document.getElementById("solarpanel_amps").value;
if (solarpanel_volts > 0 && solarpanel_watts > 0 && solarpanel_amps > 0) { compiled = "For this calculator to work, only two values should be supplied."; }
else {
      if (solarpanel_volts > 0 && solarpanel_watts > 0) {
							 compiled = "The solar panel with " + solarpanel_volts + " Volts and " + solarpanel_watts + " Watts has " + parseFloat(solarpanel_watts / solarpanel_volts).toFixed(2) + " Amps.";
						        }
      else if (solarpanel_volts > 0 && solarpanel_amps > 0) {
							     compiled = "The solar panel with " + solarpanel_volts + " Volts and " + solarpanel_amps + " Amps has " + parseFloat(solarpanel_volts * solarpanel_amps).toFixed(2) + " Watts.";
						            }
      else if (solarpanel_watts > 0 && solarpanel_amps > 0) {
							     compiled = "The solar panel with " + solarpanel_watts + " Watts and " + solarpanel_amps + " Amps has " + parseFloat(solarpanel_watts / solarpanel_amps).toFixed(2) + " Volts.";
							    }
      else { compiled = "There does not seem to be enough information to perform a calculation."; }
     }
document.getElementById("solarpanel_solution").innerHTML = compiled;
}
function solarpanel_reset() {
document.getElementById("solarpanel_volts").value = 12;
document.getElementById("solarpanel_watts").value = 100;
document.getElementById("solarpanel_amps").value = 0;
document.getElementById("solarpanel_solution").innerHTML = "The solar panel with 12 Volts and 100 Watts has 8.33 Amps.";
}
/* Device Calculation */
var device_volts = 0;
var device_watts = 0;
var device_amps = 0;
function device_calculate() {
var compiled = "";
device_volts = document.getElementById("device_volts").value;
device_watts = document.getElementById("device_watts").value;
device_amps = document.getElementById("device_amps").value;
if (device_volts > 0 && device_watts > 0 && device_amps > 0) { compiled = "For this calculator to work, only two values should be supplied."; }
else {
      if (device_volts > 0 && device_watts > 0) {
						 device_amps = parseFloat(device_watts / device_volts).toFixed(2);
						 compiled = "The device with " + device_volts + " Volts and " + device_watts + " Watts has " + device_amps + " Amps.";
						}
      else if (device_volts > 0 && device_amps > 0) {
						     device_watts = parseFloat(device_volts * device_amps).toFixed(2);
						     compiled = "The device with " + device_volts + " Volts and " + device_amps + " Amps has " + device_watts + " Watts.";
						    }
      else if (device_watts > 0 && device_amps > 0) {
						     device_volts = parseFloat(device_watts / device_amps).toFixed(2);
						     compiled = "The device with " + device_watts + " Watts and " + device_amps + " Amps has " + device_volts + " Volts.";
						    }
      else { compiled = "There does not seem to be enough information to perform a calculation."; }
     }
document.getElementById("device_solution").innerHTML = compiled;
}
function device_reset() {
device_volts = 12;
device_watts = 100;
device_amps = 8.33;
document.getElementById("device_volts").value = 12;
document.getElementById("device_watts").value = 100;
document.getElementById("device_amps").value = 0;
document.getElementById("device_solution").innerHTML = "The device with 12 Volts and 100 Watts has 8.33 Amps.";
}
function device_pass() {
var compiled = "";
var total_volts = 0;
var total_watts = 0;
var total_amps = 0;
var d1_volts = 0; var d1_watts = 0; var d1_amps = 0;
var d2_volts = 0; var d2_watts = 0; var d2_amps = 0;
var d3_volts = 0; var d3_watts = 0; var d3_amps = 0;
var d4_volts = 0; var d4_watts = 0; var d4_amps = 0;
var d5_volts = 0; var d5_watts = 0; var d5_amps = 0;
var d6_volts = 0; var d6_watts = 0; var d6_amps = 0;
var d7_volts = 0; var d7_watts = 0; var d7_amps = 0;
var d8_volts = 0; var d8_watts = 0; var d8_amps = 0;
var d9_volts = 0; var d9_watts = 0; var d9_amps = 0;
var d10_volts = 0; var d10_watts = 0; var d10_amps = 0;
if (device_volts > 0 && device_watts > 0 && device_amps > 0) {
							      if (d10_volts > 0) {
										  compiled = "No more open slots exist under the \"Device Load Calculator\" to add to.";
										 }
							      else {
								    if (document.getElementById("d1_volts").value == 0) {
															 d1_volts = device_volts; document.getElementById("d1_volts").value = d1_volts;
															 d1_watts = device_watts; document.getElementById("d1_watts").value = d1_watts;
															 d1_amps = device_amps; document.getElementById("d1_amps").value = d1_amps;
															}
								    else if (document.getElementById("d2_volts").value == 0) {
															      d2_volts = device_volts; document.getElementById("d2_volts").value = d2_volts;
															      d2_watts = device_watts; document.getElementById("d2_watts").value = d2_watts;
															      d2_amps = device_amps; document.getElementById("d2_amps").value = d2_amps;
															     }
								    else if (document.getElementById("d3_volts").value == 0) {
															      d3_volts = device_volts; document.getElementById("d3_volts").value = d3_volts;
															      d3_watts = device_watts; document.getElementById("d3_watts").value = d3_watts;
															      d3_amps = device_amps; document.getElementById("d3_amps").value = d3_amps;
															     }
								    else if (document.getElementById("d4_volts").value == 0) {
															      d4_volts = device_volts; document.getElementById("d4_volts").value = d4_volts;
															      d4_watts = device_watts; document.getElementById("d4_watts").value = d4_watts;
															      d4_amps = device_amps; document.getElementById("d4_amps").value = d4_amps;
															     }
								    else if (document.getElementById("d5_volts").value == 0) {
															      d5_volts = device_volts; document.getElementById("d5_volts").value = d5_volts;
															      d5_watts = device_watts; document.getElementById("d5_watts").value = d5_watts;
															      d5_amps = device_amps; document.getElementById("d5_amps").value = d5_amps;
															     }
								    else if (document.getElementById("d6_volts").value == 0) {
															      d6_volts = device_volts; document.getElementById("d6_volts").value = d6_volts;
															      d6_watts = device_watts; document.getElementById("d6_watts").value = d6_watts;
															      d6_amps = device_amps; document.getElementById("d6_amps").value = d6_amps;
															     }
								    else if (document.getElementById("d7_volts").value == 0) {
															      d7_volts = device_volts; document.getElementById("d7_volts").value = d7_volts;
															      d7_watts = device_watts; document.getElementById("d7_watts").value = d7_watts;
															      d7_amps = device_amps; document.getElementById("d7_amps").value = d7_amps;
															     }
								    else if (document.getElementById("d8_volts").value == 0) {
															      d8_volts = device_volts; document.getElementById("d8_volts").value = d8_volts;
															      d8_watts = device_watts; document.getElementById("d8_watts").value = d8_watts;
															      d8_amps = device_amps; document.getElementById("d8_amps").value = d8_amps;
															     }
								    else if (document.getElementById("d9_volts").value == 0) {
															      d9_volts = device_volts; document.getElementById("d9_volts").value = d9_volts;
															      d9_watts = device_watts; document.getElementById("d9_watts").value = d9_watts;
															      d9_amps = device_amps; document.getElementById("d9_amps").value = d9_amps;
															     }
								    else {
									  d10_volts = device_volts; document.getElementById("d10_volts").value = d10_volts;
									  d10_watts = device_watts; document.getElementById("d10_watts").value = d10_watts;
									  d10_amps = device_amps; document.getElementById("d10_amps").value = d10_amps;
									 }
								    compiled = "Results passed over to \"Device Load Calculator\".";
								    d1_volts = document.getElementById("d1_volts").value;
								    d1_watts = document.getElementById("d1_watts").value;
								    d1_amps = document.getElementById("d1_amps").value;
								    d2_volts = document.getElementById("d2_volts").value;
								    d2_watts = document.getElementById("d2_watts").value;
								    d2_amps = document.getElementById("d2_amps").value;
								    d3_volts = document.getElementById("d3_volts").value;
								    d3_watts = document.getElementById("d3_watts").value;
								    d3_amps = document.getElementById("d3_amps").value;
								    d4_volts = document.getElementById("d4_volts").value;
								    d4_watts = document.getElementById("d4_watts").value;
								    d4_amps = document.getElementById("d4_amps").value;
								    d5_volts = document.getElementById("d5_volts").value;
								    d5_watts = document.getElementById("d5_watts").value;
								    d5_amps = document.getElementById("d5_amps").value;
								    d6_volts = document.getElementById("d6_volts").value;
								    d6_watts = document.getElementById("d6_watts").value;
								    d6_amps = document.getElementById("d6_amps").value;
								    d7_volts = document.getElementById("d7_volts").value;
								    d7_watts = document.getElementById("d7_watts").value;
								    d7_amps = document.getElementById("d7_amps").value;
								    d8_volts = document.getElementById("d8_volts").value;
								    d8_watts = document.getElementById("d8_watts").value;
								    d8_amps = document.getElementById("d8_amps").value;
								    d9_volts = document.getElementById("d9_volts").value;
								    d9_watts = document.getElementById("d9_watts").value;
								    d9_amps = document.getElementById("d9_amps").value;
								    d10_volts = document.getElementById("d10_volts").value;
								    d10_watts = document.getElementById("d10_watts").value;
								    d10_amps = document.getElementById("d10_amps").value;
								    total_watts = parseFloat(d1_watts) + parseFloat(d2_watts) + parseFloat(d3_watts) + parseFloat(d4_watts) + parseFloat(d5_watts) + parseFloat(d6_watts) + parseFloat(d7_watts) + parseFloat(d8_watts) + parseFloat(d9_watts) + parseFloat(d10_watts);
								    total_amps = parseFloat(d1_amps) + parseFloat(d2_amps) + parseFloat(d3_amps) + parseFloat(d4_amps) + parseFloat(d5_amps) + parseFloat(d6_amps) + parseFloat(d7_amps) + parseFloat(d8_amps) + parseFloat(d9_amps) + parseFloat(d10_amps);
								    total_volts = d1_volts;
								    if (d2_volts > total_volts) { total_volts = d2_volts; }
								    if (d3_volts > total_volts) { total_volts = d3_volts; }
								    if (d4_volts > total_volts) { total_volts = d4_volts; }
								    if (d5_volts > total_volts) { total_volts = d5_volts; }
								    if (d6_volts > total_volts) { total_volts = d6_volts; }
								    if (d7_volts > total_volts) { total_volts = d7_volts; }
								    if (d8_volts > total_volts) { total_volts = d8_volts; }
								    if (d9_volts > total_volts) { total_volts = d9_volts; }
								    if (d10_volts > total_volts) { total_volts = d10_volts; }
								    document.getElementById("total_volts").value = total_volts;
								    document.getElementById("total_watts").value = total_watts;
								    document.getElementById("total_amps").value = total_amps;
								   }
							     }
else { compiled = "Perform a calculation before trying to pass over results to the \"Device Load Calculator\"."; }
document.getElementById("device_solution").innerHTML = compiled;
}
function load_reset() {
document.getElementById("d1").value = "Device #1 (edit)";
document.getElementById("d1_volts").value = 0;
document.getElementById("d1_watts").value = 0;
document.getElementById("d1_amps").value = 0;
document.getElementById("d2").value = "Device #2 (edit)";
document.getElementById("d2_volts").value = 0;
document.getElementById("d2_watts").value = 0;
document.getElementById("d2_amps").value = 0;
document.getElementById("d3").value = "Device #3 (edit)";
document.getElementById("d3_volts").value = 0;
document.getElementById("d3_watts").value = 0;
document.getElementById("d3_amps").value = 0;
document.getElementById("d4").value = "Device #4 (edit)";
document.getElementById("d4_volts").value = 0;
document.getElementById("d4_watts").value = 0;
document.getElementById("d4_amps").value = 0;
document.getElementById("d5").value = "Device #5 (edit)";
document.getElementById("d5_volts").value = 0;
document.getElementById("d5_watts").value = 0;
document.getElementById("d5_amps").value = 0;
document.getElementById("d6").value = "Device #6 (edit)";
document.getElementById("d6_volts").value = 0;
document.getElementById("d6_watts").value = 0;
document.getElementById("d6_amps").value = 0;
document.getElementById("d7").value = "Device #7 (edit)";
document.getElementById("d7_volts").value = 0;
document.getElementById("d7_watts").value = 0;
document.getElementById("d7_amps").value = 0;
document.getElementById("d8").value = "Device #8 (edit)";
document.getElementById("d8_volts").value = 0;
document.getElementById("d8_watts").value = 0;
document.getElementById("d8_amps").value = 0;
document.getElementById("d9").value = "Device #9 (edit)";
document.getElementById("d9_volts").value = 0;
document.getElementById("d9_watts").value = 0;
document.getElementById("d9_amps").value = 0;
document.getElementById("d10").value = "Device #10 (edit)";
document.getElementById("d10_volts").value = 0;
document.getElementById("d10_watts").value = 0;
document.getElementById("d10_amps").value = 0;
document.getElementById("total_volts").value = 0;
document.getElementById("total_watts").value = 0;
document.getElementById("total_amps").value = 0;
}
function load_pass() {
bank_selection = 1;
document.getElementById("bank_choice").selectedIndex = 1;
document.getElementById("bank_hours").value = 24;
document.getElementById("bank_watts").value = document.getElementById("total_watts").value;
document.getElementById("bank_volts").value = document.getElementById("total_volts").value;
document.getElementById("bank_amps").value = document.getElementById("total_amps").value;
bank_start(1);
}
/* Bank Calculation */
var bank_selection = 0;
function bank_start(choice) {
var compiled = "";
if (choice == 1) {
		  bank_selection = 1;
		  document.getElementById("bank_buttons").style.display = "block";
		  document.getElementById("bank_hours_range").style.display = "block";
		  document.getElementById("bank_watts_volts").style.display = "block";
		  document.getElementById("bank_amps_only").style.display = "none";
		  bank_calculate();
		 }
else {
      bank_selection = 2;
      document.getElementById("bank_buttons").style.display = "block";
      document.getElementById("bank_hours_range").style.display = "block";
      document.getElementById("bank_amps_only").style.display = "block";
      document.getElementById("bank_watts_volts").style.display = "none";
      bank_calculate();
     }
}
function bank_calculate() {
var compiled = ""; var value = 0;
var sa_watts = 0; var sa_amps = 0;
var bank_hours = document.getElementById("bank_hours").value;
var bank_watts = document.getElementById("bank_watts").value;
var bank_volts = document.getElementById("bank_volts").value;
var bank_amps = document.getElementById("bank_amps").value;
var solararray_hours = document.getElementById("solararray_hours").value;
if (bank_hours > 0) {
		     if (bank_selection == 1) {
					       if (bank_watts > 0 && bank_volts > 0) {
										      /* Battery Bank Size */
										      value = parseFloat(((((bank_watts * bank_hours) / .85) / bank_volts) / .5)).toFixed(2);
										      compiled = "The battery bank should be rated at " + bank_volts + " Volts and " + value + " Ah in order to supply power for " + bank_hours + " hours.";
										      /* Solar Array Size */
										      sa_watts = parseFloat((bank_hours * bank_watts) / solararray_hours).toFixed(2);
										      sa_amps = parseFloat(((bank_watts / bank_volts) * bank_hours) / solararray_hours).toFixed(2);
										      document.getElementById("solararray_volts").value = bank_volts;
										      document.getElementById("solararray_watts").value = sa_watts;
										      document.getElementById("solararray_amps").value = sa_amps;
										      ar_calculate();
										     }
					       else { compiled = "The number of watts and volts is needed."; }
					      }
		     else {
			   if (bank_amps > 0) {
					       /* Battery Bank Size */
					       value = parseFloat((((bank_amps * bank_hours) / .85) / .5)).toFixed(2);
					       compiled = "The battery bank should be rated at " + value + " Ah in order to supply power for " + bank_hours + " hours.  Voltage rating is unknown.";
					       /* Solar Array Size */
					       sa_amps = parseInt((bank_hours * bank_amps) / solararray_hours).toFixed(2);
					       document.getElementById("solararray_volts").value = "?";
					       document.getElementById("solararray_watts").value = "?";
					       document.getElementById("solararray_amps").value = sa_amps;
					       ar_calculate();
					      }
			   else { compiled = "The number of Amps drawn by device(s) is missing."; }
			  }
		    }
else { compiled = "The number of hours of operation is missing."; }
document.getElementById("bank_solution").innerHTML = compiled;
}
function bank_reset() {
bank_selection = 0;
document.getElementById("bank_buttons").style.display = "none";
document.getElementById("bank_hours_range").style.display = "none";
document.getElementById("bank_watts_volts").style.display = "none";
document.getElementById("bank_amps_only").style.display = "none";
document.getElementById("bank_choice").selectedIndex = 0;
document.getElementById("bank_hours").value = 24;
document.getElementById("bank_watts").value = 48;
document.getElementById("bank_volts").value = 12;
document.getElementById("bank_amps").value = 4;
document.getElementById("solararray_volts").value = 0;
document.getElementById("solararray_watts").value = 0;
document.getElementById("solararray_amps").value = 0;
document.getElementById("bank_solution").innerHTML = "";
}
/* Array Calculator */
function ar_calculate() {
var compiled = "";
var sa_watts = 0; var sa_amps = 0;
var bank_hours = document.getElementById("bank_hours").value;
var bank_watts = document.getElementById("bank_watts").value;
var bank_volts = document.getElementById("bank_volts").value;
var bank_amps = document.getElementById("bank_amps").value;
var solararray_hours = document.getElementById("solararray_hours").value;
var solararray_output = document.getElementById("solararray_output").value;
var solararray_volts = document.getElementById("solararray_volts").value;
var solararray_watts = document.getElementById("solararray_watts").value;
var solararray_amps = document.getElementById("solararray_amps").value;
if (solararray_hours > 0 && solararray_output > 0) {
						    if (solararray_volts > 0 && solararray_watts > 0) {
												       sa_watts = parseFloat((bank_hours * bank_watts) / solararray_hours).toFixed(2);
												       var sa_panels = parseFloat(sa_watts / solararray_output).toFixed(2);
												       compiled = sa_panels + " solar panels, each rated at " + solararray_output + " Watts and " + bank_volts + " Volts, are needed to generate " + solararray_watts + " Watts of power per hour of full sunlight for the battery bank.";
												      }
						    else { compiled = "A collection of solar panels with a cummulative output of " + solararray_amps + " Amps per hour are needed."; }
						   }
else { compiled = "Hours of sunlight and watt rating of a single solar panel are needed."; }
document.getElementById("ar_solution").innerHTML = compiled;
}
function ar_reset() {
document.getElementById("solararray_hours").value = 5;
document.getElementById("solararray_output").value = 100;
document.getElementById("solararray_volts").value = 0;
document.getElementById("solararray_watts").value = 0;
document.getElementById("solararray_amps").value = 0;
document.getElementById("ar_solution").innerHTML = "";
}

