//
//==========================================================================
//====================== popup de DEBUG ================================
var ns4=document.layers
var ie4=document.all
var ns6=document.getElementById&&!document.all

//drag drop function for NS 4////
/////////////////////////////////

var dragswitch=0
var nsx
var nsy
var nstemp

function drag_dropns(name){
if (!ns4)
return
temp=eval(name)
temp.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP)
temp.onmousedown=gons
temp.onmousemove=dragns
temp.onmouseup=stopns
}

function gons(e){
temp.captureEvents(Event.MOUSEMOVE)
nsx=e.x
nsy=e.y
}
function dragns(e){
if (dragswitch==1){
temp.moveBy(e.x-nsx,e.y-nsy)
return false
}
}

function stopns(){
temp.releaseEvents(Event.MOUSEMOVE)
}

//drag drop function for ie4+ and NS6////
/////////////////////////////////


function drag_drop(e){
	if (ie4&&dragapproved){
		crossobj.style.left=tempx+event.clientX-offsetx
		crossobj.style.top=tempy+event.clientY-offsety
		return false
	}
	else if (ns6&&dragapproved){
		crossobj.style.left=tempx+e.clientX-offsetx+"px"
		crossobj.style.top=tempy+e.clientY-offsety+"px"
		return false
	}

}

function initializedrag(e){
crossobj=ns6? document.getElementById("showimage") : document.all.showimage
var firedobj=ns6? e.target : event.srcElement
var topelement=ns6? "html" : document.compatMode!="BackCompat"? "documentElement" : "body"
while (firedobj.tagName!=topelement.toUpperCase() && firedobj.id!="dragbar"){
firedobj=ns6? firedobj.parentNode : firedobj.parentElement
}

if (firedobj.id=="dragbar"){
offsetx=ie4? event.clientX : e.clientX
offsety=ie4? event.clientY : e.clientY

tempx=parseInt(crossobj.style.left)
tempy=parseInt(crossobj.style.top)

dragapproved=true
document.onmousemove=drag_drop
}
}
document.onmouseup=new Function("dragapproved=false")

////drag drop functions end here//////

function hidebox(){

if (ie4||ns6)
crossobj.style.visibility="hidden"
else if (ns4)
document.showimage.visibility="hide"
}

//====================== Fin popup de DEBUG ================================
//==========================================================================


var nb_msg_debug=0;
var html="";
var debugWindow;

/*
 * @return void
 * @param mixed 	value	 	la variable à afficher
 * @param string 	nom_var 	le nom de la variable, ou tout autre valeur permettant d'identifier le debug à l'affichage : "&nbsp;" par défaut
 * @param boolean 	indent 		cette variable permet de demander de demander l'affichage
         						des retour à la ligne, ainsi que les indentations pour permettre plus de
         						lisibilité. Cette variable est à "false" par défaut, car dans le cas contraire,
         						certaines variables pourraient ne pas s'afficher correctement (comme du code HTML
         						par exemple) : false par défaut
 * @desc Fonction permettant d'afficher la valeur d'une variable dans le cadre d'un debuggage
 */
function debug(value, nom_var, indent) {
	nb_msg_debug++;
	indent=true;
	type=getTypeDebug(value);
	if(!debugWindow) {
		var debug= window.open("about:blank");
		debugWindow=debug;
	}
	html='<br /><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR><TD BGCOLOR="#000000"><TABLE BORDER="0" CELLSPACING="1" CELLPADDING="4"><TR BGCOLOR="#9999CC"><TD COLSPAN="2" ALIGN="center"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFB43E" SIZE="1"><B>DEBUG N&deg;' + nb_msg_debug + '</B></FONT></TD></TR><TR BGCOLOR="#9999CC"><TD ALIGN="center"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="1"><B>INFORMATION</B></FONT></TD><TD ALIGN="center"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="1"><B>VALEUR</B></FONT></TD></TR>';
	if (nom_var) {
		html+='<TR><TD BGCOLOR="#CCCCFF" ALIGN="left"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="1"><B>Nom de la variable</B></FONT></TD><TD BGCOLOR="#CCCCCC" ALIGN="center"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FF0000" SIZE="2"><B>' + nom_var + '</B></FONT></TD></TR>';
	}
	html+='<TR><TD BGCOLOR="#CCCCFF" ALIGN="left"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="1"><B>Type de la variable</B></FONT></TD><TD BGCOLOR="#CCCCCC" ALIGN="center"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="2"><B>' + type + '</B></FONT></TD></TR>';
	html+='<TR><TD BGCOLOR="#CCCCFF" ALIGN="left"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="1"><B>Valeur de la variable</B></FONT></TD><TD BGCOLOR="#CCCCCC" ALIGN="left">' + afficheVarDebug(value,indent) + '</TD></TR></TABLE></TD></TR></TABLE>';
	debugWindow.document.write(html);
}

/**
 * @return void
 * @param mixed 	value 		la variable à afficher
 * @param string 	indent 		cette variable permet de demander de demander l'affichage
						        des retour à la ligne, ainsi que les indentations pour permettre plus de
						        lisibilité. Cette variable est à "false" par défaut, car dans le cas contraire,
						        certaines variables pourraient ne pas s'afficher correctement (comme du code HTML
						        par exemple)
 * @desc Fonction permettant d'afficher une variable suivant son type
 */
function afficheVarDebug(value, indent)
{
	var vReturn="";
	// suivant le type de la variable, on affiche celle-ci
	
	switch (getTypeDebug(value))
	{
		case "instance de tableau" : {
			vReturn=afficheTabDebug(value, indent);
			break;
		}
		case "objet inconnu": 
		case "objet" : {
			vReturn=afficheTabDebug(value, indent, "attribut");
			break;
		}
		default : {
			if (!value)
			{
				vReturn=afficheTexteDebug("La variable n'est pas d&eacute;finie","I");
			} else {
				if (value === 0)
				{
					vReturn=afficheTexteDebug("0","I");
				}
				else if (value == "")
				{
					vReturn=afficheTexteDebug("La variable est vide","I");
				}
				else if (value == null)
				{
					vReturn=afficheTexteDebug("La variable est nulle","I");
				}
				else
				{
					// si une indentation est demandé, on remplace les \n par de <br />
					// et les espaces par des "&nbsp;"
					if (indent)
					{
						value = htmlentities(value);
						value = nl2br(value);
					} else {
						value = htmlentities(value);
					}
					vReturn=afficheTexteDebug(value);
				}
			}
		}
	}
	return vReturn;
}

/**
 * @return string
 * @param mixed 	value 	la variable
 * @desc Fonction permettant d'afficher le type d'une variable en français
*/
function getTypeDebug(value)
{
	switch (typeof(value))
	{
		case "boolean" : {
			return "booléen";
			break;
		}
		case "number" : {
			return "nombre";
			break;
		}
		case "string" : {
			return "chaîne de caractères";
			break;
		}
		case "object" : {
			if (value instanceof Array) {
				return "instance de tableau";
			} else if (value instanceof Boolean) {
				return "instance de booléen";
			} else if (value instanceof Date) {
				return "instance de date";
			} else if (value instanceof Function) {
				return "instance de fonction";
			} else if (value instanceof Number) {
				return "instance de nombre";
			} else if (value instanceof Object) {
				return "objet";
			} else if (value instanceof RegExp) {
				return "instance d'expression régulière";
			} else if (value instanceof String) {
				return "instance de chaîne de caractère";
			} else {
				return "objet inconnu";
			}
			break;
		}
		case "function" : {
			return "fonction";
			break;
		}
		case "undefined" : {
			return "non défini";
			break;
		}
		default : {
			return "type inconnu";
		}
	}
}

function afficheTexteDebug(value,format) {
	if (!format) format="B";
	return '<FONT FACE="Verdana,Helvetica,Arial" COLOR="#000000" SIZE="2"><' + format + '>' + value + '</' + format + '></FONT>';
}

/**
 * @return void
 * @param string 	tab 		le tableau à afficher
 * @param string 	indent 		cette variable permet de demander de demander l'affichage
								des retour à la ligne, ainsi que les indentations pour permettre plus de
								lisibilité. Cette variable est à "false" par défaut, car dans le cas contraire,
								certaines variables pourraient ne pas s'afficher correctement (comme du code HTML
								par exemple) :  false par défaut
 * @param string 	nom_indice 	intitulé à donner aux indices du tableau. Cette fonction étant
								utilisé aussi pour l'affichage 'casté' des objets, on peut ainsi demandé
								à ce que cet intitulé soit "attribut" : "indice" par défaut
 * @desc Fonction permettant d'afficher un tableau
 */
function afficheTabDebug(tab, indent, nom_indice) {
	var vReturn="";
	var tmp;
	if (!nom_indice) nom_indice = "indice";
	if (tab)
	{
		vReturn='<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR><TD BGCOLOR="#000000"><TABLE BORDER="0" CELLSPACING="1" CELLPADDING="4"><TR BGCOLOR="#9999CC"><TD ALIGN="center"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="1"><B>' + nom_indice + '</B></FONT></TD><TD ALIGN="center"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFFFF" SIZE="1"><B>valeur</B></FONT></TD></TR>';
		is_array=((tab instanceof Array));
		if (is_array) {
			for (var i=0; i<tab.length; i++)
			{
				value=tab[i];
				if (value) {
					vReturn+=afficheTabValue(i,value, indent);
				} else {
					tmp="Tableau non défini";
					vReturn+=afficheTabValue(i,tmp);
				}
			}
		} else {
			for ( key in tab )
			{
//debugWindow.document.write(key + "<br />");
				if (valideKey(key)) {
					value=tab[key];
					if (value) {
						vReturn+=afficheTabValue(key,value, indent);
					} else {
						tmp="Objet non défini";
						vReturn+=afficheTabValue(key,tmp);
					}
				} else {
					tmp="--Objet non traité--";
					vReturn+=afficheTabValue(key,tmp);
				}
			}
		}
		vReturn+='</TR></TABLE></TD></TR></TABLE>';
	} else {
		vReturn+=afficheTexteDebug("L'objet est vide","I");
	}
	return vReturn;
}

function afficheTabValue(key,value, indent) {
	vReturn='<TR><TD BGCOLOR="#CCCCFF" ALIGN="left"><FONT FACE="Verdana,Helvetica,Arial" COLOR="#FFFD3E" SIZE="2"><B>'+ key + '</B></FONT></TD><TD BGCOLOR="#CCCCCC" ALIGN="left">';
	vReturn+=afficheVarDebug(value);
	vReturn+='</TD></TR>\n';
	return vReturn;
}

function valideKey(key) {
//// mimeTypes, plugins ne marche pas pour IE WIN !!!!!
//// userProfile, opsProfile fonctionnement spécial
	if (key != "userProfile" && key != "mimeTypes" && key != "opsProfile" && key != "plugins" ) {
// propriétés à problème
		if ( key != "nextSibling" && key != "previousSibling" && key != "document" && key != "all" && key != "DOM" && key != "form" && key != "attributes" && key != "childNodes" && key != "firstChild" && key != "lastChild" && key != "offsetParent" && key != "parentTextEdit" && key != "ownerDocument" && key != "parentNode" && key != "parentElement" && key != "children" ) {
// trop long => crée des stacks overflow
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function htmlentities(value) {
	var re,
	text = value.toString();
	if (text) {
		re = new RegExp ('<', 'gi') ;
		text = text.replace(re, '&lt;') ;
		re = new RegExp ('>', 'gi') ;
		text = text.replace(re, '&gt;') ;
		re = new RegExp ('\t', 'gi') ;
		text = text.replace(re, '&nbsp;&nbsp;&nbsp;&nbsp;') ;
	}
	return text;
}

function nl2br(value) {
	var re,
	text = value.toString();
	if (text) {
		re = new RegExp ('\n', 'gi') ;
		text = text.replace(re, '<br />') ;
	}
	return text;
}

/*
<? PHP
	function phpToJs($value) {
		$content=var_export($value,true);
		$content=str_replace("\r","",$content);
		$content=str_replace("\n","",$content);
		$content=str_replace("\t","",$content);
		$content=str_replace("  "," ",$content);
		$content=str_replace(", )",",)",$content);
		$content=str_replace(",)","}",$content);
		$content=str_replace("array (","{",$content);
		$content=str_replace("=>",":",$content);
	}
?>*/
