/*******************************************************************************************/

function getXhr()
{
			var xhr = null; 
			if(window.XMLHttpRequest) // Firefox et autres
			   xhr = new XMLHttpRequest(); 
			else if(window.ActiveXObject)
			{ // Internet Explorer 
			   try {
						xhr = new ActiveXObject("Msxml2.XMLHTTP");
				   } catch (e) 
				   {
						xhr = new ActiveXObject("Microsoft.XMLHTTP");
				   }
			}
			else { // XMLHttpRequest non supporté par le navigateur 
					alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
					xhr = false; 
				 } 

			return xhr;
}

/*******************************************************************************************/

function creer_fichier()
{
			var xhr = getXhr()

			xhr.onreadystatechange = function()
			{
				//  le serveur répond ok
				if(xhr.readyState == 4 && xhr.status == 200)
				{
					retour = xhr.responseText;
					document.getElementById('msg_debug').innerHTML = retour;
					
					if(retour == "1")
					{
						
						$(function()
						{
						$( "#message_creer_fichier" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
												recharger();
											}
									 }
							});
						});
					}
					
					if(retour == "2")
					{
						
						$(function()
						{
						$( "#message_fichier_existe" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
											}
									 }
							});
						});
					}
						
				}
			}
				
			fichier = document.getElementById('new_file').value;
			base = document.getElementById('base').value;
			chemin = document.getElementById('chemin').value;
			//alert(fichier+" " + base + "  " +chemin);
			xhr.open("POST","editeur_creer_fichier.php",true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			//xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_score="+selection_score);
			xhr.send("fichier="+fichier+ "&base="+base+ "&chemin="+chemin);

}

/*******************************************************************************************/

function sauver_fichier()
{
				var xhr = getXhr()

				xhr.onreadystatechange = function()
				{
					//  le serveur répond ok
					if(xhr.readyState == 4 && xhr.status == 200)
					{
						
						retour = xhr.responseText;
													
						$(function()
						{
									$( "#message_sauvegarde" ).dialog({
									modal: true,
									title: "Infos",
									buttons: {
												Ok: function()
												{
													$( this ).dialog( "close" );
													
												}
											 }
										});
						});
						
					}
				}
				
				fichier = document.getElementById('fichier').value;
				contenu = document.getElementById('contenu').value;
				xhr.open("POST","editeur_sauver_fichier.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				//xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_score="+selection_score);
				xhr.send("fichier="+fichier+"&contenu="+contenu);
			
}

/*******************************************************************************************/

function creer_repertoire()
{
			var xhr = getXhr()

			xhr.onreadystatechange = function()
			{
				//  le serveur répond ok
				if(xhr.readyState == 4 && xhr.status == 200)
				{
					retour = xhr.responseText;
					document.getElementById('msg_debug').innerHTML = retour;
					
					if(retour == "1")
					{
						
						$(function()
						{
						$( "#message_creer_repertoire" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
												recharger();
											}
									 }
							});
						});
					}
					
					if(retour == "2")
					{
						
						$(function()
						{
						$( "#message_repertoire_existe" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
											}
									 }
							});
						});
					}
						
				}
			}
				
			repertoire = document.getElementById('new_dir').value;
			base = document.getElementById('base').value;
			chemin = document.getElementById('chemin').value;
			//alert(repertoire+" " + base + "  " +chemin);
			xhr.open("POST","editeur_creer_repertoire.php",true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			//xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_score="+selection_score);
			xhr.send("repertoire="+repertoire+ "&base="+base+ "&chemin="+chemin);

}

/*******************************************************************************************/

function upload_fichier()
{
		document.getElementById("message_patience").style.display='block';
		
        var formData = new FormData();
        //$_FILES["file" + i]
        for (var i = 0, len = document.getElementById('fichiers_choix').files.length; i < len; i++)
		{
            formData.append("file" + i, document.getElementById('fichiers_choix').files[i]);
        }
 
        $.ajax({
            url: "upload_multiple03.php", 
            type: 'post',
            data: formData,
            dataType: 'html', 
            async: true,
            processData: false, 
            contentType: false, 
            success: function (response) 
			{
				document.getElementById('message').innerHTML = response;
				$( "#message" ).dialog({
									modal: true,
									title: "Infos",
									buttons: {
												Ok: function()
												{
													$( this ).dialog( "close" );
													recharger();
												}
											 }
										});
				
				
            },
			
			/*success : function(data) {
                $('#upload-result').append('<div class="alert alert-success"><p>Fichier(s) envoyés avec succes!</p><br />');
                $('#upload-result .alert').append(data);
            },*/
			
            error : function(request) 
			{
                console.log(request.responseText);
            }
        });
   
		document.getElementById("message_patience").style.display='none';
	
}


function operation_fichier(chemin,fichier,operation)
{
	if(operation == "copie")
	{
			$( "#fenetre_copier" ).dialog({
					modal: true,
					title: "Copier",
					buttons: {
								Valider: function()
								{
									$( this ).dialog( "close" );
									traitement_fichier(chemin,fichier,operation);
								},
								Non: function()
								{
									$( this ).dialog( "close" );
								}
							 }
			});
	}
	
	if(operation == "efface")
	{
		
			$( "#message_confirmation_suppression" ).dialog({
					modal: true,
					title: "Infos",
					buttons: {
								Oui: function()
								{
									$( this ).dialog( "close" );
									traitement_fichier(chemin,fichier,operation);
								},
								Non: function()
								{
									$( this ).dialog( "close" );
								}
							 }
			});
	}
	
	if(operation == "renome")
	{
	
		$( "#fenetre_renomer" ).dialog({
					modal: true,
					title: "Renomer",
					buttons: {
								Valider: function()
								{
									$( this ).dialog( "close" );
									traitement_fichier(chemin,fichier,operation);
								},
								Non: function()
								{
									$( this ).dialog( "close" );
								}
							 }
		});
			
	}
	
	if(operation == "deplace")
	{
		
		$( "#fenetre_deplace" ).dialog({
					modal: true,
					title: "Déplacer",
					 width: 800,
					height: 600,
					modal: true,
					buttons: {
								Valider: function()
								{
									$( this ).dialog( "close" );
									traitement_fichier(chemin,fichier,operation);
								},
								Non: function()
								{
									$( this ).dialog( "close" );
								}
							 }
					});
	}
	
	
}


function traitement_fichier(chemin,fichier,operation)
{

	if(operation == "copie")
	{
			fichier = document.getElementById('nom_copie').value;
			//alert(fichier+chemin);
			
			$.ajax({
            url: "operation_fichier.php", 
            type: 'post',
            data: 'fichier=' + fichier + '&chemin=' + chemin + '&operation=' + operation,
            dataType: 'html', 
            success: function (response) 
			{
				//alert(response);
				//document.getElementById('message').innerHTML = response;
				
				$(function()
				{
						$( "#message_fichier_copier").dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
												recharger();
											}
									 }
							});
				});
				
            },
            error : function(request) 
			{
                console.log(request.responseText);
            }
        }); 
			
	}
	
	if(operation == "efface")
	{
	
		$.ajax({
            url: "operation_fichier.php", 
            type: 'post',
            data: 'chemin=' + chemin + '&operation=' + operation,
            dataType: 'html', 
            success: function (response) 
			{
				alert(response);
				$(function()
				{
						$( "#message_suppression_fichier" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
												recharger();
											}
									 }
							});
				});
				
            },
            error : function(request) 
			{
                console.log(request.responseText);
            }
        }); 
	}
	
	if(operation == "renome")
	{
		
		fichier = document.getElementById('nouveau_nom').value;
		    
        $.ajax({
            url: "traitement_fichier.php", 
            type: 'post',
            data: 'nouveau_nom=' + fichier + '&chemin=' + chemin,
            dataType: 'html', 
            success: function (response) 
			{
				$(function()
				{
						$( "#message_fichier_renomer" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
												recharger();
											}
									 }
							});
				});
				
            },
            error : function(request) 
			{
                console.log(request.responseText);
            }
        }); 
	}
	
	if(operation == "deplace")
	{
			sel = document.getElementById('select_repertoire');
			selection_repertoire = sel.options[sel.selectedIndex].value;
			//alert(chemin + fichier + selection_repertoire);	
			
			$.ajax({
            url: "operation_fichier.php", 
            type: 'post',
            data: 'fichier=' + fichier + '&selection_repertoire=' + selection_repertoire + '&chemin=' + chemin + '&operation=' + operation,
			dataType: 'html', 
            success: function (response) 
			{
				alert(response);
				if(response == "1")
				{
						$(function()
						{
						$( "#message_fichier_deplacer" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
												recharger();
											}
									 }
							});
						});
				}
				
				if(response == "2")
				{
						$(function()
						{
						$( "#message_fichier_deplacer_erreur" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
											{
												$( this ).dialog( "close" );
											}
									 }
							});
						});
				}
				
            },

            error : function(request)
			{
                console.log(request.responseText);
            }

		}); 
		
	}
}

/*******************************************************************************************/
