var num = 1
var dig = new Array(6)
var units = new Array(10)
units[0]=""
units[1]="one"
units[2]="two"
units[3]="three"
units[4]="four"
units[5]="five"
units[6]="six"
units[7]="seven"
units[8]="eight"
units[9]="nine"
var teens = new Array(10)
teens[0]="ten"
teens[1]="eleven"
teens[2]="twelve"
teens[3]="thirteen"
teens[4]="fourteen"
teens[5]="fifteen"
teens[6]="sixteen"
teens[7]="seventeen"
teens[8]="eighteen"
teens[9]="nineteen"
var tens = new Array(10)
tens[0]=""
tens[1]="ten"
tens[2]="twenty"
tens[3]="thirty"
tens[4]="forty"
tens[5]="fifty"
tens[6]="sixty"
tens[7]="seventy"
tens[8]="eighty"
tens[9]="ninety"

// Convert numbers

function convnum() {

  outlit = '<IMG SRC="/gifs/numbersystems/egypt/zero.gif">'
  if (num < 10000000 && num > 0) {
    power = 0
    outlit = ''
    wnum = num + ''
    for (i = wnum.length - 1; i >= 0; i--) { 
      digit = wnum.substring(i, i + 1)
      for (j = 1; j <= digit; j++) { 
        outlit = '<IMG SRC="/gifs/numbersystems/egypt/snum' + power + '.gif">' + outlit
      }
      power++   
    } 
  } 
  document.getElementById('outpeg').innerHTML = outlit

  outlit = '<IMG SRC="/gifs/numbersystems/babylon/num0.gif">'
  if (num < 100000 && num > 0) {
    wnum = parseInt(num)
    power = 0
    while (wnum > 0) { 
      work = wnum % 60 
      wnum = parseInt(wnum / 60)
      unts = work % 10 
      tns = work - unts 
      if (unts > 0) {outlit = '<IMG SRC="/gifs/numbersystems/babylon/num' + unts + '.gif">' + outlit}
      if (tns > 0) {outlit = '<IMG SRC="/gifs/numbersystems/babylon/num' + tns + '.gif">' + outlit}
      if (unts == 0 && tns == 0 && power > 0) {outlit = '<IMG SRC="/gifs/numbersystems/babylon/zero.gif">' + outlit}
      power++
    } 
  }
  document.getElementById('outpbab').innerHTML = outlit

  outlit = '<IMG SRC="/gifs/numbersystems/machine/none.gif">'
  if (num < 10000000 && num >= 0) {
    outlit = '<IMG SRC="/gifs/numbersystems/machine/frame.gif"><BR>'
    wnum = "000000" + num
    wnum = wnum.substring(wnum.length-7,wnum.length)
    for (i = 10; i >= 0; i--) { 
      outlit = outlit + '<IMG SRC="/gifs/numbersystems/machine/bar.gif">'
      for (j = 0; j <= 6; j++) { 
        if (i == wnum.substring(j, j + 1)) {outlit = outlit + '<IMG SRC="/gifs/numbersystems/machine/bar.gif">'}
        else {outlit = outlit + '<IMG SRC="/gifs/numbersystems/machine/bead.gif">'}
      }
      outlit = outlit + '<IMG SRC="/gifs/numbersystems/machine/bar.gif"><BR>'
    }
    outlit = outlit + '<IMG SRC="/gifs/numbersystems/machine/frame.gif">'
  } 
  document.getElementById('outpaba').innerHTML = outlit

  outlit = '<IMG SRC="/gifs/numbersystems/greek/slet0.gif">'
  if (num < 1000 && num > 0) {
    power = 1
    outlit = ''
    wnum = num + ''
    for (i = wnum.length - 1; i >= 0; i--) { 
      digit = power * parseInt(wnum.substring(i, i + 1))
      if (digit > 0) {outlit = '<IMG SRC="/gifs/numbersystems/greek/slet' + digit + '.gif">' + outlit}
      power = power * 10   
    } 
  }
  document.getElementById('outpgr').innerHTML = outlit

  ol = '&nbsp;'
  if (num < 4000 && num > 0) {
    ol = ''
    wnum = num + ''
    i = wnum.length
    i--; d = parseInt(wnum.substring(i, i + 1))
    if (d==1) {ol = 'I'} else if (d==2) {ol = 'II'} else if (d==3) {ol = 'III'}
    else if (d==4) {ol = 'IV'} else if (d==5) {ol = 'V'} else if (d==6) {ol = 'VI'}
    else if (d==7) {ol = 'VII'} else if (d==8) {ol = 'VIII'} else if (d==9) {ol = 'IX'}
    i--; d = parseInt(wnum.substring(i, i + 1))
    if (d==1) {ol = 'X' + ol} else if (d==2) {ol = 'XX' + ol} else if (d==3) {ol = 'XXX' + ol}
    else if (d==4) {ol = 'XL' + ol} else if (d==5) {ol = 'L' + ol} else if (d==6) {ol = 'LX' + ol}
    else if (d==7) {ol = 'LXX' + ol} else if (d==8) {ol = 'LXXX' + ol} else if (d==9) {ol = 'XC' + ol}
    i--; d = parseInt(wnum.substring(i, i + 1))
    if (d==1) {ol = 'C' + ol} else if (d==2) {ol = 'CC' + ol} else if (d==3) {ol = 'CCC' + ol}
    else if (d==4) {ol = 'CD' + ol} else if (d==5) {ol = 'D' + ol} else if (d==6) {ol = 'DC' + ol}
    else if (d==7) {ol = 'DCC' + ol} else if (d==8) {ol = 'DCCC' + ol} else if (d==9) {ol = 'CM' + ol}
    i--; d = parseInt(wnum.substring(i, i + 1))
    if (d==1) {ol = 'M' + ol} else if (d==2) {ol = 'MM' + ol} else if (d==3) {ol = 'MMM' + ol}
  }
  document.getElementById('outprom').innerHTML = ol

  outlit = ''
  if (num < 8000 && num >= 0) {
    wnum = parseInt(num)
    while (wnum > 0) { 
      work = wnum % 20 
      wnum = parseInt(wnum / 20)
      unts = work % 5
      fives = parseInt(work / 5)
      if (unts==0 && fives==0) {lit = '<IMG SRC="/gifs/numbersystems/maya/sshell.gif">'}
      else {
        lit = ''
        bl = 3 - fives; if (unts==0) {bl++}
        for (i = 1; i <= bl; i++) {lit = lit + '<IMG SRC="/gifs/numbersystems/maya/ssp.gif"><BR>'}
        for (i = 1; i <= unts; i++) {lit = lit + '<IMG SRC="/gifs/numbersystems/maya/sdot.gif">'}
        if (unts>0) {lit = lit + '<BR>'}
        for (i = 1; i <= fives; i++) {lit = lit + '<IMG SRC="/gifs/numbersystems/maya/sline.gif"><BR>'}
      }
      outlit = '<IMG SRC="/gifs/numbersystems/maya/ssp.gif"><BR>' + lit + outlit
    } 
  }
  if (num==0) {outlit='<IMG SRC="/gifs/numbersystems/maya/sshell.gif">'}
  document.getElementById('outpmay').innerHTML = outlit

  power = 1
  outlit = ''
  if (num < 100000 && num >= 0) {
    num = parseInt(num)
    wnum = num + ''
    len = wnum.length - 1
    for (i = len; i >= 0; i--) { 
      digit = wnum.substring(i, i + 1)
      if (digit!='0') {
        if (i < len) {outlit = '<IMG SRC="/gifs/numbersystems/china/chin' + power + '.gif">' + outlit}
        if (num<9 || num>19 || i==len) {outlit = '<IMG SRC="/gifs/numbersystems/china/chin' + digit + '.gif">' + outlit}
      }
      power = power * 10   
    } 
  }
  if (num==0) {outlit='<IMG SRC="/gifs/numbersystems/china/zero.gif">'}
  document.getElementById('outpchin').innerHTML = outlit

  outlit = ''
  if (num >= 0) {
    wnum = parseInt(num)
    power = 0
    while (wnum > 0) { 
      work = (wnum % 2) + ''
      outlit = work + outlit
      wnum = parseInt(wnum / 2)
    } 
  }
  document.getElementById('outpbin').innerHTML = outlit

  outlit = ''
  if (num >= 0) {
    wnum = parseInt(num)
    power = 0
    while (wnum > 0) { 
      work = (wnum % 16) + ''
      if (work==10) {work = 'A'} else if (work==11) {work = 'B'} else if (work==12) {work = 'C'}
      else if (work==13) {work = 'D'} else if (work==14) {work = 'E'} else if (work==15) {work = 'F'}
      outlit = work + outlit
      wnum = parseInt(wnum / 16)
    } 
  }
  document.getElementById('outphex').innerHTML = outlit

  outlit = ''
  if (num<1000000000000000) {
    wnum = num + ""; n = 15 - wnum.length
    for (i = 1; i <= n; i++) {wnum="0" + wnum} 
    nhun  = parseInt(wnum.substring(12,15),10)
    nthou = parseInt(wnum.substring(9,12),10)
    nmil  = parseInt(wnum.substring(6,9),10)
    nbil  = parseInt(wnum.substring(3,6),10)
    ntril = parseInt(wnum.substring(0,3),10)
    outlit = num999(nhun)
    if (nthou > 0) {
      x = " thousand"
      if (nhun > 99) {x=x+","}
      else if (nhun > 0) {x=x+" and"}
      outlit = num999(nthou) + x + " " + outlit
    }
    if (nmil > 0) {
      x = " million"
      if (nthou > 0 || nhun > 99) {x=x+","}
      else if (nhun > 0) {x=x+" and"}
      outlit = num999(nmil) + x + " " + outlit
    }
    if (nbil > 0) {
      x = " billion"
      if (nmil > 0 || nthou > 0 || nhun > 99) {x=x+","}
      else if (nhun > 0) {x=x+" and"}
      outlit = num999(nbil) + x + " " + outlit
    }
    if (ntril > 0) {
      x = " trillion"
      if (nbil > 0 || nmil > 0 || nthou > 0 || nhun > 99) {x=x+","}
      else if (nhun > 0) {x=x+" and"}
      outlit = num999(ntril) + x + " " + outlit
    }
    if (wnum==0) {outlit='zero'}
  } 
  document.getElementById('outpword').innerHTML = outlit

}

// Convert number up to 1000

function num999(nabc) {

  nabc = nabc+""; n = 3 - nabc.length
  for (i = 1; i <= n; i++) {nabc="0"+nabc} 

  na = parseInt(nabc.substring(2,3),10)
  nb = parseInt(nabc.substring(1,2),10)
  nc = parseInt(nabc.substring(0,1),10)

  lit = units[na]
  if (nb==0 && nc==0) {return lit}

  if (tens[nb]=="ten") {lit = teens[na]} else {lit = tens[nb] + " " + lit}
  if (nc==0) {return lit}

  if (na > 0 || nb > 0) {lit = " and " + lit} 
  lit = units[nc] + " hundred " + lit 

  return lit

}
