<html>
<script language="javascript" src="xp_progress.js"></script>
<body>
<?php
	$sPage = "couple_search";
	include("admin/§lib_inc.php");

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

	echo "<center>";

	echo "<form name=rec id=rec>";

	echo "<table cellpadding=10 cellspacing=2 border=1>";

	echo "<tr bgcolor=#ccccff><th>";
	display_lng_text("SEARCH_HUSBAND");
	echo "</th><td>";
	// Husband Patronyme 
	display_lng_text("STATS_PATRO");
	echo " : <input id=list_husband_patro name=list_husband_patro></select></td><td>";
	// Husband first name
	display_lng_text("SEARCH_FIRSTNAME");	
	echo " : <input id=list_husband_firstname name=list_husband_firstname></select></td>";

	echo "</tr><tr bgcolor=#ffcccc><th>";
	display_lng_text("SEARCH_WIFE");
	echo "</th><td>";

	// Wife Patronyme 
	display_lng_text("STATS_PATRO");
	echo " : <input id=list_wife_patro name=list_wife_patro></select></td><td>";
	// Wife first name
	display_lng_text("SEARCH_FIRSTNAME");	
	echo " : <input id=list_wife_firstname name=list_wife_firstname></select></td>";

	// Date
	echo "</tr><tr bgcolor=#dddddd><th>";
	display_lng_text("EVNT_DATE");
	echo "</td><td>";
	display_lng_text("DETAIL_DATE_BEGIN");
	echo " : <input id=date_begin name=date_begin size=6> &nbsp; ";
	display_lng_text("DETAIL_DATE_END");
	echo " : <input id=date_end name=date_end size=6><br>";

	echo "<td align=center><input type=button value='".decode_lng_text("PRIVATE_ENTER")."' onclick='search();'></td></tr>";

	echo "</table>";
	echo "</form><br>";
?>
	<div id="progress_bar">
	<script type="text/javascript">
	var progress_bar= createBar(450,15,'white',1,'blue','#dd2727',85,7,3,"");
	</script>
	</div>
<?php
	echo "<div id=tab>".decode_lng_text("SEARCH_NO_CRITERIA")."</div>";
	echo "<div id=bottom_pages></div>";
//	echo "<div id=debug></div>";


	$sId_Event_Type_UNION=get_id_event_type('UNION');
	$sId_Event_Type_BIRTH=get_id_event_type('BIRT');
	$sId_Event_Type_MARR=get_id_event_type('MARR');
	$sId_Event_Type_DATE=get_id_event_type('DATE');
	$sId_Event_Type_PLACE=get_id_event_type('PLAC');
	$sId_Event_Type_PLACE=get_id_event_type('PLAC');
	
	mysql_close(); // Déconnexion de MySQL
	affiche_pied_page(true);

?>
</td></tr></table>
<script type="text/javascript">

	glb_iPage=1;
	glb_iMessagesParPage=25;
	document.getElementById('progress_bar').style.display="none"

function postSQLreqHTTP(sReq, fct)
{
	var oRequest = new XMLHttpRequest();
	var sURL  = "§req.php?REQ="+sReq;

	oRequest.open("POST", sURL, true);
	oRequest.setRequestHeader("User-Agent", navigator.userAgent);

	oRequest.onreadystatechange = function ()
	{
		if (oRequest.readyState == 4 && oRequest.status == 200)
		{
			fct(oRequest.responseText);
		}
	};

	oRequest.send(null)
}

function GenerateReg()
{
	glb_iPage=1;

	sId_Event_Type_UNION=<?php echo $sId_Event_Type_UNION; ?>;
	sId_Event_Type_BIRTH=<?php echo $sId_Event_Type_BIRTH; ?>;
	sId_Event_Type_MARR=<?php echo $sId_Event_Type_MARR; ?>;
	sId_Event_Type_DATE=<?php echo $sId_Event_Type_DATE; ?>;
	sId_Event_Type_PLACE=<?php echo $sId_Event_Type_PLACE; ?>;

	// With oxg_events2
	sSqlFrom = " FROM oxg_events2 e_union " +

		 " LEFT JOIN oxg_individuals i_husband ON e_union.id_individu=i_husband.id_individual and e_union.id_family=i_husband.id_family " +			// Husband

		 " LEFT JOIN oxg_patronyms p_husband ON i_husband.id_patronym=p_husband.id_patronym " +								// Husband patronyme

		 " LEFT JOIN oxg_events2 e_husband_birth ON e_husband_birth.id_family=e_union.id_family and e_husband_birth.id_individu=e_union.id_individu AND e_husband_birth.type="+sId_Event_Type_BIRTH+" " + // Husband birth

		 " LEFT JOIN oxg_individuals i_wife ON e_union.id_zone=i_wife.id_individual and e_union.id_family=i_wife.id_family" +					// Wife

		 " LEFT JOIN oxg_patronyms p_wife ON i_wife.id_patronym=p_wife.id_patronym " +									// Wife patronyme

		 " LEFT JOIN oxg_events2 e_wife_birth ON e_wife_birth.id_individu=e_union.id_zone AND e_wife_birth.type="+sId_Event_Type_BIRTH+" AND e_union.id_family=e_union.id_family " + // Wife birth

//		 " LEFT JOIN oxg_events e_marr ON e_marr.id_family=e_union.id_family and e_marr.type_father=0 and e_marr.id_father=e_union.id_individu and e_marr.info2=e_union.id_zone AND e_marr.type="+sId_Event_Type_MARR+" " + // Marriage

//		 " LEFT JOIN oxg_events e_marr_date ON e_marr_date.id_family=e_union.id_family and e_marr_date.type_father=1 and e_marr_date.id_father=e_marr.id_event AND e_marr_date.type="+sId_Event_Type_DATE+" " + // Marriage date

		 " LEFT JOIN oxg_events2 e_marr2 ON e_marr2.id_family=e_union.id_family and e_marr2.id_individu=e_union.id_individu and e_marr2.id_partner=e_union.id_partner AND e_marr2.type="+sId_Event_Type_MARR+" " + // Marriage event2

//		 " LEFT JOIN oxg_events e_marr_place ON e_marr_place.id_family=e_union.id_family and e_marr_place.type_father=1 and e_marr_place.id_father=e_marr2.id_event AND e_marr_place.type="+sId_Event_Type_PLACE+" " + // Marriage place

		 " LEFT JOIN oxg_countries e_marr_country ON e_marr_country.id_country=e_marr2.id_country " + // Marriage country place

		 " LEFT JOIN oxg_zones e_marr_zone ON e_marr_zone.id_country=e_marr2.id_country and e_marr_zone.id_zone=e_marr2.id_zone " + // Marriage zone place

		 " LEFT JOIN oxg_cities e_marr_city ON e_marr_city.id_country=e_marr2.id_country and e_marr_city.id_zone=e_marr2.id_zone and e_marr_city.id_city=e_marr2.id_city " + // Marriage zone place

		 " ";
	sSqlFrom += " WHERE i_husband.sex=0 and e_union.type="+sId_Event_Type_UNION;

/*	// With oxg_events
	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.info1=i4.id_individual AND i4.id_family=e.id_family " +	// Union conjoint
		 " 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 " +
	sSqlFrom += " WHERE i.sex=0 and e2.type="+sId_Event_Type_UNION;*/

	if(document.rec.list_husband_patro.value!="")
	{
		sSqlFrom += " AND (p_husband.libelle like '%"+document.rec.list_husband_patro.value+"%' ";
		sSqlFrom += " OR p_husband.libelle like '"+document.rec.list_husband_patro.value+"%' ";
		sSqlFrom += " OR p_husband.libelle like '%"+document.rec.list_husband_patro.value+"') ";
	}
	if(document.rec.list_husband_firstname.value!="")
	{
		sSqlFrom += " AND (i_husband.first_name like '%"+document.rec.list_husband_firstname.value+"%' ";
		sSqlFrom += " OR i_husband.first_name like '"+document.rec.list_husband_firstname.value+"%' ";
		sSqlFrom += " OR i_husband.first_name like '%"+document.rec.list_husband_firstname.value+"') ";
	}
	if(document.rec.list_wife_patro.value!="")
	{
		sSqlFrom += " AND (p_wife.libelle like '%"+document.rec.list_wife_patro.value+"%' ";
		sSqlFrom += " OR p_wife.libelle like '"+document.rec.list_wife_patro.value+"%' ";
		sSqlFrom += " OR p_wife.libelle like '%"+document.rec.list_wife_patro.value+"') ";
	}
	if(document.rec.list_wife_firstname.value!="")
	{
		sSqlFrom += " AND (i_wife.first_name like '%"+document.rec.list_wife_firstname.value+"%' ";
		sSqlFrom += " OR i_wife.first_name like '"+document.rec.list_wife_firstname.value+"%' ";
		sSqlFrom += " OR i_wife.first_name like '%"+document.rec.list_wife_firstname.value+"') ";
	}
	if(document.rec.date_begin.value!="")
	{
		sSqlFrom += " AND (coalesce(EXTRACT(YEAR FROM (e_marr2.devnt)),IF(length(e_marr2.sDtEvnt)=4, e_marr2.sDtEvnt,NULL),IF(e_marr2.sDtEvnt like '%25ABT%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'ABT')%2B4),NULL),IF(e_marr2.sDtEvnt like '%25BEF%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'BEF')%2B4),NULL),IF(e_marr2.sDtEvnt like '%25AFT%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'AFT')%2B4),NULL),IF(e_marr2.sDtEvnt like '%25BET%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'BET')%2B4,instr(e_marr2.sDtEvnt,'BET')%2B8),NULL),IF(e_marr2.sDtEvnt like '%25AND%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'AND')%2B4,instr(e_marr2.sDtEvnt,'AND')%2B8),NULL))>="+document.rec.date_begin.value+" ";
		sSqlFrom += " OR IF(e_marr2.sDtEvnt like '%25AND%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'AND')%2B4,instr(e_marr2.sDtEvnt,'AND')%2B8),NULL)>="+document.rec.date_begin.value+") ";
	}
	if(document.rec.date_end.value!="")
	{
		sSqlFrom += " AND (coalesce(EXTRACT(YEAR FROM (e_marr2.devnt)),IF(length(e_marr2.sDtEvnt)=4, e_marr2.sDtEvnt,NULL),IF(e_marr2.sDtEvnt like '%25ABT%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'ABT')%2B4),NULL),IF(e_marr2.sDtEvnt like '%25BEF%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'BEF')%2B4),NULL),IF(e_marr2.sDtEvnt like '%25AFT%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'AFT')%2B4),NULL),IF(e_marr2.sDtEvnt like '%25BET%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'BET')%2B4,instr(e_marr2.sDtEvnt,'BET')%2B8),NULL),IF(e_marr2.sDtEvnt like '%25AND%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'AND')%2B4,instr(e_marr2.sDtEvnt,'AND')%2B8),NULL))<="+document.rec.date_end.value+" ";
		sSqlFrom += " OR IF(e_marr2.sDtEvnt like '%25AND%25',substr(e_marr2.sDtEvnt,instr(e_marr2.sDtEvnt,'AND')%2B4,instr(e_marr2.sDtEvnt,'AND')%2B8),NULL)<="+document.rec.date_end.value+") ";
	}
}

function launch_PageLink()
{
	sSqlHead = " SELECT count(*) from (select null ";
	postSQLreqHTTP(sSqlHead+sSqlFrom + ") cnt_lig", PageLink);
}

function PageLink(sListRes)
{
	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)
{
	document.getElementById("tab").innerHTML="";
	document.getElementById("bottom_pages").innerHTML="";
	GenerateReg();
	launch_search(1);
}

function s2(iPage)
{
	glb_iPage=iPage;
	launch_search(iPage);
}

function launch_search(iPage)
{
	iPage--;
	sTab="<table border=1 cellspacing=0>";

	sTab+="<th><?php display_lng_text("SEARCH_HUSBAND"); ?></th>";
	sTab+="<th><?php display_lng_text("MAR_DBH"); ?></th>";
	sTab+="<th><?php display_lng_text("SEARCH_WIFE"); ?></th>";
	sTab+="<th><?php display_lng_text("MAR_DBW"); ?></th>";
	sTab+="<th><?php display_lng_text("SEARCH_MARR_PLACE"); ?></th>";
	sTab+="<th><?php display_lng_text("SEARCH_MARR_DATE"); ?></th>";

	// With oxg_events2
	sSqlHead = " SELECT e_union.id_family, e_union.id_individu, concat(p_husband.libelle,' ',i_husband.first_name), coalesce(DATE_FORMAT(e_husband_birth.devnt,'%Y-%m-%d'), e_husband_birth.sDtEvnt)";
	sSqlHead += ", i_wife.id_individual, concat(p_wife.libelle,' ',i_wife.first_name), coalesce(DATE_FORMAT(e_wife_birth.devnt,'%Y-%m-%d'),e_wife_birth.sDtEvnt)";
	sSqlHead += ", concat(e_marr_country.libelle, ', ', e_marr_zone.libelle, ', ', e_marr_city.libelle, ' (', e_marr2.sPlace,')'), coalesce(DATE_FORMAT(e_marr2.libelle,'%Y-%m-%d'),e_marr2.sDtEvnt) ";

	// With oxg_events
//	sSqlHead = " SELECT concat(p_husband.libelle,' ',i_husband.first_name), coalesce(DATE_FORMAT(e_husband_birth.devnt,'%Y-%m-%d'),e_husband_birth.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_husband.libelle ASC, i_husband.first_name ASC " +
				" LIMIT "+glb_iMessagesParPage*iPage+", "+glb_iMessagesParPage;

	document.getElementById('progress_bar').style.display="block";
//	document.getElementById('debug').innerHTML=sSqlHead+sSqlFrom+sSqlLimit;

	postSQLreqHTTP(sSqlHead+sSqlFrom+sSqlLimit, display_results);
}

function display_results(sListRes)
{
//	document.getElementById("rec").innerHTML="";
	if(sListRes=="")
	{
		document.getElementById("tab").innerHTML="<?php display_lng_text("SEARCH_NO_RESULTS"); ?>"; // DEBUG sSqlHead+sSqlFrom+sSqlLimit;
		document.getElementById("bottom_pages").innerHTML="";
		document.getElementById('progress_bar').style.display="none"
	}

	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[1]+"&FAMILY="+lListCol[0]+"\">"+lListCol[2]+"</a><td>"+Date2StringLang(lListCol[3])+"<td><a href=\"§detail_individu.php?INDIVIDU="+lListCol[4]+"&FAMILY="+lListCol[0]+"\">"+lListCol[5]+"</a><td>"+Date2StringLang(lListCol[6])+"<td>"+lListCol[7]+"<td>"+Date2StringLang(lListCol[8]);
	}
	sTab+="</tr></table>";

	document.getElementById('progress_bar').style.display="none";
	document.getElementById("tab").innerHTML=sTab;
	launch_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) // event date
	{
		dlDate=sDate.split(":");
		lDate=dlDate[0].split("/");
		sDay=lDate[2]+" ";
		sMonth=sMois[parseInt(lDate[1])]+ " ";
		sYear=lDate[0];
		return sDateFormat.replace("D",sDay).replace("Y",sYear).replace("M",sMonth);
	}
	else */
	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>