<html>
<body>
<?php
	$sPage = "birth_list";
	include("admin/§lib_inc.php");

	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 
	$CITY=isset($_REQUEST["CITY"])?$_REQUEST["CITY"] : -1; 
	$sTitle = decode_lng_text("EVNT_TITL");
	require("§menu.php");

	echo "<center>";

	echo "<form name=rec>";

	if($CITY=="-1")
	{
		// Countries
		display_lng_text("GEDTAG_CTRY");
		echo " : <select id=list_country name=list_country onchange='LoadListZones()'>";
		echo "</select> ";
	
		// Zones
		display_lng_text("EVNT_ZONE");	
		echo " : <select id=list_zone name=list_zone onchange='LoadListCities()'>";
		echo "</select> ";
	
		// City
		display_lng_text("GEDTAG_CITY");	
		echo " : <select id=list_city name=list_city>";
		echo "</select><br>";
	}
	
	// Events
	display_lng_text("EVNT_EVNT");
	echo " : <select id=list_event name=list_event>";
	echo "</select><br>";

	// Date
	display_lng_text("EVNT_DATE");
	echo " : ";
	display_lng_text("DETAIL_DATE_BEGIN");
	echo " : <input id=date_begin name=date_begin> : ";
	display_lng_text("DETAIL_DATE_END");
	echo " : <input id=date_end name=date_end><br>";

	echo "<input type=button value='".decode_lng_text("EVNT_SEA")."' onclick='GenerateReg();search(1);PageLink();'>";

	echo "</form>";

//	echo "<div id=top_pages></div>";
	echo "<div id=tab></div>";
	echo "<div id=bottom_pages></div>";

	mysql_close(); // Déconnexion de MySQL
?>
<script type="text/javascript">

	glb_iPage=1;
	glb_iMessagesParPage=25;
	iPremiereEntree=0;
	sCity=<?php echo $CITY; ?>;
	
	sSqlFrom="";
	sReq="select oxg_events_type.id_type_event, concat('GEDTAG_',oxg_events_type.code) as code from oxg_events2, oxg_events_type ";
	sReq+=" where oxg_events2.type=oxg_events_type.id_type_event ";
	sReq+=" and oxg_events_type.code not in ('UNION','TITL','OCCU','ASSO','DSCR','EDUC','GIVN','REFN') ";
	sReq+=" group by oxg_events_type.id_type_event, oxg_events_type.code";
	sListEvent=getSQLreqHTTP(sReq);
	lListEvent=sListEvent.split("|");
	elSel = document.getElementById("list_event");
	addSel(elSel, "Tous", "All");
	for(iCpt=0;iCpt<lListEvent.length;iCpt++)
	{
		lListCol=lListEvent[iCpt].split("¤");
		addSel(elSel, lListCol[1], lListCol[0]);
	}

	if(sCity!=-1)
	{
		GenerateReg(sCity);
		search(1);
		PageLink();
	}
	else
	{
		sListPays=getSQLreqHTTP("select oxg_countries.id_country, concat(oxg_countries.libelle,' (',count(*),')') libelle from oxg_events2, oxg_countries where oxg_events2.id_country = oxg_countries.id_country group by oxg_countries.id_country, oxg_countries.libelle");
		lListPays=sListPays.split("|");
		elSel = document.getElementById("list_country");
		addSel(elSel, "Tous", "All");
		for(iCpt=0;iCpt<lListPays.length;iCpt++)
		{
			lListCol=lListPays[iCpt].split("¤");
			addSel(elSel, lListCol[1], lListCol[0]);
		}
	
		LoadListZones();
	}	

function LoadListZones()
{
	sCountry=document.rec.list_country.value;
	sListZones=getSQLreqHTTP("select oxg_zones.id_zone, concat(oxg_zones.libelle,' (',count(*), ')') from oxg_events2, oxg_zones where oxg_events2.id_country=oxg_zones.id_country and oxg_events2.id_zone=oxg_zones.id_zone and oxg_zones.id_country="+sCountry+" group by oxg_zones.id_zone, oxg_zones.libelle");
	lListZones=sListZones.split("|");
	elSel = document.getElementById("list_zone");
	elSel.length=0;
	addSel(elSel, "Toutes", "All");
	for(iCpt=0;iCpt<lListZones.length;iCpt++)
	{
		lListCol=lListZones[iCpt].split("¤");
		if(lListCol[1]!=null && trim(lListCol[1])!="")
		{
			addSel(elSel, lListCol[1], lListCol[0]);
		}
	}
	LoadListCities();
}

function trim(str, chars)
{
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function LoadListCities()
{
	sCountry=document.rec.list_country.value;
	sZone=document.rec.list_zone.value;
	sListCities=getSQLreqHTTP("select oxg_cities.id_city, concat(oxg_cities.libelle,' (',count(*),')') libelle from oxg_events2, oxg_cities where oxg_events2.id_country=oxg_cities.id_country and oxg_events2.id_zone=oxg_cities.id_zone and oxg_events2.id_city=oxg_cities.id_city and oxg_cities.id_country="+sCountry+" and oxg_cities.id_zone="+sZone+" group by oxg_cities.id_city, oxg_cities.libelle");
	lListCities=sListCities.split("|");
	elSel = document.getElementById("list_city");
	elSel.length=0;
	addSel(elSel, "Toutes", "All");
	for(iCpt=0;iCpt<lListCities.length;iCpt++)
	{
		lListCol=lListCities[iCpt].split("¤");
		if(lListCol[1]!=null && trim(lListCol[1])!="")
		{
			addSel(elSel, lListCol[1], lListCol[0]);
		}
	}
}

function getSQLreqHTTP(sReq)
{
	var oRequest = new XMLHttpRequest();
	var sURL  = "req.php?REQ="+sReq;
	oRequest.open("GET", sURL, false);
	oRequest.setRequestHeader("User-Agent", navigator.userAgent);
	oRequest.send(null)

	if (oRequest.status==200)
	{
		return oRequest.responseText;
	}
	else
	{
		alert("Error executing XMLHttpRequest call!");
	}
}

function GenerateReg()
{
	glb_iPage=1;

	sSqlFrom = " FROM oxg_events2 e2 " +
		 " LEFT JOIN oxg_events e ON e.id_father=e2.id_individu AND e.id_event=e2.id_event AND e.id_family=e2.id_family " +
		 " LEFT JOIN oxg_individuals i4 ON e.info2=i4.id_individual AND i4.id_family=e.id_family " +
		 " LEFT JOIN oxg_patronyms p4 ON i4.id_patronym=p4.id_patronym " +
		 " LEFT JOIN oxg_individuals i ON e2.id_individu=i.id_individual " +
		 " LEFT JOIN oxg_individuals i2 ON i.id_father=i2.id_individual " +
		 " LEFT JOIN oxg_individuals i3 ON i.id_mother=i3.id_individual " +
		 " LEFT JOIN oxg_patronyms p ON i.id_patronym=p.id_patronym " +
		 " LEFT JOIN oxg_patronyms p2 ON i2.id_patronym=p2.id_patronym " +
		 " LEFT JOIN oxg_patronyms p3 ON i3.id_patronym=p3.id_patronym " +
		 " LEFT JOIN oxg_countries cnt ON e2.id_country=cnt.id_country " +
		 " INNER JOIN oxg_events_type evnt ON e2.type=evnt.id_type_event and evnt.code not in ('UNION','TITL','OCCU','ASSO','DSCR','EDUC','GIVN','REFN') " +
		 " LEFT JOIN oxg_zones zn ON e2.id_zone=zn.id_zone and e2.id_country=zn.id_country " +
		 " LEFT JOIN oxg_cities c ON e2.id_city=c.id_city and e2.id_zone=c.id_zone and e2.id_country=c.id_country ";
	sSqlFrom += " WHERE 1=1 ";
	if(document.rec.list_event.value!="All")
	{
		sSqlFrom += " AND e2.type="+document.rec.list_event.value+" ";
	}
	if(sCity!=-1)
	{
			sSqlFrom += " AND e2.id_city="+sCity+" ";
	}
	else
	{
		if(document.rec.list_city.value!="All")
		{
			sSqlFrom += " AND e2.id_city="+document.rec.list_city.value+" ";
		}
		if(document.rec.list_zone.value!="All")
		{
			sSqlFrom += " AND e2.id_zone="+document.rec.list_zone.value+" ";
		}
		if(document.rec.list_country.value!="All")
		{
			sSqlFrom += " AND e2.id_country="+document.rec.list_country.value+" ";
		}
	}
	if(document.rec.date_begin.value!="")
	{
		sSqlFrom += " AND (coalesce(EXTRACT(YEAR FROM (e2.devnt)),IF(length(sDtEvnt)=4, sDtEvnt,NULL),IF(sDtEvnt like '%25ABT%25',substr(sDtEvnt,instr(sDtEvnt,'ABT')%2B4),NULL),IF(sDtEvnt like '%25BEF%25',substr(sDtEvnt,instr(sDtEvnt,'BEF')%2B4),NULL),IF(sDtEvnt like '%25AFT%25',substr(sDtEvnt,instr(sDtEvnt,'AFT')%2B4),NULL),IF(sDtEvnt like '%25BET%25',substr(sDtEvnt,instr(sDtEvnt,'BET')%2B4,instr(sDtEvnt,'BET')%2B8),NULL),IF(sDtEvnt like '%25AND%25',substr(sDtEvnt,instr(sDtEvnt,'AND')%2B4,instr(sDtEvnt,'AND')%2B8),NULL))>="+document.rec.date_begin.value+" ";
		sSqlFrom += " OR IF(sDtEvnt like '%25AND%25',substr(sDtEvnt,instr(sDtEvnt,'AND')%2B4,instr(sDtEvnt,'AND')%2B8),NULL)>="+document.rec.date_end.value+") ";
	}
	if(document.rec.date_end.value!="")
	{
		sSqlFrom += " AND (coalesce(EXTRACT(YEAR FROM (e2.devnt)),IF(length(sDtEvnt)=4, sDtEvnt,NULL),IF(sDtEvnt like '%25ABT%25',substr(sDtEvnt,instr(sDtEvnt,'ABT')%2B4),NULL),IF(sDtEvnt like '%25BEF%25',substr(sDtEvnt,instr(sDtEvnt,'BEF')%2B4),NULL),IF(sDtEvnt like '%25AFT%25',substr(sDtEvnt,instr(sDtEvnt,'AFT')%2B4),NULL),IF(sDtEvnt like '%25BET%25',substr(sDtEvnt,instr(sDtEvnt,'BET')%2B4,instr(sDtEvnt,'BET')%2B8),NULL),IF(sDtEvnt like '%25AND%25',substr(sDtEvnt,instr(sDtEvnt,'AND')%2B4,instr(sDtEvnt,'AND')%2B8),NULL))<="+document.rec.date_end.value+" ";
		sSqlFrom += " OR IF(sDtEvnt like '%25AND%25',substr(sDtEvnt,instr(sDtEvnt,'AND')%2B4,instr(sDtEvnt,'AND')%2B8),NULL)<="+document.rec.date_end.value+") ";
	}
//	sSqlFrom += " group by p.libelle, i.first_name, e2.sDtEvnt, cnt.libelle, zn.libelle, c.libelle, p2.libelle, i2.first_name, p3.libelle, i3.first_name, i.id_individual, i.id_family, evnt.code, i4.first_name, p4.libelle ";
}

function PageLink()
{
	sSqlHead = " SELECT count(*) from (select null ";
	sListRes=getSQLreqHTTP(sSqlHead+sSqlFrom + ") cnt_lig");

	if(sListRes>glb_iMessagesParPage)
	{
		sPages="<?php 	display_lng_text("STATS_PAGE"); ?>"+" : ";//+"+sListRes+"+ /"+glb_iMessagesParPage + " = " + glb_iPage + " :";
	
		iMaxPage=sListRes;
		iMaxPage/=glb_iMessagesParPage;
		iMaxPage+=1.5;
		iMaxPage=Math.round(iMaxPage);
		if(glb_iPage>iMaxPage-10)
		{
			if(iMaxPage-10>=1)
			{
				iCpt=iMaxPage-10;
			}
			else
			{
				iCpt=1;
			}
		}
		else
		{
			iCpt=glb_iPage;
		}
		if(iCpt>1)
		{
			sPages+="<a onclick='s2(1)'><<</a> "
			sPages+="<a onclick='s2("+(glb_iPage-1)+")'><</a> "
		}
		for(;iCpt<iMaxPage && iCpt<glb_iPage+10;iCpt++)
		{
			if(iCpt==glb_iPage)
			{
				sPages+="<b>"+iCpt+ "</b> ";
			}
			else
			{
				sPages+="<a onclick='s2("+iCpt+")'>"+iCpt+"</a> ";
			}
		}
		if(iCpt<iMaxPage)
		{
			sPages+="<a onclick='s2("+(glb_iPage+1)+")'>></a> ";
			sPages+="<a onclick='s2(iMaxPage-1)'>>></a> ";
		}
	//	document.getElementById("top_pages").innerHTML=sPages;
		document.getElementById("bottom_pages").innerHTML=sPages;
	}
	else
	{
		document.getElementById("bottom_pages").innerHTML="";
	}
}

function search(iPage)
{
	iPage--;
	sTab="<table border=1 cellspacing=0>";
   
	sTab+="<th><?php display_lng_text("GEDTAG_NAME"); ?></th>";
	sTab+="<th><?php display_lng_text("EVNT_EVNT"); ?></th>";
	sTab+="<th><?php display_lng_text("GEDTAG_DATE"); ?></th>";
	sTab+="<th><?php display_lng_text("GEDTAG_PLAC"); ?></th>";
	sTab+="<th><?php display_lng_text("DETAIL_IND_FATHER"); ?></th>";
	sTab+="<th><?php display_lng_text("DETAIL_IND_MOTHER"); ?></th>";
	sTab+="<th><?php display_lng_text("EVNT_SPS"); ?></th>";

	sSqlHead = " SELECT concat(p.libelle,' ',i.first_name), coalesce(DATE_FORMAT(e2.devnt,'%Y-%m-%d'),e2.sDtEvnt), cnt.libelle, zn.libelle, c.libelle, concat(p2.libelle, ' ', i2.first_name), concat(p3.libelle, ' ',i3.first_name), i.id_individual, i.id_family, concat('GEDTAG_', evnt.code), i4.first_name, p4.libelle ";
//	, i.id_patronym, i.id_father, i.id_mother, i2.id_patronym, i3.id_patronym, p.id_patronym, cnt.libelle as pays, zn.libelle as zone, c.libelle as city ";

	sSqlLimit = " ORDER BY p.libelle ASC, i.first_name ASC " +
				" LIMIT "+glb_iMessagesParPage*iPage+", "+glb_iMessagesParPage;

	sListRes=getSQLreqHTTP(sSqlHead+sSqlFrom+sSqlLimit);

	lListReq=sListRes.split("|");
	for(iCpt=0;iCpt<lListReq.length;iCpt++)
	{
		sTab+="<tr>";
		lListCol=lListReq[iCpt].split("¤");
		sPlace=lListCol[2];
		if(lListCol[3]!=null && lListCol[3]!="")
		{
			sPlace+=", "+lListCol[3];
		}
		if(lListCol[4]!=null && lListCol[4]!="")
		{
			sPlace+=", "+lListCol[4];
		}
		sTab+="<td><a href=\"§detail_individu.php?INDIVIDU="+lListCol[7]+"&FAMILY="+lListCol[8]+"\">"+lListCol[0]+"</a><td>"+lListCol[9]+"<td>"+Date2StringLang(lListCol[1])+"<td>"+sPlace+"<td>"+lListCol[5]+"<td>"+lListCol[6]+"<td>"+lListCol[11]+" "+lListCol[10];
	}

	document.getElementById("tab").innerHTML=sTab;
}

function s2(iPage)
{
	glb_iPage=iPage;
	search(iPage);
	PageLink();
}

function addSel(eSel, sVal, sNum)
{
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		eSel.add(new Option(sVal, sNum));
	}
	else
	{
		eSel.add(new Option(sVal, sNum), null);
	}
}

function Date2StringLang(sDate)
{
	sBefore="<?php display_lng_text("BEFORE"); ?>";
	sAfter="<?php display_lng_text("AFTER"); ?>";
	sAbout="<?php display_lng_text("ABOUT"); ?>";
	sBetween="<?php display_lng_text("BETWEEN"); ?>";
	sAnd="<?php display_lng_text("AND"); ?>";
	sCal="<?php display_lng_text("CAL"); ?>";
	sDateFormat="<?php display_lng_text("DATE_FORMAT"); ?>";
	sMois=["","<?php display_lng_text("MONTH1"); ?>","<?php display_lng_text("MONTH2"); ?>","<?php display_lng_text("MONTH3"); ?>","<?php display_lng_text("MONTH4"); ?>","<?php display_lng_text("MONTH5"); ?>",
		"<?php display_lng_text("MONTH6"); ?>","<?php display_lng_text("MONTH7"); ?>","<?php display_lng_text("MONTH8"); ?>","<?php display_lng_text("MONTH9"); ?>","<?php display_lng_text("MONTH10"); ?>",
		"<?php display_lng_text("MONTH11"); ?>","<?php display_lng_text("MONTH12"); ?>"];
	if(sDate.indexOf("-")!=-1)
	{
		lDate=sDate.split("-");
		sDay=lDate[2]+" ";
		sMonth=sMois[parseInt(lDate[1])]+ " ";
		sYear=lDate[0];
		return sDateFormat.replace("D",sDay).replace("Y",sYear).replace("M",sMonth);
	}
	else
	{
		sDate=sDate.replace("JAN", sMois[1]);
		sDate=sDate.replace("FEB", sMois[2]);
		sDate=sDate.replace("MAR", sMois[3]);
		sDate=sDate.replace("APR", sMois[4]);
		sDate=sDate.replace("MAY", sMois[5]);
		sDate=sDate.replace("JUN", sMois[6]);
		sDate=sDate.replace("JUL", sMois[7]);
		sDate=sDate.replace("AUG", sMois[8]);
		sDate=sDate.replace("SEP", sMois[9]);
		sDate=sDate.replace("OCT", sMois[10]);
		sDate=sDate.replace("NOV", sMois[11]);
		sDate=sDate.replace("DEC", sMois[12]);
		sDate=sDate.replace("BEF", sBefore);
		sDate=sDate.replace("AFT", sAfter);
		sDate=sDate.replace("ABT", sAbout);
		sDate=sDate.replace("BET", sBetween);
		sDate=sDate.replace("AND", sAnd);
		sDate=sDate.replace("CAL", sCal);
		return sDate;
	}
}
</script>
</body>
</html>