window.onload = initAll;

function initAll()
{
    initTemps();
    initWeight();
    initLengths();
    initParallel();
    initSeries();
    initTC();
    //initHexRGB();
}

function clearForm(formId)
{
    var thisForm = document.getElementById(formId);
	for (i=0; i<thisForm.length; i++) 
    {
         thisForm[i].value = "";
    }
}


//TEMPERATURE

function initTemps()
{
    var theForm = document.getElementById('temps');
    //alert(theForm.length);
	for (i=0; i< theForm.length; i++) 
    {
        //alert (i);
        theForm[i].onclick = clearTemps;
        theForm[i].onkeyup = temps;
     }  
}

function clearTemps()
{
    clearForm('temps');
    return false;
}

function temps()
{
    var thename = this.id;
    var C = document.getElementById("C");
    var F = document.getElementById("F");

    switch (thename)
    {
        case "F":
        {
            C.value = Math.round(100/(212-32) * (F.value - 32 ));
            break;
        }
        case "C":
        {
            F.value = Math.round((212-32)/100 * C.value + 32);
            break;
        }
    }
    return false;
}


//WEIGHT

function initWeight()
{
    var theForm = document.getElementById('weight');
    //alert(theForm.length);
	for (i=0; i< theForm.length; i++) 
    {
        //alert (i);
        theForm[i].onclick = clearWeight;
        theForm[i].onkeyup = weight;
     }  
}

function clearWeight()
{
    clearForm('weight');
    return false;
}

function weight()
{
    var thename = this.id;
    var Pounds = document.getElementById("Pounds");
    var Kilograms = document.getElementById("Kilograms");

    switch (thename)
    {
        case "Pounds":
        {
            Kilograms.value = this.value/2.205;
            break;
        }
        case "Kilograms":
        {
            Pounds.value = this.value*2.205;
            break;
        }
    }
    return false;
}


//LENGTHS

function initLengths()
{
    var lengthForm = document.getElementById('length');
	for (i=0; i<lengthForm.length; i++) 
    {
        //alert (lengthForm[i].id);
        lengthForm[i].onclick = clearLengths;
        lengthForm[i].onkeyup = lengths;
     }
}

function clearLengths()
{
    clearForm('length');
    return false;
}

function lengths() 
{
/*
1 inch = 2.54cm; 
inches = cm/2.54; 
cm = inches*2.54

1 foot = 12 inches; 
feet = inches/12; 
inches = feet*12

1 meter = 100cm; 
meters = cm/100; 
cm = meters*100;

1 cm = 10mm; 
cm = mm/10; 
mm = cm*10;

1 meter = 1000mm;
meters = mm/1000;
mm = meters*1000;

1 mile = 5280 feet;
miles = feet/5280;
feet = miles*5280;

1 km = 0.621371192 miles;
km = miles/0.621371192;
miles = Km*0.621371192;

*/

var thename = this.id

var cm;
var inches;
var feet;
var mm;
var meters;
var yards;
var miles;
var km;

    switch (thename)
    {
        case "cm":
        {
            cm = document.getElementById('cm').value
            mm = cm*10;
            meters = cm/100;
            inches = cm / 2.54;
            feet = inches/12;
            yards = feet/3;
            miles = feet/5280;
            km = miles/0.621371192;
            break;
        }
        case "inches":
        {
            inches = document.getElementById('inches').value
            feet = inches/12;
            cm = inches*2.54;
            mm = cm*10;
            meters = cm/100;
            yards = feet/3;
            miles = feet/5280;
            km = miles/0.621371192;
            break;
        }
        case "feet":
        {
            feet = document.getElementById('feet').value
            inches = feet*12;
            cm = inches*2.54;
            mm = cm*10;
            meters = cm/100;
            yards = feet/3;
            miles = feet/5280;
            km = miles/0.621371192;            
            break;
        }
        case "mm":
        {
            mm = document.getElementById('mm').value
            cm = mm/10;
            meters = cm/100;
            inches = cm/2.54;
            feet = inches/12;
            yards = feet/3;
            miles = feet/5280;
            km = miles/0.621371192;
            break;
        }
        case "meters":
        {
            meters = document.getElementById('meters').value
            cm = meters*100;
            mm = cm*10;
            inches = cm/2.54;
            feet = inches/12;
            yards = feet/3;
            miles = feet/5280;
            km = miles/0.621371192;
            break;
        }
        case "yards":
        {
            yards = document.getElementById('yards').value
            feet = yards*3;
            inches = feet*12;
            cm = inches*2.54;
            meters = cm/100;
            mm = cm*10;
            miles = feet/5280;
            km = miles/0.621371192;
            break;
        }  
        case "miles":
        {
            miles = document.getElementById('miles').value
            km = miles/0.621371192;
            feet = miles*5280;
            inches = feet*12;
            cm = inches*2.54;
            mm = cm*10;
            meters = cm/100;
            yards = feet/3;            
            break;
        }
        case "km":
        {
            km = document.getElementById('km').value
            miles = km*0.621371192;
            feet = miles*5280;
            inches = feet*12;
            cm = inches*2.54;
            mm = cm*10;
            meters = cm/100;
            yards = feet/3; 
            break;
        }
    }        
    document.getElementById('cm').value = cm ;       
    document.getElementById('mm').value = mm;
    document.getElementById('meters').value = meters;
    document.getElementById('feet').value = feet;
    document.getElementById('inches').value = inches;
    document.getElementById('yards').value = yards;
    document.getElementById('miles').value = miles;
    document.getElementById('km').value = km;
    
    return false;
}


//PARALLEL RESISTORS

function initParallel()
{
    var theForm = document.getElementById('parallel');
	for (i=0; i< theForm.length; i++) 
    {
        theForm[i].onclick = clearParallel;
        theForm[i].onkeyup = calcParallel;
     }  
}

function clearParallel()
{
    this.value="";
    document.getElementById("RT").value="";
    return false;
}

function calcParallel()
{
/* 
1/RTotal = 1/R1 + 1/R2 + 1/R3 + 1/R4 + 1/R5
RTotal = 1/(1/R1 + 1/R2 + 1/R3 + 1/R4 + 1/R5)
R5 = 1/(1/RTotal - 1/R1 - 1/R2 - 1/R3 - 1/R4)
*/

    var thename = this.id;
    var R1 = document.getElementById("R1").value;
    var R2 = document.getElementById("R2").value;
    var R3 = document.getElementById("R3").value;
    var R4 = document.getElementById("R4").value;
    var R5 = document.getElementById("R5").value;
    var RT = document.getElementById("RT").value;
    
    var RArray = new Array (5);
    RArray[0] = R1;
    RArray[1] = R2;
    RArray[2] = R3;
    RArray[3] = R4;
    RArray[4] = R5;

    switch (thename)
    {
        case "R1": case "R2": case "R3": case "R4": case "R5":
        {
            subtot = 0;
            for (i=0; i <= 4; i++)
                {
                    if (RArray[i] > 0) {subtot = subtot + 1/RArray[i];}
                }
            RT = 1/subtot;
            document.getElementById("RT").value = RT;
            break;
        }
        case "RT":
        {
            subtot = 1/RT;
            for (i=0; i <= 3; i++)
                {
                    if (RArray[i] > 0) {subtot = subtot - 1/RArray[i];}
                }
            if (subtot > 0) {R5 = 1/subtot;} else if (subtot = 0) {R5 = 0;} else {R5 = 0;}
            document.getElementById("R5").value = R5;
            break;
        }
    }
    return false;
}


//SERIES RESISTORS

function initSeries()
{
    var theForm = document.getElementById('series');
	for (i=0; i< theForm.length; i++) 
    {
        theForm[i].onclick = clearSeries;
        theForm[i].onkeyup = calcSeries;
     }  
}

function clearSeries()
{
    this.value="";
    document.getElementById("Rc").value="";
    return false;
}

function calcSeries()
{
    var thename = this.id;
    var Ra = document.getElementById("Ra");
    var Rb = document.getElementById("Rb");
    var Rc = document.getElementById("Rc");

    switch (thename)
    {
        case "Ra": case "Rb":
        {
            Rc.value = parseInt(Ra.value) + parseInt(Rb.value);
            break;
        }
        case "Rc": 
        {
            Rb.value = parseInt(Rc.value) - parseInt(Ra.value);
            break;
        }
    }
    return false;
}    
    

//TIME CONSTANT

function initTC()
{
    document.getElementById('volts').onkeyup = SetVolts;
    document.getElementById('ohms').onkeyup = SetOhms;
    document.getElementById('farads').onkeyup = SetFarads;
    document.getElementById('time').onkeyup = SetTime;
    document.getElementById('ivolts').onkeyup = SetIvolts;
    
    document.getElementById('volts').onclick = clearthis;
    document.getElementById('ohms').onclick = clearthis;
    document.getElementById('farads').onclick = clearthis;
    document.getElementById('time').onclick = clearthis;
    document.getElementById('ivolts').onclick = clearthis;
    
     document.getElementById('calcohms').onclick = calcohms;
}

function clearthis() {this.value = '';}

function round(X) {
//round to 4 decimal places
return Math.round(X*10000)/10000;}

/*
function clearTimeConstants()
{
    Volts=0;
    Ohms=0;
    Farads=0;
    Time=0;
    Ivolts=0;
    document.getElementById('volts').value = 0;
    document.getElementById('ohms').value = 0;
    document.getElementById('farads').value = 0;
    document.getElementById('time').value = 0;
    document.getElementById('ivolts').value = 0;
}
*/

var Volts =0;
var Ohms=0;
var Farads=0;
var Time=0;
var Ivolts=0;

function getValues()
{
    Volts = parseFloat(document.getElementById('volts').value); 
    Ohms = parseFloat(document.getElementById('ohms').value);
    Farads = parseFloat(document.getElementById('farads').value); 
    Ivolts = parseFloat(document.getElementById('ivolts').value);
    Time = parseFloat(document.getElementById('time').value);
}

function checkIvolts()
{
    if (Ivolts >Volts && Volts >0 && Ivolts >0)
    {
        alert ("Instantaneous Voltage cannot be > Supply Voltage."); 
        return false;
    }
    return true;
}

function SetVolts()     
{ 
    getValues();
    tctime();
}
function SetOhms()      
{ 
    getValues();
    tctime();
}
function SetFarads()    
{ 
    getValues();
    tctime();
}
function SetIvolts()    
{ 
    getValues();
    tctime();

}
function SetTime()      
{ 
    getValues();
    tcivolts();
}

function calcohms()
{
    getValues();
    tcohms();
    return false;
}

function tctime()
{
    if (Volts >0 && Ohms >0 && Farads >0 && Ivolts >0 && checkIvolts) 
        {Time= -Math.log((Volts-Ivolts)/Volts)*Ohms*Farads;}
        Time = Math.round(Time);
        document.getElementById('time').value = Time;
}

function tcivolts()
{
    if (Volts >0 && Ohms >0 && Farads >0 && Time >0 && checkIvolts)
        {Ivolts= Volts-(Volts * Math.exp(-Time/(Ohms*Farads)));}
        Ivolts = round(Ivolts);
     document.getElementById('ivolts').value = Ivolts;
}

function tcohms()
{
    if (Volts >0 && Farads >0 && Time >0 && Ivolts >0 && checkIvolts)
        {Ohms= -Time/(Math.log((Volts-Ivolts)/Volts)*Farads);}
        document.getElementById('ohms').value = round(Ohms);
}

/*future functions
function tcfarads()
{
    if (Volts >0 && Ohms >0 && Time >0 && Ivolts >0 && checkIvolts)
        {Farads= -Time/(Math.log((Volts-Ivolts)/Volts)*Ohms);}
        document.getElementById('farads').value = Farads;
}

function tcvolts()
{
    if (Ohms >0 && Farads>0 && Time >0 && Ivolts >0 && checkIvolts)
        {Volts= -Ivolts/(Math.exp(-(1/(Ohms*Farads))* Time)-1);}
        document.getElementById('volts').value = Volts;
}

*/



/* RGB
function initHexRGB()
{
    document.getElementById('RGB').onkeyup = doHex;
    //document.getElementById('RGB').onclick = clearthis;
    document.getElementById('Hex').onkeyup = doRGB;
    //document.getElementById('Hex').onclick = clearthis;
    setBackColor(document.getElementById('color'),document.getElementById('hex').value);
}

function doHex()
{
    var rgb = document.getElementById('RGB').value;
    var rgbs = new Array();
    rgbs = rgb.split(",");
    if (rgbs.length > 0) {var R = rgbs[0].replace(' ','');} else {var R = '0';}
    if (rgbs.length > 1) {var G = rgbs[1].replace(' ','');} else {var G = '0';}
    if (rgbs.length > 2) {var B = rgbs[2].replace(' ','');} else {var B = '0';}
    //var B = (rbss.length>2) ? rgbs[2].replace(' ','') : '0';

    var hex = "#"+toHex(R)+toHex(G)+toHex(B);
    document.getElementById('Hex').value = hex;
    setBackColor(document.getElementById('color'),hex);
}

function toHex(N) 
{
    if (N==null) return "00";
    N=parseInt(N); 
    if (N==0 || isNaN(N) || N>255 || N<0) return "00";
    return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}

function doRGB()
{
    var hex = document.getElementById('Hex').value.replace(' ','');
    hex = hex.replace("#",'');
    while (hex.length < 6)
    {
       hex = hex + '0';
    }
    R = HexToR(hex);
    G = HexToG(hex);
    B = HexToB(hex);
    document.getElementById('RGB').value = R + ',' + G + ',' + B;
    setBackColor(document.getElementById('color'),hex);
}

function HexToR(h) 
{
    return parseInt(h.substring(0,2),16)
}
function HexToG(h) 
{
    return parseInt(h.substring(2,4),16)
}
function HexToB(h) 
{
    return parseInt(h.substring(4,6),16)
}

function setBackColor(obj,acolor)
{
    if (acolor.substring(0,1) != '#') {acolor = '#' + acolor; }
    while (acolor.length < 7)
    {
       acolor = acolor + '0';
    }
    try 
    {
        obj.style.backgroundColor = acolor;
    }
    catch(e)
    {}
}

function round(num, d) 
{ 
    n = Math.pow(10, !d ? 2 : d);
    return Math.round(num*n) / n;
}

*/