// script for operating a toggle pushbutton switch
var switch_off = new Image();
switch_off.src = "gifs/red_button_up.gif";
var switch_on = new Image();
switch_on.src = "gifs/red_button_dn.gif";
var switch_state = 0;
var output_no = 0;

function not_toggle(){
//
if (switch_state == 0 )
{
switch_state=1;
document.images.red_btn.src = switch_on.src;
run_led();
}
else
{
switch_state=0;
document.images.red_btn.src = switch_off.src;
}
//end
}

//script for 555 flasher circuit
var gled_off = new Image();
gled_off.src = "gifs/green_off.gif";
var gled_on = new Image();
gled_on.src = "gifs/green_on.gif";
var rled_off = new Image();
rled_off.src = "gifs/red_off.gif";
var rled_on = new Image();
rled_on.src = "gifs/red_on.gif";

var ra_value = 4700; //4.7K Ra Value
var rb_value = 100000; //100K Rb value
var ct_value = 0.00001; //10uF Ct value
//var period_555 =1000*(((4700+(2*(r2_value+1000)))*c3_value)/1.44);
//period of the pin 3 of 555 1/frequency f=1.44/(R4+2(R2+R3))C3
var charge_t = 1000*(0.693*(ra_value + rb_value)*ct_value);
//charge time is t1 = 0.693(Ra + Rb)Ct the output is high.
var discharge_t = 1000*(0.693*(rb_value)*ct_value);
//discharge time is t2 = 0.693(Rb*Ct) the output is low.


function run_led() {
counter_run(); //this is the positive edge i think?, hence advance counter
document.images.led_555.src = rled_on.src;
window.setTimeout('turn_off()',charge_t);
//end
}


function turn_on() {
counter_run(); //this is the positive edge i think?, hence advance counter
document.images.led_555.src = rled_on.src;
window.setTimeout('turn_off()',charge_t);
//end
}

function turn_off() {
var previous_led = "q" + (output_no-1) + "_led";
if (switch_state == 1)
{
document.images.led_555.src = rled_off.src;
window.setTimeout('turn_on()',discharge_t);
}
else
{
document.images.led_555.src = rled_off.src;
document.images[previous_led].src = gled_off.src;
output_no = 0 //reset counter
}
//end
}

//the below code for a pot for Rb resistor
function dec_rb(speedy) {
if (rb_value > 10000) {     //limit to 4.7K or discharge pin 7 transistor goes.
rb_value = rb_value-(1000*speedy);
}
rebuild_all();
document.rb_set.rb_dsp.value = rb_value/1000+"K";
//end
}

function inc_rb(speedy) {
if (rb_value < 10000000) {      //limit to 10M due to leakage
rb_value = rb_value+(1000*speedy);
}
rebuild_all();
document.rb_set.rb_dsp.value = rb_value/1000+"K";
//end
}

function rebuild_all() {
charge_t = 1000*(0.693*(ra_value + rb_value)*ct_value);
discharge_t = 1000*(0.693*(rb_value)*ct_value);
//end
}

//the code for IC CD4017 counter

function counter_run() {
var current_led = "q" + output_no + "_led";
var previous_led = "q" + (output_no-1) + "_led";
if (output_no < 10){
document.images[current_led].src = gled_on.src;
if (output_no > 0){
document.images[previous_led].src = gled_off.src;
}
output_no = output_no+1; // increment counter
}
else
{
document.images[previous_led].src = gled_off.src;
output_no = 0; //reset counter
counter_run();
}
//end
}
























