var ko="1", lastindex=0, lmax=500, ccur="1", lastmodel="0", lasts="";

function cinv() {
  ccur = (ccur=="1") ? "2" : "1";
}

function majko(len) {
  var i, u, l = lastindex;
  for(i=l; ko.length < len; i++) {
    if (ccur=="1")
      ccur = "2";
    else 
      ccur = "1";
    u = ko.charAt(i);
    ko = ko + ccur;
    if(u =="2") ko = ko + ccur;
    lastindex++;
  }
}
function reinit() {
  var l = 1*(document.frm.lg.value);
  lastmodel="0";
  lasts="";
  document.frm.area.value = "Construction du mot ...";
  majko(l);
  afficheko();
}

function init() {
  var i, n, s;
  document.frm.area.value = "Initialisations ...";
  majko(lmax);
  afficheko();
  lastmodel="0";
  lasts="";
}

function afficheko() {
  var n, s, i;
  n = ko.length;
  s = "longueur du mot : " + n+"\n------------------------------\n";
  for(i=0;i<n; i+=50) {
     s += "    "+ko.substr(i, 50) + "\n";
  }
  document.frm.area.value = s;
}
/*
function effectue() {
  var i, j, t = document.frm.model.value, ts = new Array(), r, p, l, s="";
  ts[0] = t; ts[1]=""; ts[2]="";  ts[3]="", re = new Array();
  for(i=0;i<t.length;i++) {
    if(t.charAt(i)=="1") {
      ts[1] = ts[1]+"2";
      ts[2]= "1"+ts[2];
      ts[3]= "2"+ts[3];
    } else {
      ts[1] = ts[1]+"1";
      ts[2]= "2"+ts[2];
      ts[3]= "1"+ts[3];
    }
  }    
  r = t.length;
  k = ko.length;
  l = k - r;
  for(j=0;j<4;j++) {
    re[j] = new Regexp(ts[j],"g");
    a = ko.match(re[j]);
    n=a.length;
    s += "mot : "+ ts[j] + "  " + "nb occurrences : " + n +"\n";
  }
  document.frm.area.value = s;
}
*/
function effectue() {
  var i, n = 0, t = document.frm.model.value;
  if(t==void(0) || t == "" || (t.charAt(0) !="1" && t.charAt(0) != "2")) { return; }
  var tl=t.length, ts = new Array(), r, p, l, s="";
  ts[0] = t; ts[1]=""; ts[2]="";  ts[3]="";
  for(i=0;i<tl;i++) {
    if(t.charAt(i)=="1") {
      ts[1] = ts[1]+"2";
      ts[2]= "1"+ts[2];
      ts[3]= "2"+ts[3];
    } else {
      ts[1] = ts[1]+"1";
      ts[2]= "2"+ts[2];
      ts[3]= "1"+ts[3];
    }
  }
  if(lastmodel==ts[0] || lastmodel==ts[1] || lastmodel==ts[2] || lastmodel==ts[3]) {
    s = lasts;
  } else {
    document.frm.area.value = "Décompte des occurrences...";
    r = tl;
    k = ko.length;
    l = k - tl;
    for(j=0;j<4;j++) {
      n=0;
      var ca = ts[j].charAt(0);
      for(i=0;i < l ;i++) {
        if(ca==ko.charAt(i) &&  ko.substr(i,r)==ts[j]) n++;
      }
      s += "mot : "+ ts[j] + "  " + "nb occurrences : " + n +"\n";
    }
    lastmodel=t;
    lasts=s;
  }
  document.frm.area.value = s;
}


