/* La promenade des demoiselles

   Jean-Paul Davalan le 17 juin 2005
*/

function classe() {
  this.g=150;
  this.n=0;
  this.l=0;
  this.parite=0;
  this.noms=new Array();
  this.njours=0;
  this.ncouples=0;
  this.jours=new Array();
  this.liste="";
  this.ordre=new Array();

  function ordonne(x) {
    var i, j, a, b;
    for(i=0;i< this.l;i++) // this.l non mélangé
      this.ordre[i]=i;
    if(x==true)
    for(i=this.n-1;i>0;i--) {  // 1 à this.n mélangés
      for(j=0;j<i;j++) {
        a = Math.floor((i+1)*Math.random());
        b=this.ordre[i];
        this.ordre[i]=this.ordre[a];
        this.ordre[a]=b;
      }
    }
  } 
  this.ordonne=ordonne;

  function lit(s) {
    var u
    this.liste=s;
    do {
       u = s;
       s = s.replace(/\n/g,";");
       s = s.replace(/,/g,";");
       s = s.replace(/;\s+/g,";");
       s = s.replace(/\s+;/g,";");
       s = s.replace(/\;+/g,";");
       s = s.replace(/^\s+/,"");
       s = s.replace(/\s+$/,"");
       s = s.replace(/^;/,"");
       s = s.replace(/;$/,"");
    } while(s != u);
    this.noms = s.split(/;/g);
    this.n = this.noms.length;
    if(this.n%2==1) {
      this.parite = 1;

      this.noms[this.n]="LeFantome";
    }
    this.l =  this.noms.length;
  }
  this.lit = lit;

  function set(n) {
    this.n = n;
    this.liste = "";
    for(i=0;i<n;i++) {
      this.noms[i] = i+1;
      this.liste += i+1+", ";
    }
     if(this.n%2==1) {
      this.parite = 1;
      this.noms[this.n]="LeFantome";
    }
    this.l =  this.noms.length;
  }
  this.set=set;

  function tour() {
     var z = "", u, v, w, i, j;
     for(i=1; i< this.l; i++) {
       if(i==1) {
          z += "1er jour\n";
       } else {
          z += i+"ième jour\n";
       }
       z += "---------------\n";
       if(this.parite==0) {
          z += this.noms[this.ordre[i-1]]+" --- "+this.noms[this.ordre[this.l -1]]+"\n";
       } else {
          z += "("+this.noms[this.ordre[i-1]]+")\n";
       }
       for(j=1;j< this.l/2;j++) {
         u = i+j;
         if(u>this.l-1)
            u -= this.l-1;
         v = i + this.l -1 -j;
         if(v>this.l-1) v -= this.l -1;
         if(u<v) {
            w=u;u=v;v=w;
         }
         z += this.noms[this.ordre[v-1]]+" --- "+this.noms[this.ordre[u-1]]+"\n"
       }
       z += "\n";
     }
     return z;
   }
   this.tour = tour;


}

var liste="";

function cherche() {
  var cl = new classe();
  liste = document.frm.ar.value;
  cl.lit(document.frm.ar.value);
  cl.ordonne(document.frm.check.checked);
  document.frm.ar.value = cl.tour();
}
function ncherche() {
  var cl = new classe();
  cl.set(document.frm.n.value);
  liste = cl.liste;
  cl.ordonne(document.frm.check.checked);
  document.frm.ar.value = cl.tour();
}
function inccherche() {
  var cl = new classe();
  var n=1+parseInt(document.frm.n.value);
  document.frm.n.value=n;
  if(document.frm.n.value=="NaN")
     document.frm.n.value=8;
  cl.set(document.frm.n.value);
  liste = cl.liste;
  cl.ordonne(document.frm.check.checked);
  document.frm.ar.value = cl.tour();
}
function efface() {
  document.frm.ar.value = "";
  document.frm.n.value = "";
}

function rappelle() {
  document.frm.ar.value = liste;
}

var exo = new Array(
"Ahmes\nAnaxagore\nAnaximandre\nAnaximène\nAntiphon\nThalès de Milet\nAppolonius\n"+
"Apollonios de Perga\nArchimède de Syracuse\nAristarque de Samos\nArchytas de Tarente\n"+
"Aristarque de Samothrace\nCallippe de Cyzique\nConon de Samos\nDémocrite\nDinostrate\n"+
"Ménèchme\nDioclès\nDioclès de Carystos\nDiophante\nÉratosthène de Cyrène\nEuclide\n"+
"Eudoxe de Cnide\nEudème de Rhodes\nHéron d'Alexandrie\nHipparque de Nicée\nHippase\n"+
"Hippias d'Elis\nHippocrate de Chio\nHypatie d'Alexandrie\nThéon de Smyrne\nThéon d'Alexandrie\n"+
"Imhotep\nMénèchme de Proconnèse\nMéton\nNicomaque de Gerase\nNicomaque de Stagire\n"+
"Nicomède\nPappus d'Alexandrie\nPhilolaos\nPlaton\nAristoclès\nPolybius\nPosidonios\n"+
"Proclus de Lycie\nClaude Ptolémée\nAlmageste\nPhidias\nPythagore de Samos\n"+
"Pythéas\nSynésius\nThalès\nThéodore de Cyrène\nThéodosius de Bithynie\n"+
"Vitruve Marcus Pollio\nZénodore\nZenodorus\nZénon d'Élée\n",
"un\ndeux\ntrois\nquatre\n",
"un\ndeux\ntrois\nquatre\ncinq\n",
"un\ndeux\ntrois\nquatre\ncinq\nsix\n",
"I\nII\nIII\nIV\nV\nVI\nVII",
"Antoine\nCamille\nFélix\nJade\nPhilippe\nChloé\n",
"Thomas\nJustin\nGabrielle\nAudrey\nOcéane\nTristan\nJulien\n",
"Agnès\nAudrey\nCameron\nCécile\nÉlodie\nGrace\nJulia\nMarie\n",
"Alain\nAlfred\nAntonio\nArnold\nBernard\nBruce\nDavid\nEddie\nFabrice\n",
"Jamel\nJohnny\nKeanu\nLeonardo\nMathieu\nPascal\nPatrick\nQuentin\nRaoul\nTim\n");
var nbjoueurs = new Array(58,4,5,6,7,6,7,8,9,10);

function exemple(n) {
  document.frm.ar.value=exo[n];
  document.frm.n.value=nbjoueurs[n];
}



