// -*- mode: java -*-
// $Id: methcall.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// From David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

function Toggle(start_state) {
  this.bool = start_state;
  
  this.value = ToggleValue;
  this.activate = ToggleActivate;
  
}
function ToggleValue () {
  return this.bool;
}
function ToggleActivate () {
  this.bool = !this.bool;
  return this;
}


function NthToggle (start_state, max_counter) {
  this.base = Toggle;
  this.base(start_state);
  this.count_max = max_counter;
  this.count = 0;

  this.activate = NthToggleActivate;

}
NthToggle.prototype = new Toggle;

function NthToggleActivate () {
if (++this.count >= this.count_max) {
  this.bool = !this.bool;
  this.count = 0;
}
return this;
}

var n, i;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {
  n = 1;
}

var val = true;
var toggle = new Toggle(val);
for (i=0; i<n; i++) {
  val = toggle.activate().value();
}
WScript.Echo(toggle.value() ? "true" : "false");

val = true;
var ntoggle = new NthToggle(val, 3);
for (i=0; i<n; i++) {
  val = ntoggle.activate().value();
}
WScript.Echo(ntoggle.value() ? "true" : "false");