/* ---------------------------------------------------------------------------------
   Fenêtre de debugging, pour afficher divers informations en cours de développement
   de programmes javascript.
   Fortement inspiré de la version de :
   Philippe Sarcher 1998     philippe@sarcher.org   http://www.sarcher.org/

   Repris par Bernard Gisin : http://www.juggling.ch/gisin/javascript/javascript.html
   -------------------------------------------------------------------------------- */

var LEV0 =   1;
var LEV1 =   2;
var LEV2 =   4;
var LEV3 =   8;
var STP0 =  16;
var STP1 =  32;
var STP2 =  64;
var STP3 = 128;
var ON   = 255;
var DATE = 512;
var ESC = 1024;
//var jsTraceOptions = DATE + ESC + LEV0;
var jsTraceOptions = LEV0;
var jsTraceSize = 3;
var oDebugWin = null;
var jsTraceWSize = "top=3, left=600, width=400, height=500";

function Beep() {
//===============
// fait un beep
// pour cela, il faut avoir mis la ligne suivante dans le code HTML :
// <DIV id="divBeep" style="position:absolute; left:1px; top:1px; width:1px; height:1px"><!--  --></DIV>
divBeep.innerHTML="<embed src='BIP.WAV', height=50 width=100 hidden autoplay='true'>";
} // Beep

function TraceSetOptions (strOpt) {
//=================================
// setTraceOption("opt:value;opt:value.....");
var LsOpt = strOpt.split(";");
 	for (var i=0;i<LsOpt.length;i++) {
	  	Nmv = LsOpt[i].split(":");

		  if (Nmv[0] == "size") {
   			jsTraceSize = Nmv[1];
		   	oDebugWin.document.write("</font><font size=" + jsTraceSize + ">");
	  	  }
    else if (Nmv[0] == "on") {
      if (Nmv[1]==1) jsTraceOptions |= ON; else jsTraceOptions &= ~ON;
		    }else if (Nmv[0] == "heure") {
			     if (Nmv[1]==1) jsTraceOptions |= DATE; else jsTraceOptions &= ~DATE;
		    }else if (Nmv[0] == "echap") {
			     if (Nmv[1]==1) jsTraceOptions |= ESC; else jsTraceOptions &= ~ESC;
		    }else if (Nmv[0] == "lev1") {
		     	if (Nmv[1]==1) jsTraceOptions |= LEV1; else jsTraceOptions &= ~LEV1;
		    }else if (Nmv[0] == "lev2") {
			     if (Nmv[1]==1) jsTraceOptions |= LEV2; else jsTraceOptions &= ~LEV2;
		    }else if (Nmv[0] == "lev3") {
			     if (Nmv[1]==1) jsTraceOptions |= LEV3; else jsTraceOptions &= ~LEV3;
		    }else if (Nmv[0] == "stp0") {
			     if (Nmv[1]==1) jsTraceOptions |= STP0; else jsTraceOptions &= ~STP0;
		    }else if (Nmv[0] == "stp1") {
			     if (Nmv[1]==1) jsTraceOptions |= STP1; else jsTraceOptions &= ~STP1;
		    }else if (Nmv[0] == "stp2") {
			     if (Nmv[1]==1) jsTraceOptions |= STP2; else jsTraceOptions &= ~STP2;
		    }else if (Nmv[0] == "stp3") {
			     if (Nmv[1]==1) jsTraceOptions |= STP3; else jsTraceOptions &= ~STP3;
		    }else if (Nmv[0] == "focus") {
			     if (Nmv[1]==1) oDebugWin.focus(); else oDebugWin.blur();
		    }else if (Nmv[0] == "wsiz") {
     			jsTraceWSize = Nmv[1];
		      }
      else {
			     oDebugWin.document.write("INTERNAL ERROR : invalid option : " + LsOpt[i]+ "<br>");
		      }
	} // for
//	oDebugWin.document.write("Apres setOption : " + jsTraceOptions + "<br>");
} // TraceSetOptions
		
function TraceClear() {
//=====================
if (oDebugWin) {
			oDebugWin.document.close();
			oDebugWin.document.open();
   oDebugWin.document.writeln("<html>");
			oDebugWin.document.writeln("<head><title>Debbuging Window</title></head>");
			oDebugWin.document.writeln("<html><body bgcolor=white>");
			oDebugWin.document.writeln("<font size=" + jsTraceSize + ">");
  	}
} // TraceClear

function Trace(strS, Lev) {
//=========================
//  window.alert("Dans Trace=" + jsTraceOptions + '  Lev=' + Lev);
//	if ((jsTraceOptions & ON) && (jsTraceOptions & ((!Lev) ? LEV0 : LEV0<<Lev ))) {
if (jsTraceOptions & ON) {
  // ouvre la fenêtre de debugging si elle n'est pas déjà ouverte.
  if (!oDebugWin) {
		 	oDebugWin = window.open('','Console','resizable=yes,scrollbars=yes,menubar=no,' + jsTraceWSize);
			 oDebugWin.document.open();
			 oDebugWin.document.writeln("<html>");
			 oDebugWin.document.writeln("<head><title>Debbuging Window</title></head>");
			 oDebugWin.document.writeln("<html><body bgcolor=white>");
			 oDebugWin.document.writeln("<font size=" + jsTraceSize + ">");
  		}

  if (!oDebugWin.document) {
    // Beep();
		 	oDebugWin = window.open('','Console','resizable=yes,scrollbars=yes,menubar=no,' + jsTraceWSize);
			 oDebugWin.document.open();
			 oDebugWin.document.writeln("<html>");
			 oDebugWin.document.writeln("<head><title>Debbuging Window</title></head>");
			 oDebugWin.document.writeln("<html><body bgcolor=white>");
			 oDebugWin.document.writeln("<font size=" + jsTraceSize + ">");
    }

		var Chaine = "";
		if (jsTraceOptions & DATE) {
		 	var Hr = new Date();
			 Chaine = Hr.getHours() + ":" + Hr.getMinutes() + ":" + Hr.getSeconds();
			 oDebugWin.document.write(Chaine + " ->");
	  	}

		Chaine = strS;
		if (jsTraceOptions & ESC) {
			 var x=0;
			 var from = 0;
			 var Resu = "";
			
    while ((x=strS.indexOf("<",from)) != -1) {
				  Resu += strS.substring(from,x) + "&lt;";
			  	from = x+1;
			   }

			Resu += strS.substring(from,strS.length);
			Chaine = Resu;
		 } // if

		oDebugWin.document.write(Chaine + "<br>");
		if (jsTraceOptions & ((!Lev) ? STP0 : STP0<<Lev )) {
			 var Resp = prompt("POINT D'ARRET : \nTapez : \tstop\tpour arreter le programme\n\tou des options de trace pour les positionner et continuer.","");
			 if (Resp.toLowerCase() == "stop") {
			 } else if (Resp != "") {
				  setTraceOptions(Resp);
			   }
		  }
 	} // if
} // Trace

