/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Curt Turner http://www.turner3d.net
Modified for use at vk3khb.gak.net.au 06-2011 */

ohmStr=String.fromCharCode(937);
base10=Math.log(10);
function preLoad(){ // preload color-band images
arrCols=new Array();
srcBase=document.getElementById('sel1');
for(i=0;i<srcBase.length;i++){arrCols[i]=srcBase.options[i].value;}
arrImg=new Array();
for(i=0;i<arrCols.length;i++)
{for(j=0;j<3;j++)
 {arrImg[i*3+j]=new Image();
  arrImg[i*3+j].src='images/resistor/band_'+arrCols[i]+(j+1).toString()+'.jpg';
 }
}
pre1=new Image();
pre1.src='images/resistor/band_d3.jpg';
pre2=new Image();
pre2.src='images/resistor/band_s3.jpg';
pre3=new Image();
pre3.src='images/resistor/band_mult_s.jpg';
}
function selCol(what,band){ // display color-band image
eval("document.getElementById('band"+band+"').src='images/resistor/band_"+what.value+band+".jpg';");
}
function selTol(){ // display tolerance band, set tolerance numeric dropdown
document.getElementById('mult').src='images/resistor/band_mult_'+document.getElementById('tolerance').value+'.jpg';
document.getElementById('tolNum').selectedIndex=document.getElementById('tolerance').selectedIndex;
calc();
}
function tolRev(){ // display tolerance band, set tolerance color dropdown
document.getElementById('tolerance').selectedIndex=document.getElementById('tolNum').selectedIndex;
document.getElementById('mult').src='images/resistor/band_mult_'+document.getElementById('tolerance').value+'.jpg';
calc();
}
function calc(){ // calculate values from color selections
pow=document.getElementById('sel3').selectedIndex;
if(pow==10)pow=-1; //gold
if(pow==11)pow=-2; //silver
//note: Multiplied resistance value by 100 (pow+2), rounded to nearest integer, then divided
//  result by 100 to compensate for Javascript exponentiation errors (only need 2 significant digits)
res=Math.round((document.getElementById('sel1').selectedIndex*10+document.getElementById('sel2').selectedIndex)*Math.pow(10,pow+2))/100;
if(res>990000){document.getElementById('multSel').selectedIndex=2; res/=1000000;
} else if(res>990){ document.getElementById('multSel').selectedIndex=1; res/=1000;
} else {document.getElementById('multSel').selectedIndex=0;}

if(res>999){
res=res.toString();
res=res.substr(0,res.length-3)+','+res.substr(res.length-3,3);
}
tolOut=5*Math.pow(2,document.getElementById('tolerance').selectedIndex)
document.getElementById('txtNum').value=res;
}
function calcRev(){ // calculates color selections from numeric input - convert input to ohms
document.getElementById('errSpan').innerHTML='';
base=document.getElementById('txtNum').value;
if(parseFloat(base).toString()=='NaN')return false;
if(base<0){base=base*-1};// no neg #
if(base<0.01) {base=0};// zero #
multBy=Math.pow(1000,document.getElementById('multSel').selectedIndex);
res=base*multBy;
if((res<0)||(res>99000000000)){inputError();return false;}
raisedTo=Math.floor(Math.log(res)/base10);
res=Math.round(res/Math.pow(10,raisedTo-1))/10;
b1=Math.floor(res);//band1
b2=Math.round((res-b1)*10);//band2
if(b1==10){b1=1;b2=0;raisedTo+=1;}//1k

mIndex=raisedTo-1;//band3
if(mIndex==-1)mIndex=10;//gold
if(mIndex==-2)mIndex=11;//silver
if(mIndex==-3){mIndex=11;b2=b1;b1=0;}//u-ohms
document.getElementById('sel1').selectedIndex=b1;
selCol(document.getElementById('sel1'),1);
document.getElementById('sel2').selectedIndex=b2;
selCol(document.getElementById('sel2'),2);
document.getElementById('sel3').selectedIndex=mIndex;
selCol(document.getElementById('sel3'),3);
}
function inputError()
{for(i=1;i<4;i++)
 {
 eval("document.getElementById('sel"+i+"').selectedIndex=0;");
 eval("document.getElementById('band"+i+"').src='band_k"+i+".jpg';");
 }
 document.getElementById('errSpan').innerHTML="out of range";
}
