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

		/* data for selected record, or defaults if none is selected */
		var data = {
			option_prime: <?php echo json_encode(array('id' => $rdata['option_prime'], 'value' => $rdata['option_prime'], 'text' => $jdata['option_prime'])); ?>,
			statut: <?php echo json_encode(array('id' => $rdata['statut'], 'value' => $rdata['statut'], 'text' => $jdata['statut'])); ?>,
			direction: <?php echo json_encode(array('id' => $rdata['direction'], 'value' => $rdata['direction'], 'text' => $jdata['direction'])); ?>,
			fonction: <?php echo json_encode(array('id' => $rdata['fonction'], 'value' => $rdata['fonction'], 'text' => $jdata['fonction'])); ?>,
			service: <?php echo json_encode(array('id' => $rdata['service'], 'value' => $rdata['service'], 'text' => $jdata['service'])); ?>,
			division: <?php echo json_encode(array('id' => $rdata['division'], 'value' => $rdata['division'], 'text' => $jdata['division'])); ?>,
			categorie: <?php echo json_encode(array('id' => $rdata['categorie'], 'value' => $rdata['categorie'], 'text' => $jdata['categorie'])); ?>,
			prime: <?php echo json_encode($jdata['prime']); ?>,
			qty: <?php echo json_encode($jdata['qty']); ?>,
			banque: <?php echo json_encode(array('id' => $rdata['banque'], 'value' => $rdata['banque'], 'text' => $jdata['banque'])); ?>
		};

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

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

			return false;
		});

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

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

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

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

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

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

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

			return false;
		});

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

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

