/*	electric.js		3-5-2009		JavaScript
	Copyright (C)2008-09 Steven Whitney.
	Initially published by http://25yearsofprogramming.com.

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License (GPL)
	Version 3 as published by the Free Software Foundation.
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

JavaScript functions for the electrical calculation page.

*/
//----------------------------------------------------------------------------------------------
function OhmsLawCalc()
{
var Unknown;
var UnknownCount = 0;
var Volts = parseFloat(document.getElementById("Volts").value.replace(/[^0-9.\-\+eE]+/gi," "));
if(isNaN(Volts))
{
	Unknown = "Volts";
	UnknownCount++;
}
var Amps = parseFloat(document.getElementById("Amps").value.replace(/[^0-9.\-\+eE]+/gi," "));
if(isNaN(Amps))
{
	Unknown = "Amps";
	UnknownCount++;
}
var Ohms = parseFloat(document.getElementById("Ohms").value.replace(/[^0-9.\-\+eE]+/gi," "));
if(isNaN(Ohms))
{
	Unknown = "Ohms";
	UnknownCount++;
}

// Write parsed values back to their input boxes.
document.getElementById("Volts").value = Volts;
document.getElementById("Amps").value = Amps;
document.getElementById("Ohms").value = Ohms;

if(UnknownCount === 1)
{
	switch(Unknown)
	{
		case "Volts":
			Volts = Amps * Ohms;
			document.getElementById("Volts").value = Volts;
			break;

		case "Amps":
			Amps = Volts / Ohms;
			document.getElementById("Amps").value = Amps;
			break;

		case "Ohms":
			Ohms = Volts / Amps;
			document.getElementById("Ohms").value = Ohms;
			break;
	}
	document.getElementById("Watts").innerHTML = Volts * Amps;
}
else
{
	alert("Input error: Please make sure exactly one field is left blank.");
}
}
//----------------------------------------------------------------------------------------------
function CalcSeriesParallel()
{
// Strip out all chars that are illegal in a number.
// Then split at whitespace, treating multiple whitespace as one.
// Now raw[] contains a list of all the input numbers. 
var raw = document.getElementById("SeriesResistorsIn").value.replace(/[^0-9.\-\+eE]+/gi," ").split(/\s+/);
var src = new Array();			// raw with blank or illegal values omitted
var src2 = new Array();
var i, x;
for(i = 0 ; i < raw.length ; i++)
{
	x = parseFloat(raw[i]);
	if(!isNaN(x))
	{
		src.push(x);
		src2.push(1 / x);
	}
}
var stats = DescriptiveStatistics(src);	
document.getElementById("SeriesResistanceOut").innerHTML = stats["SumX"];
stats = DescriptiveStatistics(src2);	
document.getElementById("ParallelResistanceOut").innerHTML = 1 / stats["SumX"];
}
//----------------------------------------------------------------------------------------------

