function include(src) {
	$('body').append('<script src="'+src+'"></script>');
}

var isIe = $.browser.msie;
var typologie, origine, destination, destinationFrance, destinationDOM, destinationDOMListe, destinationMonde, destinationMondeListe, destinationDOMtoFrance, destinationDOMToMonde, destinationDOMToDOM, details, blocPoids, nature, recommandation, poids, actions;
var etape; // position dans les étapes du formulaire (courrier/colis, origine/destination...) pour gestion bouton retour.
var defaultValCountry = '';
var exceptionIdApplied = new Array();
var exceptionLevel = -1;
var det_envoi = $('#ct_det_envoi');
var natureChecked = false;
var postExportList = $('#ct_det_envoi_postExportListe');
var postExportListSave = '';
var rebuild_reco;
var returnForm = $('#returnForm')[0] ? true : false;
var recoFlag = returnForm;
var resultForm = $("input[name='isResult']")[0] ? true : false;
var expSuivi = $('#ct_det_reco_exportSuivi_wrap');
var securExportWrapper = $('#ct_det_reco_securexport_container');
var fromException = {239: '#dom_tom_to_etranger'};
var postExportEmbFiltre = new Array();

var gestionPostExportListTrigger;

var allowedPaysSecurExportPostExportSecur = ['57', '23', '59', '67', '69', '76', '103', '133', '164', '180', '77', '192', '43'];
//Allemagne, Belgique, Danemark, Espagne, Finlande, Grande-Bretagne, Irlande, Luxembourg, Pays-Bas, Portugal, Royaume-uni, Suède, Suisse. Pour securExport et PostExportSecur
var pays = $('select.destination_pays');

include('/design/laposte/javascript/tarifs.utils.js');
include('/design/laposte/javascript/tarifs.masse.js');
include('/design/laposte/javascript/tarifs.details.js');

$(function() {
	init();

	/**** Functions ****/
	function init() {
		setMasse.init({daddy:'#ct_nature_envoie', target:'#ct_det_poids', returnForm:returnForm});
		typologie = $('#ct_typologie');
		destination = $('#ct_destination').turnOff();
		details = $('#ct_details').turnOff();
		blocPoids = $('#ct_poids').turnOff();
		actions = $('#actions');
		if (!resultForm) {
			actions.turnOff();
		}
		destinationDOMListe = $('#ct_dest_ff_liste_dom');
		destinationMondeListe = $('#ct_dest_ff_liste_pays');
		nature = $('#ct_nature_envoie');
		$('.destination_pays').find('option[value=""]').text('');
		$('#country_choice').focus(function() {
			if (defaultValCountry == '') {
				defaultValCountry = $(this).val();
				$(this).val('')
			}
		});

		$('.destination_pays_colis').find('option[value=""]').text('');

		$('#calcTarif').submit(function() {
			return calculer_tarif_verifForm()
		});
		
		if(returnForm){
			gerePostExportEmbList(postExportEmbFiltre);
		}

		etapeTypologie();
		etapeDestination();
		etapeNature();
		etapeDetails();
		etapePoids();
		if (!resultForm) {
			boutonRetour();
			nouvelleRecherche();
		}
		gestionPostExportList();
		exportSuivi();
		securexport();
		gestionOptionsColisIntl();
		returnForm = false;

		infosPopIn();
		adjustheight($('#resumerRecherche .radio_back'), 2);

		pageType.init();
	}

	function etapeTypologie() {
		typologie.find(':radio').click(function() {
			selectionOption($(this));
			if($('#ct_typo_courrier').attr('class')=="radio off") {
				typo_envoi="colis";
				paysAutoCompletion($('#country_choice'), $('.destination_pays_colis option'));
			} else if($('#ct_typo_colis').attr('class')=="radio off") {
				typo_envoi="courrier";
				paysAutoCompletion($('#country_choice'), $('.destination_pays option'));
			}
			actions.show();
			destination.show().find(':checked').attr('checked','checked');
			destinationDOMListe.hide();
			destinationMondeListe.turnOff();
			details.turnOff();
			if ($(this).val() == "courrier" /*&& !$('#ct_dest_ff_monde:selected')*/) {
				$('#ct_det_reco_ar, #ct_det_reco_ar_int').attr('disabled', 'disabled');
				$('.warningsColisTarifs').hide();
				// J'ai commenté la zone pour montrer la pratique à ne pas faire... C'est pas dynamique du tout ça et pas clair.
				// $('#destination_ff_dom_tom').find(':option[value=239]').hide();
			} else {
				$('#ct_det_reco_ar_int, #ct_det_reco_ar').removeAttr('disabled');
				$('.warningsColisTarifs').show();
			}
		}).filter(':checked').click();
	}

	function etapeDestination() {
	
		destinationFrance = $('#ct_dest_ff_france');
		destinationDOM = $('#ct_dest_ff_dom');
		destinationMonde = $('#ct_dest_ff_monde');
		destinationFromDOM = $('#destination_ff_dom_tom');
		destinationToDOM = $('#ct_to_dom_tom');
		
		destinationFromDOMToMonde = $('#dom_tom_to_etranger');
		destinationFromDOMToFrance = $('#dom_tom_to_france');
		destinationFromDOMToDOM = $('#destination_ff_to_dom_tom');
		
		destinationDOMListe.hide();
		destinationMondeListe.turnOff();
		destinationToDOM.turnOff();
		
		var colissimoEmballages = $("#ct_det_envoi_colissimoEmballage_choix");
		var chronopostEmballages = $("#ct_det_envoi_chronopostPaE_choix");
		var allEmballagesFrance = colissimoEmballages.add(chronopostEmballages);
		
		var colissimoIntlEmballages = $("#ct_det_envoi_colissimoEmballageIntl_choix");
		var chronopostIntlEmballages = $("#ct_det_envoi_chronopostPaEIntl_choix");
		var allEmballagesInternational = colissimoIntlEmballages.add(chronopostIntlEmballages);
		
		var allEmballages = allEmballagesFrance.add(allEmballagesInternational);
		
		backupSelectOptions(allEmballages);
		
		destinationFrance.click(function() {
			selectionOption($(this), null, $(this).parent().find('label:eq(1)'));
			$('#ct_from_france, #ct_bloc_destination_dom_tom').turnOff();
			handleDetails.init();
			destinationDOMListe.hide();
			destinationMondeListe.turnOff();
			if (typologie.find(':radio:checked').is('.colis')){
				reinjectAllOptions(chronopostEmballages);
				chronopostEmballages.find('.chrono_fd').not('.chrono_ff, .national').remove();
			}
			$('#ct_bloc_destination_dom_tom input').attr('checked', false);			
		}).filter(':checked').click();

		destinationDOM.click(function() {
			selectionOption($(this), null, $(this).parent().find('label:eq(1)'));
			$('#ct_from_france, #ct_bloc_destination_dom_tom').turnOff();
			handleDetails.init();
			details.turnOff();
			destinationDOMListe.show();
			destinationMondeListe.turnOff();
			if (typologie.find(':radio:checked').is('.colis')){
				reinjectAllOptions(chronopostEmballages);
				chronopostEmballages.find('.chrono_fd').not('.chrono_ff').remove();
				allEmballagesFrance.find('.national').remove();
			}
			$('#ct_bloc_destination_dom_tom input').attr('checked', false);
		}).filter(':checked').click();

		destinationMonde.click(function() {
			selectionOption($(this), null, $(this).parent().find('label:eq(1)'));
			$('#ct_from_france, #ct_bloc_destination_dom_tom').turnOff();
			handleDetails.init();
			details.turnOff();
			destinationDOMListe.hide();
			destinationMondeListe.turnOn();
			
			if (typologie.find(':radio:checked').is('.colis')){
				reinjectAllOptions(chronopostIntlEmballages);
				chronopostIntlEmballages.find('.chronoIntl_fd').remove();
			}
			$('#ct_bloc_destination_dom_tom input').attr('checked', false);
			
		}).filter(':checked').click();
		
		destinationFromDOM.change(function() {
			var elmt = $(this);
			if ($('#ct_dest_ff_dom_tom').is(':checked') || elmt.find('option:selected').val() != '') {
			
				if (!$('#ct_dest_ff_dom_tom').is(':checked')) {
					$('#ct_dest_ff_dom_tom').attr('checked', 'checked');
				}
				destSelected = elmt.find('option:selected').val();
				selectionOption(elmt, null, elmt.parent().find('label:eq(1)'));
				$('#ct_bloc_destination, #ct_dest_ff_dom_tom, #destination_ff_dom_tom').turnOff();
				$('#ct_bloc_destination input').attr('checked', false);
				elmt.parent().find('.result').text(elmt.find('option:selected').text());
				destinationToDOM.turnOn();
				
				postExp = postExportList.find('option');
				postExportEmbFiltre = postExp;
				postExp.filter(function(){
					var elmt = $(this);
					var value = elmt.attr('value');
					return !(value == '' || value == 'P01' || value == 'P02' || value == 'E01' || value == 'E02');
				}).remove();
			}
		}).find("option[value!='']:selected:first").parent().change();
				
		$('#ct_dest_ff_dom_tom, #dom_tom_to_dom_tom').click(function(){
			$(this).parent().find('select option:selected').attr('selected', false).parent().find('option:eq(0)').attr('selected', 'selected');
		});
		
		destinationToDOM.find('input:radio').click(function() {
			if ($('#dom_tom_to_etranger')[0] == $(this)[0]) {
				$('#ct_dest_ff_liste_pays').turnOn();
				$('#ct_to_dom_tom .destination_from_france_ul').turnOff();
			} else if ($('#dom_tom_to_france')[0] == $(this)[0]) {
				gere_from_DomTom($('#destination_ff_dom_tom').find('option:selected'), $(this));
				handleDetails.init();
			}
			reinjectAllOptions(colissimoEmballages);
			colissimoEmballages.find('option.national').remove();
		}).end().find('select').change(function() {
			if ($('#destination_ff_to_dom_tom')[0] == $(this)[0] && ($('#dom_tom_to_dom_tom').is(':checked') || $(this).find('option:selected').val() != ''))
			{
				$('#dom_tom_to_dom_tom').attr('checked', true);
				if(isDomLointain()) {
					$('#ct_det_envoi_colissimoEmballage, #ct_det_envoi_colissimoEmballage_choix').attr('disabled', 'disabled');
				} else {
					$('#ct_det_envoi_colissimoEmballage, #ct_det_envoi_colissimoEmballage_choix').removeAttr('disabled');
				}
				gere_from_DomTom($('#destination_ff_dom_tom').find('option:selected'), $(this).find('option:selected'));
				handleDetails.init();
			}
		});
		
		destinationDOMListe.find('select').add(destinationMondeListe.find('select')).change(function() {
			if ($(":selected:not([value=''])", $(this)).length) {
				handleDetails.init();
			} else if (!$('#ct_dest_ff_dom:checked') && !$('#ct_dest_ff_monde:checked')) {
				details.turnOff();
			}
		}).find("option[value!='']:selected").parent().change();
		
		destinationFromDOMToMonde.click(function(){
			if (typologie.find(':radio:checked').is('.colis')){
				if ($('#ct_dest_ff_dom_tom').is(':checked') && $(this).find('option:selected').val() != ''){
					reinjectAllOptions(chronopostIntlEmballages);
					chronopostIntlEmballages.find('.chronoIntl_ff').remove();						
				}
			}
			if(returnForm) $('#country_submit').click();
		}).filter(':checked').click();
		
		destinationFromDOMToFrance.each(function(){
			var elmt = $(this);
			elmt.data('options', elmt.html());
		}).click(function(){
			var elmt = $(this);
			elmt.html()
			if (typologie.find(':radio:checked').is('.colis')){
				if ($('#ct_dest_ff_dom_tom').is(':checked') && elmt.find('option:selected').val() != ''){
					reinjectAllOptions(chronopostEmballages);
					chronopostEmballages.find('.chrono_ff').not('.chrono_fd').remove();
				}
			}
		}).filter(':checked').click();
		
		destinationFromDOMToDOM.change(function(){
			if (typologie.find(':radio:checked').is('.colis')){
				if ($('#ct_dest_ff_dom_tom').is(':checked') && $(this).find('option:selected').val() != ''){
					reinjectAllOptions(chronopostEmballages);
					chronopostEmballages.find('.chrono_ff, .chrono_fd').not('.chronoIntl').remove();
				}
			}
		}).filter(':selected').change();
		
		$("#country_choice").bind("keypress", function(e) {
			if (e.keyCode == 13) {
				if($('ul.ui-autocomplete').css("display") != "none"){
					$('#country_submit').click();
					$('ul.ui-autocomplete').css("display","none");
				}
				return false;
			}
		});

		function backupSelectOptions(elmt) {
			elmt.each(function() {
				var elmt = $(this);
				if (elmt.data('options')) {
					return;
				}
				var elmt = $(this);
				elmt.data('options', elmt.html());
			});
		}
		
		function reinjectAllOptions(elmt) {
			elmt.each(function() {
				var elmt = $(this);
				var selected = elmt.find(':selected').attr('value');
				elmt.html(elmt.data('options'));
				setSelectedOptions(elmt, selected);
			});
			
			function setSelectedOptions(elmt, selected) {
				elmt.find(':selected').removeAttr('selected');
				elmt.find('option[value='+selected+']').attr('selected', 'selected');
			}
		}
	}
	
	function etapeDetails() {
		var detailsRecommande = $('#ct_det_reco, #ct_det_recoInt');
		var choixRecommande = $('#ct_det_reco_oui_choix').hide();
		var choixRecommandeInt = $('#ct_det_reco_int_oui_choix').hide();
		
		var recommandeOuiNon = $(":radio[name=recoON]", detailsRecommande);
		var recommandeIntOuiNon = $(":radio[name=recoIntON]", detailsRecommande);
		var choixDuRecommande = $(":radio[name=reco], :radio[name=reco_int]", detailsRecommande);
		var optionsDiverses = $(":checkbox", detailsRecommande);
		var optionAr = $('#ct_det_reco_ar, #ct_det_reco_ar_int');
		
		recommandeOuiNon.click(function() {
			var elmt = $(this);
			
			hideR4R5ifNeeded();
			
			recommandeOuiNon.parents('.valid:first').removeClass('valid');
			elmt.parents('li:first').addClass('valid');
			
			if (elmt.is('[value=oui]')) {
				choixRecommande.show();
				if (!optionAr.filter(':checked').is(':checked')) {
					optionAr.attr("disabled", "disabled");
				}
			} else {
				$('#ct_det_reco_oui_r1, #ct_det_reco_oui_r2, #ct_det_reco_oui_r3, #ct_det_reco_oui_r4, #ct_det_reco_oui_r5, #ct_det_reco_ar, #ct_det_reco_ar_int').each(function() {
					var elmt = $(this);
					setParentElements(elmt, 'li', 'valid', 'ul', 1);
					elmt.removeAttr('checked');
				});
				choixRecommande.hide();
				optionAr.attr("disabled", "disabled");
			}
			
			etapePoids();
			
			function hideR4R5ifNeeded() {
				if (
					$('#ct_det_envoi_lp').attr('checked') && 
					(
						$('#ct_dest_ff_france').attr('checked') || 
						$('#ct_dest_ff_dom').attr('checked') || 
						$('#ct_dest_ff_dom_tom').attr('checked') || 
						$('#dom_tom_to_france').attr('checked')
					)
				) {
					$('#ct_det_reco_oui_choix li:eq(3), #ct_det_reco_oui_choix li:eq(4)').turnOff();
				}
			}
			
		}).filter(':checked').click();
		
		recommandeIntOuiNon.click(function() {
			var elmt = $(this);
			
			recommandeIntOuiNon.parents('.valid:first').removeClass('valid');
			elmt.parents('li:first').addClass('valid');
			
			if (elmt.is('[value=oui]')) {
				choixRecommandeInt.show();
				if (!optionAr.filter(':checked').is(':checked')) {
					optionAr.attr("disabled", "disabled");
				}
			} else {
				$('#ct_det_reco_exportSuivi, #ct_det_reco_securexport, #ct_det_reco_r1, #ct_det_reco_oui_r2, #ct_det_reco_ar_int').each(function() {
					var elmt = $(this);
					setParentElements(elmt, 'li', 'valid', 'ul', 1);
					elmt.removeAttr('checked');
				});
				choixRecommandeInt.hide();
				optionAr.attr("disabled", "disabled");
			}
			
			etapePoids();
			
		}).filter(':checked').click();
		
		choixDuRecommande.click(function() {
			var elmt = $(this);
			
			optionAr.removeAttr('disabled');
			
			$('#ct_det_poids_wrapper').turnOn();
			setParentElements(choixDuRecommande, 'li', 'valid', 'ul', 1);
			setParentElements(elmt, 'li', 'valid', 'ul');
			
			etapePoids();
			
		}).filter(':checked').click();
		
		optionsDiverses.click(function() {
			$('#ct_det_poids_wrapper').turnOn();
			setParentElements($(this), 'li', 'valid', 'ul', ($(this).attr('checked') ? 0 : 1));
		});
	};
	
	function etapeNature() {
		/*
		Ces 2 tableaux sont utilisés pour le système de règles d'affichage des blocs en fonction des options choisies.
		Le 1er tableau contient les id des input qui doivent cacher un bloc dont l'id est contenu dans le 2eme tableau.
		Ils sont liés par leur clé.
		le second tableau peut contenir un id, un tableau d'id ou faire appel à une fonction -- sans paramètres.
		*/
		var exceptionClick = [	'ct_det_envoi_eco',					'ct_det_envoi_docEco',				'ct_det_envoi_marchEco',
								/*'ct_det_envoi_docPrio', 			*/'ct_det_envoi_colissimoEmballage',	'ct_det_envoi_chronopost',
								'ct_det_envoi_chronopostPaEIntl', 	'ct_det_envoi_chronopostPaE',		'ct_det_envoi_postExport',
								'ct_det_envoi_colissimoEmballageIntl', 'ct_det_envoi_chronopostDomTom_PaE', 'ct_det_envoi_chronopostDomTom_PaEIntl',
								'ct_det_envoi_lverte_nat',	    'ct_det_envoi_lverte_inter'];

		var exceptionTarget = [	'ct_det_reco',						'ct_det_recoInt',					'ct_det_recoInt',
								/*'ct_det_reco_securexport_container',*/'ct_det_reco',						'ct_det_reco',
								'ct_det_recoInt',					'ct_det_reco',						'ct_det_recoInt',
								'ct_det_recoInt', 					'ct_det_reco', 						'ct_det_reco',
								'ct_det_reco',		    'ct_det_recoInt'];
		
		var level = 0;	
		nature.find('option').click(function(){
			idselect = $(this).parent().attr('id');
			suppl = 6;
			idradio = idselect.substr(0, idselect.length-suppl);
			
			if($('#'+idradio).attr('checked') == false){
				$('#'+idradio).click();
				$('#'+idradio).attr('checked', true);
			}
		});
		
		nature.find('input').click(function() {
			var elmt = $(this);
			
			if (!$('#ct_det_reco_oui').is(':checked')) {
				$('#ct_det_reco_oui_choix').hide();
			}
			
			if (!$('#ct_det_reco_int_oui').is(':checked')) {
				$('#ct_det_reco_int_oui_choix').hide();
			}
			
			if (destination.find(':radio:checked').is('.international')) {
				$('#ct_det_recoInt').show();
			} else {
				$('#ct_det_reco').turnOn();
				$('#ct_det_recoInt').hide();
			}
			
			try {
				if ($('#'+exceptionIdApplied[level]).hasClass('off')) {
					$('#'+exceptionIdApplied[level]).turnOn();
				}
				
				if ($('#'+exceptionIdApplied[level]).css('display') != 'show') {
					$('#'+exceptionIdApplied[level]).show();
				}
			} catch(e) {;}
			
			dim = exceptionClick.in_array(elmt.attr('id'));
			if (dim != -1) {
				exceptionIdApplied[level] = exceptionTarget[dim];
				treateElement(exceptionTarget[dim]);
			} else {
				exceptionIdApplied[level] = -1;
			}
			handleDetails.setException(exceptionIdApplied[level]);
			
			if (!returnForm) {
				if (elmt.parentHasClass('valid', 'li') == false) {
					$('#ct_bloc_recommandation, #ct_bloc_recommandation_options').resetInput();
				}
				nature.find('.valid').removeClass('valid');
			}
			
			setParentElements(elmt, 'li', 'valid', 'ul');
			if ($('#ct_dest_ff_dom_tom').attr('checked')) {
				$('option:not(option:eq(0), option:eq(1), option:eq(2))', postExportList).hide();
			}
			
			etapePoids();
			
		}).filter(':checked').click();
	}
	
	function exportSuivi() {
		expSuivi.hide();
		var natureEnvoi = $('#ct_nature_envoie :radio[name=nature]');
		
		var expSuiviCheckbox = $('#ct_det_reco_exportSuivi');
		natureEnvoi.click(function() {
			var elmt = $(this);
			if (
				(
					elmt.is('#ct_det_envoi_docPrio') || 
					elmt.is('#ct_det_envoi_marchPrio')
				) && 
				//allowedPaysSecurExportPostExportSecur.in_array(pays.val()) != -1 && 
				!$('#ct_dest_ff_dom_tom').attr('checked')
			) {
				expSuivi.show();
			} else {
				expSuivi.hide();
			}
		});
		
		expSuiviCheckbox.filter(function() {
			return !$(this).data('exported')}
		).click(function() {
			expSuiviClick($(this));
		});
		
		if (expSuiviCheckbox.is(':checked')) {
			expSuiviClick(expSuiviCheckbox);
			$('#ct_det_recoInt').turnOn();
		}
		
		function expSuiviClick(elmt) {
			elmt.data('exported', true);
			var natureChecked = '';
			if (elmt.is(':checked')) {
				setElmCousins(elmt);
			} else  {
				nature.find('input:checked').each(function(){
					if ( ($(this).attr('id') == 'ct_det_envoi_postExport') || ($(this).attr('id') == 'ct_det_envoi_docPrio') || ($(this).attr('id') == 'ct_det_envoi_marchPrio') ){
						var natu = $(this).attr('id');
						elmt.parents('li').siblings().each(function(){
							if(!(natu == 'ct_det_envoi_docPrio' && $(this).attr('id') == 'ct_det_reco_securexport_container')){
								$(this).turnOn();
							}
						});
						natureChecked = $(this).attr('id');							
					} else {
						$(this).click();
					}
				});
				if(natureChecked == 'ct_det_envoi_postExport'){
					postExportList.change();
				}
			}
			
			etapePoids();
		}
	}

	function securexport() {
		securExportWrapper.hide();
		$('#ct_det_envoi_docPrio, #ct_det_envoi_docEco, #ct_det_envoi_marchEco').click(function() {
			securExportWrapper.hide();
		}).filter(':checked').click();
		
		$('#ct_det_envoi_postExport, #ct_det_envoi_marchPrio').click(function() {
			if (!$('#ct_dest_ff_dom_tom').attr('checked')) {
				securExportWrapper.show();
			}
		}).filter(':checked').click();
		
		var secureExportCheckbox = $('#ct_det_reco_securexport');
		
		secureExportCheckbox.filter(function(){
			return !$(this).data('secured')
		}).click(function() {
			// console.log('secureExportCheckbox .click');
			secureExportClick($(this));
			
			etapePoids();
		});
		
		if (secureExportCheckbox.is(':checked')) {
			secureExportClick(secureExportCheckbox);
		}
		
		function secureExportClick(elmt) {
			elmt.data('secured', true);
			if (elmt.is(':checked')) {
				setElmCousins(elmt);
				$('#ct_det_reco_r1, #ct_det_reco_r2, #ct_det_reco_ar_int').each(function() {
					if ($(this).parentHasClass('valid', 'li')) {
						if (!returnForm) {
							$(this).removeAttr('checked');
						}
					}
					if ($(this).attr('id') == 'ct_det_reco_ar_int') {
						$(this).attr('disabled','disabled');
					}
				});
			} else {
				nature.find('input:checked').each(function(){
					if ($(this).attr('id') == 'ct_det_envoi_postExport' || $(this).attr('id') == 'ct_det_envoi_marchPrio') {
						elmt.parents('li').siblings().turnOn();
						natureChecked = $(this).attr('id');
					} else {
						$(this).click();
					}
				});
				if(natureChecked == 'ct_det_envoi_postExport'){
					postExportList.change();
				}
			}
		}
	}
	
	function gestionPostExportList() {
		var expSuivis = ['ct_det_envoi_postExportListe_P01', 'ct_det_envoi_postExportListe_P02', 'ct_det_envoi_postExportListe_P03', 'ct_det_envoi_postExportListe_P04', 'ct_det_envoi_postExportListe_P05', 'ct_det_envoi_postExportListe_P06', 'ct_det_envoi_postExportListe_P08', 'ct_det_envoi_postExportListe_P09', 'ct_det_envoi_postExportListe_P10', 'ct_det_envoi_postExportListe_E01', 'ct_det_envoi_postExportListe_E02', 'ct_det_envoi_postExportListe_E03', 'ct_det_envoi_postExportListe_E04', 'ct_det_envoi_postExportListe_E05', 'ct_det_envoi_postExportListe_E06', 'ct_det_envoi_postExportListe_P07', 'ct_det_envoi_postExportListe_E07', 'ct_det_envoi_postExportListe_E08', 'ct_det_envoi_postExportListe_E09', 'ct_det_envoi_postExportListe_E10'];
		var secuExports = ['ct_det_envoi_postExportListe_P07', 'ct_det_envoi_postExportListe_P08', 'ct_det_envoi_postExportListe_P09', 'ct_det_envoi_postExportListe_P10', 'ct_det_envoi_postExportListe_E07', 'ct_det_envoi_postExportListe_E08', 'ct_det_envoi_postExportListe_E09', 'ct_det_envoi_postExportListe_E10' ];
		
		postExportList.change(function(){
			// console.log('postExportList .change');
			handlePostExportListChange($(this));
		}).blur(function(){
			handlePostExportListChange($(this));
		}).find('option').filter(':selected').parent().change();
		
		function handlePostExportListChange(elmt) {
			//console.info('handlePostExportListChange');
			//console.log(elmt.val());
			if (elmt.val()) {
				expSuivi.hide();
				securExportWrapper.hide();
				var ids = elmt.find('option:selected').attr('id');
				var blocRecoInt = $('#ct_det_recoInt');
				
				if (
					ids == 'ct_det_envoi_postExportListe_P05' || 
					ids == 'ct_det_envoi_postExportListe_P06' || 
					ids == 'ct_det_envoi_postExportListe_E05' || 
					ids == 'ct_det_envoi_postExportListe_E06'
				) {
					blocRecoInt.hide();
				} else {
					blocRecoInt.show();
					
					if (
						expSuivis.indexOf(ids) != -1 && 
						//allowedPaysSecurExportPostExportSecur.in_array(pays.val()) != -1 && 
						!$('#ct_dest_ff_dom_tom').attr('checked') &&
						!securExportWrapper.is('.valid')
					) {
						expSuivi.show();
					}
					
					if (
						secuExports.indexOf(ids) != -1 && 
						!$('#ct_dest_ff_dom_tom').attr('checked')
					) {
						securExportWrapper.show();
					}
				}
				
				if (!$('#ct_det_envoi_postExport').is(':checked')) {
					$('#ct_det_envoi_postExport').attr('checked', 'checked').parents('.select').addClass('valid'); // pas propre du tout, mais ça permet d'éviter les boucles infinies...
				}
			}
		}
	}
	
	gestionPostExportListTrigger = gestionPostExportList;
	
	function gestionOptionsColisIntl() {
		var ColissimoIntl = $('#ct_det_envoi_colissimoIntl');
		var autresTypes = $('#ct_det_envoi_colissimoEmballageIntl, #ct_det_envoi_chronopostIntl, #ct_det_envoi_chronopostPaEIntl, #ct_det_envoi_chronopostDomTom_PaE, #ct_det_envoi_chronopostDomTom_PaEIntl');
		
		var ar = $('#ct_det_reco_ar_int');
		var autresOptions = $('#ct_det_reco_exportSuivi, #ct_det_reco_securexport, #ct_det_reco_r1, #ct_det_reco_r2');
		var blocReco = $('#ct_det_recoInt');
		
		ColissimoIntl.click(function(){
			blocReco.show();
			ar.parents('li').show();
			autresOptions.parents('li').not('#outilCalculTarifs').hide();
		});
		
		autresTypes.click(function(){
			blocReco.hide();
		});
	}
	
	function boutonRetour() {
		$('#retour').click(function(e) {
			e.preventDefault();
			recoFlag = false;
			returnForm = false;
			setMasse.set_returnForm(false);
			exceptionIdApplied[0] = false;
			$('#ct_det_poids').purge()
			$('#ct_det_poids_wrapper').turnOff();
			$('#ct_det_recoInt').hide();
			handleDetails.setNatureStep(false);		
			$('#ct_bloc_recommandation, #ct_bloc_recommandation_options').find('li').show();
			destinationFromDOM.find('option[value=""]').attr('selected', true);
			$('#country_choice').autocomplete({data:[""]});
			if(details.is(':visible')) {
				details.turnOff();
				blocPoids.turnOff();
				details.find('.valid').removeClass('valid');
				details.find('li').css('display', '');
				$('#ct_det_reco .off').removeClass('off');
				destination.find('.off:not(.result, #ct_dest_ff_liste_pays, #ct_to_dom_tom, #ct_dest_dom_tom_liste_pays)').removeClass('off');
				destination.find('.on').turnOff();
				destination.find('.valid').removeClass('valid');
				$('option', postExportList).show();
				$('.destination_pays').val('');
				$('.destination_pays_colis').val('');
				$('#ct_det_reco_oui_choix').hide();
				$('#ct_det_reco_int_oui_choix').hide();
				$('#ct_dest_ff_liste_dom').hide();
				$('#ct_det_reco').turnOff().css('display', '');
				$('#ct_bloc_destination_dom_tom .result').empty();
				$('#ct_dest_ff_dom_tom').attr('checked', false);
				$('#ct_dest_ff_france').attr('checked', false);
				destination.find(':radio[name=destination]').removeAttr('checked');
				gerePostExportEmbList(postExportEmbFiltre);
				nature.find('input:radio').removeAttr('checked');
				$('.warningsColisTarifs').show();
				//alert("detail");
			} else if (destination.is(':visible')) {
				window.location=window.location;
			}
		});
	}
	
	function nouvelleRecherche() {
		$('#new_search').click(function(e){
			window.location=window.location;
		});
	}
});

function etapePoids() {
	var montrerImmediatementLePoids = $('#ct_det_envoi_eco, #ct_det_envoi_lverte_nat, #ct_det_envoi_lverte_inter, #ct_det_reco_non, #ct_det_reco_oui_r1, #ct_det_reco_oui_r2, #ct_det_reco_oui_r3, #ct_det_reco_oui_r4, #ct_det_reco_oui_r5, #ct_det_reco_ar, #ct_det_reco_ar_int, #ct_det_envoi_chronopost, #ct_det_envoi_colissimoIntl, #ct_det_envoi_chronopostIntl, #ct_det_reco_exportSuivi, #ct_det_reco_securexport, #ct_det_reco_r1, #ct_det_reco_r2, #ct_det_envoi_docEco, #ct_det_envoi_marchEco, #ct_det_reco_int_non');
	
	if (montrerImmediatementLePoids.filter(':checked').size()) {
		blocPoids.turnOn();
	} else {
		blocPoids.turnOff();
	}
}
