function selectFile(tb) {
	var file;
	if (tb == "audio") {
		file = goOpenFile(
				"Sélectionner un fichier...",
				false, "",
				[{name:"Fichier Wav", ext:"*.wav"}]);
	} else if (tb == "def") {
		file = goOpenFile(
				"Sélectionner un fichier...",
				false, "",
				[{name:"Fichier XML", ext:"*.xml"}]);
	} else {
		file = goOpenFile(
				"Sélectionner un fichier...",
				false, "",
				[{name:"Fichier XML", ext:"*.xml"}], Components.interfaces.nsIFilePicker.modeSave);
	}

	if(file) {
		var io = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
		var node = document.getElementById(tb);
		var descFileURI = io.newFileURI(file);
		if (tb == "def") {
			node.value = descFileURI.spec;
		} else {
			node.value = file.leafName;
			node.uri = descFileURI;
		}
	}
}

function doCancel() {
}

function doOK() {
	var io = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);

	var titleTb = document.getElementById("title");
	var defTb = document.getElementById("def");
	var descTb = document.getElementById("desc");
	var audioTb = document.getElementById("audio");

	var title = titleTb.value;
	var defURI_str = defTb.value;
	var defURI;
	try {
		defURI = io.newURI(defURI_str, null, null);
	} catch (e) {alert("Source de définition: Erreur."); return false;};
	if (!audioTb.uri) {
		alert("Source audio: Erreur."); return false;};
	if (!descTb.uri) {
		alert("Source de description: Erreur."); return false;};
	var descURI = descTb.uri;
	var audioURI = audioTb.uri;

	descURI.QueryInterface(Ci.nsIFileURL);
	if (!descURI.file.exists()) {
		try {
			descURI.file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0664); 
		} catch (e) {alert("Impossible de créer le fichier de description."); return false;};
	}



	var templateURI = io.newURI("chrome://just/content/template.xml", null, null);


	var channel = io.newChannelFromURI(templateURI);
	var DS = Components.classes["@mozilla.org/content/syncload-dom-service;1"].
		getService(Components.interfaces.nsISyncLoadDOMService);
	var doc = DS.loadDocumentAsXML(channel, null);
	var rootElt = doc.getElementsByTagName("audiofile")[0];
	rootElt.setAttribute("title", title);
	rootElt.setAttribute("def", defURI.spec);
	rootElt.setAttribute("src", audioURI.spec);

	var XS = new XMLSerializer();
	var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		.createInstance(Components.interfaces.nsIFileOutputStream);
	foStream.init(descURI.file, 0x02 | 0x08 | 0x20, 0664, 0);
	XS.serializeToStream(doc, foStream, "");
	foStream.close();

	window.arguments[0].fileOpened(descURI.file);
	return true;
}
