var id_filtre = 1;
var corps_mailing_visible = 1;

function focus_url() {
	$('methode_url').checked = 1;
}

function init_url() {
	$('url').value = 'http://';
	$('url').select();
}

function init_ftp() {
	$('ftp').value = 'ftp://';
	$('ftp').select();
}

function blur_url() {
	if($F('url') == '') $('url').value = 'http://';
}

function focus_ftp() {
	$('methode_ftp').checked = 1;
	$('ftp').select();
}

function init_ftp() {
	$('ftp').value = 'ftp://';
	$('ftp').select();
}

function blur_ftp() {
	if($F('ftp') == '') $('ftp').value = 'ftp://';
}

function change_suivre_liens() {
	if($('suivre_liens').checked) {
		$('methode_url').checked = 1;
		new Effect.BlindDown('bloc_profondeur', {duration:0.2});

	} else new Effect.BlindUp('bloc_profondeur', {duration:0.2});
}

function change_ftp_besoin_login() {
	if($('chbx_ftp_besoin_login').checked) {
		$('methode_ftp').checked = 1;
		$('login_ftp').select();
		new Effect.BlindDown('identification_ftp', {duration:0.3});

	} else new Effect.BlindUp('identification_ftp', {duration:0.3});
}

function focus_fichier() {
	$('methode_fichier').checked = 1;
}

function focus_cc() {
	$('methode_cc').checked = 1;
}

function verifForm() {
	var formOK = true;

	// ''''' RECHERCHE PAR SITE WEB '''''
	if($F('methode_url')){

		// Si aucune URL n'a été saisie
		if($F('url') == '' || $F('url') == 'http://') {
			alert("You did not enter any web address!");
			init_url();
			formOK = false;
		}

		// Si l'URL ne commence pas par http://
		else if(!$F('url').startsWith('http://')) {
			alert("The site address should begin with \"http://\" !");
			init_url();
			formOK = false;
		}

		if(!formOK) $("url").focus();
	}

	// ''''' RECHERCHE SUR SERVEUR FTP '''''
	else if($F('methode_ftp')){

		// Si aucune URL n'a été saisie
		if($F('ftp') == '' || $F('ftp') == 'ftp://') {
			alert("You did not enter any FTP address!");
			init_ftp();
			formOK = false;
		}

		// Si l'URL ne commence pas par ftp://
		else if(!$F('ftp').startsWith('ftp://')) {
			alert("The address of the FTP server must begin with \"ftp://\" !");
			init_url();
			formOK = false;
		}

		if(!formOK) $("ftp").focus();
	}

	// ''''' RECHERCHE PAR FICHIER '''''
	else if($F('methode_fichier')){
		if($F('fichier') == '') {
			alert("Please select a file to send.");
			$("fichier").focus();
			formOK = false;
		}
	}

	// ''''' RECHERCHE PAR COPIER/COLLER '''''
	else if($F('methode_cc')){
		if($F('texte') == '') {
			alert("You don't have past text!");
			$("texte").focus();
			formOK = false;
		}
	}

	// Validation des CGU
	if(formOK && $('cgu').checked == false) {
		alert("You must accept the Terms and conditions.");
		formOK = false;
	}

	return formOK;
}

function focus_a_copier() {
	$('a_copier').select();
}

function focus_separateur_car() {
	$('caractere_separateur').select();
}

function focus_separateur_car_print() {
	$('caractere_separateur_print').select();
}

function focus_separateur_car_print() {
	$('caractere_separateur_print').select();
}

function keyup_caractere_separateur() {
	if($F('caractere_separateur').empty())  $('chbx_separateur_car').checked = 0;
	else if(!$('chbx_separateur_car').checked) $('chbx_separateur_car').checked = 1;
}

function keyup_caractere_separateur_print() {
	if($F('caractere_separateur_print').empty())  	 $('chbx_separateur_car_print').checked = 0;
	else if(!$('chbx_separateur_car_print').checked) $('chbx_separateur_car_print').checked = 1;
}

function form_resultats_copier() {
	$('submit_resultats_copier').disable();
	$('submit_resultats_copier').value = 'Please wait...';

	var params = '';

	if($('chbx_separateur_ligne').checked) 	params  = '&chbx_separateur_ligne=1';
	if($('chbx_separateur_car').checked) 	params += '&chbx_separateur_car=1&caractere_separateur='+escape($F('caractere_separateur'));

	if($('chbx_inclure_chaine').checked) {
		var er = new RegExp("{m}");
		if(!er.test($F('inclure_chaine'))) {
			alert("The text must contain this string: {m}");
			init_btn_valider_export();
			return;
		}
		params += '&chbx_inclure_chaine=1&inclure_chaine='+encodeURIComponent($F('inclure_chaine'));
	}

	new Ajax.Updater(
		'a_copier',
		'index.php',{
			parameters: "xhr=a_copier&export=export_copier"+params,
			onComplete: init_btn_valider_export()
		}
	);
}

function init_btn_valider_export() {
	$('submit_resultats_copier').enable();
	$('submit_resultats_copier').value = 'Submit';
}

function check_chbx_inclure_chaine() {
	if(!$F("inclure_chaine").blank()) {
		$("chbx_inclure_chaine").checked = true;
	} else $("chbx_inclure_chaine").checked = false;
}

function focus_methode_sauvegarde_mail() {
	$('sauvegarde_email').select();
}

function focus_sauvegarde_email() {
	$('methode_sauvegarde_mail').checked = 1;
}

function form_resultats_sauvegarder() {
	if($('methode_sauvegarde_mail').checked && !isMail($F('sauvegarde_email'))) {
		alert('Invalid email address!');
		$('sauvegarde_email').select();
		return false;
	}
}

function ajouteFiltre() {
	new Ajax.Updater(
	'filtres',
	'index.php',{
		parameters: "xhr=ajout_filtre",
		insertion: Insertion.Bottom,
		onComplete: function(xhr){
			$("supprimer_filtres").show();

			if(xhr.responseText == '') alert("Your current status does not allow you to add additional filter.");
		}
	}
	);
	id_filtre++;
}

function suppFiltre(id) {
	new Ajax.Request(
	'index.php',{
		parameters: "xhr=supp_filtre&id_filtre="+id,
		onSuccess: function(xhr) {
			$("filtre"+id).remove();
			j = 1;

			for(i=0; i<=id+id_filtre+10; i++) {

				if($("filtre_span"+i) != null) {
					$("filtre_span"+i).update(j);
					j++;
				}

			} if(xhr.responseText == 0) $("supprimer_filtres").hide();
		}
	}
	);
}

function suppTousLesFiltres() {
	if(confirm('Are you sure you want to remove all filters?')) {
		new Ajax.Updater(
		'filtres',
		'index.php',{
			parameters: "a=supprimer_filtres&xhr=supprimer_filtres",
			method: 'get',
			onFinish: $("supprimer_filtres").hide()
		}
		);
	}
}

/*function scroll(id, offset) {
	new Effect.ScrollTo(id, {duration:2, offset:offset});
}*/

function toutCocher() {
	var max = $('liste_emails').immediateDescendants().length;
	for(i=1; i<=max; i++) $('email_'+i).checked = 1;
}

function toutDecocher() {
	var max = $('liste_emails').immediateDescendants().length;
	for(i=1; i<=max; i++) $('email_'+i).checked = 0;
}

function toutCocherMailing() {
	var max = $('liste_emails_mailing').immediateDescendants().length;
	for(i=1; i<=max; i++) $('email_mailing_'+i).checked = 1;
}

function toutDecocherMailing() {
	var max = $('liste_emails_mailing').immediateDescendants().length;
	for(i=1; i<=max; i++) $('email_mailing_'+i).checked = 0;
}

function ajouter_adresses_emailing(membre) {

	if(membre > 0) {

		var ids_emails = '';

		var max = $('liste_emails').immediateDescendants().length;
		for(i=1; i<=max; i++) if($('email_'+i).checked) ids_emails += i+",";

		if(ids_emails.blank()) alert("You have checked any email address!");

		else {
			new Ajax.Request(
				'index.php',{
					parameters: "xhr=ajouter_adresses_emailing&ids_emails="+ids_emails,
					onSuccess: function(xhr) {

						retour = xhr.responseText.evalJSON();

						$("mailing_aucune_adresse").hide();
						$("emails_mailing").show();

						$("liste_emails_mailing").update(retour.html);
						$("nb_emails_mailing").update(retour.total);

						if(retour.nb > 0) alert(retour.nb + " new email addresse(s) have been added to the mailing list.");
						else if(retour.nb == 0 && retour.max == 0) 	alert("The email addresses you have checked, are already in the mailing list.");
						else if(retour.nb == 0 && retour.max > 0) 	alert("Your current status does not allow you to add more than "+ retour.max + " emails in the mailing list.");
					}
				}
			);
		}
	} else alert("Your member status does not allow you to send a mailing.");
}

function supprimer_adresses_emailing() {

	var ids_emails = '';

	var max = $('liste_emails_mailing').immediateDescendants().length;
	for(i=1; i<=max; i++) if($('email_mailing_'+i).checked) ids_emails += i+",";

	if(ids_emails.blank()) alert("You have checked any email address!");

	else {

		if(confirm("Are you sure you want to remove all checked emails?")) {

			new Ajax.Request(
				'index.php',{
					parameters: "xhr=supprimer_adresses_emailing&ids_emails="+ids_emails,
					onSuccess: function(xhr) {

						retour = xhr.responseText.evalJSON();

						$("mailing_aucune_adresse").hide();
						$("emails_mailing").show();

						$("liste_emails_mailing").update(retour.html);
						$("nb_emails_mailing").update(retour.nb);

						if(retour.nb > 0) $("liste_emails_mailing").update(retour.html);
						else {
							$("emails_mailing").hide();
							$("mailing_aucune_adresse").show();
							nb_emails_mailing = 0;
						}
					}
				}
			);
		}
	}
}

function afficheAugmenteEnvois() {

	if(!$("ajout_envoi").visible()) {
		Effect.BlindDown('ajout_envoi', { duration: 0.5 });
		new Effect.Highlight('ajout_envoi', { delay: 0.6 });

	} else Effect.BlindUp('ajout_envoi', { duration: 0.5 });
}

function augmenteNbEnvois() {

	$("submit_augmente_envois").value = "...";
	$("submit_augmente_envois").disable();

	if($F("code_augmente_envois").blank()) {
		alert("You did not enter any code!");
		$("code_augmente_envois").focus();
		initBtnAugmenteNbEnvois();
	}

	else {
		new Ajax.Request(

			'index.php',{
				parameters: "xhr=augmente_envois&code="+$F("code_augmente_envois"),
				onSuccess: function(xhr) {

					retour = xhr.responseText;

					if(retour == 'vide') {
						alert("You did not enter any code!");
						$("code_augmente_envois").select();
						initBtnAugmenteNbEnvois();
					}
					else if(retour == 'bad') {
						alert("The code you entered is incorrect.");
						$("code_augmente_envois").select();
						initBtnAugmenteNbEnvois();
					}
					else {
						$("nb_envois_restant").update(retour);
						alert("The code is correct! The number of shipments mailing was increased to "+ retour +".");
						$("code_augmente_envois").clear();
						initBtnAugmenteNbEnvois();
					}
				}
			}
		);
	}
}

function initBtnAugmenteNbEnvois() {
	$("submit_augmente_envois").value = "OK";
	$("submit_augmente_envois").enable();
}

function focus_url_mailing_web() {
	$('format_mailing_web').checked = 1;
	new Effect.BlindUp('mailing_corps', {duration:0.3});
	corps_mailing_visible = 0;
	$('url_mailing_web').focus();
}

function active_corps_mailing() {
	if(corps_mailing_visible == 0) {
		new Effect.BlindDown('mailing_corps', {duration:0.3});
		corps_mailing_visible = 1;
	}
}

function setCorpsMailingValue(value) {
	$('mailing_corps').value = value;
}

function verifFormMailing(membre) {

	if(membre == 0) {
		alert("Your member status does not allow you to send a mailing.");
	}

	else if(!$("nom_expediteur").present()) {
		alert("You did not enter the sender's name!");
		$("nom_expediteur").focus();
	}

	else if(!$("email_expediteur").present()) {
		alert("You have not specified the email sender!");
		$("email_expediteur").focus();
	}

	else if(!isMail($F("email_expediteur"))) {
		alert("The e-mail sender is incorrect!");
		$("email_expediteur").focus();
	}

	else if($('format_mailing_web').checked == 0 && !$("mailing_corps").present()) {
		alert("The contents of your mailing is empty!");
		$("mailing_corps").focus();
	}

	else if($('format_mailing_web').checked == 1 && !$F('url_mailing_web').startsWith('http://')) {
		alert("The address of the website must begin with \"http://\" !");
		init_url_mailing_web();
	}

	else if($('format_mailing_web').checked == 1 && ($F('url_mailing_web') == '' || $F('url_mailing_web') == 'http://')) {
		alert("You have not specified the address of the web page's mailing!");
		init_url_mailing_web();
	}

	else if($("nb_emails_mailing").innerHTML == 0) {
		alert("You have not included any person in the mailing list!");
	}

	else if($("rapport_detaille_chbx").checked && !$("rapport_detaille").present()) {
		alert("You have not specified email address for the detailed report.");
		$("rapport_detaille").focus();
	}

	else if($("rapport_detaille_chbx").checked && !isMail($F("rapport_detaille"))) {
		alert("The email asked for the detailed report is not correct.");
		$("rapport_detaille").select();
	}

	else if(confirm("Are you sure you want to send your mailing?\n(Click OK to start sending)")) envoiMailing();
}

function init_url_mailing_web() {
	$('url_mailing_web').value = 'http://';
	$("url_mailing_web").select();
}

function check_rapport_detaille_chbx() {

	if(!$F("rapport_detaille").blank()) {
		$("rapport_detaille_chbx").checked = true;
	} else $("rapport_detaille_chbx").checked = false;
}

function envoiMailing() {

//	$("nb_mailing_a_envoyer").update($('liste_emails_mailing').immediateDescendants().length);

	$('details-mailing').hide();
	$('nb_envois_restants').hide();
	$('ajout_envoi').hide();
	$('sending_mailing').show();

	var format_mailing;

	if($('format_mailing_txt').checked) 		format_mailing = 'txt';
	else if($('format_mailing_html').checked) 	format_mailing = 'html';
	else if($('format_mailing_web').checked) 	format_mailing = 'web';

	var email_rapport = $("rapport_detaille_chbx").checked ? $F("rapport_detaille") : 0;

	new Ajax.Request(
	'index.php',{
		parameters: "xhr=envoi_mailing"
					+"&nom_expediteur="+encodeURIComponent($F('nom_expediteur'))
					+"&email_expediteur="+$F('email_expediteur')
					+"&format_mailing="+format_mailing
					+"&url_mailing="+$F('url_mailing_web')
					+"&sujet_mailing="+encodeURIComponent($F('sujet_mailing'))
					+"&email_rapport="+email_rapport
					+'&mailing_corps='+encodeURIComponent($F('mailing_corps')),

		onSuccess: function(xhr) {

			retour = xhr.responseText;

			if(retour == 'membre_ko') 					alert("Your member status does not allow you to send a mailing.");
			else if(retour == 'expediteur_email_ko') 	alert("The e-mail sender is incorrect!");
			else if(retour == 'nb_envoi_ko') 			alert("There is not enough shipments! Please remove emails from the mailing list, or click on the link \"Increasing the number of items\" above.");
			else {
				alert("The mailing has been sent !");
				$("nb_envois_restant").update(retour);
				$("nom_expediteur").clear();
				$("email_expediteur").clear();
				$("sujet_mailing").clear();
				$("mailing_corps").clear();
				$('url_mailing_web').value = 'http://';
			}

			$('details-mailing').show();
			$('nb_envois_restants').show();
			$('sending_mailing').hide();
		}
	});
}