<?php

// Data functions (insert, update, delete, form) for table assurances

// This script and data application were generated by AppGini 5.81
// Download AppGini for free from https://bigprof.com/appgini/download/

function assurances_insert() {
	global $Translation;

	// mm: can member insert record?
	$arrPerm = getTablePermissions('assurances');
	if(!$arrPerm[1]) return false;

	$data = array();
	$data['date'] = intval($_REQUEST['dateYear']) . '-' . intval($_REQUEST['dateMonth']) . '-' . intval($_REQUEST['dateDay']);
	$data['date'] = parseMySQLDate($data['date'], '1');
	$data['nom_prenom'] = $_REQUEST['nom_prenom'];
		if($data['nom_prenom'] == empty_lookup_value) { $data['nom_prenom'] = ''; }
	$data['intermediare'] = $_REQUEST['intermediare'];
		if($data['intermediare'] == empty_lookup_value) { $data['intermediare'] = ''; }
	$data['pays_residence'] = $_REQUEST['pays_residence'];
		if($data['pays_residence'] == empty_lookup_value) { $data['pays_residence'] = ''; }
	$data['adresse'] = $_REQUEST['adresse'];
		if($data['adresse'] == empty_lookup_value) { $data['adresse'] = ''; }
	$data['telephone'] = $_REQUEST['nom_prenom'];
		if($data['telephone'] == empty_lookup_value) { $data['telephone'] = ''; }
	$data['email'] = $_REQUEST['nom_prenom'];
		if($data['email'] == empty_lookup_value) { $data['email'] = ''; }
	$data['destination'] = $_REQUEST['destination'];
		if($data['destination'] == empty_lookup_value) { $data['destination'] = ''; }
	$data['formule'] = $_REQUEST['formule'];
		if($data['formule'] == empty_lookup_value) { $data['formule'] = ''; }
	$data['certificat'] = $_REQUEST['certificat'];
		if($data['certificat'] == empty_lookup_value) { $data['certificat'] = ''; }
	$data['date_depart'] = intval($_REQUEST['date_departYear']) . '-' . intval($_REQUEST['date_departMonth']) . '-' . intval($_REQUEST['date_departDay']);
	$data['date_depart'] = parseMySQLDate($data['date_depart'], '');
	$data['date_retour'] = intval($_REQUEST['date_retourYear']) . '-' . intval($_REQUEST['date_retourMonth']) . '-' . intval($_REQUEST['date_retourDay']);
	$data['date_retour'] = parseMySQLDate($data['date_retour'], '');
	$data['passport'] = $_REQUEST['nom_prenom'];
		if($data['passport'] == empty_lookup_value) { $data['passport'] = ''; }
	$data['nom_prenomb'] = $_REQUEST['nom_prenomb'];
		if($data['nom_prenomb'] == empty_lookup_value) { $data['nom_prenomb'] = ''; }
	$data['date_de_naissance'] = $_REQUEST['nom_prenom'];
		if($data['date_de_naissance'] == empty_lookup_value) { $data['date_de_naissance'] = ''; }
	$data['statut'] = $_REQUEST['statut'];
		if($data['statut'] == empty_lookup_value) { $data['statut'] = ''; }
	$data['question1'] = $_REQUEST['question1'];
		if($data['question1'] == empty_lookup_value) { $data['question1'] = ''; }
	$data['question2'] = $_REQUEST['question2'];
		if($data['question2'] == empty_lookup_value) { $data['question2'] = ''; }
	$data['question2a'] = $_REQUEST['question2a'];
		if($data['question2a'] == empty_lookup_value) { $data['question2a'] = ''; }
	$data['question3'] = $_REQUEST['question3'];
		if($data['question3'] == empty_lookup_value) { $data['question3'] = ''; }
	$data['question4'] = $_REQUEST['question4'];
		if($data['question4'] == empty_lookup_value) { $data['question4'] = ''; }
	$data['question5'] = $_REQUEST['question5'];
		if($data['question5'] == empty_lookup_value) { $data['question5'] = ''; }
	$data['dossier_pdf'] = PrepareUploadedFile('dossier_pdf', 10240000,'txt|doc|docx|docm|odt|pdf|rtf', true, '');
	$data['dossier_zippe'] = PrepareUploadedFile('dossier_zippe', 10240000,'zip|rar|gz|tar|iso', true, '');
	if($data['statut'] == '') $data['statut'] = "En cours";
	if($data['statut']== '') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">" . $Translation['error:'] . " 'Statut': " . $Translation['field not null'] . '<br><br>';
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}

	/* for empty upload fields, when saving a copy of an existing record, copy the original upload field */
	if($_REQUEST['SelectedID']) {
		$res = sql("select * from assurances where idassurance='" . makeSafe($_REQUEST['SelectedID']) . "'", $eo);
		if($row = db_fetch_assoc($res)) {
			if(!$data['dossier_pdf']) $data['dossier_pdf'] = $row['dossier_pdf'];
			if(!$data['dossier_zippe']) $data['dossier_zippe'] = $row['dossier_zippe'];
		}
	}

	// hook: assurances_before_insert
	if(function_exists('assurances_before_insert')) {
		$args = array();
		if(!assurances_before_insert($data, getMemberInfo(), $args)) { return false; }
	}

	$error = '';
	// set empty fields to NULL
	$data = array_map(function($v) { return ($v === '' ? NULL : $v); }, $data);
	insert('assurances', backtick_keys_once($data), $error);
	if($error)
		die("{$error}<br><a href=\"#\" onclick=\"history.go(-1);\">{$Translation['< back']}</a>");

	$recID = db_insert_id(db_link());

	// hook: assurances_after_insert
	if(function_exists('assurances_after_insert')) {
		$res = sql("select * from `assurances` where `idassurance`='" . makeSafe($recID, false) . "' limit 1", $eo);
		if($row = db_fetch_assoc($res)) {
			$data = array_map('makeSafe', $row);
		}
		$data['selectedID'] = makeSafe($recID, false);
		$args=array();
		if(!assurances_after_insert($data, getMemberInfo(), $args)) { return $recID; }
	}

	// mm: save ownership data
	set_record_owner('assurances', $recID, getLoggedMemberID());

	// if this record is a copy of another record, copy children if applicable
	if(!empty($_REQUEST['SelectedID'])) assurances_copy_children($recID, $_REQUEST['SelectedID']);

	return $recID;
}

function assurances_copy_children($destination_id, $source_id) {
	global $Translation;
	$requests = array(); // array of curl handlers for launching insert requests
	$eo = array('silentErrors' => true);
	$uploads_dir = realpath(dirname(__FILE__) . '/../' . $Translation['ImageFolder']);
	$safe_sid = makeSafe($source_id);

	// launch requests, asynchronously
	curl_batch($requests);
}

function assurances_delete($selected_id, $AllowDeleteOfParents=false, $skipChecks=false) {
	// insure referential integrity ...
	global $Translation;
	$selected_id=makeSafe($selected_id);

	// mm: can member delete record?
	$arrPerm=getTablePermissions('assurances');
	$ownerGroupID=sqlValue("select groupID from membership_userrecords where tableName='assurances' and pkValue='$selected_id'");
	$ownerMemberID=sqlValue("select lcase(memberID) from membership_userrecords where tableName='assurances' and pkValue='$selected_id'");
	if(($arrPerm[4]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[4]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[4]==3) { // allow delete?
		// delete allowed, so continue ...
	}else{
		return $Translation['You don\'t have enough permissions to delete this record'];
	}

	// hook: assurances_before_delete
	if(function_exists('assurances_before_delete')) {
		$args=array();
		if(!assurances_before_delete($selected_id, $skipChecks, getMemberInfo(), $args))
			return $Translation['Couldn\'t delete this record'];
	}

	// delete file stored in the 'dossier_pdf' field
	$res = sql("select `dossier_pdf` from `assurances` where `idassurance`='$selected_id'", $eo);
	if($row=@db_fetch_row($res)) {
		if($row[0]!='') {
			@unlink(getUploadDir('').$row[0]);
		}
	}

	// delete file stored in the 'dossier_zippe' field
	$res = sql("select `dossier_zippe` from `assurances` where `idassurance`='$selected_id'", $eo);
	if($row=@db_fetch_row($res)) {
		if($row[0]!='') {
			@unlink(getUploadDir('').$row[0]);
		}
	}

	sql("delete from `assurances` where `idassurance`='$selected_id'", $eo);

	// hook: assurances_after_delete
	if(function_exists('assurances_after_delete')) {
		$args=array();
		assurances_after_delete($selected_id, getMemberInfo(), $args);
	}

	// mm: delete ownership data
	sql("delete from membership_userrecords where tableName='assurances' and pkValue='$selected_id'", $eo);
}

function assurances_update($selected_id) {
	global $Translation;

	// mm: can member edit record?
	$arrPerm=getTablePermissions('assurances');
	$ownerGroupID=sqlValue("select groupID from membership_userrecords where tableName='assurances' and pkValue='".makeSafe($selected_id)."'");
	$ownerMemberID=sqlValue("select lcase(memberID) from membership_userrecords where tableName='assurances' and pkValue='".makeSafe($selected_id)."'");
	if(($arrPerm[3]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[3]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[3]==3) { // allow update?
		// update allowed, so continue ...
	}else{
		return false;
	}

	$data['date'] = intval($_REQUEST['dateYear']) . '-' . intval($_REQUEST['dateMonth']) . '-' . intval($_REQUEST['dateDay']);
	$data['date'] = parseMySQLDate($data['date'], '1');
	$data['nom_prenom'] = makeSafe($_REQUEST['nom_prenom']);
		if($data['nom_prenom'] == empty_lookup_value) { $data['nom_prenom'] = ''; }
	$data['intermediare'] = makeSafe($_REQUEST['intermediare']);
		if($data['intermediare'] == empty_lookup_value) { $data['intermediare'] = ''; }
	$data['pays_residence'] = makeSafe($_REQUEST['pays_residence']);
		if($data['pays_residence'] == empty_lookup_value) { $data['pays_residence'] = ''; }
	$data['adresse'] = makeSafe($_REQUEST['adresse']);
		if($data['adresse'] == empty_lookup_value) { $data['adresse'] = ''; }
	$data['telephone'] = makeSafe($_REQUEST['nom_prenom']);
		if($data['telephone'] == empty_lookup_value) { $data['telephone'] = ''; }
	$data['email'] = makeSafe($_REQUEST['nom_prenom']);
		if($data['email'] == empty_lookup_value) { $data['email'] = ''; }
	$data['destination'] = makeSafe($_REQUEST['destination']);
		if($data['destination'] == empty_lookup_value) { $data['destination'] = ''; }
	$data['formule'] = makeSafe($_REQUEST['formule']);
		if($data['formule'] == empty_lookup_value) { $data['formule'] = ''; }
	$data['certificat'] = makeSafe($_REQUEST['certificat']);
		if($data['certificat'] == empty_lookup_value) { $data['certificat'] = ''; }
	$data['date_depart'] = intval($_REQUEST['date_departYear']) . '-' . intval($_REQUEST['date_departMonth']) . '-' . intval($_REQUEST['date_departDay']);
	$data['date_depart'] = parseMySQLDate($data['date_depart'], '');
	$data['date_retour'] = intval($_REQUEST['date_retourYear']) . '-' . intval($_REQUEST['date_retourMonth']) . '-' . intval($_REQUEST['date_retourDay']);
	$data['date_retour'] = parseMySQLDate($data['date_retour'], '');
	$data['passport'] = makeSafe($_REQUEST['nom_prenom']);
		if($data['passport'] == empty_lookup_value) { $data['passport'] = ''; }
	$data['nom_prenomb'] = makeSafe($_REQUEST['nom_prenomb']);
		if($data['nom_prenomb'] == empty_lookup_value) { $data['nom_prenomb'] = ''; }
	$data['date_de_naissance'] = makeSafe($_REQUEST['nom_prenom']);
		if($data['date_de_naissance'] == empty_lookup_value) { $data['date_de_naissance'] = ''; }
	$data['statut'] = makeSafe($_REQUEST['statut']);
		if($data['statut'] == empty_lookup_value) { $data['statut'] = ''; }
	if($data['statut']=='') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">{$Translation['error:']} 'Statut': {$Translation['field not null']}<br><br>";
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	$data['question1'] = makeSafe($_REQUEST['question1']);
		if($data['question1'] == empty_lookup_value) { $data['question1'] = ''; }
	$data['question2'] = makeSafe($_REQUEST['question2']);
		if($data['question2'] == empty_lookup_value) { $data['question2'] = ''; }
	$data['question2a'] = makeSafe($_REQUEST['question2a']);
		if($data['question2a'] == empty_lookup_value) { $data['question2a'] = ''; }
	$data['question3'] = makeSafe($_REQUEST['question3']);
		if($data['question3'] == empty_lookup_value) { $data['question3'] = ''; }
	$data['question4'] = makeSafe($_REQUEST['question4']);
		if($data['question4'] == empty_lookup_value) { $data['question4'] = ''; }
	$data['question5'] = makeSafe($_REQUEST['question5']);
		if($data['question5'] == empty_lookup_value) { $data['question5'] = ''; }
	$data['selectedID'] = makeSafe($selected_id);
	if($_REQUEST['dossier_pdf_remove'] == 1) {
		$data['dossier_pdf'] = '';
		// delete file from server
		$res = sql("select `dossier_pdf` from `assurances` where `idassurance`='".makeSafe($selected_id)."'", $eo);
		if($row = @db_fetch_row($res)) {
			if($row[0] != '') {
				@unlink(getUploadDir('') . $row[0]);
			}
		}
	} else {
		$data['dossier_pdf'] = PrepareUploadedFile('dossier_pdf', 10240000, 'txt|doc|docx|docm|odt|pdf|rtf', true, "");
		// delete file from server
		if($data['dossier_pdf'] != '') {
			$res = sql("select `dossier_pdf` from `assurances` where `idassurance`='" . makeSafe($selected_id) . "'", $eo);
			if($row = @db_fetch_row($res)) {
				if($row[0] != '') {
					@unlink(getUploadDir('') . $row[0]);
				}
			}
		}
	}
	if($_REQUEST['dossier_zippe_remove'] == 1) {
		$data['dossier_zippe'] = '';
		// delete file from server
		$res = sql("select `dossier_zippe` from `assurances` where `idassurance`='".makeSafe($selected_id)."'", $eo);
		if($row = @db_fetch_row($res)) {
			if($row[0] != '') {
				@unlink(getUploadDir('') . $row[0]);
			}
		}
	} else {
		$data['dossier_zippe'] = PrepareUploadedFile('dossier_zippe', 10240000, 'zip|rar|gz|tar|iso', true, "");
		// delete file from server
		if($data['dossier_zippe'] != '') {
			$res = sql("select `dossier_zippe` from `assurances` where `idassurance`='" . makeSafe($selected_id) . "'", $eo);
			if($row = @db_fetch_row($res)) {
				if($row[0] != '') {
					@unlink(getUploadDir('') . $row[0]);
				}
			}
		}
	}

	// hook: assurances_before_update
	if(function_exists('assurances_before_update')) {
		$args = array();
		if(!assurances_before_update($data, getMemberInfo(), $args)) { return false; }
	}

	$o = array('silentErrors' => true);
	sql('update `assurances` set       `date`=' . (($data['date'] !== '' && $data['date'] !== NULL) ? "'{$data['date']}'" : 'NULL') . ', `nom_prenom`=' . (($data['nom_prenom'] !== '' && $data['nom_prenom'] !== NULL) ? "'{$data['nom_prenom']}'" : 'NULL') . ', `intermediare`=' . (($data['intermediare'] !== '' && $data['intermediare'] !== NULL) ? "'{$data['intermediare']}'" : 'NULL') . ', `pays_residence`=' . (($data['pays_residence'] !== '' && $data['pays_residence'] !== NULL) ? "'{$data['pays_residence']}'" : 'NULL') . ', `adresse`=' . (($data['adresse'] !== '' && $data['adresse'] !== NULL) ? "'{$data['adresse']}'" : 'NULL') . ', `telephone`=' . (($data['telephone'] !== '' && $data['telephone'] !== NULL) ? "'{$data['telephone']}'" : 'NULL') . ', `email`=' . (($data['email'] !== '' && $data['email'] !== NULL) ? "'{$data['email']}'" : 'NULL') . ', `destination`=' . (($data['destination'] !== '' && $data['destination'] !== NULL) ? "'{$data['destination']}'" : 'NULL') . ', `formule`=' . (($data['formule'] !== '' && $data['formule'] !== NULL) ? "'{$data['formule']}'" : 'NULL') . ', `certificat`=' . (($data['certificat'] !== '' && $data['certificat'] !== NULL) ? "'{$data['certificat']}'" : 'NULL') . ', `date_depart`=' . (($data['date_depart'] !== '' && $data['date_depart'] !== NULL) ? "'{$data['date_depart']}'" : 'NULL') . ', `date_retour`=' . (($data['date_retour'] !== '' && $data['date_retour'] !== NULL) ? "'{$data['date_retour']}'" : 'NULL') . ', `passport`=' . (($data['passport'] !== '' && $data['passport'] !== NULL) ? "'{$data['passport']}'" : 'NULL') . ', `nom_prenomb`=' . (($data['nom_prenomb'] !== '' && $data['nom_prenomb'] !== NULL) ? "'{$data['nom_prenomb']}'" : 'NULL') . ', `date_de_naissance`=' . (($data['date_de_naissance'] !== '' && $data['date_de_naissance'] !== NULL) ? "'{$data['date_de_naissance']}'" : 'NULL') . ', ' . ($data['dossier_pdf']!='' ? "`dossier_pdf`='{$data['dossier_pdf']}'" : ($_REQUEST['dossier_pdf_remove'] != 1 ? '`dossier_pdf`=`dossier_pdf`' : '`dossier_pdf`=NULL')) . ', ' . ($data['dossier_zippe']!='' ? "`dossier_zippe`='{$data['dossier_zippe']}'" : ($_REQUEST['dossier_zippe_remove'] != 1 ? '`dossier_zippe`=`dossier_zippe`' : '`dossier_zippe`=NULL')) . ', `statut`=' . (($data['statut'] !== '' && $data['statut'] !== NULL) ? "'{$data['statut']}'" : 'NULL') . ', `question1`=' . (($data['question1'] !== '' && $data['question1'] !== NULL) ? "'{$data['question1']}'" : 'NULL') . ', `question2`=' . (($data['question2'] !== '' && $data['question2'] !== NULL) ? "'{$data['question2']}'" : 'NULL') . ', `question2a`=' . (($data['question2a'] !== '' && $data['question2a'] !== NULL) ? "'{$data['question2a']}'" : 'NULL') . ', `question3`=' . (($data['question3'] !== '' && $data['question3'] !== NULL) ? "'{$data['question3']}'" : 'NULL') . ', `question4`=' . (($data['question4'] !== '' && $data['question4'] !== NULL) ? "'{$data['question4']}'" : 'NULL') . ', `question5`=' . (($data['question5'] !== '' && $data['question5'] !== NULL) ? "'{$data['question5']}'" : 'NULL') . " where `idassurance`='".makeSafe($selected_id)."'", $o);
	if($o['error']!='') {
		echo $o['error'];
		echo '<a href="assurances_view.php?SelectedID='.urlencode($selected_id)."\">{$Translation['< back']}</a>";
		exit;
	}


	// hook: assurances_after_update
	if(function_exists('assurances_after_update')) {
		$res = sql("SELECT * FROM `assurances` WHERE `idassurance`='{$data['selectedID']}' LIMIT 1", $eo);
		if($row = db_fetch_assoc($res)) {
			$data = array_map('makeSafe', $row);
		}
		$data['selectedID'] = $data['idassurance'];
		$args = array();
		if(!assurances_after_update($data, getMemberInfo(), $args)) { return; }
	}

	// mm: update ownership data
	sql("update `membership_userrecords` set `dateUpdated`='" . time() . "' where `tableName`='assurances' and `pkValue`='" . makeSafe($selected_id) . "'", $eo);

}

function assurances_form($selected_id = '', $AllowUpdate = 1, $AllowInsert = 1, $AllowDelete = 1, $ShowCancel = 0, $TemplateDV = '', $TemplateDVP = '') {
	// function to return an editable form for a table records
	// and fill it with data of record whose ID is $selected_id. If $selected_id
	// is empty, an empty form is shown, with only an 'Add New'
	// button displayed.

	global $Translation;

	// mm: get table permissions
	$arrPerm=getTablePermissions('assurances');
	if(!$arrPerm[1] && $selected_id=='') { return ''; }
	$AllowInsert = ($arrPerm[1] ? true : false);
	// print preview?
	$dvprint = false;
	if($selected_id && $_REQUEST['dvprint_x'] != '') {
		$dvprint = true;
	}

	$filterer_nom_prenom = thisOr(undo_magic_quotes($_REQUEST['filterer_nom_prenom']), '');
	$filterer_intermediare = thisOr(undo_magic_quotes($_REQUEST['filterer_intermediare']), '');
	$filterer_pays_residence = thisOr(undo_magic_quotes($_REQUEST['filterer_pays_residence']), '');
	$filterer_adresse = thisOr(undo_magic_quotes($_REQUEST['filterer_adresse']), '');
	$filterer_destination = thisOr(undo_magic_quotes($_REQUEST['filterer_destination']), '');
	$filterer_formule = thisOr(undo_magic_quotes($_REQUEST['filterer_formule']), '');
	$filterer_nom_prenomb = thisOr(undo_magic_quotes($_REQUEST['filterer_nom_prenomb']), '');

	// populate filterers, starting from children to grand-parents

	// unique random identifier
	$rnd1 = ($dvprint ? rand(1000000, 9999999) : '');
	// combobox: date
	$combo_date = new DateCombo;
	$combo_date->DateFormat = "dmy";
	$combo_date->MinYear = 1900;
	$combo_date->MaxYear = 2100;
	$combo_date->DefaultDate = parseMySQLDate('1', '1');
	$combo_date->MonthNames = $Translation['month names'];
	$combo_date->NamePrefix = 'date';
	// combobox: nom_prenom
	$combo_nom_prenom = new DataCombo;
	// combobox: intermediare
	$combo_intermediare = new DataCombo;
	// combobox: pays_residence
	$combo_pays_residence = new DataCombo;
	// combobox: adresse
	$combo_adresse = new DataCombo;
	// combobox: destination
	$combo_destination = new DataCombo;
	// combobox: formule
	$combo_formule = new DataCombo;
	// combobox: date_depart
	$combo_date_depart = new DateCombo;
	$combo_date_depart->DateFormat = "dmy";
	$combo_date_depart->MinYear = 1900;
	$combo_date_depart->MaxYear = 2100;
	$combo_date_depart->DefaultDate = parseMySQLDate('', '');
	$combo_date_depart->MonthNames = $Translation['month names'];
	$combo_date_depart->NamePrefix = 'date_depart';
	// combobox: date_retour
	$combo_date_retour = new DateCombo;
	$combo_date_retour->DateFormat = "dmy";
	$combo_date_retour->MinYear = 1900;
	$combo_date_retour->MaxYear = 2100;
	$combo_date_retour->DefaultDate = parseMySQLDate('', '');
	$combo_date_retour->MonthNames = $Translation['month names'];
	$combo_date_retour->NamePrefix = 'date_retour';
	// combobox: nom_prenomb
	$combo_nom_prenomb = new DataCombo;
	// combobox: statut
	$combo_statut = new Combo;
	$combo_statut->ListType = 0;
	$combo_statut->MultipleSeparator = ', ';
	$combo_statut->ListBoxHeight = 10;
	$combo_statut->RadiosPerLine = 1;
	if(is_file(dirname(__FILE__).'/hooks/assurances.statut.csv')) {
		$statut_data = addslashes(implode('', @file(dirname(__FILE__).'/hooks/assurances.statut.csv')));
		$combo_statut->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions($statut_data)));
		$combo_statut->ListData = $combo_statut->ListItem;
	}else{
		$combo_statut->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions("En cours;;Valid&#233;;;Annul&#233;e")));
		$combo_statut->ListData = $combo_statut->ListItem;
	}
	$combo_statut->SelectName = 'statut';
	$combo_statut->AllowNull = false;
	// combobox: question1
	$combo_question1 = new Combo;
	$combo_question1->ListType = 2;
	$combo_question1->MultipleSeparator = ', ';
	$combo_question1->ListBoxHeight = 10;
	$combo_question1->RadiosPerLine = 1;
	if(is_file(dirname(__FILE__).'/hooks/assurances.question1.csv')) {
		$question1_data = addslashes(implode('', @file(dirname(__FILE__).'/hooks/assurances.question1.csv')));
		$combo_question1->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions($question1_data)));
		$combo_question1->ListData = $combo_question1->ListItem;
	}else{
		$combo_question1->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions("OUI;;NON")));
		$combo_question1->ListData = $combo_question1->ListItem;
	}
	$combo_question1->SelectName = 'question1';
	// combobox: question2
	$combo_question2 = new Combo;
	$combo_question2->ListType = 2;
	$combo_question2->MultipleSeparator = ', ';
	$combo_question2->ListBoxHeight = 10;
	$combo_question2->RadiosPerLine = 1;
	if(is_file(dirname(__FILE__).'/hooks/assurances.question2.csv')) {
		$question2_data = addslashes(implode('', @file(dirname(__FILE__).'/hooks/assurances.question2.csv')));
		$combo_question2->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions($question2_data)));
		$combo_question2->ListData = $combo_question2->ListItem;
	}else{
		$combo_question2->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions("OUI;;NON")));
		$combo_question2->ListData = $combo_question2->ListItem;
	}
	$combo_question2->SelectName = 'question2';
	// combobox: question3
	$combo_question3 = new Combo;
	$combo_question3->ListType = 2;
	$combo_question3->MultipleSeparator = ', ';
	$combo_question3->ListBoxHeight = 10;
	$combo_question3->RadiosPerLine = 1;
	if(is_file(dirname(__FILE__).'/hooks/assurances.question3.csv')) {
		$question3_data = addslashes(implode('', @file(dirname(__FILE__).'/hooks/assurances.question3.csv')));
		$combo_question3->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions($question3_data)));
		$combo_question3->ListData = $combo_question3->ListItem;
	}else{
		$combo_question3->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions("OUI;;NON")));
		$combo_question3->ListData = $combo_question3->ListItem;
	}
	$combo_question3->SelectName = 'question3';
	// combobox: question4
	$combo_question4 = new Combo;
	$combo_question4->ListType = 2;
	$combo_question4->MultipleSeparator = ', ';
	$combo_question4->ListBoxHeight = 10;
	$combo_question4->RadiosPerLine = 1;
	if(is_file(dirname(__FILE__).'/hooks/assurances.question4.csv')) {
		$question4_data = addslashes(implode('', @file(dirname(__FILE__).'/hooks/assurances.question4.csv')));
		$combo_question4->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions($question4_data)));
		$combo_question4->ListData = $combo_question4->ListItem;
	}else{
		$combo_question4->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions("OUII;;NON")));
		$combo_question4->ListData = $combo_question4->ListItem;
	}
	$combo_question4->SelectName = 'question4';
	// combobox: question5
	$combo_question5 = new Combo;
	$combo_question5->ListType = 2;
	$combo_question5->MultipleSeparator = ', ';
	$combo_question5->ListBoxHeight = 10;
	$combo_question5->RadiosPerLine = 1;
	if(is_file(dirname(__FILE__).'/hooks/assurances.question5.csv')) {
		$question5_data = addslashes(implode('', @file(dirname(__FILE__).'/hooks/assurances.question5.csv')));
		$combo_question5->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions($question5_data)));
		$combo_question5->ListData = $combo_question5->ListItem;
	}else{
		$combo_question5->ListItem = explode('||', entitiesToUTF8(convertLegacyOptions("OUI;;NON")));
		$combo_question5->ListData = $combo_question5->ListItem;
	}
	$combo_question5->SelectName = 'question5';

	if($selected_id) {
		// mm: check member permissions
		if(!$arrPerm[2]) {
			return "";
		}
		// mm: who is the owner?
		$ownerGroupID=sqlValue("select groupID from membership_userrecords where tableName='assurances' and pkValue='".makeSafe($selected_id)."'");
		$ownerMemberID=sqlValue("select lcase(memberID) from membership_userrecords where tableName='assurances' and pkValue='".makeSafe($selected_id)."'");
		if($arrPerm[2]==1 && getLoggedMemberID()!=$ownerMemberID) {
			return "";
		}
		if($arrPerm[2]==2 && getLoggedGroupID()!=$ownerGroupID) {
			return "";
		}

		// can edit?
		if(($arrPerm[3]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[3]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[3]==3) {
			$AllowUpdate=1;
		}else{
			$AllowUpdate=0;
		}

		$res = sql("SELECT * FROM `assurances` WHERE `idassurance`='" . makeSafe($selected_id) . "'", $eo);
		if(!($row = db_fetch_array($res))) {
			return error_message($Translation['No records found'], 'assurances_view.php', false);
		}
		$combo_date->DefaultDate = $row['date'];
		$combo_nom_prenom->SelectedData = $row['nom_prenom'];
		$combo_intermediare->SelectedData = $row['intermediare'];
		$combo_pays_residence->SelectedData = $row['pays_residence'];
		$combo_adresse->SelectedData = $row['adresse'];
		$combo_destination->SelectedData = $row['destination'];
		$combo_formule->SelectedData = $row['formule'];
		$combo_date_depart->DefaultDate = $row['date_depart'];
		$combo_date_retour->DefaultDate = $row['date_retour'];
		$combo_nom_prenomb->SelectedData = $row['nom_prenomb'];
		$combo_statut->SelectedData = $row['statut'];
		$combo_question1->SelectedData = $row['question1'];
		$combo_question2->SelectedData = $row['question2'];
		$combo_question3->SelectedData = $row['question3'];
		$combo_question4->SelectedData = $row['question4'];
		$combo_question5->SelectedData = $row['question5'];
		$urow = $row; /* unsanitized data */
		$hc = new CI_Input();
		$row = $hc->xss_clean($row); /* sanitize data */
	} else {
		$combo_nom_prenom->SelectedData = $filterer_nom_prenom;
		$combo_intermediare->SelectedData = $filterer_intermediare;
		$combo_pays_residence->SelectedData = $filterer_pays_residence;
		$combo_adresse->SelectedData = $filterer_adresse;
		$combo_destination->SelectedData = $filterer_destination;
		$combo_formule->SelectedData = $filterer_formule;
		$combo_nom_prenomb->SelectedData = $filterer_nom_prenomb;
		$combo_statut->SelectedText = ( $_REQUEST['FilterField'][1]=='20' && $_REQUEST['FilterOperator'][1]=='<=>' ? (get_magic_quotes_gpc() ? stripslashes($_REQUEST['FilterValue'][1]) : $_REQUEST['FilterValue'][1]) : "En cours");
		$combo_question1->SelectedText = ( $_REQUEST['FilterField'][1]=='22' && $_REQUEST['FilterOperator'][1]=='<=>' ? (get_magic_quotes_gpc() ? stripslashes($_REQUEST['FilterValue'][1]) : $_REQUEST['FilterValue'][1]) : "");
		$combo_question2->SelectedText = ( $_REQUEST['FilterField'][1]=='23' && $_REQUEST['FilterOperator'][1]=='<=>' ? (get_magic_quotes_gpc() ? stripslashes($_REQUEST['FilterValue'][1]) : $_REQUEST['FilterValue'][1]) : "");
		$combo_question3->SelectedText = ( $_REQUEST['FilterField'][1]=='25' && $_REQUEST['FilterOperator'][1]=='<=>' ? (get_magic_quotes_gpc() ? stripslashes($_REQUEST['FilterValue'][1]) : $_REQUEST['FilterValue'][1]) : "");
		$combo_question4->SelectedText = ( $_REQUEST['FilterField'][1]=='26' && $_REQUEST['FilterOperator'][1]=='<=>' ? (get_magic_quotes_gpc() ? stripslashes($_REQUEST['FilterValue'][1]) : $_REQUEST['FilterValue'][1]) : "");
		$combo_question5->SelectedText = ( $_REQUEST['FilterField'][1]=='27' && $_REQUEST['FilterOperator'][1]=='<=>' ? (get_magic_quotes_gpc() ? stripslashes($_REQUEST['FilterValue'][1]) : $_REQUEST['FilterValue'][1]) : "");
	}
	$combo_nom_prenom->HTML = '<span id="nom_prenom-container' . $rnd1 . '"></span><input type="hidden" name="nom_prenom" id="nom_prenom' . $rnd1 . '" value="' . html_attr($combo_nom_prenom->SelectedData) . '">';
	$combo_nom_prenom->MatchText = '<span id="nom_prenom-container-readonly' . $rnd1 . '"></span><input type="hidden" name="nom_prenom" id="nom_prenom' . $rnd1 . '" value="' . html_attr($combo_nom_prenom->SelectedData) . '">';
	$combo_intermediare->HTML = '<span id="intermediare-container' . $rnd1 . '"></span><input type="hidden" name="intermediare" id="intermediare' . $rnd1 . '" value="' . html_attr($combo_intermediare->SelectedData) . '">';
	$combo_intermediare->MatchText = '<span id="intermediare-container-readonly' . $rnd1 . '"></span><input type="hidden" name="intermediare" id="intermediare' . $rnd1 . '" value="' . html_attr($combo_intermediare->SelectedData) . '">';
	$combo_pays_residence->HTML = '<span id="pays_residence-container' . $rnd1 . '"></span><input type="hidden" name="pays_residence" id="pays_residence' . $rnd1 . '" value="' . html_attr($combo_pays_residence->SelectedData) . '">';
	$combo_pays_residence->MatchText = '<span id="pays_residence-container-readonly' . $rnd1 . '"></span><input type="hidden" name="pays_residence" id="pays_residence' . $rnd1 . '" value="' . html_attr($combo_pays_residence->SelectedData) . '">';
	$combo_adresse->HTML = '<span id="adresse-container' . $rnd1 . '"></span><input type="hidden" name="adresse" id="adresse' . $rnd1 . '" value="' . html_attr($combo_adresse->SelectedData) . '">';
	$combo_adresse->MatchText = '<span id="adresse-container-readonly' . $rnd1 . '"></span><input type="hidden" name="adresse" id="adresse' . $rnd1 . '" value="' . html_attr($combo_adresse->SelectedData) . '">';
	$combo_destination->HTML = '<span id="destination-container' . $rnd1 . '"></span><input type="hidden" name="destination" id="destination' . $rnd1 . '" value="' . html_attr($combo_destination->SelectedData) . '">';
	$combo_destination->MatchText = '<span id="destination-container-readonly' . $rnd1 . '"></span><input type="hidden" name="destination" id="destination' . $rnd1 . '" value="' . html_attr($combo_destination->SelectedData) . '">';
	$combo_formule->HTML = '<span id="formule-container' . $rnd1 . '"></span><input type="hidden" name="formule" id="formule' . $rnd1 . '" value="' . html_attr($combo_formule->SelectedData) . '">';
	$combo_formule->MatchText = '<span id="formule-container-readonly' . $rnd1 . '"></span><input type="hidden" name="formule" id="formule' . $rnd1 . '" value="' . html_attr($combo_formule->SelectedData) . '">';
	$combo_nom_prenomb->HTML = '<span id="nom_prenomb-container' . $rnd1 . '"></span><input type="hidden" name="nom_prenomb" id="nom_prenomb' . $rnd1 . '" value="' . html_attr($combo_nom_prenomb->SelectedData) . '">';
	$combo_nom_prenomb->MatchText = '<span id="nom_prenomb-container-readonly' . $rnd1 . '"></span><input type="hidden" name="nom_prenomb" id="nom_prenomb' . $rnd1 . '" value="' . html_attr($combo_nom_prenomb->SelectedData) . '">';
	$combo_statut->Render();
	$combo_question1->Render();
	$combo_question2->Render();
	$combo_question3->Render();
	$combo_question4->Render();
	$combo_question5->Render();

	ob_start();
	?>

	<script>
		// initial lookup values
		AppGini.current_nom_prenom__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['nom_prenom'] : $filterer_nom_prenom); ?>"};
		AppGini.current_intermediare__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['intermediare'] : $filterer_intermediare); ?>"};
		AppGini.current_pays_residence__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['pays_residence'] : $filterer_pays_residence); ?>"};
		AppGini.current_adresse__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['adresse'] : $filterer_adresse); ?>"};
		AppGini.current_destination__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['destination'] : $filterer_destination); ?>"};
		AppGini.current_formule__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['formule'] : $filterer_formule); ?>"};
		AppGini.current_nom_prenomb__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['nom_prenomb'] : $filterer_nom_prenomb); ?>"};

		jQuery(function() {
			setTimeout(function() {
				if(typeof(nom_prenom_reload__RAND__) == 'function') nom_prenom_reload__RAND__();
				if(typeof(intermediare_reload__RAND__) == 'function') intermediare_reload__RAND__();
				if(typeof(pays_residence_reload__RAND__) == 'function') pays_residence_reload__RAND__();
				if(typeof(adresse_reload__RAND__) == 'function') adresse_reload__RAND__();
				if(typeof(destination_reload__RAND__) == 'function') destination_reload__RAND__();
				if(typeof(formule_reload__RAND__) == 'function') formule_reload__RAND__();
				if(typeof(nom_prenomb_reload__RAND__) == 'function') nom_prenomb_reload__RAND__();
			}, 10); /* we need to slightly delay client-side execution of the above code to allow AppGini.ajaxCache to work */
		});
		function nom_prenom_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#nom_prenom-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_nom_prenom__RAND__.value, t: 'assurances', f: 'nom_prenom' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="nom_prenom"]').val(resp.results[0].id);
							$j('[id=nom_prenom-container-readonly__RAND__]').html('<span id="nom_prenom-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


							if(typeof(nom_prenom_update_autofills__RAND__) == 'function') nom_prenom_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'assurances', f: 'nom_prenom' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_nom_prenom__RAND__.value = e.added.id;
				AppGini.current_nom_prenom__RAND__.text = e.added.text;
				$j('[name="nom_prenom"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


				if(typeof(nom_prenom_update_autofills__RAND__) == 'function') nom_prenom_update_autofills__RAND__();
			});

			if(!$j("#nom_prenom-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_nom_prenom__RAND__.value, t: 'assurances', f: 'nom_prenom' },
					success: function(resp) {
						$j('[name="nom_prenom"]').val(resp.results[0].id);
						$j('[id=nom_prenom-container-readonly__RAND__]').html('<span id="nom_prenom-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

						if(typeof(nom_prenom_update_autofills__RAND__) == 'function') nom_prenom_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_nom_prenom__RAND__.value, t: 'assurances', f: 'nom_prenom' },
				success: function(resp) {
					$j('[id=nom_prenom-container__RAND__], [id=nom_prenom-container-readonly__RAND__]').html('<span id="nom_prenom-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

					if(typeof(nom_prenom_update_autofills__RAND__) == 'function') nom_prenom_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function intermediare_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#intermediare-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_intermediare__RAND__.value, t: 'assurances', f: 'intermediare' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="intermediare"]').val(resp.results[0].id);
							$j('[id=intermediare-container-readonly__RAND__]').html('<span id="intermediare-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=intermediares_view_parent]').hide(); }else{ $j('.btn[id=intermediares_view_parent]').show(); }


							if(typeof(intermediare_update_autofills__RAND__) == 'function') intermediare_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'assurances', f: 'intermediare' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_intermediare__RAND__.value = e.added.id;
				AppGini.current_intermediare__RAND__.text = e.added.text;
				$j('[name="intermediare"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=intermediares_view_parent]').hide(); }else{ $j('.btn[id=intermediares_view_parent]').show(); }


				if(typeof(intermediare_update_autofills__RAND__) == 'function') intermediare_update_autofills__RAND__();
			});

			if(!$j("#intermediare-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_intermediare__RAND__.value, t: 'assurances', f: 'intermediare' },
					success: function(resp) {
						$j('[name="intermediare"]').val(resp.results[0].id);
						$j('[id=intermediare-container-readonly__RAND__]').html('<span id="intermediare-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=intermediares_view_parent]').hide(); }else{ $j('.btn[id=intermediares_view_parent]').show(); }

						if(typeof(intermediare_update_autofills__RAND__) == 'function') intermediare_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_intermediare__RAND__.value, t: 'assurances', f: 'intermediare' },
				success: function(resp) {
					$j('[id=intermediare-container__RAND__], [id=intermediare-container-readonly__RAND__]').html('<span id="intermediare-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=intermediares_view_parent]').hide(); }else{ $j('.btn[id=intermediares_view_parent]').show(); }

					if(typeof(intermediare_update_autofills__RAND__) == 'function') intermediare_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function pays_residence_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#pays_residence-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_pays_residence__RAND__.value, t: 'assurances', f: 'pays_residence' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="pays_residence"]').val(resp.results[0].id);
							$j('[id=pays_residence-container-readonly__RAND__]').html('<span id="pays_residence-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }


							if(typeof(pays_residence_update_autofills__RAND__) == 'function') pays_residence_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'assurances', f: 'pays_residence' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_pays_residence__RAND__.value = e.added.id;
				AppGini.current_pays_residence__RAND__.text = e.added.text;
				$j('[name="pays_residence"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }


				if(typeof(pays_residence_update_autofills__RAND__) == 'function') pays_residence_update_autofills__RAND__();
			});

			if(!$j("#pays_residence-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_pays_residence__RAND__.value, t: 'assurances', f: 'pays_residence' },
					success: function(resp) {
						$j('[name="pays_residence"]').val(resp.results[0].id);
						$j('[id=pays_residence-container-readonly__RAND__]').html('<span id="pays_residence-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }

						if(typeof(pays_residence_update_autofills__RAND__) == 'function') pays_residence_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_pays_residence__RAND__.value, t: 'assurances', f: 'pays_residence' },
				success: function(resp) {
					$j('[id=pays_residence-container__RAND__], [id=pays_residence-container-readonly__RAND__]').html('<span id="pays_residence-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }

					if(typeof(pays_residence_update_autofills__RAND__) == 'function') pays_residence_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function adresse_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#adresse-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_adresse__RAND__.value, t: 'assurances', f: 'adresse' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="adresse"]').val(resp.results[0].id);
							$j('[id=adresse-container-readonly__RAND__]').html('<span id="adresse-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


							if(typeof(adresse_update_autofills__RAND__) == 'function') adresse_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'assurances', f: 'adresse' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_adresse__RAND__.value = e.added.id;
				AppGini.current_adresse__RAND__.text = e.added.text;
				$j('[name="adresse"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


				if(typeof(adresse_update_autofills__RAND__) == 'function') adresse_update_autofills__RAND__();
			});

			if(!$j("#adresse-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_adresse__RAND__.value, t: 'assurances', f: 'adresse' },
					success: function(resp) {
						$j('[name="adresse"]').val(resp.results[0].id);
						$j('[id=adresse-container-readonly__RAND__]').html('<span id="adresse-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

						if(typeof(adresse_update_autofills__RAND__) == 'function') adresse_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_adresse__RAND__.value, t: 'assurances', f: 'adresse' },
				success: function(resp) {
					$j('[id=adresse-container__RAND__], [id=adresse-container-readonly__RAND__]').html('<span id="adresse-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

					if(typeof(adresse_update_autofills__RAND__) == 'function') adresse_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function destination_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#destination-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_destination__RAND__.value, t: 'assurances', f: 'destination' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="destination"]').val(resp.results[0].id);
							$j('[id=destination-container-readonly__RAND__]').html('<span id="destination-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }


							if(typeof(destination_update_autofills__RAND__) == 'function') destination_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'assurances', f: 'destination' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_destination__RAND__.value = e.added.id;
				AppGini.current_destination__RAND__.text = e.added.text;
				$j('[name="destination"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }


				if(typeof(destination_update_autofills__RAND__) == 'function') destination_update_autofills__RAND__();
			});

			if(!$j("#destination-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_destination__RAND__.value, t: 'assurances', f: 'destination' },
					success: function(resp) {
						$j('[name="destination"]').val(resp.results[0].id);
						$j('[id=destination-container-readonly__RAND__]').html('<span id="destination-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }

						if(typeof(destination_update_autofills__RAND__) == 'function') destination_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_destination__RAND__.value, t: 'assurances', f: 'destination' },
				success: function(resp) {
					$j('[id=destination-container__RAND__], [id=destination-container-readonly__RAND__]').html('<span id="destination-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=pays_view_parent]').hide(); }else{ $j('.btn[id=pays_view_parent]').show(); }

					if(typeof(destination_update_autofills__RAND__) == 'function') destination_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function formule_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#formule-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_formule__RAND__.value, t: 'assurances', f: 'formule' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="formule"]').val(resp.results[0].id);
							$j('[id=formule-container-readonly__RAND__]').html('<span id="formule-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=formule_view_parent]').hide(); }else{ $j('.btn[id=formule_view_parent]').show(); }


							if(typeof(formule_update_autofills__RAND__) == 'function') formule_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'assurances', f: 'formule' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_formule__RAND__.value = e.added.id;
				AppGini.current_formule__RAND__.text = e.added.text;
				$j('[name="formule"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=formule_view_parent]').hide(); }else{ $j('.btn[id=formule_view_parent]').show(); }


				if(typeof(formule_update_autofills__RAND__) == 'function') formule_update_autofills__RAND__();
			});

			if(!$j("#formule-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_formule__RAND__.value, t: 'assurances', f: 'formule' },
					success: function(resp) {
						$j('[name="formule"]').val(resp.results[0].id);
						$j('[id=formule-container-readonly__RAND__]').html('<span id="formule-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=formule_view_parent]').hide(); }else{ $j('.btn[id=formule_view_parent]').show(); }

						if(typeof(formule_update_autofills__RAND__) == 'function') formule_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_formule__RAND__.value, t: 'assurances', f: 'formule' },
				success: function(resp) {
					$j('[id=formule-container__RAND__], [id=formule-container-readonly__RAND__]').html('<span id="formule-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=formule_view_parent]').hide(); }else{ $j('.btn[id=formule_view_parent]').show(); }

					if(typeof(formule_update_autofills__RAND__) == 'function') formule_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function nom_prenomb_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#nom_prenomb-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_nom_prenomb__RAND__.value, t: 'assurances', f: 'nom_prenomb' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="nom_prenomb"]').val(resp.results[0].id);
							$j('[id=nom_prenomb-container-readonly__RAND__]').html('<span id="nom_prenomb-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


							if(typeof(nom_prenomb_update_autofills__RAND__) == 'function') nom_prenomb_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'assurances', f: 'nom_prenomb' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_nom_prenomb__RAND__.value = e.added.id;
				AppGini.current_nom_prenomb__RAND__.text = e.added.text;
				$j('[name="nom_prenomb"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


				if(typeof(nom_prenomb_update_autofills__RAND__) == 'function') nom_prenomb_update_autofills__RAND__();
			});

			if(!$j("#nom_prenomb-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_nom_prenomb__RAND__.value, t: 'assurances', f: 'nom_prenomb' },
					success: function(resp) {
						$j('[name="nom_prenomb"]').val(resp.results[0].id);
						$j('[id=nom_prenomb-container-readonly__RAND__]').html('<span id="nom_prenomb-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

						if(typeof(nom_prenomb_update_autofills__RAND__) == 'function') nom_prenomb_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_nom_prenomb__RAND__.value, t: 'assurances', f: 'nom_prenomb' },
				success: function(resp) {
					$j('[id=nom_prenomb-container__RAND__], [id=nom_prenomb-container-readonly__RAND__]').html('<span id="nom_prenomb-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

					if(typeof(nom_prenomb_update_autofills__RAND__) == 'function') nom_prenomb_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
	</script>
	<?php

	$lookups = str_replace('__RAND__', $rnd1, ob_get_contents());
	ob_end_clean();


	// code for template based detail view forms

	// open the detail view template
	if($dvprint) {
		$template_file = is_file("./{$TemplateDVP}") ? "./{$TemplateDVP}" : './templates/assurances_templateDVP.html';
		$templateCode = @file_get_contents($template_file);
	}else{
		$template_file = is_file("./{$TemplateDV}") ? "./{$TemplateDV}" : './templates/assurances_templateDV.html';
		$templateCode = @file_get_contents($template_file);
	}

	// process form title
	$templateCode = str_replace('<%%DETAIL_VIEW_TITLE%%>', 'Assurances', $templateCode);
	$templateCode = str_replace('<%%RND1%%>', $rnd1, $templateCode);
	$templateCode = str_replace('<%%EMBEDDED%%>', ($_REQUEST['Embedded'] ? 'Embedded=1' : ''), $templateCode);
	// process buttons
	if($AllowInsert) {
		if(!$selected_id) $templateCode = str_replace('<%%INSERT_BUTTON%%>', '<button type="submit" class="btn btn-success" id="insert" name="insert_x" value="1" onclick="return assurances_validateData();"><i class="glyphicon glyphicon-plus-sign"></i> ' . $Translation['Save New'] . '</button>', $templateCode);
		$templateCode = str_replace('<%%INSERT_BUTTON%%>', '<button type="submit" class="btn btn-default" id="insert" name="insert_x" value="1" onclick="return assurances_validateData();"><i class="glyphicon glyphicon-plus-sign"></i> ' . $Translation['Save As Copy'] . '</button>', $templateCode);
	}else{
		$templateCode = str_replace('<%%INSERT_BUTTON%%>', '', $templateCode);
	}

	// 'Back' button action
	if($_REQUEST['Embedded']) {
		$backAction = 'AppGini.closeParentModal(); return false;';
	}else{
		$backAction = '$j(\'form\').eq(0).attr(\'novalidate\', \'novalidate\'); document.myform.reset(); return true;';
	}

	if($selected_id) {
		if(!$_REQUEST['Embedded']) $templateCode = str_replace('<%%DVPRINT_BUTTON%%>', '<button type="submit" class="btn btn-default" id="dvprint" name="dvprint_x" value="1" onclick="$j(\'form\').eq(0).prop(\'novalidate\', true); document.myform.reset(); return true;" title="' . html_attr($Translation['Print Preview']) . '"><i class="glyphicon glyphicon-print"></i> ' . $Translation['Print Preview'] . '</button>', $templateCode);
		if($AllowUpdate) {
			$templateCode = str_replace('<%%UPDATE_BUTTON%%>', '<button type="submit" class="btn btn-success btn-lg" id="update" name="update_x" value="1" onclick="return assurances_validateData();" title="' . html_attr($Translation['Save Changes']) . '"><i class="glyphicon glyphicon-ok"></i> ' . $Translation['Save Changes'] . '</button>', $templateCode);
		}else{
			$templateCode = str_replace('<%%UPDATE_BUTTON%%>', '', $templateCode);
		}
		if(($arrPerm[4]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[4]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[4]==3) { // allow delete?
			$templateCode = str_replace('<%%DELETE_BUTTON%%>', '<button type="submit" class="btn btn-danger" id="delete" name="delete_x" value="1" onclick="return confirm(\'' . $Translation['are you sure?'] . '\');" title="' . html_attr($Translation['Delete']) . '"><i class="glyphicon glyphicon-trash"></i> ' . $Translation['Delete'] . '</button>', $templateCode);
		}else{
			$templateCode = str_replace('<%%DELETE_BUTTON%%>', '', $templateCode);
		}
		$templateCode = str_replace('<%%DESELECT_BUTTON%%>', '<button type="submit" class="btn btn-default" id="deselect" name="deselect_x" value="1" onclick="' . $backAction . '" title="' . html_attr($Translation['Back']) . '"><i class="glyphicon glyphicon-chevron-left"></i> ' . $Translation['Back'] . '</button>', $templateCode);
	}else{
		$templateCode = str_replace('<%%UPDATE_BUTTON%%>', '', $templateCode);
		$templateCode = str_replace('<%%DELETE_BUTTON%%>', '', $templateCode);
		$templateCode = str_replace('<%%DESELECT_BUTTON%%>', ($ShowCancel ? '<button type="submit" class="btn btn-default" id="deselect" name="deselect_x" value="1" onclick="' . $backAction . '" title="' . html_attr($Translation['Back']) . '"><i class="glyphicon glyphicon-chevron-left"></i> ' . $Translation['Back'] . '</button>' : ''), $templateCode);
	}

	// set records to read only if user can't insert new records and can't edit current record
	if(($selected_id && !$AllowUpdate && !$AllowInsert) || (!$selected_id && !$AllowInsert)) {
		$jsReadOnly .= "\tjQuery('#date').prop('readonly', true);\n";
		$jsReadOnly .= "\tjQuery('#dateDay, #dateMonth, #dateYear').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#nom_prenom').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#nom_prenom_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#intermediare').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#intermediare_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#pays_residence').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#pays_residence_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#adresse').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#adresse_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#email, #email-edit-link').hide();\n";
		$jsReadOnly .= "\tjQuery('#destination').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#destination_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#formule').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#formule_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#certificat').replaceWith('<div class=\"form-control-static\" id=\"certificat\">' + (jQuery('#certificat').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#date_depart').prop('readonly', true);\n";
		$jsReadOnly .= "\tjQuery('#date_departDay, #date_departMonth, #date_departYear').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#date_retour').prop('readonly', true);\n";
		$jsReadOnly .= "\tjQuery('#date_retourDay, #date_retourMonth, #date_retourYear').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#nom_prenomb').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#nom_prenomb_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#dossier_pdf').replaceWith('<div class=\"form-control-static\" id=\"dossier_pdf\">' + (jQuery('#dossier_pdf').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#dossier_pdf, #dossier_pdf-edit-link').hide();\n";
		$jsReadOnly .= "\tjQuery('#dossier_zippe').replaceWith('<div class=\"form-control-static\" id=\"dossier_zippe\">' + (jQuery('#dossier_zippe').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#dossier_zippe, #dossier_zippe-edit-link').hide();\n";
		$jsReadOnly .= "\tjQuery('#statut').replaceWith('<div class=\"form-control-static\" id=\"statut\">' + (jQuery('#statut').val() || '') + '</div>'); jQuery('#statut-multi-selection-help').hide();\n";
		$jsReadOnly .= "\tjQuery('input[name=question1]').parent().html('<div class=\"form-control-static\">' + jQuery('input[name=question1]:checked').next().text() + '</div>')\n";
		$jsReadOnly .= "\tjQuery('input[name=question2]').parent().html('<div class=\"form-control-static\">' + jQuery('input[name=question2]:checked').next().text() + '</div>')\n";
		$jsReadOnly .= "\tjQuery('#question2a').replaceWith('<div class=\"form-control-static\" id=\"question2a\">' + (jQuery('#question2a').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('input[name=question3]').parent().html('<div class=\"form-control-static\">' + jQuery('input[name=question3]:checked').next().text() + '</div>')\n";
		$jsReadOnly .= "\tjQuery('input[name=question4]').parent().html('<div class=\"form-control-static\">' + jQuery('input[name=question4]:checked').next().text() + '</div>')\n";
		$jsReadOnly .= "\tjQuery('input[name=question5]').parent().html('<div class=\"form-control-static\">' + jQuery('input[name=question5]:checked').next().text() + '</div>')\n";
		$jsReadOnly .= "\tjQuery('.select2-container').hide();\n";

		$noUploads = true;
	}elseif($AllowInsert) {
		$jsEditable .= "\tjQuery('form').eq(0).data('already_changed', true);"; // temporarily disable form change handler
			$jsEditable .= "\tjQuery('form').eq(0).data('already_changed', false);"; // re-enable form change handler
	}

	// process combos
	$templateCode = str_replace('<%%COMBO(date)%%>', ($selected_id && !$arrPerm[3] ? '<div class="form-control-static">' . $combo_date->GetHTML(true) . '</div>' : $combo_date->GetHTML()), $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(date)%%>', $combo_date->GetHTML(true), $templateCode);
	$templateCode = str_replace('<%%COMBO(nom_prenom)%%>', $combo_nom_prenom->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(nom_prenom)%%>', $combo_nom_prenom->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(nom_prenom)%%>', urlencode($combo_nom_prenom->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(intermediare)%%>', $combo_intermediare->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(intermediare)%%>', $combo_intermediare->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(intermediare)%%>', urlencode($combo_intermediare->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(pays_residence)%%>', $combo_pays_residence->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(pays_residence)%%>', $combo_pays_residence->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(pays_residence)%%>', urlencode($combo_pays_residence->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(adresse)%%>', $combo_adresse->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(adresse)%%>', $combo_adresse->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(adresse)%%>', urlencode($combo_adresse->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(destination)%%>', $combo_destination->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(destination)%%>', $combo_destination->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(destination)%%>', urlencode($combo_destination->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(formule)%%>', $combo_formule->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(formule)%%>', $combo_formule->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(formule)%%>', urlencode($combo_formule->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(date_depart)%%>', ($selected_id && !$arrPerm[3] ? '<div class="form-control-static">' . $combo_date_depart->GetHTML(true) . '</div>' : $combo_date_depart->GetHTML()), $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(date_depart)%%>', $combo_date_depart->GetHTML(true), $templateCode);
	$templateCode = str_replace('<%%COMBO(date_retour)%%>', ($selected_id && !$arrPerm[3] ? '<div class="form-control-static">' . $combo_date_retour->GetHTML(true) . '</div>' : $combo_date_retour->GetHTML()), $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(date_retour)%%>', $combo_date_retour->GetHTML(true), $templateCode);
	$templateCode = str_replace('<%%COMBO(nom_prenomb)%%>', $combo_nom_prenomb->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(nom_prenomb)%%>', $combo_nom_prenomb->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(nom_prenomb)%%>', urlencode($combo_nom_prenomb->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(statut)%%>', $combo_statut->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(statut)%%>', $combo_statut->SelectedData, $templateCode);
	$templateCode = str_replace('<%%COMBO(question1)%%>', $combo_question1->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(question1)%%>', $combo_question1->SelectedData, $templateCode);
	$templateCode = str_replace('<%%COMBO(question2)%%>', $combo_question2->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(question2)%%>', $combo_question2->SelectedData, $templateCode);
	$templateCode = str_replace('<%%COMBO(question3)%%>', $combo_question3->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(question3)%%>', $combo_question3->SelectedData, $templateCode);
	$templateCode = str_replace('<%%COMBO(question4)%%>', $combo_question4->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(question4)%%>', $combo_question4->SelectedData, $templateCode);
	$templateCode = str_replace('<%%COMBO(question5)%%>', $combo_question5->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(question5)%%>', $combo_question5->SelectedData, $templateCode);

	/* lookup fields array: 'lookup field name' => array('parent table name', 'lookup field caption') */
	$lookup_fields = array('nom_prenom' => array('clients', 'Nom & pr&#233;nom'), 'intermediare' => array('intermediares', 'Intermediare'), 'pays_residence' => array('pays', 'Pays de residence'), 'adresse' => array('clients', 'Adresse'), 'destination' => array('pays', 'Destination'), 'formule' => array('formule', 'Formule'), 'nom_prenomb' => array('clients', 'Nom & Pr&#233;nom'), );
	foreach($lookup_fields as $luf => $ptfc) {
		$pt_perm = getTablePermissions($ptfc[0]);

		// process foreign key links
		if($pt_perm['view'] || $pt_perm['edit']) {
			$templateCode = str_replace("<%%PLINK({$luf})%%>", '<button type="button" class="btn btn-default view_parent hspacer-md" id="' . $ptfc[0] . '_view_parent" title="' . html_attr($Translation['View'] . ' ' . $ptfc[1]) . '"><i class="glyphicon glyphicon-eye-open"></i></button>', $templateCode);
		}

		// if user has insert permission to parent table of a lookup field, put an add new button
		if($pt_perm['insert'] && !$_REQUEST['Embedded']) {
			$templateCode = str_replace("<%%ADDNEW({$ptfc[0]})%%>", '<button type="button" class="btn btn-success add_new_parent hspacer-md" id="' . $ptfc[0] . '_add_new" title="' . html_attr($Translation['Add New'] . ' ' . $ptfc[1]) . '"><i class="glyphicon glyphicon-plus-sign"></i></button>', $templateCode);
		}
	}

	// process images
	$templateCode = str_replace('<%%UPLOADFILE(idassurance)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(date)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(nom_prenom)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(intermediare)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(pays_residence)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(adresse)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(destination)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(formule)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(certificat)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(date_depart)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(date_retour)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(beneficiaire)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(nom_prenomb)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(dossier_pdf)%%>', ($noUploads ? '' : "<div>{$Translation['upload image']}</div>" . '<i class="glyphicon glyphicon-remove text-danger clear-upload hidden"></i> <input type="file" name="dossier_pdf" id="dossier_pdf" data-filetypes="txt|doc|docx|docm|odt|pdf|rtf" data-maxsize="10240000">'), $templateCode);
	if($AllowUpdate && $row['dossier_pdf'] != '') {
		$templateCode = str_replace('<%%REMOVEFILE(dossier_pdf)%%>', '<br><input type="checkbox" name="dossier_pdf_remove" id="dossier_pdf_remove" value="1"> <label for="dossier_pdf_remove" style="color: red; font-weight: bold;">'.$Translation['remove image'].'</label>', $templateCode);
	}else{
		$templateCode = str_replace('<%%REMOVEFILE(dossier_pdf)%%>', '', $templateCode);
	}
	$templateCode = str_replace('<%%UPLOADFILE(dossier_zippe)%%>', ($noUploads ? '' : "<div>{$Translation['upload image']}</div>" . '<i class="glyphicon glyphicon-remove text-danger clear-upload hidden"></i> <input type="file" name="dossier_zippe" id="dossier_zippe" data-filetypes="zip|rar|gz|tar|iso" data-maxsize="10240000">'), $templateCode);
	if($AllowUpdate && $row['dossier_zippe'] != '') {
		$templateCode = str_replace('<%%REMOVEFILE(dossier_zippe)%%>', '<br><input type="checkbox" name="dossier_zippe_remove" id="dossier_zippe_remove" value="1"> <label for="dossier_zippe_remove" style="color: red; font-weight: bold;">'.$Translation['remove image'].'</label>', $templateCode);
	}else{
		$templateCode = str_replace('<%%REMOVEFILE(dossier_zippe)%%>', '', $templateCode);
	}
	$templateCode = str_replace('<%%UPLOADFILE(statut)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(questionnaire_medical)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(question1)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(question2)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(question2a)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(question3)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(question4)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(question5)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(prime_total)%%>', '', $templateCode);

	// process values
	if($selected_id) {
		if( $dvprint) $templateCode = str_replace('<%%VALUE(idassurance)%%>', safe_html($urow['idassurance']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(idassurance)%%>', html_attr($row['idassurance']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(idassurance)%%>', urlencode($urow['idassurance']), $templateCode);
		$templateCode = str_replace('<%%VALUE(date)%%>', @date('d/m/Y', @strtotime(html_attr($row['date']))), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date)%%>', urlencode(@date('d/m/Y', @strtotime(html_attr($urow['date'])))), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(nom_prenom)%%>', safe_html($urow['nom_prenom']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(nom_prenom)%%>', html_attr($row['nom_prenom']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(nom_prenom)%%>', urlencode($urow['nom_prenom']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(intermediare)%%>', safe_html($urow['intermediare']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(intermediare)%%>', html_attr($row['intermediare']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(intermediare)%%>', urlencode($urow['intermediare']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(pays_residence)%%>', safe_html($urow['pays_residence']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(pays_residence)%%>', html_attr($row['pays_residence']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(pays_residence)%%>', urlencode($urow['pays_residence']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(adresse)%%>', safe_html($urow['adresse']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(adresse)%%>', html_attr($row['adresse']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(adresse)%%>', urlencode($urow['adresse']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(destination)%%>', safe_html($urow['destination']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(destination)%%>', html_attr($row['destination']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(destination)%%>', urlencode($urow['destination']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(formule)%%>', safe_html($urow['formule']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(formule)%%>', html_attr($row['formule']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(formule)%%>', urlencode($urow['formule']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(certificat)%%>', safe_html($urow['certificat']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(certificat)%%>', html_attr($row['certificat']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(certificat)%%>', urlencode($urow['certificat']), $templateCode);
		$templateCode = str_replace('<%%VALUE(date_depart)%%>', @date('d/m/Y', @strtotime(html_attr($row['date_depart']))), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date_depart)%%>', urlencode(@date('d/m/Y', @strtotime(html_attr($urow['date_depart'])))), $templateCode);
		$templateCode = str_replace('<%%VALUE(date_retour)%%>', @date('d/m/Y', @strtotime(html_attr($row['date_retour']))), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date_retour)%%>', urlencode(@date('d/m/Y', @strtotime(html_attr($urow['date_retour'])))), $templateCode);
		$templateCode = str_replace('<%%VALUE(beneficiaire)%%>', safe_html($urow['beneficiaire']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(beneficiaire)%%>', urlencode($urow['beneficiaire']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(nom_prenomb)%%>', safe_html($urow['nom_prenomb']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(nom_prenomb)%%>', html_attr($row['nom_prenomb']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(nom_prenomb)%%>', urlencode($urow['nom_prenomb']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(dossier_pdf)%%>', safe_html($urow['dossier_pdf']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(dossier_pdf)%%>', html_attr($row['dossier_pdf']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(dossier_pdf)%%>', urlencode($urow['dossier_pdf']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(dossier_zippe)%%>', safe_html($urow['dossier_zippe']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(dossier_zippe)%%>', html_attr($row['dossier_zippe']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(dossier_zippe)%%>', urlencode($urow['dossier_zippe']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(statut)%%>', safe_html($urow['statut']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(statut)%%>', html_attr($row['statut']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(statut)%%>', urlencode($urow['statut']), $templateCode);
		$templateCode = str_replace('<%%VALUE(questionnaire_medical)%%>', safe_html($urow['questionnaire_medical']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(questionnaire_medical)%%>', urlencode($urow['questionnaire_medical']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(question1)%%>', safe_html($urow['question1']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(question1)%%>', html_attr($row['question1']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question1)%%>', urlencode($urow['question1']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(question2)%%>', safe_html($urow['question2']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(question2)%%>', html_attr($row['question2']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question2)%%>', urlencode($urow['question2']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(question2a)%%>', safe_html($urow['question2a']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(question2a)%%>', html_attr($row['question2a']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question2a)%%>', urlencode($urow['question2a']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(question3)%%>', safe_html($urow['question3']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(question3)%%>', html_attr($row['question3']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question3)%%>', urlencode($urow['question3']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(question4)%%>', safe_html($urow['question4']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(question4)%%>', html_attr($row['question4']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question4)%%>', urlencode($urow['question4']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(question5)%%>', safe_html($urow['question5']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(question5)%%>', html_attr($row['question5']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question5)%%>', urlencode($urow['question5']), $templateCode);
		$templateCode = str_replace('<%%VALUE(prime_total)%%>', safe_html($urow['prime_total']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(prime_total)%%>', urlencode($urow['prime_total']), $templateCode);
	}else{
		$templateCode = str_replace('<%%VALUE(idassurance)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(idassurance)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(date)%%>', '1', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date)%%>', urlencode('1'), $templateCode);
		$templateCode = str_replace('<%%VALUE(nom_prenom)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(nom_prenom)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(intermediare)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(intermediare)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(pays_residence)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(pays_residence)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(adresse)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(adresse)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(destination)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(destination)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(formule)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(formule)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(certificat)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(certificat)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(date_depart)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date_depart)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(date_retour)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date_retour)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(beneficiaire)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(beneficiaire)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(nom_prenomb)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(nom_prenomb)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(dossier_pdf)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(dossier_pdf)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(dossier_zippe)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(dossier_zippe)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(statut)%%>', 'En cours', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(statut)%%>', urlencode('En cours'), $templateCode);
		$templateCode = str_replace('<%%VALUE(questionnaire_medical)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(questionnaire_medical)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(question1)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question1)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(question2)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question2)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(question2a)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question2a)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(question3)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question3)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(question4)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question4)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(question5)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(question5)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(prime_total)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(prime_total)%%>', urlencode(''), $templateCode);
	}

	// process translations
	foreach($Translation as $symbol=>$trans) {
		$templateCode = str_replace("<%%TRANSLATION($symbol)%%>", $trans, $templateCode);
	}

	// clear scrap
	$templateCode = str_replace('<%%', '<!-- ', $templateCode);
	$templateCode = str_replace('%%>', ' -->', $templateCode);

	// hide links to inaccessible tables
	if($_REQUEST['dvprint_x'] == '') {
		$templateCode .= "\n\n<script>\$j(function() {\n";
		$arrTables = getTableList();
		foreach($arrTables as $name => $caption) {
			$templateCode .= "\t\$j('#{$name}_link').removeClass('hidden');\n";
			$templateCode .= "\t\$j('#xs_{$name}_link').removeClass('hidden');\n";
		}

		$templateCode .= $jsReadOnly;
		$templateCode .= $jsEditable;

		if(!$selected_id) {
			$templateCode.="\n\tif(document.getElementById('emailEdit')) { document.getElementById('emailEdit').style.display='inline'; }";
			$templateCode.="\n\tif(document.getElementById('emailEditLink')) { document.getElementById('emailEditLink').style.display='none'; }";
			$templateCode.="\n\tif(document.getElementById('dossier_pdfEdit')) { document.getElementById('dossier_pdfEdit').style.display='inline'; }";
			$templateCode.="\n\tif(document.getElementById('dossier_pdfEditLink')) { document.getElementById('dossier_pdfEditLink').style.display='none'; }";
			$templateCode.="\n\tif(document.getElementById('dossier_zippeEdit')) { document.getElementById('dossier_zippeEdit').style.display='inline'; }";
			$templateCode.="\n\tif(document.getElementById('dossier_zippeEditLink')) { document.getElementById('dossier_zippeEditLink').style.display='none'; }";
		}

		$templateCode.="\n});</script>\n";
	}

	// ajaxed auto-fill fields
	$templateCode .= '<script>';
	$templateCode .= '$j(function() {';

	$templateCode .= "\tnom_prenom_update_autofills$rnd1 = function() {\n";
	$templateCode .= "\t\t\$j.ajax({\n";
	if($dvprint) {
		$templateCode .= "\t\t\turl: 'assurances_autofill.php?rnd1=$rnd1&mfk=nom_prenom&id=' + encodeURIComponent('".addslashes($row['nom_prenom'])."'),\n";
		$templateCode .= "\t\t\tcontentType: 'application/x-www-form-urlencoded; charset=" . datalist_db_encoding . "',\n";
		$templateCode .= "\t\t\ttype: 'GET'\n";
	} else {
		$templateCode .= "\t\t\turl: 'assurances_autofill.php?rnd1=$rnd1&mfk=nom_prenom&id=' + encodeURIComponent(AppGini.current_nom_prenom{$rnd1}.value),\n";
		$templateCode .= "\t\t\tcontentType: 'application/x-www-form-urlencoded; charset=" . datalist_db_encoding . "',\n";
		$templateCode .= "\t\t\ttype: 'GET',\n";
		$templateCode .= "\t\t\tbeforeSend: function() { \$j('#nom_prenom$rnd1').prop('disabled', true); \$j('#nom_prenomLoading').html('<img src=loading.gif align=top>'); },\n";
		$templateCode .= "\t\t\tcomplete: function() { " . (($arrPerm[1] || (($arrPerm[3] == 1 && $ownerMemberID == getLoggedMemberID()) || ($arrPerm[3] == 2 && $ownerGroupID == getLoggedGroupID()) || $arrPerm[3] == 3)) ? "\$j('#nom_prenom$rnd1').prop('disabled', false); " : "\$j('#nom_prenom$rnd1').prop('disabled', true); ")."\$j('#nom_prenomLoading').html(''); \$j(window).resize(); }\n";
	}
	$templateCode .= "\t\t});\n";
	$templateCode .= "\t};\n";
	if(!$dvprint) $templateCode .= "\tif(\$j('#nom_prenom_caption').length) \$j('#nom_prenom_caption').click(function() { /* */ nom_prenom_update_autofills$rnd1(); });\n";


	$templateCode.="});";
	$templateCode.="</script>";
	$templateCode .= $lookups;

	// handle enforced parent values for read-only lookup fields

	// don't include blank images in lightbox gallery
	$templateCode = preg_replace('/blank.gif" data-lightbox=".*?"/', 'blank.gif"', $templateCode);

	// don't display empty email links
	$templateCode=preg_replace('/<a .*?href="mailto:".*?<\/a>/', '', $templateCode);

	/* default field values */
	$rdata = $jdata = get_defaults('assurances');
	if($selected_id) {
		$jdata = get_joined_record('assurances', $selected_id);
		if($jdata === false) $jdata = get_defaults('assurances');
		$rdata = $row;
	}
	$templateCode .= loadView('assurances-ajax-cache', array('rdata' => $rdata, 'jdata' => $jdata));

	// hook: assurances_dv
	if(function_exists('assurances_dv')) {
		$args=array();
		assurances_dv(($selected_id ? $selected_id : FALSE), getMemberInfo(), $templateCode, $args);
	}

	return $templateCode;
}
?>