/***** Fonction d'affichage des messages d'erreur et de confirmation *****/

//Affiche un message d'erreur
function Affiche_Erreur(Texte) {
	alert(Texte);
};

//Affiche un message d'erreur
function Affiche_Confirmation(Texte) {
	alert(Texte);
};

/***** Fonction d'affichage de traitement en cours *****/
//Variables globales pour l'affichage du block
var Block_Before_Scroll_Positions;
var Block_Before_Body_Overflow_Style;


function Get_Window_Dimensions() {
	//Obtiens la largeur et hauteur interieur de la fenêtre selon le navigateur
	var Window_Width = 0, Window_Height = 0;
	if( typeof( window.innerWidth ) == 'number' ) { 
		//Non-IE
		Window_Width = window.innerWidth;
		Window_Height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		Window_Width = document.documentElement.clientWidth;
		Window_Height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		Window_Width = document.body.clientWidth;
		Window_Height = document.body.clientHeight;
	};
	return [Window_Width, Window_Height];
};

function Get_Scroll_Positions() {
	var scrOfX = 0, scrOfY = 0;
 	if( typeof( window.pageYOffset ) == 'number' ) {
    	//Netscape compliant
    	scrOfY = window.pageYOffset;
    	scrOfX = window.pageXOffset;
  	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    	//DOM compliant
    	scrOfY = document.body.scrollTop;
    	scrOfX = document.body.scrollLeft;
  	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    	//IE6 standards compliant mode
    	scrOfY = document.documentElement.scrollTop;
    	scrOfX = document.documentElement.scrollLeft;
  	}
  	return [ scrOfX, scrOfY ];
};

function Show_Wait() {
	
	//Obtiens les données sur les scrolls et les sauvegarde pour réaffichage
	Block_Before_Scroll_Positions = Get_Scroll_Positions();
	Block_Before_Body_Overflow_Style = document.body.style.overflow;
	//Cache le scroll-bar
	document.body.style.overflow = "hidden";
	
	//Obtient les scroll positions après avoir caché la scroll bar
	Scroll_Positions = Get_Scroll_Positions();
	//Obtient les dimensions de la fenêtre
	Window_Dimensions = Get_Window_Dimensions();

	//Affiche le block avec les données de la date
	Wait_Block = document.getElementById("Wait");
	if (Wait_Block != null) {
		//Création du div
		Div_Wait = CreateDiv('wait_inc');
		Div_Wait.appendChild(document.createElement("BR"));
		Div_Wait.appendChild(document.createTextNode("Veuillez patienter, traitement en cours."));
		Div_Wait.appendChild(document.createElement("BR"));
		Div_Wait.appendChild(document.createElement("BR"));
		
		Wait_Block.appendChild(Div_Wait);
		
		Wait_Block.style.display = "block";
		//Centre le block dans la fenêtre
		//Left ((Largeur de fenêtre - Largeur du block) / 2) + Position de scroll gauche
		Wait_Block.style.left = (Math.round((Window_Dimensions[0]-Wait_Block.offsetWidth)/2)+Scroll_Positions[0])+"px";
		//Top ((Hauteur de fenêtre - hauteur du block)/2) + Position du scroll top
		Wait_Block.style.top = (Math.round((Window_Dimensions[1]-Wait_Block.offsetHeight)/2)+Scroll_Positions[1])+"px";
		
		//Affiche le block de Background		
		Block_Background = document.getElementById("Wait_Background");
		if (Block_Background != null) {
			//Block_Background.onclick=Hide_Calendar_Date;
			Block_Background.style.display = "block";
			Block_Background.style.top = Scroll_Positions[1]+"px";
			Block_Background.style.left = Scroll_Positions[0]+"px";
			Block_Background.style.width = Window_Dimensions[0]+"px";
			Block_Background.style.height = Window_Dimensions[1]+"px";
		};
	};
};

function Erase_Wait() {
	//Efface le wait message
	Ligne = document.getElementById('wait_inc');
	if (Ligne != null) 
		Ligne.parentNode.removeChild(Ligne);
	
	Wait_Block = document.getElementById("Wait");
	if (Wait_Block != null) {
		Wait_Block.style.display = "none";
	};
	
	Block_Background = document.getElementById("Wait_Background");
	if (Block_Background != null) {
		Block_Background.style.display = "none";
	};
};


//Affichage d'un block div et un background
function Show_Block(Block_Id,Background_Id) {
	//Obtiens les données sur les scrolls et les sauvegarde pour réaffichage
	Block_Before_Scroll_Positions = Get_Scroll_Positions();
	Block_Before_Body_Overflow_Style = document.body.style.overflow;
	//Cache le scroll-bar
	//document.body.style.overflow = "hidden";
	
	//Obtient les scroll positions après avoir caché la scroll bar
	Scroll_Positions = Get_Scroll_Positions();
	
	//Obtient les dimensions de la fenêtre
	Window_Dimensions = Get_Window_Dimensions();

	//Affiche le block avec les données de la date
	Block = document.getElementById(Block_Id);
	if (Block != null) {
		Block.style.display = "block";
		//Centre le block dans la fenêtre
		//Left ((Largeur de fenêtre - Largeur du block) / 2) + Position de scroll gauche
		Block.style.left = (Math.round((Window_Dimensions[0]-Block.offsetWidth)/2)+Scroll_Positions[0])+"px";
		//Top ((Hauteur de fenêtre - hauteur du block)/2) + Position du scroll top
		Block.style.top = (Math.round((Window_Dimensions[1]-Block.offsetHeight)/2)+Scroll_Positions[1])+"px";
		
		//Affiche le block de Background		
		Block_Background = document.getElementById(Background_Id);
		if (Block_Background != null) {
			Block_Background.style.display = "block";
			Block_Background.style.top = "0px";
			Block_Background.style.left = "0px";
			Block_Background.style.width = Window_Dimensions[0]+"px";
			
			//Position, hauteur du pied
			var PosPied = document.getElementById('pied').offsetTop; 
			var HautPied = document.getElementById('pied').offsetHeight;
			
			//On définit la hauteur de fond du popup
			if( (PosPied + HautPied) > Window_Dimensions[1] ) {
				//Si le contenu de la page est plus grand que la fenetre
				Block_Background.style.height = PosPied + HautPied + 'px'; 
			} else {
				//Si le contenu de la page est plus petit que la fenetre
				Block_Background.style.height = Window_Dimensions[1] + 'px'; 
			};
			
		};
		
	};
	
};

//Désaffichage d'un block div et un background
function Erase_Block(Block_Id,Background_Id) {
	
	Block = document.getElementById(Block_Id);
	if (Block != null) {
		Block.style.display = "none";
	};
	
	Block_Background = document.getElementById(Background_Id);
	if (Block_Background != null) {
		Block_Background.style.display = "none";
	};
	//Affiche le scroll-bar
	//document.body.style.overflow = "visible";
	
	//Rescroll à la bonne position dans la page
	window.scrollTo(Block_Before_Scroll_Positions[0],Block_Before_Scroll_Positions[1]);
};

/***** Fonction de traitement des objets avec le DOM *****/

//Céation d'une ligne dans la table
function CreateRow(ID) {
	CurrentRow = document.createElement("TR");
	CurrentRow.setAttribute("id",ID);
	return CurrentRow;
};

//Céation d'une cellule de contenu dans la table
function CreateCell(Colone,Alignement_Vertical,Alignement_Horizontal,Largeur) {
	CurrentCell = document.createElement("TD");
	CurrentCell.setAttribute("align",Alignement_Vertical);
	CurrentCell.setAttribute("colSpan",Colone);
	if (Alignement_Horizontal != null) {
		CurrentCell.setAttribute("valign",Alignement_Horizontal);
	};
	if (Largeur != null) {
		CurrentCell.setAttribute("width",Largeur);
	};
	return CurrentCell;
};

//Céation d'une table
function CreateTable(ID) {
	Table = document.createElement("TABLE");
	Table.setAttribute("width","100%");
	Table.setAttribute("class","content");
	Table.setAttribute("border",0);
	Table.setAttribute("cellpadding",0);
	Table.setAttribute("cellspacing",0);
	Table.setAttribute("id",ID);
	return Table;
};

//Céation d'un lien
function CreateLien(ID,Adresse) {
	Lien = document.createElement("A");
	Lien.setAttribute("href",Adresse);
	Lien.setAttribute("class","content");
	Lien.setAttribute("id",ID);
	return Lien;
};

//Céation d'un bloc div
function CreateDiv(ID) {
	Div = document.createElement("DIV");
	Div.setAttribute("id",ID);
	return Div;
};

//Céation d'un bloc span
function CreateSpan(ID) {
	Span = document.createElement("SPAN");
	Span.setAttribute("id",ID);
	return Span;
};

//Céation d'une image
function CreateImage(ID,Source,Border,Name,Title) {
	Image_Picture = document.createElement("img");
	Image_Picture.setAttribute("border",Border);
	Image_Picture.setAttribute("id",ID);
	Image_Picture.setAttribute("name",Name);
	Image_Picture.setAttribute("src",Source);
	Image_Picture.setAttribute("title",Title);
	Image_Picture.setAttribute("alt",Title);
	return Image_Picture;
};

/***** Fonction de traitement du texte avec le DOM *****/

//Remplace le texte
function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

//Efface le texte
function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

//Récupère le texte
function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}

/****** Fonction de traitement utilisé régulièrement ******/

function CleanWhitespace(node) {
    /* http://www.dustindiaz.com/easier-dom-walking-with-cleanwhitespace/ */
    var notWhitespace = /\S/;
 
    for (var x = 0; x < node.childNodes.length; x++) {
        var childNode = node.childNodes[x];
        if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue))) {
            node.removeChild(node.childNodes[x]);
            x--;
        };
        if (childNode.nodeType == 1) {
            CleanWhitespace(childNode);
        };
    };
};

function Select_Option_Selected(Select_Id,Selected_Option) {
	Select_Input = document.getElementById(Select_Id);
	if (Selected_Option!="") { 
		for (i=0; i<Select_Input.length; i++) {
			if (Select_Input.options[i].value == Selected_Option) {
				Select_Input.options[i].selected = true;
				i = Select_Input.length;
			};
		};
	} else {
		Select_Input.options[0].selected = true;
	};
};


// This is Javascript, not PHP!

function js_array_to_php_array (a)
// This converts a javascript array to a string in PHP serialized format.
// This is useful for passing arrays to PHP. On the PHP side you can 
// unserialize this string from a cookie or request variable. For example,
// assuming you used javascript to set a cookie called "php_array"
// to the value of a javascript array then you can restore the cookie 
// from PHP like this:
//    <?php
//    session_start();
//    $my_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
//    print_r ($my_array);
//    ?>
// This automatically converts both keys and values to strings.
// The return string is not URL escaped, so you must call the
// Javascript "escape()" function before you pass this string to PHP.
{
    var a_php = "";
    var total = 0;
    for (var key in a)
    {
        ++ total;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + String(a[key]) + "\";";
    }
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php;
}

