 function format(num)
 {
    var fnum = Math.round(num * 10.0);
    fnum = '' + (fnum / 10.0);
    if (fnum.indexOf(".") == -1) {
       fnum += '.0';
    }
    return fnum;
 }

 function CCswitch(on)
 {
    var dia = document.getElementById("dia");
    if (on) {
       dia.style.backgroundColor = "#0000ff";
    } else {
       dia.style.backgroundColor="#999999";
    }
 }
 
 function recalcAll(f)
 {
    lat = parseFloat(f.inLAT.value);
    set = parseFloat(f.inSET.value);
    eff = parseFloat(f.inEFF.value) / 100.0;

    ra = bx[5].val;
    oa = bx[0].val;

    x = ((ra*eff)-(ra*eff*eff)-(oa*eff)+oa);
    
    if (eff < 1) {
       x /= 1-(eff*eff);
       setVal(6, x);
       CCswitch(false);
       if (x + ((ra - x) * eff) > set) {
          x = lat;
          CCswitch(true);
       }
    } else {
       setVal(6, x);
    }
    y = ra - ((ra - x) * eff);
    sa = x + ((ra - x) * eff);
    ea = y - ((y - oa) * eff);

    setVal(0, oa);
    setVal(1, y);
    setVal(2, sa);
    setVal(3, ea);
    setVal(4, x);
    setVal(5, ra);

    return false;
 }

// Slider script by Ronald H. Jankowsky (http://rj-edv-beratung.de),
// parts of code (draglayer, movelayer) by DynamicDrive.com
// This script is free for use, please leave this notice intact
// Modified for B2E by Svemir Brkic (svemir@baltok.com)

var sPosition;
var showPerc = 0;
var moved = false;
document.onmousedown = dragLayer;
document.onmouseup = new Function(
 "dragMe=false;if (moved) {recalcAll(document.forms[0]); moved = false;}");
 
function hex(dec) {
  dec = Math.floor(dec);
  if (dec < 10) {
    return dec;
  } else {
    switch (dec) {
      case 10: return "a";
      case 11: return "b";
      case 12: return "c";
      case 13: return "d";
      case 14: return "e";
      default: return "f";
    }
  }
}
      
function chgBg() {
  kNum = parseInt(kObj.id.charAt(kObj.id.length - 1));
  bx[kNum].inp.value = showPerc;
  bx[kNum].out.value = showPerc;
  bx[kNum].val = showPerc;
  decPerc = (showPerc / 100.0) * 255;
  hexPerc = "" + hex(decPerc / 16) + hex(decPerc % 16);
  kObj.style.backgroundColor = "#" + hexPerc + "0000";  
}

// Drag and move engine (original code by DynamicDrive.com)
var dragMe = false, kObj, yPos, direction, temp2;

function moveLayer() {
  if (dragMe) {
    oldX = kObj.style.pixelLeft;
    kObj.style.pixelLeft = temp2 + event.clientX - xPos; 
    // Limit movement of knob to stay inside layer
   	if (kObj.style.pixelLeft > oldX) {
	    direction = "dn";
	  } else {
	    direction = "up";
	  }
   	if (kObj.style.pixelLeft < 1 && direction == "up") {
      kObj.style.pixelLeft = 0;
      direction = "dn";
    }
   	if (kObj.style.pixelLeft > 100 && direction == "dn") {
      kObj.style.pixelLeft = 100;
	    direction = "up";
    }
    // Set working variable 'showPerc' depending on 100 or 250 scaling
   	showPerc = kObj.style.pixelLeft;
    // The following line should be replaced by the function-call with the actual task to perform
   	chgBg();
    moved = true;
   	return false;
  }
}

function dragLayer() {
  if (event.srcElement.id.indexOf("knob") != -1)	{
    kNum = event.srcElement.id.charAt(4);
   	if ((kNum == '0') || (kNum == '5')) {
           dragMe = true;
           kObj = event.srcElement;
           temp2 = kObj.style.pixelLeft;
           xPos = event.clientX;
           document.onmousemove = moveLayer;
   	} else {
   	    if (--errs < 1) {
   	       alert("You may only modify the inputs (OA and RA) and Constants.");
   			  errs = 5;
   	    }
   	}
  }
}

function setCnt(num) {
  showPerc = Math.round(bx[num].val);
  bx[num].inp.value = format(bx[num].val);
  bx[num].out.value = format(bx[num].val);
  if (showPerc < 0) {
     bx[num].knb.style.pixelLeft = 0;
     decPerc = (Math.abs(showPerc) / 100.0) * 255;
     hexPerc = "" + hex(decPerc / 16) + hex(decPerc % 16);
     bx[num].knb.style.backgroundColor = "#0000" + hexPerc;
  } else if (showPerc < 101) {
     bx[num].knb.style.pixelLeft = showPerc;
     decPerc = (showPerc / 100.0) * 255;
     hexPerc = "" + hex(decPerc / 16) + hex(decPerc % 16);
     bx[num].knb.style.backgroundColor = "#" + hexPerc + "0000";
	   //alert(bx[num].nam + ": " + showPerc + ", " + decPerc + ", " + hexPerc);
  } else {
     bx[num].knb.style.pixelLeft = 100;
     decPerc = ((showPerc - 100) / 100.0) * 255;
     hexPerc = "" + hex(decPerc / 16) + hex(decPerc % 16);
     bx[num].knb.style.backgroundColor = "#ff" + hexPerc + "00";
  }
}

function setVal(num, val) {
  bx[num].val = val;
  setCnt(num);
}

function box(num, nam, val) {
  this.nam = nam;
  this.val = val;
  this.inp = document.getElementById("in" + nam);
  this.out = document.getElementById("up" + nam);
  this.knb = document.getElementById("knob" + num);
}

var bx = new Array;
var errs = 2;

function init() {
  bx[0] = new box(0, "OA", 45.0);
  bx[1] = new box(1, "Y",  65.0);
  bx[2] = new box(2, "SA", 65.0);
  bx[3] = new box(3, "EA", 55.0);
  bx[4] = new box(4, "X",  55.0);
  bx[5] = new box(5, "RA", 75.0);
  bx[6] = new box(6, "X0", 55.0);
  for (i = 0; i < 7; i++) {
    setCnt(i);
  }
  recalcAll(document.forms[0]);
}
