// ----------------------------------------------------------------
//
// set_numeric.js
//
// JavaScript code for numeric widget (included by widgets.php)
//
// 03/07/2005 - LZ - First release
//
// ----------------------------------------------------------------

/*
  TODO list, 'x' means already done:
    x add support for numeric in a table 
    x add upper and lower limits
*/

  // ----------------------------------------------------------------
  // redraw the numeric table
  function emit_numeric(val, i, d1, d2, op, butt, img, id, maxv, minv) {
    image_up = "&#x25b2;";
    image_dn = "&#x25bc;";
    // image_up = "<img align='bottom' src='"+img+"/up.png' border='0'>";
    // image_dn = "<img align='top' src='"+img+"/down.png' border='0'>";
    space = "<td><img width='3' src='"+img+"/transparent.png' border='0'></td>\n";
    par = ','+d1+','+d2;
    response = "<table border='0' cellpadding='0' cellspacing='1'>\n<tr align='center'>"+space;
    v = val[0] - 0;
    // emit upper arrows
    for (i=d1-1; i>=0; i--) {
      // do not emit arrows that would lead out of limits
      if ((maxv != "none") && (v + Math.pow(10, i) > maxv)) {
        response += space;
      }
      else {
        response += "<td><a href=javascript:set_"+id+"("+i+par+",'u1')>"+image_up+"</td>\n";
      }
    }
    if (d2>0) {
      response += space;
      for (i=1; i<=d2; i++) {
        // do not emit arrows that would lead out of limits
        if ((maxv != "none") && (v + Math.pow(10, -i) > maxv)) {
          response += space;
        }
        else {
          response += "<td><a href=javascript:set_"+id+"("+i+par+",'u2')>"+image_up+"</td>\n";
        }
      }
    }
    response += space;
    if (butt) {
      response += space;
    }
    response += "</tr>\n<tr>\n";
    // emit numeric value
    for (i=1; i<d1+d2+3-!d2; i++) {
      response += "<td>"+val[i]+"</td>\n";
    }
    response += space;
    if (butt) {
      response += "<td><input type='submit' value='submit'></td>";
    }
    response += "</tr>\n<tr>\n"+space;
    // emit lower arrows
    for (i=d1-1; i>=0; i--) {
      // do not emit arrows that would lead out of limits
      if ((minv != "none") && (v - Math.pow(10, i) < minv)) {
        response += space;
      }
      else {
        response += "<td><a href=javascript:set_"+id+"("+i+par+",'d1')>"+image_dn+"</td>\n";
      }
    }
    if (d2>0) {
      response += space;
      for (i=1; i<=d2; i++) {
        // do not emit arrows that would lead out of limits
        if ((minv != "none") && (v - Math.pow(10, -i) < minv)) {
          response += space;
        }
        else {
          response += "<td><a href=javascript:set_"+id+"("+i+par+",'d2')>"+image_dn+"</td>\n";
        }
      }
    }
    response += space;
    if (butt) {
      response += space;
    }
    response += "</tr>\n</table>\n";
    return response;
  }
  
  // ----------------------------------------------------------------
  // eval the new value for numeric table widget
  function eval_numeric(val, i, d1, d2, op) {
    s = new Object();
    if (op == 'u1') {
      val = val - 0 + Math.pow(10, i);
    }
    if (op == 'u2') {
      val = val - 0 + Math.pow(10, -i);
    }
    if (op == 'd1') {
      val = val - Math.pow(10, i);
    }
    if (op == 'd2') {
      val = val - Math.pow(10, -i);
    }
    v = ''; j=1;
    if (val < 0) {
      v = '-';
      s[j++] = v;
      val = -val;
    }
    else {
      s[j++] = '&nbsp;';
    }
    for (i=d1-1; i>=0; i--,j++) {
      // eval the i-th digit
      s[j] = '&nbsp;';
      z = Math.floor(val / Math.pow(10, i)) % 10;
      // add digit only if it isn't a leading zero
      if (z > 0 || (v != '' && v != '-') || i == 0) {
        v += z;
        s[j] = z;
      }
    }
    // add decimal digit
    if (d2 > 0) {
      v += '.';
      s[j++] = '.';
      for (i=1; i<=d2; i++,j++) {
        s[j] = Math.floor(val * Math.pow(10, i) + 0.000000001) % 10;
        v += s[j];
      }
    }
    s[0] = v;
    return s;
  }
  
  // ----------------------------------------------------------------
  // eval the new value for numeric text widget
  function set_numeric(val, i, d1, d2, op) {
    if (op == 'u1') {
      val = val - 0 + Math.pow(10, i);
    }
    if (op == 'u2') {
      val = val - 0 + Math.pow(10, -i);
    }
    if (op == 'd1') {
      val = val - Math.pow(10, i);
    }
    if (op == 'd2') {
      val = val - Math.pow(10, -i);
    }
    v = '';
    if (val < 0) {
      v = '-';
      val = -val;
    }
    for (i=d1-1; i>=0; i--) {
      // eval the i-th digit
      z = Math.floor(val / Math.pow(10, i)) % 10;
      // add digit only if it isn't a leading zero
      if (z > 0 || (v != '' && v != '-') || i == 0) {
        v += z;
      }
    }
    // add decimal digit
    if (d2 > 0) {
      v += '.';
      for (i=1; i<=d2; i++) {
        v += Math.floor(val * Math.pow(10, i) + 0.000000001) % 10;
      }
    }
    return v;
  }

