<?php
	$rdata = array_map('to_utf8', array_map('nl2br', array_map('html_attr_tags_ok', $rdata)));
	$jdata = array_map('to_utf8', array_map('nl2br', array_map('html_attr_tags_ok', $jdata)));
?>
<script>
	$j(function() {
		var tn = 'assurances';

		/* data for selected record, or defaults if none is selected */
		var data = {
			nom_prenom: <?php echo json_encode(array('id' => $rdata['nom_prenom'], 'value' => $rdata['nom_prenom'], 'text' => $jdata['nom_prenom'])); ?>,
			intermediare: <?php echo json_encode(array('id' => $rdata['intermediare'], 'value' => $rdata['intermediare'], 'text' => $jdata['intermediare'])); ?>,
			pays_residence: <?php echo json_encode(array('id' => $rdata['pays_residence'], 'value' => $rdata['pays_residence'], 'text' => $jdata['pays_residence'])); ?>,
			adresse: <?php echo json_encode(array('id' => $rdata['adresse'], 'value' => $rdata['adresse'], 'text' => $jdata['adresse'])); ?>,
			telephone: <?php echo json_encode($jdata['telephone']); ?>,
			email: <?php echo json_encode($jdata['email']); ?>,
			destination: <?php echo json_encode(array('id' => $rdata['destination'], 'value' => $rdata['destination'], 'text' => $jdata['destination'])); ?>,
			formule: <?php echo json_encode(array('id' => $rdata['formule'], 'value' => $rdata['formule'], 'text' => $jdata['formule'])); ?>,
			passport: <?php echo json_encode($jdata['passport']); ?>,
			nom_prenomb: <?php echo json_encode(array('id' => $rdata['nom_prenomb'], 'value' => $rdata['nom_prenomb'], 'text' => $jdata['nom_prenomb'])); ?>,
			date_de_naissance: <?php echo json_encode($jdata['date_de_naissance']); ?>
		};

		/* initialize or continue using AppGini.cache for the current table */
		AppGini.cache = AppGini.cache || {};
		AppGini.cache[tn] = AppGini.cache[tn] || AppGini.ajaxCache();
		var cache = AppGini.cache[tn];

		/* saved value for nom_prenom */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'nom_prenom' && d.id == data.nom_prenom.id)
				return { results: [ data.nom_prenom ], more: false, elapsed: 0.01 };
			return false;
		});

		/* saved value for nom_prenom autofills */
		cache.addCheck(function(u, d) {
			if(u != tn + '_autofill.php') return false;

			for(var rnd in d) if(rnd.match(/^rnd/)) break;

			if(d.mfk == 'nom_prenom' && d.id == data.nom_prenom.id) {
				$j('#telephone' + d[rnd]).html(data.telephone);
				$j('#email' + d[rnd]).html(data.email);
				$j('#passport' + d[rnd]).html(data.passport);
				$j('#date_de_naissance' + d[rnd]).html(data.date_de_naissance);
				return true;
			}

			return false;
		});

		/* saved value for intermediare */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'intermediare' && d.id == data.intermediare.id)
				return { results: [ data.intermediare ], more: false, elapsed: 0.01 };
			return false;
		});

		/* saved value for pays_residence */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'pays_residence' && d.id == data.pays_residence.id)
				return { results: [ data.pays_residence ], more: false, elapsed: 0.01 };
			return false;
		});

		/* saved value for adresse */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'adresse' && d.id == data.adresse.id)
				return { results: [ data.adresse ], more: false, elapsed: 0.01 };
			return false;
		});

		/* saved value for destination */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'destination' && d.id == data.destination.id)
				return { results: [ data.destination ], more: false, elapsed: 0.01 };
			return false;
		});

		/* saved value for formule */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'formule' && d.id == data.formule.id)
				return { results: [ data.formule ], more: false, elapsed: 0.01 };
			return false;
		});

		/* saved value for nom_prenomb */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'nom_prenomb' && d.id == data.nom_prenomb.id)
				return { results: [ data.nom_prenomb ], more: false, elapsed: 0.01 };
			return false;
		});

		cache.start();
	});
</script>

