<?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 = 'suivi_client';

		/* data for selected record, or defaults if none is selected */
		var data = {
			nom: <?php echo json_encode(array('id' => $rdata['nom'], 'value' => $rdata['nom'], 'text' => $jdata['nom'])); ?>,
			prenom: <?php echo json_encode($jdata['prenom']); ?>,
			raison_sociale: <?php echo json_encode($jdata['raison_sociale']); ?>,
			telephone: <?php echo json_encode($jdata['telephone']); ?>,
			email: <?php echo json_encode($jdata['email']); ?>,
			whatsapp: <?php echo json_encode($jdata['whatsapp']); ?>,
			service_demande: <?php echo json_encode(array('id' => $rdata['service_demande'], 'value' => $rdata['service_demande'], 'text' => $jdata['service_demande'])); ?>,
			type_de_visa: <?php echo json_encode(array('id' => $rdata['type_de_visa'], 'value' => $rdata['type_de_visa'], 'text' => $jdata['type_de_visa'])); ?>
		};

		/* 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 */
		cache.addCheck(function(u, d) {
			if(u != 'ajax_combo.php') return false;
			if(d.t == tn && d.f == 'nom' && d.id == data.nom.id)
				return { results: [ data.nom ], more: false, elapsed: 0.01 };
			return false;
		});

		/* saved value for nom 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' && d.id == data.nom.id) {
				$j('#prenom' + d[rnd]).html(data.prenom);
				$j('#raison_sociale' + d[rnd]).html(data.raison_sociale);
				$j('#telephone' + d[rnd]).html(data.telephone);
				$j('#email' + d[rnd]).html(data.email);
				$j('#whatsapp' + d[rnd]).html(data.whatsapp);
				return true;
			}

			return false;
		});

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

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

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

