// LED series resistor functions
// copyright Simon Carter 2001-2008
// Please contact me via www.electronics2000.co.uk if you wish to use these
// and ensure this header block remains intact

//Array of common LED forward voltages

var commonvf = new Array()
commonvf[0] = 2			//standard
commonvf[1] = 2			//low current
commonvf[2] = 2			//high brightness
commonvf[3] = 1.8		//super bright
commonvf[4] = 1.8		//ultra bright
commonvf[5] = 3.3		//standard blue

//Array of common LED forward currents

var commonif = new Array()
commonif[0] = 20		//standard
commonif[1] = 2			//low current
commonif[2] = 30		//high brightness
commonif[3] = 30		//super bright
commonif[4] = 30		//ultra bright
commonif[5] = 30		//standard blue

function calculatecommon (obj) {
	HideTip("tipbox_1");
	if ((obj.supply_1.value == "") || (obj.supply_1.value == "-"))
		{SetText("res_1", "?");
		SetText("prefres_1", "?");}
	else{
		with (Math) {
			vin=abs(eval(obj.supply_1.value));
			v=commonvf[obj.ledtype_1.selectedIndex];
			i=calculatemult2(1,commonif[obj.ledtype_1.selectedIndex]);
			}
		if(vin<=v){
			ShowErrorTip("tipbox_1", "supply_1", "Supply voltage must be greater than LED forward voltage (" + v + " Volts)!");
			SetText("res_1", "?");
			SetText("prefres_1", "?");
			}
		else{
			r = (vin-v)/i;
			prefr = format((GetPrefRes(r, getCheckedValue(obj.prefseries))));
			r = format(r);
			SetText("res_1", r + " Ohms");
			SetText("prefres_1", prefr + " Ohms");
		}
	}
}

function calculateled (obj) {
	HideTip("tipbox_2");
	if((obj.supply_2.value=="") || (obj.ledVf_2.value=="") || (obj.ledIf_2.value=="") || (obj.supply_2.value=="-") || (obj.ledVf_2.value=="-") || (obj.ledIf_2.value=="-"))
		{SetText("res_2", "?");
		SetText("prefres_2", "?");}
	else{
		with (Math) {
			vin=abs(eval(obj.supply_2.value));
			v=abs(eval(obj.ledV2_2.value));
			i=calculatemult2(1,eval(obj.ledIf_2.value));
		}
		if(vin<=v){
			ShowErrorTip("tipbox_2", "supply_2", "Supply voltage must be greater than LED forward voltage (" + v + " Volts)!");
			SetText("res_2", "?");
			SetText("prefres_2", "?");
			}
		else{
			r=(vin-v)/i;
			prefr = format((GetPrefRes(r, getCheckedValue(obj.prefseries))));
			r=format(r);
			SetText("res_2", r + " Ohms");
			SetText("prefres_2", prefr + " Ohms");
			}
	}
}