<?php

if ($_SERVER["SERVER_NAME"]=="master") error_reporting(E_ALL);
else error_reporting();
if (eregi("master.php", $_SERVER["PHP_SELF"])) kick_me('http://'.$_SERVER['SERVER_NAME'].'/',0,0);
set_time_limit(0);
if (!isset($EXPORT_GLOBAL)){
	ob_implicit_flush(0);
	ob_start("my_header");
	$GLOBAL_TITRE='';
	}

function my_header($buffer){
	global $GLOBAL_TITRE;
	return '<!doctype html><html lang="fr"><head><title>'.$GLOBAL_TITRE.' - Cabinet LUCIANI AXA Assurances Porto Vecchio - Banque, Crédit, Placements</title>'.$buffer;
	}

connect_DB();


setlocale(LC_TIME,"fr_FR");
setlocale(LC_CTYPE,"fr_FR.iso8859-1@euro");


if (!eregi("login.php", $_SERVER["PHP_SELF"])){
	ini_set('session.use_only_cookies', "0");
	ini_set('session.use_trans_sid', "0");
	ini_set('url_rewriter.tags', "");
	unset($_GET["axa"]);
	}
	
$SESS_LENGTH=3600 * 4;
$SESS_LIFE=(time() + $SESS_LENGTH);
$maxlifetime=get_cfg_var("session.gc_maxlifetime");
session_cache_expire($SESS_LENGTH);
//session_set_cookie_params(($SESS_LENGTH * 12), "/", ".gipso.org");
session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_name('axa');
session_start();



$CLIENTS=array(
	"civilite"=>array(
		1=>"Mademoiselle",
		2=>"Madame",
		3=>"Monsieur",
		5=>"Madame et Monsieur",
		4=>"Société")
	);


$CONTRATS=array(
	"souscription_lieu"=>array(
		2=>"Pompidou",
		1=>"Ville",
		),
		
	"souscription_origine"=>array(
		4=>"Spontané",
		1=>"Internet Perso",
		2=>"Internet AXA",
		3=>"Apporteur d'affaire",
		5=>"Recommandation",
		6=>"Démarchage téléphonique",
		7=>"Démarchage par mailing"
		),
		
	"resiliation_par"=>array(
		1=>"Client",
		2=>"Compagnie"
		),
		
	"resiliation_motif"=>array(
		"Echéance"=>array(
			"1"=>"Meilleur tarif compagnie adverse",
			"2"=>"Offre spéciale chez concurrent",
			"3"=>"Mécontentement sinistre",
			"4"=>"Mécontentement gestion",
			"5"=>"Mécontentement contrat",
			"6"=>"Mécontentement garantie",
			"7"=>"Autre risque impossible à souscrire chez AXA"
			),
		
		"Loi HAMON"=>array(
			"50"=>"Meilleur tarif compagnie adverse",
			"51"=>"Offre spéciale chez concurrent",
			"52"=>"Mécontentement sinistre",
			"53"=>"Mécontentement gestion",
			"54"=>"Mécontentement contrat",
			"55"=>"Mécontentement garantie",
			"56"=>"Autre risque impossible à souscrire chez AXA"
			),
		
		"Loi CHATEL"=>array(
			"10"=>"Meilleur tarif compagnie adverse",
			"11"=>"Offre spéciale chez concurrent",
			"12"=>"Mécontentement sinistre",
			"13"=>"Mécontentement gestion",
			"13"=>"Mécontentement gestion",
			"14"=>"Mécontentement contrat",
			"15"=>"Mécontentement garantie",
			"16"=>"Autre risque impossible à souscrire chez AXA"
			),
			
		"Changement de situation"=>array(
			"20"=>"Déménagement",
			"21"=>"Chômage",
			"22"=>"Changement de régime matrimonial",
			"23"=>"Changement profession",
			"24"=>"Retraite Professionnelle",
			"25"=>"Suspension"
			),
		
		"Autre"=>array(
			"30"=>"Remplacé autre contrat",
			"31"=>"Non paiement de prime",
			"32"=>"Justificatif",
			"33"=>"Décès",
			"34"=>"Vente",
			"35"=>"Disparition du risque",
			"36"=>"Sinistre",
			"37"=>"Refus de majoration",
			"38"=>"Sans effet",
			"39"=>"Pas de motif",
			"40"=>"Contrat temporaire"
			)
		),
		
	"sinistre_expert"=>array(
		"Aucun",
		"TEXA CORSE",
		"POLYEXPERT",
		"SERENA EXPERTISE",
		"GMC",
		"S A D",
		"[Garage] BALESI",
		"[Garage] MAGLIOLO",
		"[Garage] PIETRI",
		"[Garage] DETTORI",
		"[Garage] GROTTOLI",
		"[Garage] autre",
		"[Entreprise Service] SOS NETTOYAGE",
		"[Entreprise Service] Po-Vo Vitrage",
		"[Entreprise Service] Cyril TITRANT",
		"[Entreprise Service] Autre", // NE PAS TOUCHER
		"Autre" // NE PAS TOUCHER
		)
	);


$USERS=array(
	"type"=>array(
		1=>"administrateur",
		2=>"collaborateur",
		3=>"apporteur")
	);


$tmp_self_test=strtolower(substr(strrchr($_SERVER["PHP_SELF"], "/"), 1));
if (($tmp_self_test!="login.php") && ($tmp_self_test!="delog.php")) {
	$FROM_GLOBAL=strtolower(substr(strrchr($_SERVER["PHP_SELF"], "/"), 1)).(($_SERVER["QUERY_STRING"]!="")?"?":"").$_SERVER["QUERY_STRING"];
	$_SESSION["FROM_GLOBAL"]=$FROM_GLOBAL;
	}

if ($_SERVER["REQUEST_METHOD"]=="GET") 	foreach ($_GET as $g_key=>$g_value) ${$g_key}=$g_value;
if ($_SERVER["REQUEST_METHOD"]=="POST") foreach ($_POST as $p_key=>$p_value) ${$p_key}=$p_value;
foreach ($_SESSION as $s_key=>$s_value) ${$s_key}=$s_value;


unset($g_key, $g_value, $p_key, $p_value, $s_key, $s_value);

function connect_DB(){
	global $link;
	if ($_SERVER["SERVER_NAME"]=="master"){
		$host="localhost";
		$user="root";
		$bdd="axa";
		$password="";
		}
	else{
		$host="axadeperaxa.mysql.db";
		$user="axadeperaxa";
		$bdd="axadeperaxa";
		$password="Charlotte0";
		}
	
	
	$link=mysql_connect($host, $user, $password) or die("Le serveur est momentanément hors-service ! Désolé pour cet incident indépendant de notre volonté... Merci de revenir plus tard."); 
	mysql_select_db($bdd, $link) or die("Le serveur est momentanément hors-service ! Merci de revenir plus tard.");
	}
	
function sess_open($save_path, $session_name){
	return true;
	}

function sess_close(){
	return true;
	}

function sess_read($key){
	global $link;
	$qry="SELECT value FROM sessions WHERE sesskey='$key' AND expiry>'" . time()."'";
	$qid=mysql_query($qry, $link);
	if (list($value)=mysql_fetch_row($qid)){
		return $value;
		}
	else{
		$qry="DELETE FROM sessions WHERE sesskey='$key'";
		$qid=mysql_query($qry, $link);
		return '';
		}
	}

function sess_write($key, $val){
	global $SESS_LIFE, $link;
	$value= mysql_escape_string(trim($val));
	//$_SESSION["SS_nbclicks"]
	$qry="INSERT INTO sessions VALUES ('$key', '$SESS_LIFE', '$value')";
	$qid=mysql_query($qry, $link);
	if (!$qid){
		$qry="UPDATE sessions SET expiry='$SESS_LIFE', value='$value' WHERE sesskey='$key' ";
		$qid=mysql_query($qry, $link);
	}
	else mysql_query("OPTIMIZE TABLE sessions", $link);
	return $qid;
	}

function sess_destroy($key){
	global $link;
	$qry="DELETE FROM sessions WHERE sesskey='$key'";
	$qid=mysql_query($qry, $link);
	return $qid;
	}

function sess_gc($maxlifetime){
	global $link;
	$qry="DELETE FROM sessions WHERE expiry<'" . time()."'";
	$qid=mysql_query($qry, $link);
	return mysql_affected_rows($link);
	}

function sn($var){
	return mysql_escape_string($var);
	}

function kick_me($url="da_root",$time=0, $footer=1){
	if ($url=="root") $url=$_SERVER["PHP_SELF"];
	if ($url=="da_root") $url="index.php";
	if (!header("Location: http://".$_SERVER['SERVER_NAME'].substr($_SERVER["PHP_SELF"], 0, strrpos($_SERVER["PHP_SELF"], "/"))."/".$url)) echo '<!DOCTYPE html><html lang=fr><head><META HTTP-EQUIV=Refresh CONTENT="'.$time.'; URL='.$url.'"></head><body></body></html>';
	return true;
	}

function is_online(){
	if (isset($_SESSION['SS_session_online'])) {
		if (($_SESSION['SS_session_online']=="true") && (isset($_SESSION['SS_user_id']))) return true;
		else return false;
		}
	else return false;
	}

function is_admin(){
	if (isset($_SESSION['SS_session_online'])) {
		if (($_SESSION['SS_user_type']==1) && (isset($_SESSION['SS_user_id']))) return true;
		else return false;
		}
	else return false;
	}
	
function is_collaborateur(){
	if (is_admin()) return true;
	if (isset($_SESSION['SS_session_online'])) {
		if (($_SESSION['SS_user_type']==2) && (isset($_SESSION['SS_user_id']))) return true;
		else return false;
		}
	else return false;
	}

function is_apporteur(){
	if (is_collaborateur()) return true;
	if (isset($_SESSION['SS_session_online'])) {
		if (($_SESSION['SS_user_type']==3) && (isset($_SESSION['SS_user_id']))) return true;
		else return false;
		}
	else return false;
	}
	
function contrat_id_axa($id){
	global $link;
	$result=mysql_query("SELECT id_axa FROM contrats WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "id_axa");
	}
	
function contrat_id($id_axa){
	global $link;
	$result=mysql_query("SELECT id FROM contrats WHERE id_axa='$id_axa'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "id");
	}

	
function contrat_id_contrat($id){
	global $link;
	$result=mysql_query("SELECT id_contrat FROM contrats WHERE id='$id' AND id_contrat!='0'", $link);
	if (mysql_num_rows($result)==0) return $id;
	else return mysql_result($result, 0, "id_contrat");
	}
	
	
function contrat_id_client($id){
	global $link;
	$result=mysql_query("SELECT id_client FROM contrats WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "id_client");
	}
	
function contrat_type_nom($id){
	global $link;
	$result=mysql_query("SELECT nom FROM contrats_type WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "nom");
	}
		
function contrat_id_type($id){
	global $link;
	$result=mysql_query("SELECT id_type FROM contrats WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "id_type");
	}
	
function avenant_id($id){
	global $link;
	$result=mysql_query("SELECT id_contrat FROM contrats WHERE id='$id'", $link);
	$id_contrat=mysql_result($result, 0, "id_contrat");
	if ($id_contrat!=0){// MOD else ADD :D
		$result=mysql_query("SELECT id FROM contrats WHERE id_contrat='$id_contrat' ORDER BY date_effet_debut", $link);
		$i=1;
		while ($row=mysql_fetch_array($result)){
			if ($row["id"]==$id) return (($i<10)?"00":"0").$i;
			$i++;
			}
			}
	else {
		$count_me=mysql_num_rows(mysql_query("SELECT * FROM contrats WHERE id_contrat='".sn($id)."'"))+1;
		return (($count_me<10)?"00":"0").$count_me;
		}
	}
	
function contrat_relance_get($id){
	global $link;
	$result=mysql_query("SELECT relance FROM contrats WHERE id='$id'", $link);
	if (mysql_result($result, 0, "relance")=="") return array();
	else return unserialize(mysql_result($result, 0, "relance"));
	}

function contrat_relance_set($id, $relance){
	global $link;
	mysql_query("UPDATE contrats SET relance='".mysql_escape_string(serialize($relance))."' WHERE id='".$id."'", $link);
	return true;
	}


function contrat_incomplet_set($id){
	global $link;
	
	$result_date=mysql_query("SELECT max(date_add) as date_max FROM contrats WHERE id_contrat='".contrat_id_contrat($id)."'", $link);
	$date_last=substr(mysql_result($result_date, 0, "date_max"), 0, 10);
	if ($date_last=="") $date_last=substr(mysql_result(mysql_query("SELECT date_add FROM contrats WHERE id='".contrat_id_contrat($id)."'", $link), 0, "date_add"), 0, 10);

	
	
	$incomplet=0;
	if (mysql_num_rows(mysql_query("SELECT fournie FROM contrats_pieces WHERE id_contrat='".$id."' AND fournie='0'", $link))>0)
		$incomplet=1;
	mysql_query("UPDATE contrats SET incomplet='".$incomplet."' WHERE id='".$id."'", $link);
	
	if (mysql_num_rows(mysql_query("SELECT incomplet FROM contrats WHERE id='".contrat_id_contrat($id)."' AND incomplet='1'", $link))>0)
		$incomplet=1;
	mysql_query("UPDATE contrats SET date_last='".$date_last."', incomplet_global='".$incomplet."' WHERE id='".contrat_id_contrat($id)."'", $link);
	return true;
	}


function contrat_incomplet_get($id){
	global $link;
	$result=mysql_query("SELECT incomplet FROM contrats WHERE id='".$id."'", $link);
	if (mysql_result($result, 0, "incomplet")=="") return "";
	else return mysql_result($result, 0, "incomplet");
	}



function contrat_is_incomplet($id){
	global $link;
	$result=mysql_query("SELECT incomplet_global FROM contrats WHERE id='".$id."'", $link);
	if (mysql_result($result, 0, "incomplet_global")=="") return "";
	else return mysql_result($result, 0, "incomplet_global");
	}
	
function contrat_is_incomplet_id($id){
	global $link;
	if (mysql_num_rows(mysql_query("SELECT fournie FROM contrats_pieces WHERE id_contrat='".$id."' AND fournie='0'", $link))>0) 
		return true;
	else return false;
	}

	
	
function contrat_resiliation_init($resiliation){
	if (!isset($resiliation["par"])) 			$resiliation["par"]="1";
	if (!isset($resiliation["motif"])) 			$resiliation["motif"]="1";
	if (!isset($resiliation["more"])) 			$resiliation["more"]="";
	if (!isset($resiliation["remboursement"])) 	$resiliation["remboursement"]="0";
	if (!isset($resiliation["cheque"])) 		$resiliation["cheque"]="1";
	}
	
	
function contrat_resiliation_get($id){
	global $link;
	$result=mysql_query("SELECT resiliation FROM contrats WHERE id='$id'", $link);
	if (mysql_result($result, 0, "resiliation")=="") return array();
	else return unserialize(mysql_result($result, 0, "resiliation"));
	}

function contrat_resiliation_set($id, $resiliation){
	global $link;
	if ($resiliation!=""){
		$sql_more="";
		if ($resiliation["motif"]=="30") {
			$sql_more=", remplace_par='".sn($resiliation["more"])."'";
			mysql_query("UPDATE contrats SET remplace_par='".$id."' WHERE id_axa='".sn($resiliation["more"])."'", $link);
			}
		mysql_query("UPDATE contrats SET resiliation='".mysql_escape_string(serialize($resiliation))."' ".$sql_more." WHERE id='".$id."'", $link);
		}
	
	$resilie=0;
	if (mysql_num_rows(mysql_query("SELECT resiliation FROM contrats WHERE id_contrat='".contrat_id_contrat($id)."' AND resiliation!=''", $link))>0)
		$resilie=1;
	
	mysql_query("UPDATE contrats SET resilie='".$resilie."' WHERE id='".contrat_id_contrat($id)."'", $link);
	return true;
	}

function contrat_is_resiliation($id){// est-ce que cet avenant est une résiliation ?
	global $link;
	$result=mysql_query("SELECT id FROM contrats WHERE id='$id' AND resiliation!=''", $link);
	if (mysql_num_rows($result)==0) return false;
	else return true;
	}
	

function contrat_is_resilie($id){// est-ce que ce contrat qui n'est pas un avenant est résilié ?
	global $link;
	//$result=mysql_query("SELECT id FROM contrats WHERE id_contrat='$id' AND resiliation!=''", $link);
	$result=mysql_query("SELECT id FROM contrats WHERE id='$id' AND resilie='1'", $link);
	if (mysql_num_rows($result)==0) return false;
	else return true;
	}
	
function devis_type_nom($id){
	global $link;
	$result=mysql_query("SELECT nom FROM devis_type WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "nom");
	}
	
function relance_get_max($tab){
	$max=0;
	foreach ($tab as $key=>$value){
		$max=$key;
		}
	return $max;
	}

function client_id_axa($id){
	global $link;
	$result=mysql_query("SELECT id_axa FROM clients WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "id_axa");
	}

function client_id($id_axa){
	global $link;
	$result=mysql_query("SELECT id FROM clients WHERE id_axa='".mysql_escape_string($id_axa)."'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "id");
	}

function client_civilite($id){
	global $link;
	$result=mysql_query("SELECT civilite FROM clients WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "civilite");
	}
	
function client_nom($id){
	global $link;
	$result=mysql_query("SELECT nom, prenom FROM clients WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return strtoupper(mysql_result($result, 0, "nom"))." ".ucwords(mysql_result($result, 0, "prenom"));
	}

function utilisateur_nom($id){
	global $link;
	$result=mysql_query("SELECT prenom, nom FROM utilisateurs WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return ucwords(mysql_result($result, 0, "prenom"))." ".(mysql_result($result, 0, "nom"));
	}

function utilisateur_login($id){
	global $link;
	$result=mysql_query("SELECT login FROM utilisateurs WHERE id='$id'", $link);
	return mysql_result($result, 0, "login");
	}

function utilisateur_email($id){
	global $link;
	$result=mysql_query("SELECT email FROM utilisateurs WHERE id='$id'", $link);
	return mysql_result($result, 0, "email");
	}
	
function sinistre_type_nom($id){
	global $link;
	$result=mysql_query("SELECT nom FROM sinistres_type WHERE id='$id'", $link);
	if (mysql_num_rows($result)==0) return "";
	else return mysql_result($result, 0, "nom");
	}
	
function date_sql2txt($date){
	if ($date!=""){
		$tmp=explode("-", $date);
		if ($tmp[0]=="0000") $tmp[0]="";
		if ($tmp[1]=="00") $tmp[1]="";
		if ($tmp[2]=="00") $tmp[2]="";
		return $tmp[2].$tmp[1].$tmp[0];
		}
	else return "";
	}
	
	
function date_txt2sql($date){
	if ($date!=""){
		$date=str_numeric_only($date);
		$len=strlen($date);
			if ($len==8) return substr($date, 4, 4)."-".substr($date, 2, 2)."-".substr($date, 0, 2);
		elseif ($len==7) return "2".substr($date, 5, 3)."-".substr($date, 2, 2)."-".substr($date, 0, 2);
		elseif ($len==6) return "20".substr($date, 4, 2)."-".substr($date, 2, 2)."-".substr($date, 0, 2);
		elseif ($len==5) return "2".substr($date, 2, 3)."-".substr($date, 0, 2)."-00";
		elseif ($len==4) return "20".substr($date, 2, 2)."-".substr($date, 0, 2)."-00";
		elseif ($len==3) return "20".substr($date, 1, 2)."-".substr($date, 0, 1)."-00";
		elseif ($len==2) return "20".$date."00-00";
		elseif ($len==1) return "200".$date."-00-00";
		elseif ($len==0) return "0000-00-00";
		}
	else return "0000-00-00";
	}
	
function cleanstr($str){
	$str=strtolower($str);
	for ($i=0;$i < strlen($str);$i++){
		$test_str=ord(substr($str, $i, 1));
		$test_str_real=substr($str, $i, 1);
		if ((($test_str >= 97) && ($test_str <= 122)) || (($test_str <= 57) && ($test_str >= 48)) || (($test_str_real=="_"))) continue;
		else $str=substr_replace($str, " ", $i, 1);
		}
	$str=str_replace(" ", "", $str);
	if (trim($str)=="") $str="_";
	return $str;
	}

function str_numeric_only($str){
	for ($i=0;$i < strlen($str);$i++){
		$test_str=ord(substr($str, $i, 1));
		if ($test_str<=57 && $test_str>=48) continue;
		else $str=substr_replace($str, "0", $i, 1);
		}
	return $str;
	}
	
function mk_pass(){
	srand ((double) microtime() * 10000000);
	$voy=array("A", "E", "I", "O", "U", "Y");
	$voy_res=array_rand($voy, 2);
	$password="";
	for ($i=0;$i < 2;$i++){
		array_rand($voy);
		$password .= strtoupper(chr(rand(97, 122))) . $voy[$voy_res[$i]];
	}
	$password .= rand(10, 99);

	return $password;
	}

function html_form_date($date="0000-00-00", $prefix="", $month=false){
	if ($date=="") $date="0000-00-00";
	$dates=explode("-", $date);
	$jour=$dates[2];
	$mois=$dates[1];
	$annee=$dates[0];
	$return_me='<table border="0" cellpadding="0" cellspacing="0"><tr>
		<td><select name="'.$prefix.'jour" id="'.$prefix.'jour" style="width:50px;"><option value="00" >Jour</option>';
		
	for ($i=1;$i<=31;$i++){
		$return_me.='<option value="'.(($i<10)?"0".$i:$i).'" '.(($jour==$i)?"SELECTED":"").'>'.$i.'</option>';
		}
	$return_me.='</select>&nbsp;';
		
	$return_me.='<select name="'.$prefix.'mois" id="'.$prefix.'mois" style="width:85px;"><option value="00" >Mois</option>';
	for ($i=1;$i<=12;$i++){
		$return_me.='<option value="'.(($i<10)?"0".$i:$i).'" '.(($mois==$i)?"SELECTED":"").'>'.mois($i).'</option>';
		}
	$return_me.='</select>';
	
	if (!$month){
		$return_me.='&nbsp;<select name="'.$prefix.'annee" id="'.$prefix.'annee" style="width:65px;"><option value="0000" >Année</option>';
		for ($i=date("Y");$i>="1900";$i--){
			$return_me.='<option value="'.$i.'" '.(($annee==$i)?"SELECTED":"").'>'.$i.'</option>';
			}
		$return_me.='</select>';
		}
	$return_me.='</td></tr></table>';

	return $return_me;
	}
	
function html($text){
	// return nl2br(utf8_encode(html_kill_specialchars(trim((string)$text))), ENT_QUOTES);
	return nl2br(htmlentities(html_kill_specialchars(trim((string)$text)), ENT_QUOTES));
	}

function html_make_clickable($text, $font_class="normal"){ 
	//$ret=eregi_replace("www.([^[:space:]]*)([[:alnum:]#?/&=])", strtolower("<a href=\"www.\\1\\2\" target=\"_blank\">www.\\1\\2</a>"), $text);
	// grrrrrrrrrrrrrrrrrr :-(
	$ret=eregi_replace("((www\.)|([[:alpha:]{2,10}]+://)|([[:alpha:]{2,10}]+://www\.)){1}". "[^\.<>[:space:]]+\.[^<>[:space:]]+",  strtolower("<a class=\"".$font_class."\" href=\"http://\\0\" target=\"_blank\">\\0</a>"), $text);
	$ret=ereg_replace("http://([[:alpha:]]+://)","\\1",$ret);
	$ret=eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", strtolower(" <a class=\"".$font_class."\" href=\"mailto:\\1\" target=\"_new\">\\1</a>"), $ret);
	return($ret);
	}

function html_kill_specialchars($txt){
	$txt=str_replace('’', '\'', $txt);
	$txt=str_replace('&#8217;', '\'', $txt);
	$txt=str_replace('&#8216;', '\'', $txt);
	return $txt;
	}
	
function valid_ascii($string){
	$result="";
	$length=strlen($string);
	$idx=0;
	while($length--){
		$c=$string[$idx++];
		if ((ord($c)) > 127)
			return -1;
		$result .= $c;
		}
	return $result;
	}
	
	
function valid_mail($email, $empty_is_false=1){
	$regexp='/^(?:[^\s\000-\037\177\(\)<>@,;:\\"\[\]]\.?)+@(?:[^\s\000-\037\177\(\)<>@,;:\\\"\[\]]\.?)+\.[a-z]{2,6}$/Ui';
	$email=valid_ascii(trim($email));
	if(empty($email)){
		return ($empty_is_false)?-1:$email;
	}else{
		$result=preg_match($regexp, $email);
		return ($result)?$email:-1;
		}
	}

	
function datetime($date){
	$date=explode (" ", $date);
	$datedate=explode ("-", $date[0]);
	$dateheure=explode (":", $date[1]);
	$date=mktime($dateheure[0], $dateheure[1], $dateheure[2], $datedate[1], $datedate[2], $datedate[0]);
	return semaine(strftime("%w", $date)) . "&nbsp;" . strftime("%d", $date) . "&nbsp;" . mois(strftime("%m", $date)) . "&nbsp;" . strftime("%Y", $date).strftime("&nbsp;&agrave;&nbsp;%Hh%M", $date);
	}

	
function datetime_short($date){
	$date=explode (" ", $date);
	$datedate=explode ("-", $date[0]);
	$dateheure=explode (":", $date[1]);
	$date=mktime($dateheure[0], $dateheure[1], $dateheure[2], $datedate[1], $datedate[2], $datedate[0]);
	return strftime("%d/%m/%Y&nbsp;&agrave;&nbsp;%Hh%M", $date);
	}

function dateshort($date){
	if ($date=="0000-00-00") return "";
	$date=explode ("-", $date);
	if ($date[0] < "1970")
		return $date[0];
	else{
		if (($date[1]=="00") && ($date[2]=="00")) return $date[0];
		elseif (($date[2])=="00") return mois($date[1]) . " " . $date[0];
		else{
			$date=mktime(0, 0, 0, $date[1], $date[2], $date[0]);
			return strftime("%d/%m/%Y", $date);
			}
		}
	}

function datetime_locale($date){
	$date=explode (" ", $date);
	$datedate=explode ("-", $date[0]);
	//if ($_SESSION["langue"]!="en") $format="%d/%m";
	//else
	$format="%m/%d";
	$date=mktime(0, 0, 0, $datedate[1], $datedate[2], $datedate[0]);
	return strftime($format."/%Y", $date);
	}

function datenow_full(){
	$date=date("Y-m-d H:i:s");
	$date=explode (" ", $date);
	$datedate=explode ("-", $date[0]);
	$dateheure=explode (":", $date[1]);
	$date=mktime($dateheure[0], $dateheure[1], $dateheure[2], $datedate[1], $datedate[2], $datedate[0]);
	return strftime("&nbsp;%H&nbsp;h&nbsp;%Mm&nbsp;%Ss", $date)."&nbsp;&nbsp;<br>".semaine(strftime("%w", $date)) . "&nbsp;" . strftime("%d", $date) . "&nbsp;" . mois(strftime("%m", $date)) . "&nbsp;" . strftime("%Y", $date);
	}

function datedate($date){
	$date=explode ("-", $date);
	if ($date[0] < "1970") return $date[0];
	else{
		if (($date[1]=="00") && ($date[2]=="00")) return $date[0];
		elseif (($date[2])=="00") return mois($date[1]) . " " . $date[0];
		else{
			$date=mktime(0, 0, 0, $date[1], $date[2], $date[0]);
			return semaine(strftime("%w", $date)) . "&nbsp;" . strftime("%d", $date) . "&nbsp;" . mois(strftime("%m", $date)) . "&nbsp;" . strftime("%Y", $date);
			}
		}
	}

function datedate_short($date){
	$date=explode ("-", $date);
	if ($date[0] < "1970") return $date[0];
	else{
		if (($date[1]=="00") && ($date[2]=="00")) return $date[0];
		elseif (($date[2])=="00") return mois($date[1]) . " " . $date[0];
		else{
			$date=mktime(0, 0, 0, $date[1], $date[2], $date[0]);
			return strftime("%d", $date) . "&nbsp;" . mois(strftime("%m", $date)) . "&nbsp;" . strftime("%Y", $date);
			}
		}
	}

function html_form_calendar($id, $date_init, $first=false){
	$return_me="";
	
	if ($first) $return_me.='		
		<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar.css"  />
		<script type="text/javascript" src="calendar/calendar.js"></script>
		<script type="text/javascript" src="calendar/calendar-fr.js"></script>
		<script type="text/javascript" src="calendar/calendar-setup.js"></script>
		';
	$return_me.='
		<table cellpadding="0" cellspacing="0" border="0">
			<tr>
			<td width="80"><div align="center" id="'.$id.'_div">'.dateshort($date_init).'</div></td>
			<td><input type="button" id="'.$id.'_but" value="Modifier"></td>
			</tr>
		</table>
		<input type="hidden" id="'.$id.'" name="'.$id.'" value="'.$date_init.'">
		<script type="text/javascript">
		    Calendar.setup({
		        inputField     :    "'.$id.'",
		        ifFormat       :    "%Y-%m-%d", 
		        daFormat       :    "%d/%m/%Y",
				showsTime      :    false,
		        displayArea    :    "'.$id.'_div",
				button         :    "'.$id.'_but",
		        singleClick    :    true,       // double-click mode    
				cache  		   :    true,
		        step           :    1,
				firstDay 	   :	1
		    });
		</script>';
	return $return_me;//  daFormat       :    "%A %d %B %Y",
	}


function html_textarea($id_div, $content, $width, $height, $font, $first_item=false){
	
	if ($first_item) {
		$dh=opendir("images"); 
		$images="''";
		while (false !== ($dir=readdir($dh))) { 
			if (!is_dir($dir) && $dir !== '.' && $dir !== '..') $images.=", 'images/".$dir."'";
			}
		closedir($dh);
				
		$return_me='
		<script>
		function preload_plz() {
			var d=document; if(d.images){ if(!d.pre_l) d.pre_l=new Array();
			var i,j=d.pre_l.length,a=preload_plz.arguments; for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){ d.pre_l[j]=new Image; d.pre_l[j++].src=a[i];}}
			}
		document.onload=preload_plz('.$images.');
		function submit_div_html(){
			if (document.all){var divs=document.all.tags("DIV");}
			else {var divs =document.getElementsByTagName("DIV");}
			for (var d=0; d < divs.length; d++) { 
				if (divs[d].id.substr(0, 7)=="textes_") {
					z=divs[d].id.substr(7);
					eval("document.form1."+z+".value=divs[d].innerHTML");
					}
				}
			document.form1.submit();
			}
		
		
		
		</script>';
		}
	else $return_me="";
	
	
	$buttons=array(
	"bold"=>"Gras",
	"italic"=>"Italique",
	"Underline"=>"Souligné",
	"JustifyLeft"=>"Aligné à gauche",
	"JustifyCenter"=>"Centré",
	"JustifyRight"=>"Aligné à droite",
	"JustifyFull"=>"Justifié",
	"InsertOrderedList"=>"Insérer une liste ordonnée",
	"InsertUnorderedList"=>"Insérer une liste",
	"Indent"=>"Augmenter le retrait",
	"Outdent"=>"Diminuer le retrait",
	"CreateLink"=>"Créer un lien"
	);
	
	$buttons_after=array(
	"Undo"=>"Annuler",
	"Redo"=>"Répéter"
	);
	$format_status_function='format_test=document.queryCommandValue(\'FormatBlock\'); if(format_test==\'Normal\') formats_'.$id_div.'.selectedIndex=\'0\'; if(format_test==\'Heading 1\') formats_'.$id_div.'.selectedIndex=\'1\';  if(format_test==\'Heading 2\') formats_'.$id_div.'.selectedIndex=\'2\';';
	$color_status_function='color_test=document.queryCommandValue(\'ForeColor\'); if(color_test==\'0\') color_'.$id_div.'.selectedIndex=\'0\'; if(color_test==\'8202496\') color_'.$id_div.'.selectedIndex=\'1\'; if(color_test==\'12542757\') color_'.$id_div.'.selectedIndex=\'2\';';
	$return_me.= '<script>
				'.$id_div.'_drag_on=false;
				'.$id_div.'_paste_on=false;
				</script>
				
				
				<table cellpadding="2" cellspacing="0" border="0" width="'.$width.'">
					<tr><td bgcolor="#dddddd" width="'.($width).'">
					<table cellpadding="2" cellspacing="0" border="0" width="'.($width).'" >
						<tr>';
				
				if ($font) {
					$return_me.='<td bgcolor="#eeeeee"><select name="formats_'.$id_div.'" onchange="textes_'.$id_div.'.focus(); execCommand(\'FormatBlock\', false, \'<\'+this.options[this.selectedIndex].value+\'>\'); "><option value="p">Normal<option value="h1">Titre<option value="h2">Sous titre</select></td>
					<td bgcolor="#eeeeee"><select name="color_'.$id_div.'" onchange="execCommand(\'ForeColor\', false, this.options[this.selectedIndex].value); "><option value="black">Noir<option value="#00297D">Bleu<option value="#2563BF">Bleu clair</select></td>';//violet : 9E398B
					$font_handler='onkeyup="'.$format_status_function.' '.$color_status_function.'" onclick="'.$format_status_function.' '.$color_status_function.'"';
					}
				else $font_handler="";
				foreach($buttons as $key=>$value) $return_me.='<td bgcolor="#eeeeee" style="cursor:hand" onmouseover="this.style.background=\'#ffffff\';" onmouseDown="this.style.background=\'#dddddd\'; " onmouseout="this.style.background=\'#eeeeee\'"><img onclick="textes_'.$id_div.'.focus(); execCommand(\''.$key.'\');" ondragstart="event.returnValue=false; this.parentElement.style.backgroundColor=\'#eeeeee\'" src="images/'.$key.'.gif" border="0" alt="'.$value.'"></td>';
				$return_me.='<td width="100%" bgcolor="#eeeeee">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>';
				foreach($buttons_after as $key=>$value) $return_me.='<td bgcolor="#eeeeee" style="cursor:hand" onmouseover="this.style.background=\'#ffffff\';" onmouseDown="this.style.background=\'#dddddd\';" onmouseout="this.style.background=\'#eeeeee\'"><img onclick="textes_'.$id_div.'.focus(); execCommand(\''.$key.'\');" ondragstart="event.returnValue=false; this.parentElement.style.backgroundColor=\'#eeeeee\'" src="images/'.$key.'.gif" border="0" alt="'.$value.'"></td>';
				$return_me.= '</tr>
					</table></td></tr>
				
					<tr><td bgcolor="#eeeeee" width="'.($width).'">
						<div style="behavior:url(#default#savehistory);background-color:#ffffff; width:'.($width).'px; height:'.($height-15).'px; padding:4px; overflow:auto;" '.$font_handler.' 
						ondragleave="'.$id_div.'_drag_on=false;"
						onclick="this.focus()"
						ondragstart="'.$id_div.'_drag_on=true" 
						ondrop="if('.$id_div.'_drag_on) '.$id_div.'_drag_on=false; else event.returnValue=false;" 
						onPaste="if(!'.$id_div.'_paste_on){event.returnValue=false; '.$id_div.'_paste_on=true; if (window.clipboardData.getData(\'Text\')!=null) {execCommand(\'Paste\', false, window.clipboardData.getData(\'Text\'));} '.$id_div.'_paste_on=false;}" 
						id="textes_'.$id_div.'" 
						name="textes_'.$id_div.'" 
						contentEditable="true">'.$content.'</div>
						<input type="hidden" name="'.$id_div.'" value="">
					</td></tr>
				</table>
			';

	return $return_me;
	}

	
	
function html_textarea_var_post_post($texte, $class=""){

	if (trim(strip_tags(str_replace("&nbsp;", " ", $texte)))=="") $texte="";
	if ($class!="") {
		$class="class=".$class;
		$more_div_start='<div style="overflow:hidden; width:150px">';
		$more_div_end='</div>';
		}
	else {
		$more_div_start='';
		$more_div_end='';
		}
	
	$search=array("<STRONG>", "</STRONG>", "<EM>", "</EM>", "\r\n", '\r\n', '<A href=\"', '</A>');
	$replace=array("<b>", "</b>", "<i>", "</i>", "", "", $more_div_start.'<A target=\"_blank\" '.$class.' href=\"', '</a>'.$more_div_end);

	$texte=mysql_escape_string(trim(stripslashes($texte)));
	$texte=str_replace($search, $replace, $texte);
//	$texte=eregi_replace ("<a", '<a class="normal"', $texte);
	return $texte;
	}

function var_post_post($var){
	return mysql_escape_string(trim(stripslashes($var)));
	}
	
function rm_dir($dir) {
	if($objs=glob($dir."/*")){
		foreach($objs as $obj) {
        	is_dir($obj)? rmdirr($obj) : unlink($obj);
       		}
   		}
	rmdir($dir);
	} 
	
function mois ($mois){
	$liste_des_mois=array('01' => 'janvier', '02' => 'f&eacute;vrier', '03' => 'mars', '04' => 'avril', '05' => 'mai', '06' => 'juin', '07' => 'juillet', '08' => 'ao&ucirc;t', '09' => 'septembre', '10' => 'octobre', '11' => 'novembre', '12' => 'd&eacute;cembre',
		1 => 'janvier',
		2 => 'f&eacute;vrier',
		3 => 'mars',
		4 => 'avril',
		5 => 'mai',
		6 => 'juin',
		7 => 'juillet',
		8 => 'août',
		9 => 'septembre',
		);
	return $liste_des_mois[$mois];
	}

function semaine ($semaine){
	
	$liste_jour_semaine=array(
		0 => 'dimanche',
		1 => 'lundi',
		2 => 'mardi',
		3 => 'mercredi',
		4 => 'jeudi',
		5 => 'vendredi',
		6 => 'samedi'
		);
	return $liste_jour_semaine[$semaine];
	}

?>