/************************************************************************************************
*	Programmeur :		Daniel Rioux											*
*																		*
*	Date de céation :		14 Décembre 2008										*
*																		*
*	Description :		Script servant à charger le contenu dans divDocument.					*
************************************************************************************************/


var selectedTab = null;


//Chargement d'une page.
function loadPage(Page)
{
	
	var cHttp = new clsAJAX();
	var ServerResult;

	var DocBody = document.getElementById("divDocument");
	
	//alert(Page);
	ServerResult = cHttp.Get("scripts/php/body.php","forms",Page,false);
	//alert(ServerResult);
	DocBody.innerHTML = null;
	DocBody.innerHTML = ServerResult;
	var DocScript = DocBody.getElementsByTagName("script");
	
	if (DocScript != null)
	{
		for (i = 0 ; i < DocScript.length ; i++)
		{
			eval (DocScript[i].innerHTML);
		}
	}
	
	if (arguments[1] != undefined)
	{
		if (arguments[1] != "")
		{
			switch (arguments[1])
			{
				case "add_user":
					var SubmitButton = document.getElementById("Send");
					var PasswordInput = document.getElementById("Password");
					var ConfirmInput = document.getElementById("ConfirmPassword");
					
					SubmitButton.setAttribute("onclick","javascript: if(comparePasswords(this.form.reqPassword.value,this.form.reqConfirmPassword.value)){submitForm(this.form);}else{alert('Les mots de passes sont différent.');}");
					PasswordInput.setAttribute("id","reqPassword");
					ConfirmInput.setAttribute("id","reqConfirmPassword");
					ConfirmInput.setAttribute("onblur","javascript: comparePasswords(this.form.reqPassword.value,this.form.reqConfirmPassword.value);");
					var HiddenInput = document.getElementById("type");
					
					HiddenInput.value = arguments[1];
				break;
				
				case "sourd" || "organisme" :
				
					var HiddenInput = document.getElementById("type");
				
					HiddenInput.value = arguments[1];
					cHttp.Post ("scripts/php/sessions.php","hidden_value",arguments[1],false);
				break;
				
				case "organisme":
					var HiddenInput = document.getElementById("type");
				
					HiddenInput.value = arguments[1];
					cHttp.Post ("scripts/php/sessions.php","hidden_value",arguments[1],false);
				break;
				
				case "session_person_infos":
					
					var FrmCoords = document.getElementById("coords");
					ServerResult = cHttp.Get ("scripts/php/sessions.php",arguments[1],arguments[1],false);
					
					fillInfosForm(FrmCoords,ServerResult);
					
					if (FrmCoords.Nom.value == "")
					{
						FrmCoords.Nom.value = Lastname;
					}
					
					if (FrmCoords.Prenom.value == "")
					{
						FrmCoords.Prenom.value = Firstname;
					}
				break;
				
				case "edit":
					
					if (arguments[2] != undefined)
					{
						if (arguments[2] != "")
						{
							switch (arguments[2])
							{
								case "sort":
									
									if (arguments[3] != undefined)
									{
										if (arguments[3] != "")
										{
											var GetVars = Array("action","sort","sorting");
											var GetValues = Array(arguments[1],arguments[3],arguments[4]);
											
											ServerResult = cHttp.Get("scripts/php/ajax.php",GetVars,GetValues,false);
											fillListView(ServerResult);
										}
									}
									
								break;
								
								case "modify":
									var Form = document.getElementById("user_manager");
									var GetVars = Array("action","id");
									var GetValues = Array(arguments[2],arguments[3]);
									var SubmitButton = document.getElementById("Send");
									
									SubmitButton.value = "Modifier";
									
									var HiddenInput = document.getElementById("type");
									
									HiddenInput.value = "mod_user";
									
									ServerResult = cHttp.Get("scripts/php/ajax.php",GetVars,GetValues,false);
									fillInfosForm(Form,ServerResult);
								break;
								
							}
						}
					}
					else
					{
						ServerResult = cHttp.Get("scripts/php/ajax.php","action",arguments[1],false);
						fillListView(ServerResult);
					}
					
				break;
			}
			
		}
	}
	
	cHttp = null;
	ServerResult = null;
}

function loadMenu(File,Position)
{
	var cHttp = new clsAJAX();
	var ServerResult;
	
	var MenuDiv = document.getElementById("div" + Position);
	
	ServerResult = cHttp.Get("scripts/php/body.php","menu",File,false);
	
	MenuDiv.innerHTML = "";
	MenuDiv.innerHTML = ServerResult;
}

function unloadMenu(Position)
{
	var MenuDiv = document.getElementById("div" + Position);
	MenuDiv.innerHTML = "";
}

function loadVideo(File,Position)
{
	var cHttp = new clsAJAX();
	var ServerResult;
	
	var VideoDiv = document.getElementById(Position);
	
	ServerResult = cHttp.Get("scripts/php/body.php","video",File,false);
	
	VideoDiv.innerHTML = "";
	VideoDiv.innerHTML = ServerResult;
}

//Afficher/masquer les onglets
function showTabs(mode)
{
	var DocTabs = document.getElementById("divTabs");
	var Logoff = document.getElementById("divTabLeft");
	
	if (mode == "show")
	{
		DocTabs.style.display = "block";
		Logoff.style.display = "block";
	}
	else if (mode == "hide")
	{
		DocTabs.style.display = "none";
		Logoff.style.display = "none";
	}
	else
	{
		DocTabs.style.display = "none";
		Logoff.style.display = "none";
	}
}

//Afficher/masquer les lignes d'un tablau HTML. (Ne fonctionne pas sous Microsoft Internet explorer 6 et 7. Pas testé sous la version 8.)
function showRows(table,mode)
{
	var DocTable = document.getElementById(table);
	var Node = DocTable.firstChild;
	while (Node != null) 
	{
		if (Node.nodeName == "CAPTION")
		{
			if (mode == "hide")
			{
				Node.setAttribute("onclick",'javascript:showRows("' + table + '","show")');
			}
			else if (mode == "show")
			{
				Node.setAttribute("onclick",'javascript:showRows("' + table + '","hide")');
			}
		}
		
		
		if (Node.nodeName == "TR")
		{
			if (mode == "hide")
			{
				
				Node.style.display = "none";
			}
			else if (mode == "show")
			{
				
				Node.style.display = "table-row";
			}
		}
		Node = Node.nextSibling;
	}
}

//Mettre en surbrillance un onglet
function hightLightTab(tabId)
{
	var tab = document.getElementById(tabId);
	
	tab.style.background = "#b0c6ef";
	tab.style.color = "red";
	tab.style.borderStyle = "outset";
	tab.style.cursor = "pointer";
}

//Remettre un onglet en surbrillance à son état normal
function resetTab(tabId)
{
	var tab = document.getElementById(tabId);
	
	if (selectedTab != tabId)
	{
		tab.style.background = "#b0c4de";
		tab.style.color = "black";
		tab.style.borderStyle = "inset";
	}
	else
	{
		tab.style.color = "black";
	}
}

//Sélectionner un onglet et charger la page relative
function selectTab(tabId)
{
	var cApp = new clsStartApp();
	var cHttp = new clsAJAX();
	var ServerResult;
	var tab = document.getElementById(tabId);
	var oldTab;
	//alert("selectedTab : " + selectedTab +" , tabId : " + tabId + " , oldTab : " + oldTab);
	if (tabId != selectedTab)
	{
		oldTab = selectedTab;
		selectedTab = tabId;
		
		cHttp.Post("scripts/php/sessions.php","session_current_page",selectedTab,false);
		
		if (oldTab != null)
		{
			resetTab(oldTab);
		}
		
		tab.style.background = "#b0c6ef";
		tab.style.color = "black";
		tab.style.borderStyle = "outset";
		
		cApp.load(tabId);
		//alert("selectedTab : " + selectedTab +" , tabId : " + tabId + " , oldTab : " + oldTab);
	}
	
}

function loadListView()
{
	var cHttp = new clsAJAX();
	var ServerResult;
	
	if (arguments[1] != undefined)
	{
		if (arguments[1] != "")
		{
			switch (arguments[1])
			{
				case "sort":
					
					if (arguments[2] != undefined)
					{
						if (arguments[2] != "")
						{
							var GetVars = Array("action","sort","sorting");
							var GetValues = Array(arguments[0],arguments[2],arguments[3]);
							
							ServerResult = cHttp.Get("/scripts/php/ajax.php",GetVars,GetValues,false);
							fillListView(ServerResult);
							
							
							changeListHeader("th_sort_" + arguments[2],"img_sort_" + arguments[2],arguments[2]);
						}
					}
					
				break;
				
				case "modify":
					alert (arguments[2]);
				break;
				
				case "delete":
					alert (arguments[2]);
				break;
			}
		}
	}
	else
	{
		ServerResult = cHttp.Get("/scripts/php/ajax.php","action",arguments[1],false);
		fillListView(ServerResult);
	}
}

function fillListView(Data)
{
	var TblListView;
	var ListViewLine;
	var ListViewCell;
	var ListViewCellText;
	var ActionButton;
	var Color;
	var NodeLength;
	var Users = eval ('(' + Data + ')');
	
	TblListView = document.getElementById("listview");
	
	if (NavigatorType == 'MSIE')
	{
		NodeLength = 1;
	}
	else
	{
		NodeLength = 3
	}
	
	if (TblListView.childNodes.length > NodeLength)
	{
		for (var i = TblListView.childNodes.length; i > NodeLength; i--)
		{
			
			TblListView.removeChild(TblListView.lastChild);
		}
	}
	
	
	for (var i = 0; i < Users.length; i++)
	{
		if (isEven(i))
		{
			Color = "#e8fdff";
		}
		else
		{
			Color = "#d1deef"
		}
		
		ListViewLine = document.createElement("TR");
		ListViewLine.setAttribute("style","background-color: " + Color + ";");
		
		ListViewCell = document.createElement("TD");
		ListViewCell.setAttribute("align","center");
		ListViewCell.setAttribute("valign","middle");
		
		ActionButton = document.createElement("IMG");
		ActionButton.setAttribute("src","/images/icones/pencil_16.png");
		ActionButton.setAttribute("style","cursor: pointer;");
		ActionButton.setAttribute("onclick","loadPage('secure_user_manager','edit','modify','" + Users[i].id + "')");
		ActionButton.setAttribute("title","Modifier");
		
		ListViewCell.appendChild(ActionButton);
		
		ActionButton = document.createElement("IMG");
		ActionButton.setAttribute("src","/images/icones/trash_16.png");
		ActionButton.setAttribute("style","cursor: pointer;");
		ActionButton.setAttribute("onclick","deleteUser('" + Users[i].id + "')");
		ActionButton.setAttribute("title","Supprimer");
		
		ListViewCell.appendChild(ActionButton);
		
		ListViewLine.appendChild(ListViewCell);
		
		ListViewCell = document.createElement("TD");
		ListViewCell.setAttribute("align","center");
		ListViewCell.setAttribute("valign","middle");
		ListViewCell.setAttribute("onmouseover","");
		ListViewCell.setAttribute("onmmouseout","");
		ListViewCell.setAttribute("onclick","");
		ListViewCellText = document.createTextNode(Users[i].id);
		
		ListViewCell.appendChild(ListViewCellText);
		ListViewLine.appendChild(ListViewCell);
		
		ListViewCell = document.createElement("TD");
		ListViewCell.setAttribute("align","center");
		ListViewCell.setAttribute("valign","middle");
		ListViewCell.setAttribute("onmouseover","");
		ListViewCell.setAttribute("onmmouseout","");
		ListViewCell.setAttribute("onclick","");
		ListViewCellText = document.createTextNode(Users[i].first_name + ' ' + Users[i].last_name);
		
		ListViewCell.appendChild(ListViewCellText);
		ListViewLine.appendChild(ListViewCell);
		
		ListViewCell = document.createElement("TD");
		ListViewCell.setAttribute("align","center");
		ListViewCell.setAttribute("valign","middle");
		ListViewCell.setAttribute("onmouseover","");
		ListViewCell.setAttribute("onmmouseout","");
		ListViewCell.setAttribute("onclick","");
		ListViewCellText = document.createTextNode(Users[i].is_admin);
		
		ListViewCell.appendChild(ListViewCellText);
		ListViewLine.appendChild(ListViewCell);
		
		TblListView.appendChild(ListViewLine);
	}
}

function changeListHeader(ThId,ImgId,SortItem)
{
	var SortTh = document.getElementById(ThId);
	var SortImg = document.getElementById(ImgId);
	
	if (SortImg.src.match(/up/))
	{
		SortImg.setAttribute("src","/images/icones/down_16.png");
		SortImg.setAttribute("title","tri décroissant");
		
		SortTh.setAttribute("onclick","loadListView('edit','sort','" + SortItem + "','ASC');");
		
	}
	else
	{
		SortImg.setAttribute("src","/images/icones/up_16.png");
		SortImg.setAttribute("title","tri croissant");
		SortTh.setAttribute("onclick","loadListView('edit','sort','" + SortItem + "','DESC');");
	}
}
