const CAR = '"';
const RETOUR = "\n";

function exportation()
{
	var dir = filePicker("modeGetFolder", "", "");
	
	if (!dir)
	{
		return false;
	}
	else
	{
		var sepa=	$('sepa').value;

		var sql = "SELECT * FROM tbl_adresses " +
							"ORDER BY adr_nom, adr_prenom";
		
		var req=_exec_sql(sql);
		
		var txt="";
		
		if ($('afficher').value==1)
		{
			txt+=
				CAR+"Appellation"					+	CAR+sepa+
				CAR+"Titre"								+	CAR+sepa+
				CAR+"Nom"									+	CAR+sepa+
				CAR+"Prénom"							+	CAR+sepa+
				CAR+"Nom de jeune fille"	+	CAR+sepa+
				CAR+"Date de naissance"		+	CAR+sepa+
				
				CAR+"Pays 1"							+	CAR+sepa+
				CAR+"Adresse 1"						+	CAR+sepa+
				CAR+"CP 1"								+	CAR+sepa+
				CAR+"Ville 1"							+	CAR+sepa+
				CAR+"État 1"							+	CAR+sepa+
				
				CAR+"Pays 2"							+	CAR+sepa+
				CAR+"Adresse 2"						+	CAR+sepa+
				CAR+"CP 2"								+	CAR+sepa+
				CAR+"Ville 2"							+	CAR+sepa+
				CAR+"État 2"							+	CAR+sepa+
				
				CAR+"Tél. 1"							+	CAR+sepa+
				CAR+"Tél. 2"							+	CAR+sepa+
				CAR+"GSM 1"								+	CAR+sepa+
				CAR+"GSM 2"								+	CAR+sepa+
				CAR+"Fax."								+	CAR+sepa+
				CAR+"Mail 1"							+	CAR+sepa+
				CAR+"Mail 2"							+	CAR+sepa+
				CAR+"Web"									+	CAR+sepa+
				CAR+"Chat"								+	CAR+sepa+
				CAR+"VoIp"								+	CAR+
			RETOUR;
		}
		
		var c=0;
		while(req.executeStep())
		{
			let Row=_recupDatas(req);
			
			for (let i in Row)
			{
				Row[i] = String(Row[i]).replace(/\"/g, "\"\"");
				Row[i] = String(Row[i]).replace(/null/g, "");
			}
			
			let ddn="";			
			if (Row['adr_ddn']!="")
			{
				ddn = _date_francais(Row['adr_ddn']);
			}
			
			
			if (c>0)
				txt+=	RETOUR;
			

			let pays1 = _trouve_valeur("tbl_pays", Row['adr_fk_pays1'], "pay_id", "pay_libelle", "");
			let pays2 = _trouve_valeur("tbl_pays", Row['adr_fk_pays2'], "pay_id", "pay_libelle", "");
			
			c++;
			
			txt+=
				CAR+Row['adr_type']				+	CAR+sepa+
				CAR+Row['adr_titre']			+	CAR+sepa+
				CAR+Row['adr_nom']				+	CAR+sepa+
				CAR+Row['adr_prenom']			+	CAR+sepa+
				CAR+Row['adr_nom2']				+	CAR+sepa+
				CAR+ddn										+	CAR+sepa+
				
				CAR+pays1									+	CAR+sepa+
				CAR+Row['adr_adresse1']		+	CAR+sepa+
				CAR+Row['adr_cp1']				+	CAR+sepa+
				CAR+Row['adr_ville1']			+	CAR+sepa+
				CAR+Row['adr_etat1']			+	CAR+sepa+
				
				CAR+pays2									+	CAR+sepa+
				CAR+Row['adr_adresse2']		+	CAR+sepa+
				CAR+Row['adr_cp2']				+	CAR+sepa+
				CAR+Row['adr_ville2']			+	CAR+sepa+
				CAR+Row['adr_etat2']			+	CAR+sepa+
				
				CAR+Row['adr_tel1a']+Row['adr_tel1b']		+	CAR+sepa+
				CAR+Row['adr_tel2a']+Row['adr_tel2b']		+	CAR+sepa+
				CAR+Row['adr_gsm1a']+Row['adr_gsm1b']		+	CAR+sepa+
				CAR+Row['adr_gsm2a']+Row['adr_gsm2b']		+	CAR+sepa+
				CAR+Row['adr_faxa']+Row['adr_faxb']			+	CAR+sepa+
				
				CAR+Row['adr_mail1']		+	CAR+sepa+
				CAR+Row['adr_mail2']		+	CAR+sepa+
				CAR+Row['adr_web']			+	CAR+sepa+
				CAR+Row['adr_chat1']+" "+Row['adr_chat2']			+	CAR+sepa+
				CAR+Row['adr_voip1']+" "+Row['adr_voip2']			+	CAR;
		}
		
		req.reset();
		
		//$('exp').value=txt;
		
		save(dir, txt);
		
		if ($('open').checked)
		{
			_xul_open_directory(dir.path);
		}
		
		return true;
	}
}

var fileName = "XuloContacts-"+date("Y-m-d_H\\hi-s")+".csv";
function save(dir, output)
{
	dir = (dir.path+"\\").replace(/\\/g, "\\\\");
	
	var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
	file.initWithPath( dir + fileName );
	
	if ( file.exists() == false )
	{
		file.create( Ci.nsIFile.NORMAL_FILE_TYPE, 420 );
	}
	
	var outputStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance( Ci.nsIFileOutputStream );
	
	/* Open flags 
	#define PR_RDONLY       0x01
	#define PR_WRONLY       0x02
	#define PR_RDWR         0x04
	#define PR_CREATE_FILE  0x08
	#define PR_APPEND      0x10
	#define PR_TRUNCATE     0x20
	#define PR_SYNC         0x40
	#define PR_EXCL         0x80
	*/
	/*
	** File modes ....
	**
	** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
	** The 'mode' argument may be ignored by PR_Open on other platforms.
	**
	**   00400   Read by owner.
	**   00200   Write by owner.
	**   00100   Execute (search if a directory) by owner.
	**   00040   Read by group.
	**   00020   Write by group.
	**   00010   Execute by group.
	**   00004   Read by others.
	**   00002   Write by others
	**   00001   Execute by others.
	**
	*/
	outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );

	var result = outputStream.write( output, output.length );
	outputStream.close();
}