/* ------------------------------------------------------------------
 *
 * ordering by Martin Vit (c) 2002
 * version 2002.10.16
 * usage: see example below
 *
 * ------------------------------------------------------------------ */

//constructor
function cntObj(map, sep)
{
  //params (implicit init)
  this.tbl = new Array(10);
  this.map = '1';
  this.sep = '.';
  this.last_level = 0;
  this.status;

  //methods
  this.setMap    = cntSetMap;
  this.setSep    = cntSetSep;
  this.setMapSep = cntSetMapSep;
  this.reset     = cntReset;
  this.getMapLetter = cntGetMapLetter;
  this.getMap    = cntGetMap;
  this.getSep    = cntGetSep;
  this.number    = cntNumber;
  this.getStatus = cntGetStatus;
  this.setStatus = cntSetStatus;
  this.clone     = cntClone;
  this.store     = cntStore;
  this.restore   = cntRestore;

  //init
  this.reset();
  this.setMapSep(map, sep);
}

function cntSetMap(map)
{
  if(map == undefined) return;
  this.map = map;
}

function cntSetSep(sep)
{
  if(sep == undefined) return;
  this.sep = sep;
}

function cntSetMapSep(map, sep)
{
  this.setMap(map);
  this.setSep(sep);
}

function cntGetStatus()
{
  var i,st = new Array(4);

  st[0] = new Array(this.tbl.length);
  for(i=0; i<this.tbl.length; i++) { st[0][i] = this.tbl[i]; }
  st[1] = this.map;
  st[2] = this.sep;
  st[3] = this.last_level;
  return st;
}

function cntSetStatus(st)
{
  var i;
  
  if(st == undefined) return;
  for(i=0; i<this.tbl.length; i++) { this.tbl[i] = st[0][i]; }
  this.map = st[1];
  this.sep = st[2];
  this.last_level = st[3];
}

function cntStore()
{
  this.status = this.getStatus();
}

function cntRestore()
{
  this.setStatus(this.status);
}


function cntClone(cntObj)
{
  if(cntObj == undefined) return;

  this.tbl = new Array(cntObj.tbl.length);
  for(i=0; i<cntObj.tbl.length; i++) { this.tbl[i] = cntObj.tbl[i]; }
  this.map = cntObj.map;
  this.sep = cntObj.sep;
  this.last_level = cntObj.last_level;
  this.status = cntObj.status;
}

function cntReset(level)
{
  var i;

  if(level == undefined) { level = 1; this.last_level = 0; }
  if((level < 1) || (level > this.tbl.length)) return;
  for(i=level-1; i<this.tbl.length; i++) { this.tbl[i] = 1; }
}

function cntGetMapLetter(n, offset)
{
  var out = '';
  
  while(n>0)
  {
    n--;
    out = String.fromCharCode((n % 26) + offset) + out;
    n = parseInt(n / 26);
  }
  return out;
}

function cntGetMap(n, pos)
{
  var c;

  if(pos < this.map.length) c = this.map.charAt(pos); 
  else c = this.map.charAt(this.map.length - 1);

  if(c == 'a') return this.getMapLetter(n, 97);
  if(c == 'A') return this.getMapLetter(n, 65);
  return n;
}

function cntGetSep(pos)
{
  var c;

  if(this.sep.length == 0) return '';
  if(pos < this.sep.length) c = this.sep.charAt(pos); 
  else c = this.sep.charAt(this.sep.length - 1);

  return c;
}

function cntNumber(level, last_only)
{
  var i, out = '';
  
  if(last_only == 1) last_only = level - 1; 
  else last_only = 0;

  if((level < 1) || (level > this.tbl.length)) return '?';

  this.reset(level + 1);
  if(level < this.last_level) this.tbl[level - 1]++;
  if(level > this.last_level) this.tbl[this.last_level - 1]--;

  for(i=last_only; i<level; i++) { out += this.getMap(this.tbl[i], i) + this.getSep(i); }
  this.tbl[level - 1]++;
  this.last_level = level;

  return out;
}

//numbering example
function cntExample()
{
  //var cnt = new cntObj('aaA', '):.');
  var cnt = new cntObj();

  document.writeln(cnt.number(1) + '<br>');
  document.writeln(cnt.number(1) + '<br>');
  document.writeln(cnt.number(1) + '<br>');
  document.writeln('get status & clone: <hr>');
  var st = cnt.getStatus();
  var cnt2 = new cntObj();
  cnt2.clone(cnt);
  document.writeln(cnt.number(2) + '<br>');
  document.writeln(cnt.number(2) + '<br>');
  document.writeln(cnt.number(3) + '<br>');
  document.writeln(cnt.number(2) + '<br>');
  document.writeln(cnt.number(1) + '<br>');
  document.writeln('set map & sep:<hr>');
  cnt.setMapSep('1A1a', '}.:');
  document.writeln(cnt.number(3) + '<br>');
  document.writeln(cnt.number(3) + '<br>');
  document.writeln(cnt.number(3) + '<br>');
  document.writeln(cnt.number(4) + '<br>');
  document.writeln(cnt.number(4) + '<br>');
  document.writeln(cnt.number(4) + '<br>');
  document.writeln(cnt.number(1) + '<br>');
  //cnt.reset();
  document.writeln('set status:<hr>');
  cnt.setStatus(st);
  //cnt.setMapSep('1', '.');
  document.writeln(cnt.number(2) + '<br>');
  document.writeln(cnt.number(2) + '<br>');
  document.writeln(cnt.number(2) + '<br>');
  
  document.writeln('<br>cloned:<hr>');
  document.writeln(cnt2.number(2) + '<br>');
  document.writeln(cnt2.number(2) + '<br>');
  document.writeln(cnt2.number(2) + '<br>');
}
