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

		/* data for selected record, or defaults if none is selected */
		var data = {
			la_date: <?php echo json_encode($jdata['la_date']); ?>,
			numero: <?php echo json_encode(array('id' => $rdata['numero'], 'value' => $rdata['numero'], 'text' => $jdata['numero'])); ?>,
			designation: <?php echo json_encode(array('id' => $rdata['designation'], 'value' => $rdata['designation'], 'text' => $jdata['designation'])); ?>,
			tarif: <?php echo json_encode($jdata['tarif']); ?>
		};

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

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

			return false;
		});

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

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

			return false;
		});

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

