// Test la compatibilité du navigateur
// 2007-08-03
function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	return xhr;
}

// Affiche un menu selon le paramètre passé
// 2007-04-09
function loadmenu(menu){
	var str = "";
	if(menu == "1"){
		str = str + "<a href='javascript:addBox()'>" + __('Add a member') + "</a> | ";
		str = str + "<a href='javascript:selectModMember()'>" + __('Modify a member') + "</a> | ";
		str = str + "<a href='javascript:selectDelMember()'>" + __('Delete a member') + "</a>";
		
		$("updatemenu").innerHTML = str;
	}
	else{
		$("updatemenu").innerHTML = "";
	}
}

function downloadFile(){
	var xhr = getXhr();
	xhr.open("GET","newpedigree/downloadForm.php" ,false);
	xhr.send(null);

	$("selectMember").style.display = "none";
	$("form").style.display = "block";
	$("form").innerHTML = xhr.responseText;
}

function manageAnswer(target,xhr){
	var message="";
	switch (xhr.readyState){
		case 0:
			message="Uninitialized!";
			break;
		case 1:
			message="Loading...";
			break;
		case 2:
			message="Loaded!";
			break;
		case 3:
			message="Interactivities...";
			break;
		case 4:
			message="Completed!<br><br>";
			message+=manageServerStatusCode(xhr);
			break;
		default:
			message="Ouhaou... I really don't know with which status you are dealing...";
			break;
	}
	$(target).innerHTML=message;
}

function manageServerStatusCode(xhr){
	var message=0;
	var urlCodePage="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html";
	switch (xhr.status){
		case 200:
			message=xhr.responseText;
			break;
		case 404:
			message="Hey guy what are you trying to do. This page doesn't exist! Try again...";
			break;
		default:
			message="I got a wrong ack from the server ";
			message+="<a href="+urlCodePage+">("+xhr.status+")</a>!<br>";
			message+="I will display below what I got from the server (If I got something)<br><br>";	
			message+=xhr.responseText;
			break;
	
	}
	return message;
}


// Mise à jour du div informations
// 2007-04-16
function updateInformations(operation,familyname){
	var xhr = getXhr()
	manageAnswer("informations",xhr);
	$("informations").style.display = "block";
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		manageAnswer("informations",xhr);
	}
	xhr.open("GET","newpedigree/informations.php?operation=" + operation + "&family=" + familyname ,true);
	xhr.send(null);
}

// Affiche le svg avec ou sans les upn
// 2007-04-19
function viewUPN(choice){
	var xhr = getXhr();
	var familyname = $("familyname").value;
	xhr.open("GET","newpedigree/viewUPN.php?choice=" + choice + "&familyname=" + familyname ,false);
	xhr.send(null);

	reloadFrame();
}

// Change le svg selon la famille choisie
// 2007-04-27
function changeSVG(pedigreeSVGFile){
	var xhr = getXhr();
	var familyname = $("familyname").value;
	xhr.open("GET","newpedigree/changeSVG.php?pedigreeSVGFile=" + pedigreeSVGFile ,false);
	xhr.send(null);
	
	reloadFrame();
}

// Affiche le formulaire pour entrer l'upn du membre à modifier
// 2007-04-16
function selectModMember(){
	var str = "";
	str = str + __('Enter the upn of the member to modify') + " : ";
	str = str + "<input type='text' id='modUpnMember' size='2'><br>";
	str = str + "<input type='button' onclick='modifyMember()' value='"+ __('Modify') + "' id='buMod'>";
	
	$("message").style.display = "none";
	$("form").style.display = "none";
	$("selectMember").style.display = "block";
	$("selectMember").innerHTML = str; 
}

// Affiche le formulaire pour entrer l'upn du membre à supprimer
// 2007-04-16
function selectDelMember(){
	var str = "";
	str = str + "<i>" + __('It is impossible to remove a member having children') + "</i><br>";
	str = str + __('Enter the upn of the member to delete') + " : ";
	str = str + "<input type='text' id='delUpnMember' size='2'><br>";
	str = str + "<input type='button' onclick='deleteMember()' value='" + __('Delete') + "' id='buDelete'>";
	
	$("selectMember").style.display = "block";
	$("selectMember").innerHTML = str; 
	$("message").style.display = "none";
	$("form").style.display = "none";
}

// Affiche les erreurs
// 2007-05-03
function viewError(error){
	$("message").style.display = "block";
	$("message").className = "error";
	$("message").innerHTML = __(error);
}

// Affiche les avertissements
// 2007-05-03
function viewAvertissment(avertissment){
	$("message").style.display = "block";
	$("message").className = "avertissment";
	$("message").innerHTML = __(avertissment);
}

// Affiche message success
// 2007-05-03
function viewSuccess(success){
	$("message").style.display = "block";
	$("message").className = "success";
	$("message").innerHTML = __(success);
}

// Change la couleur des cases du formulaire
//2007-05-04
function changeCellColor(id,statut){
	if(statut == "red"){
		$(id).style.backgroundColor = "#FFCCCC";
	}else{
		$(id).style.backgroundColor = "#99FF66";
	}
}

// Recharge le cadre
// 2007-05-03
function reloadFrame(){
	$("viewPedigree").innerHTML = "<iframe width='100%' height='540' id='image' src='newpedigree/reloadpedigree.php' border='0' frameborder='0'></iframe>";
}

// Vérifie si il y a des erreurs dans le fichier report.txt
function verifyError(){
	var xhr = getXhr()
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			if(xhr.responseText > 0){
				$("message").innerHTML = "Errors were noted you can consult them in this file : <a href='newpedigree/downloadfile.php?f=report'>report.txt</a>";
			}
		}
	}
	xhr.open("GET","newpedigree/verifycreateSVG.php",true);
	xhr.send(null);
	
}

// Affiche le formulaire en fonction du type sélectionné
// 2007-04-16
function addBox(type){
	var str ="";
	
	switch(type){
		case "parent" :
			str = str + "<table><tr height='50'><td>" + __('Add a') + " <select id='type' onchange='addBox(this.value)'>";
			str = str + "<option value='select'>" + __('Select') + "</option>";
			str = str + "<option value='parent' selected='selected'>" + __('Parent') + "</option>";
			str = str + "<option value='conjoint'>Conjoint</option><option value='child'>" + __('Child') + "</option>";
			str = str + "<option value='fratery'>" + __('Fratery') + "</option>";
			str = str + "</select> &nbsp;" + __('to') + "&nbsp; <input type='text' size='2' id='childUPN' maxlength='3' value='' onkeyup='checkParent()'</td></tr>";
			str = str + "<tr><td><b><u>" + __('Father') + "</u></b></td></tr>";
			str = str + "<tr height='50'><td>" + __('State') + " : <select id='fatherDiag'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Healthy') + "</option>";
			str = str + "<option value='2'>" + __('Affected') + "</option>";
			str = str + "</select>  " + __('Statute') + " : <select id='fatherStatut'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Alive') + "</option>"; 
			str = str + "<option value='2'>" + __('Dead') + "</option>";
			str = str + "</select></td></tr>";
			
			str = str + "<tr><td><b><u>" + __('Mother') + "</u></b></td></tr>";
			str = str + "<tr height='50'><td>" + __('State') + " : <select id='motherDiag'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Healthy') + "</option>";
			str = str + "<option value='2'>" + __('Affected') + "</option>";
			str = str + "</select>  " + __('Statute') + " : <select id='motherStatut'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Alive') + "</option>"; 
			str = str + "<option value='2'>" + __('Dead') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td><input type='button' id='buAdd' value='"+ __('Add parents') + "' onclick='addNewMember()'></td></tr></table>";
			
			$("form").innerHTML = str;
			$("message").style.display = "none";
			break;

		case "child" :
			str = str + "<table><tr height='50'><td>" + __('Add a') + " <select id='type' onchange='addBox(this.value)'>";
			str = str + "<option value='select'>" + __('Select') + "</option>";
			str = str + "<option value='parent'>" + __('Parent') + "</option>";
			str = str + "<option value='conjoint'>Conjoint</option>";
			str = str + "<option value='child' selected='selected'>" + __('Child') + "</option>";
			str = str + "<option value='fratery'>" + __('Fratery') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td>" + __('Sex') + " : <select id='sex'>";
			str = str + "<option value='1'>" + __('Male') + "</option><option value='2'>" + __('Female') + "</option>";
			str = str + "</select> &nbsp;" + __('to') + "&nbsp; <input type='text' size='2' id='father' maxlength='3' value='" + __('M') + "' onkeyup='checkSexMember(this.value,1)'>";
			str = str + " " +  __('and')  + "&nbsp; <input type='text' size='2' id='mother' maxlength='3' value='"+ __('W') + "' onkeyup='checkSexMember(this.value,2)'></td></tr>";
			str = str + "<tr height='50'><td>" + __('State') + " : <select id='diag'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Healthy') + "</option>";
			str = str + "<option value='2'>" + __('Affected') + "</option>";
			str = str + "</select> " + __('Statute') + " : <select id='statut'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Alive') + "</option>"; 
			str = str + "<option value='2'>" + __('Dead') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td><input type='button' id='buAdd' value='" + __('Add child') + "' onclick='addNewMember()'></td></tr></table>";
			
			$("form").innerHTML = str;
			$("message").style.display = "none";
			break;

		case "fratery" :
			str = str + "<table><tr height='50'><td>" + __('Add a') + " <select id='type' onchange='addBox(this.value)'>";
			str = str + "<option value='select'>" + __('Select') + "</option>";
			str = str + "<option value='parent'>" + __('Parent') + "</option>";
			str = str + "<option value='conjoint'>Conjoint</option><option value='child'>" + __('Child') + "</option>";
			str = str + "<option value='fratery' selected='selected'>" + __('Fratery') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td>" + __('Sex') + " : <select id='sex'>";
			str = str + "<option value='1'>" + __('Male') + "</option><option value='2'>" + __('Female') + "</option>";
			str = str + "</select> &nbsp;" + __('to') + "&nbsp; <input type='text' size='2' id='father' maxlength='3' value='" + __('M') + "' onkeyup='checkSexMember(this.value,1)'>";
			str = str + " " +  __('and')  + "&nbsp; <input type='text' size='2' id='mother' maxlength='3' value='"+ __('W') + "' onkeyup='checkSexMember(this.value,2)'></td></tr>";
			str = str + "<tr height='50'><td>" + __('State') + " : <select id='diag'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Healthy') + "</option>";
			str = str + "<option value='2'>" + __('Affected') + "</option>";
			str = str + "</select> " + __('Statute') + " : <select id='statut'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Alive') + "</option>"; 
			str = str + "<option value='2'>" + __('Dead') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td><input type='button' id ='buAdd' value='" + __('Add fratery') + "' onclick='addNewMember()'></td></tr></table>";
			
			$("form").innerHTML = str;
			$("message").style.display = "none";
			break;

		case "conjoint" :
			str = str + "<table><tr height='50'><td>" + __('Add a') + " <select id='type' onchange='addBox(this.value)'>";
			str = str + "<option value='select'>" + __('Select') + "</option>";
			str = str + "<option value='parent'>" + __('Parent') + "</option>";
			str = str + "<option value='conjoint' selected='selected'>Conjoint</option>";
			str = str + "<option value='child'>" + __('Child') + "</option>";
			str = str + "<option value='fratery'>" + __('Fratery') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td>" + __('Sex') + " : <select id='sex'>";
			str = str + "<option value='1'>" + __('Male') + "</option><option value='2'>" + __('Female') + "</option>";
			str = str + "</select> &nbsp;" + __('to') + "&nbsp; <input type ='text' size='2' id='conjoint' maxlength='3' onkeyup='checkMember()'></td></tr>";
			str = str + "<tr height='50'><td>" + __('State') + " : <select id='diag'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Healthy') + "</option>";
			str = str + "<option value='2'>" + __('Affected') + "</option>";
			str = str + "</select> " + __('Statute') + " : <select id='statut'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Alive') + "</option>"; 
			str = str + "<option value='2'>" + __('Dead') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td>" + __('Child sex') + " : <select id='childSex'>";
			str = str + "<option value='1'>" + __('Male') + "</option><option value='2'>" + __('Female') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td>" + __('Child state') + " : <select id='childDiag'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Healthy') + "</option>";
			str = str + "<option value='2'>" + __('Affected') + "</option>";
			str = str + "</select>";
			str = str + " &nbsp; " + __('Child statute') + " : <select id='childStatut'>";
			str = str + "<option value='0'>" + __('Unkown') + "</option><option value='1'>" + __('Alive') + "</option>"; 
			str = str + "<option value='2'>" + __('Dead') + "</option>";
			str = str + "</select></td></tr>";
			str = str + "<tr><td><input type='button' id='buAdd' value='" + __('Add conjoint') + "' onclick='addNewMember()'></td></tr></table>";
			
			$("form").innerHTML = str;
			$("message").style.display = "none";
			break;

		default :
			str = str + "<table><tr height='50'><td>" + __('Add a') + " <select id='type' onchange='addBox(this.value)'>";
			str = str + "<option value='select' selected='selected'>" + __('Select') + "</option>";
			str = str + "<option value='parent'>" + __('Parent') + "</option>";
			str = str + "<option value='conjoint'>Conjoint</option>";
			str = str + "<option value='child'>" + __('Child') + "</option>";
			str = str + "<option value='fratery'>" + __('Fratery') + "</option>";
			str = str + "</select></td></tr></table>";
			
			$("selectMember").style.display = "none";
			$("form").style.display = "block";
			$("message").style.display = "none";
			$("form").innerHTML = str;
			break;
			
	}
}

/************************************************
*                                               *
*        FONCTIONS DE CHECK SUR LE MEMBRE       *
*	                                        *
*************************************************/

// Contrôler si l'enfant à déja des parents
// 2007-05-04
function checkParent(){
	var xhr = getXhr();
	var upn = $("childUPN").value;
	var familyname = $("familyname").value;

	xhr.open("GET","newpedigree/checkparent.php?family=" + familyname + "&upn=" + upn ,false);
	xhr.send(null);
		
	switch(xhr.responseText){
		case "1" :
			changeCellColor("childUPN","red");
			viewAvertissment('This member has already parents');
			return "0";
			break;
			
		case "3" :
			changeCellColor("childUPN","red");
			viewError('This member does not exist');
			return "3";
			break;
		
		default :
			changeCellColor("childUPN","green");
			$("message").style.display = "none" ;
			return "1";
			break;
	}		
}

// Contrôler si le conjoint peut être ajouté
// 2007-05-04
function checkMember(){
	var upn = $("conjoint").value;
	var familyname = $("familyname").value;
	var sex = $("sex").value;
	var sexConjoint = checkSexConjoint();
	
	var xhr = getXhr();
	xhr.open("GET","newpedigree/checkmember.php?family=" + familyname + "&upn=" + upn  ,false);// Synchrone
	xhr.send(null);

	if(xhr.responseText == "0"){
		changeCellColor("conjoint","red");
		viewError('This member does not exist');
		return "notadd";
	}
		
	if(sex == sexConjoint){ // Sex = sex de la liste déroulante
		changeCellColor("conjoint","red");
		viewAvertissment('You cannnot add a conjoint of the same sex');
		return "notadd";
	}
	else{
		changeCellColor("conjoint","green");
		$("message").style.display = "none";
	}
	return upn;
}

// Contrôler le sex du membre
// 2007-05-04
function checkSexMember(upn,sex){
	var xhr = getXhr();
	var familyname = $("familyname").value;
	var member = "";
	var sumSex = "";

	xhr.open("GET","newpedigree/checksex.php?family=" + familyname + "&upn=" + upn  ,false);
	xhr.send(null);
	
	sumSex = parseInt(xhr.responseText) + sex;
	if(sex == "1"){
		member = "father";
	}
	else{
		member = "mother";
	}
	switch(sumSex){
		case 2 :
		case 4 :
			changeCellColor(member,"green");
			$("message").style.display = "none" ;
			return "0";
			break;
		
		case 3 :
		default :
			changeCellColor(member,"red");
			viewAvertissment('Check the upn of the member');
			return "1";
			break;
	}
}

// Contrôler le sex du conjoint
// 2007-04-16
function checkSexConjoint(){
	var xhr = getXhr();
	var upn = $("conjoint").value;
	var familyname = $("familyname").value;
	
	xhr.open("GET","newpedigree/checksex.php?family=" + familyname + "&upn=" + upn  ,false);
	xhr.send(null);
	return xhr.responseText;
}

/************************************************
*                                               *
*	        AJOUT D'UN MEMBRE               *  
*	                                        *
*************************************************/

// Ajouter les parents à un membre
// 2007-05-04
function addParent2Member(){
	var chUPN = $("childUPN").value;
	var familyname = $("familyname").value;
	var fatherDiag = $("fatherDiag").value;
	var fatherStatut = $("fatherStatut").value;
	var motherDiag = $("motherDiag").value;
	var motherStatut = $("motherStatut").value;
	
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			if (xhr.responseText == "added"){
				return "1";
			}
			return "0";
		}
	}
	xhr.open("GET","newpedigree/addparent.php?familyname=" + familyname + 
			"&chUPN=" + chUPN + 
			"&fatherDiag=" + fatherDiag + 
			"&fatherStatut=" + fatherStatut +
			"&motherDiag=" + motherDiag + 
			"&motherStatut=" + motherStatut 
			,true);
	xhr.send(null);
}

// Ajout le conjoint et son enfant
// 2007-05-04
function addConjoint(familyname){
	var xhr = getXhr()
	// Le nouveau membre
	var memberSex = $("sex").value;
	var memberDiag = $("diag").value;
	var memberStatut = $("statut").value;
	// L'enfant du membre
	var childSex = $("childSex").value;
	var childDiag = $("childDiag").value;
	var childStatut = $("childStatut").value;
	var upnParent = $("conjoint").value;
	
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			if (xhr.responseText == "added"){
				return "1";
			}
			viewError('Error in the addition of the child');
			return "0";
		}
	}
		
	xhr.open("GET","newpedigree/addconjoint.php?familyname=" + familyname + 
			"&upnParent=" + upnParent + 
			"&memberSex=" + memberSex + 
			"&memberDiag=" + memberDiag + 
			"&memberStatut=" + memberStatut + 
			"&childSex=" + childSex + 
			"&childDiag=" + childDiag + 
			"&childStatut=" + childStatut
			,true);
	xhr.send(null);
}

// Ajouter un enfant ou une fraterie 
// 2007-05-04
function addChild(familyname,sex,diag,statut){
	var xhr = getXhr();
	var father = $("father").value;
	var mother = $("mother").value;
	
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			if (xhr.responseText == "added"){
				return "1";
			}
			return "0";
		}
	}
		
	// Contrôle si l'upn de la case father est bien un homme
	var resultCheckFather = checkSexMember(father,1);
	if(resultCheckFather != "0"){
		changeCellColor("father","red");
		viewError('Want control the upn of the father');
		return "0";
	}
	
	// Contrôle si l'upn de la case mother est bien une femme
	var resultCheckMother = checkSexMember(mother,2);
	if(resultCheckMother !="0"){
		changeCellColor("mother","red");
		viewError('Want control the upn of the mother');
		return "0";
	}
	xhr.open("GET","newpedigree/createChild.php?familyname=" + familyname + 
			"&sex=" + sex +
			"&diag=" + diag +
			"&statut=" + statut +
			"&father=" + father +
			"&mother=" + mother,
			true);
			
	xhr.send(null);
}

// Appel fonction ajout de membre en fonction du choix
// 2007-05-04
function addNewMember(){
	var type = $("type").value;
	var familyname = $("familyname").value;
	var resultAdd = "0";
	$("buAdd").disabled = "true";
	
	switch(type){
		// Si le type de membre choisi est un conjoint
		case "conjoint":
			// Récupérer retour fonction checkMember
			var resultCheck = checkMember();
			if(resultCheck == "notadd"){
				$("buAdd").disabled = "";
				break;
			}
					
			resultAdd = addConjoint(familyname);
			$("buAdd").disabled = "";
			break;

		case "parent":
			// Contrôle si l'enfant à déja des parents
			var resultCheckParent = checkParent();
			if(resultCheckParent == "1"){
				resultAdd = addParent2Member();
			}
			$("buAdd").disabled = "";
			break;

		default :
			var sex = $("sex").value;
			var diag = $("diag").value;
			var statut = $("statut").value;
			resultAdd = addChild(familyname,sex,diag,statut);
			$("buAdd").disabled = "";
		break;
	}
	if(resultAdd == "0"){
		verifyError();
		return;
	}
	viewSuccess('Correctly added member');
	updateInformations(__('Add a member'),familyname);
	reloadFrame();
}

/************************************************
*                                               *
*	     MODIFICATION D'UN MEMBRE           *  
*	                                        *
*************************************************/

// Contrôler le sex du père ou de la mère 
// 2007-05-04 
function checkSexModMember(upn,sex){
	var xhr = getXhr();
	var familyname = $("familyname").value;
	
	xhr.open("GET","newpedigree/checkmodsex.php?family=" + familyname + "&upn=" + upn  ,false);
	xhr.send(null);
	if(xhr.responseText == sex || xhr.responseText == "0"){
		if(sex == "1"){
			changeCellColor("father","green");
		}
		else{
			changeCellColor("mother","green");
		}
		$("message").style.display = "none" ;
		return "0";
	}
	else{
		if(sex == "1"){
			changeCellColor("father","red");
		}
		else{
			changeCellColor("mother","red");
		}
		viewAvertissment('Check the upn of the member')
		return "1";
	}		
}

// Génère le formulaire de modification d'un membre
// 2007-05-04
function modifyMember(){
	var upnMember = $("modUpnMember").value;
	var familyname = $("familyname").value;
	$("form").style.display = "none";
	$("buMod").disabled = "true";
	$("modUpnMember").disabled = "true";
	
	var xhr = getXhr()
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			if(xhr.responseText == "0"){
				$("form").style.display = "none";
				viewError('This member does not exist');
				$("buMod").disabled = "";
				$("modUpnMember").disabled = "";
			}
			else{
				$("message").style.display = "none";
				$("form").style.display = "block";
				$("form").innerHTML = xhr.responseText;
				$("buMod").disabled = "";
				$("modUpnMember").disabled = "";
			}
		}
	}
	xhr.open("GET","newpedigree/modMember.php?family=" + familyname + "&upn=" + upnMember  ,true);
	xhr.send(null);
}

// Update les informations du membre
// 2007-05-04
function updateMember(line,family){
	var upnMember = $("upnMember").innerHTML;
	var family = $("familyname").value;
	var father = $("father").value;
	var mother = $("mother").value;
	var sex = $("sex").value;
	var diag = $("diag").value;
	var statut = $("statut").value;
	$("message").style.display = "none";
	$("validMod").disabled = "true";
	
	var xhr = getXhr()
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			$("validMod").disabled = "";
			viewSuccess('This member was updated');
			updateInformations(__('Modify a member'),family);
			reloadFrame();
			verifyError();
		}
	}
	var resultCheckFather = checkSexModMember(father,1);
	
	// Le membre ne peut pas être parent lui même
	if(father == upnMember || mother == upnMember){
		viewError('You cannot add the member like parent');
		return;
	}
	// Vérifie si le membre à deux parents différents de 0
	if(father == 0 && mother != 0){
		viewError('A member cannot have one parent');
		return;
	}
	if(father != 0 && mother == 0){
		viewError('A member cannot have one parent');
		return;
	}
	if(resultCheckFather != "0"){
		viewError('Check the upn of the father');
		return;
	}
	
	resultCheckMother = checkSexModMember(mother,2);
	if(resultCheckMother != "0"){
		viewError('Check the upn of the mother');
		return;
	}
			
	xhr.open("GET","newpedigree/updateMember.php?line=" + line + 
			"&family=" + family + 
			"&father=" + father + 
			"&mother=" + mother + 
			"&sex=" + sex + 
			"&diag="+ diag+ 
			"&statut=" + statut  
			,true);
	xhr.send(null);
}

/************************************************
*                                               *
*	     SUPPRESSION D'UN MEMBRE            *  
*	                                        *
*************************************************/

// Supprime le membre
// 2007-04-16
function deleteMember(){
	var upnMember = $("delUpnMember").value;
	var familyname = $("familyname").value;
	var xhr = getXhr()
	$("buDelete").disabled = "true";

	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			$("buDelete").disabled = "";
			if(xhr.responseText == "1"){
				viewError('You cannot remove this member. Check which it does not have any more children');
			}
			else if(xhr.responseText == "3"){
				viewError('This member does not exist');
			}
			else{
				viewSuccess('This member was removed');
				verifyError();
				updateInformations(__('Delete a member'),family);
			}
		}
	}
	xhr.open("GET","newpedigree/deleteMember.php?family=" + familyname + "&upn=" + upnMember  ,true);
	xhr.send(null);
	reloadFrame();
}

/************************************************
*                                               *
*	   AJOUTER UNE NOUVELLE FAMILLE         *  
*	                                        *
*************************************************/

// Génère le formulaire pour ajouter une famille
// 2007-05-07
function createNewFamilyForm(){
	var xhr = getXhr();
	
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			$("message").style.display = "none";
			$("selectMember").style.display = "none";
			$("form").style.display = "block";
			$("form").innerHTML = xhr.responseText;
		}
	}
	
	xhr.open("GET","newpedigree/createfamilyform.php" ,true);
	xhr.send(null);
}

// Ajouter une nouvelle famille
// 2007-05-07
function addNewFamily(){
	var xhr = getXhr();
	var familyname = $("newFamilyName").value;
	var fatherDiag = $("fatherDiag").value;
	var fatherStatut = $("fatherStatut").value;
	var motherDiag = $("motherDiag").value;
	var motherStatut = $("motherStatut").value;
	var chSex = $("chSex").value;
	var chDiag = $("chDiag").value;
	var chStatut = $("chStatut").value;
	$("buAddFamily").disabled = "true";
	
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			if(xhr.responseText == "createfamily"){
				$("buAddFamily").disabled = "";
				viewSuccess('The family was created');
				updateInformations(__('New family'),familyname);
				reloadFrame();
			}
			else{
				viewError('The family was not created because it already exists');
				$("buAddFamily").disabled = "";
			}
		}
	}
	xhr.open("GET","newpedigree/addnewfamily.php?familyname=" + familyname + 
			"&fatherDiag=" + fatherDiag +
			"&fatherStatut=" + fatherStatut +
			"&motherDiag=" + motherDiag +
			"&motherStatut=" + motherStatut +
			"&chSex=" + chSex +
			"&chDiag=" + chDiag +
			"&chStatut=" + chStatut 
			,true);
	xhr.send(null);
}
