/*#######################################
	LISTES UTILES
#########################################*/
var listeMois = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
var anneeCourante = (new Date()).getFullYear();
var listeAnnee = new Array(anneeCourante-1899);
cpt=0;
for (a=anneeCourante; a>1899; a--){
	listeAnnee[cpt] = a;
	cpt++;
}

var listeAnneeFutur = new Array(2100-anneeCourante);
cpt=0;
for (a=anneeCourante; a<2100; a++){
	listeAnneeFutur[cpt] = a;
	cpt++;
}

/*#######################################
	FONCTIONS DE CREATION D'ELEMENTS HTML
#########################################*/

/**
 * Créer un champ de sélection (combo list)
 */
function creerSelect(selectId, selectName, selectClass, firstOptionValue, firstOptionLabel, list, labelEqualsValue, onchange){
	select = document.createElement('select');
	idSelect = document.createAttribute('id');
	idSelect.nodeValue = selectId;
	nameSelect = document.createAttribute('name');
	nameSelect.nodeValue = selectName;
	if (selectClass && selectClass!=''){
		classSelect = document.createAttribute('class');
		classSelect.nodeValue = selectClass;
		select.setAttributeNode(classSelect);
	}
	if (onchange && onchange!=''){
		select.onchange = function(){eval(onchange)}
	}
	select.setAttributeNode(idSelect);
	select.setAttributeNode(nameSelect);
	option = document.createElement('option');
	valueOption = document.createAttribute('value');
	valueOption.nodeValue = firstOptionValue;
	option.setAttributeNode(valueOption);
	libelleOption = document.createTextNode(firstOptionLabel);
	option.appendChild(libelleOption);
	select.appendChild(option);
	for(i=0; i<list.length; i++){
		option = document.createElement('option');
		valueOption = document.createAttribute('value');
		if (!labelEqualsValue){
			valueOption.nodeValue = list[i][0];
			libelleOption = document.createTextNode(list[i][1]);
		} else {
			valueOption.nodeValue = list[i];
			libelleOption = document.createTextNode(list[i]);
		}
		option.setAttributeNode(valueOption);
		option.appendChild(libelleOption);
		select.appendChild(option);
	}
	
	return select;
}

/**
 * Crée un champ input du type donné
 */
function creerInput(inputId, inputName, inputType, inputValue, inputClass){
	input = document.createElement('input');
	typeInput = document.createAttribute('type');
	typeInput.nodeValue = inputType;
	idInput = document.createAttribute('id');
	idInput.nodeValue = inputId;
	nameInput = document.createAttribute('name');
	nameInput.nodeValue = inputName;
	valueInput = document.createAttribute('value');
	valueInput.nodeValue = inputValue;
	if (inputClass && inputClass!=''){
		classInput = document.createAttribute('class');
		classInput.nodeValue=inputClass;
		input.setAttributeNode(classInput);
	}
	input.setAttributeNode(typeInput);
	input.setAttributeNode(idInput);
	input.setAttributeNode(nameInput);
	input.setAttributeNode(valueInput);
	
	return input;
}

/**
 * Crée une image
 */
function creerImage(imageId, imageSrc, imageWidth, imageHeight, imageAlt){
	image = document.createElement('img');
	idImage = document.createAttribute('id');
	idImage.nodeValue = imageId;
	srcImage = document.createAttribute('src');
	srcImage.nodeValue = imageSrc;
	wImage = document.createAttribute('width');
	wImage.nodeValue = imageWidth;
	hImage = document.createAttribute('height');
	hImage.nodeValue = imageHeight;
	altImage = document.createAttribute('alt');
	altImage.nodeValue = imageAlt;
	titleImage = document.createAttribute('title');
	titleImage.nodeValue = imageAlt;
	
	image.setAttributeNode(idImage);
	image.setAttributeNode(srcImage);
	image.setAttributeNode(wImage);
	image.setAttributeNode(hImage);
	image.setAttributeNode(altImage);
	
	return image;
}

/**
 * Cree un lien
 */
function creerLien(lienHref, lienClass){
	lien = document.createElement('a');
	hrefLien = document.createAttribute('href');
	hrefLien.nodeValue = lienHref;
	lien.setAttributeNode(hrefLien);
	
	if (lienClass && lienClass!=''){
		classLien = document.createAttribute('class');
		classLien.nodeValue = lienClass;
		lien.setAttributeNode(classLien);
	}
	
	return lien
}

/**
 * Crée un élément paragraphe
 */
function creerParagraphe(paragId, paragClass){
	ligne = document.createElement('p');
	if (paragId && paragId!=''){
		idLigne = document.createAttribute('id');
		idLigne.nodeValue = paragId;
		ligne.setAttributeNode(idLigne);
	}
	if (paragClass && paragClass!=''){
		classLigne = document.createAttribute('class');
		classLigne.nodeValue = paragClass;
		ligne.setAttributeNode(classLigne);
	}
	
	return ligne;
}

/**
 * Crée un label
 */
function creerLabel(labelFor, labelText, required, labelClass){
	label = document.createElement('label');
	forLabel = document.createAttribute('for');
	forLabel.nodeValue = labelFor;
	textLabel = document.createTextNode(labelText);
	if (labelClass){
		classLabel = document.createAttribute('class');
		classLabel.nodeValue = labelClass
		label.setAttributeNode(classLabel);
	}
	label.setAttributeNode(forLabel);
	label.appendChild(textLabel);
	
	if (required){	
		span = document.createElement('span');
		classSpan = document.createAttribute('class');
		classSpan.nodeValue = 'rouge';
		textSpan = document.createTextNode(' *');
		span.setAttributeNode(classSpan);
		span.appendChild(textSpan);
		
		space = document.createTextNode(' ');
		
		label.appendChild(span);
		label.appendChild(space);
	}
	
	return label;
}

/**
 * Crée un hr
 */
function creerSeparationHr(hrId, hrClass){
	hr = document.createElement('hr');
	idHr = document.createAttribute('id');
	idHr.nodeValue = hrId;
	hr.setAttributeNode(idHr);
	if (hrClass){
		classHr = document.createAttribute('class');
		classHr.nodeValue = hrClass;
		hr.setAttribute(classHr);	
	}
	
	return hr;
}

/**
 * Crée un fieldset
 */
function creerFieldset(fsId, fsClass){
	fs = document.createElement('fieldset');
	idFs = document.createAttribute('id');
	idFs.nodeValue=fsId;
	fs.setAttributeNode(idFs);
	if (fsClass){
		classFs = document.createAttribute('id');
		classFs.nodeValue = fsClass;
		fs.setAttributeNode(classFs);
	}
	return fs;
}

/**
 * Cree une légende
 */
function creerLegend(legendText, legendClass, legendId){
	legend = document.createElement('legend');
	if (legendId){
		idLegend = document.createAttribute('id');
		idLegend.nodeValue=legendId;
		legend.setAttributeNode(idLegend);
	}
	if (legendClass){
		classLegend = document.createAttribute('id');
		classLegend.nodeValue = legendClass;
		legend.setAttributeNode(classLegend);
	}
	txtLegend = document.createTextNode(legendText);
	legend.appendChild(txtLegend);
	
	return legend;
}

/*###############################################
	FONCTIONS DE CREATION DE LIGNES DE FORMULAIRE
#################################################*/

/**
 *
 */
function creerLigneInput(ligneId, inputId, inputName, labelText, inputType, inputValue, required, inputClass ){
	ligne = creerParagraphe(ligneId);
	label = creerLabel(inputId, labelText, required);
	input = creerInput(inputId, inputName, inputType, inputValue, inputClass);
	
	ligne.appendChild(label);
	ligne.appendChild(input);
	
	return ligne;
}

function creerLigneSelect(ligneId, selectId, selectName, selectClass, firstOptionValue, firstOptionLabel, list, labelEqualsValue, labelText, required, onchange){
	ligne = creerParagraphe(ligneId);
	label = creerLabel(selectId, labelText, required);
	select = creerSelect(selectId, selectName, selectClass, firstOptionValue, firstOptionLabel, list, labelEqualsValue, onchange);
	
	ligne.appendChild(label);
	ligne.appendChild(select);
	
	return ligne;
	
}

function creerLigneDateMmAaaa(ligneId, selectMoisId, selectMoisName, selectAnneeId, selectAnneeName, labelText, required, futur){
	ligne = creerParagraphe(ligneId);
	label = creerLabel(selectMoisId, labelText, required);
	selectMois = creerSelect(selectMoisId, selectMoisName, 'select-mmaa-mm', '', 'Mois', listeMois, true);
	if (futur){
		liste = listeAnneeFutur;
	} else {
		liste = listeAnnee;
	}
	selectAnnee = creerSelect(selectAnneeId, selectAnneeName, 'select-mmaa-aa', '', 'Annee', liste, true);
	
	ligne.appendChild(label);
	ligne.appendChild(selectMois);
	ligne.appendChild(selectAnnee);
	
	return ligne;
}

/*###############################################
	FONCTIONS D'AFFICHAGE/MASQUAGE SPECIFIQUE
#################################################*/


/**
 * Fonction qui ajoute un champ de saisie de revenu sur la page budget
 */
function ajouterChampRevenu(listTypes, nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbRevenusAffiches'].value;
	if (nb<3){
		nb++;
		//on récupère l'élément "père" auquel on veut ajouter une ligne
		blocRevenus = document.getElementById('blocRevenus');
		//on crée le paragraphe et ses éléments
		ligneType = creerLigneSelect('typeRevenu'+nb, 'natureAutreRevenu'+nb, 'natureAutreRevenu'+nb, classSelect, '', 'Choisissez un type de revenu', listTypes, false, 'Type de revenu', true);
		ligneMontant = creerLigneInput('montantRevenu'+nb, 'montantAutreRevenu'+nb, 'montantAutreRevenu'+nb, 'Montant net', 'text', '0', true );
		postHr = creerSeparationHr('hrRevenu'+nb);

		//on met à jour la ligne contenant le bouton de suppression
		ligneAjout = document.getElementById('ajoutRevenu');
		ligneSuppr = document.getElementById('supprRevenu');
		if (ligneSuppr){
			blocRevenus.removeChild(ligneSuppr);
		}
		ligneSuppr = creerBoutonSuppressionRevenu(nomListeTypes, classSelect, classInput, nb)
		blocRevenus.insertBefore(ligneSuppr, ligneAjout);
		
		//on place les éléments créés dans l'arbre de la page
		ligneSuppr = document.getElementById('supprRevenu');
		blocRevenus.insertBefore(postHr, ligneSuppr);
		blocRevenus.insertBefore(ligneMontant, postHr);
		blocRevenus.insertBefore(ligneType, ligneMontant);
		if (nb==1){
			preHr = creerSeparationHr('hrRevenu'+(nb-1));
			blocRevenus.insertBefore(preHr, ligneType);
		}
		
		//on supprime le bouton d'ajout si il y a 3 credits affichés
		if(nb==3){
			blocRevenus.removeChild(ligneAjout);
		}
		
		//on incrémente le nombre de crédit
		document.forms['ouverture'].elements['nbRevenusAffiches'].value=nb;
	}
}

/**
 * Fonction qui ajoute le bouton d'ajout de revenu sur la page budget
 */
function ajouterBoutonAjoutRevenu(nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbRevenusAffiches'].value;
	if (nb<3){
		ligne = creerParagraphe('ajoutRevenu','text-aligner-input');
		
		image = creerImage('ajouterRevenu', '../../../ressources/commun/img/boutons/bouton-vert-clair-ajouter-s.png', '190', '19', 'Ajouter une source de revenu');
		lien = creerLien("javascript:ajouterChampRevenu("+nomListeTypes+",'"+nomListeTypes+"','"+classSelect+"','"+classInput+"');");
		lien.appendChild(image);
		
		ligne.appendChild(lien);
		
		blocRevenus = document.getElementById('blocRevenus');
		ligneBas = blocRevenus.lastChild;
		while (ligneBas.nodeType!=1){
			ligneBas = ligneBas.previousSibling;
		}
		
		blocRevenus.insertBefore(ligne, ligneBas);
	}
}

/**
 * Fonction qui crée le bouton d'ajout de revenu sur la page budget
 */
function creerBoutonSuppressionRevenu(nomListeTypes, classSelect, classInput, nb){
	ligne = creerParagraphe('supprRevenu','text-aligner-input');
	
	image = creerImage('supprimerRevenu'+nb, '../../../ressources/commun/img/boutons/bouton-vert-clair-sup-s.png', '190', '19', 'Supprimer un crédit');
	lien = creerLien("javascript:supprimerChampRevenu('"+nomListeTypes+"', '"+classSelect+"', '"+classInput+"');");
	lien.appendChild(image);
	ligne.appendChild(lien);
	
	return ligne;
}

/**
 *
 */
function ajouterBoutonSuppressionRevenu(nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbRevenusAffiches'].value;
	if (nb>0){		
		ligneBtSuppr = creerBoutonSuppressionRevenu(nomListeTypes, classSelect, nb);
				
		blocRevenus = document.getElementById('blocRevenus');
		if (nb<3){
			ligneBtAjout = document.getElementById('ajoutRevenu');
			blocRevenus.insertBefore(ligneBtSuppr, ligneBtAjout);
		} else {
			ligneBas = blocRevenus.lastChild;
			while (ligneBas.nodeType!=1){
				ligneBas = ligneBas.previousSibling;
			}
			blocRevenus.insertBefore(ligneBtSuppr, ligneBas);
		}
	}
}

/**
 * Fonction qui supprime un champ de saisie de revenu sur la page budget
 */
function supprimerChampRevenu(nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbRevenusAffiches'].value;
	blocRevenus = document.getElementById('blocRevenus');
	
	//on supprime les deux lignes correspondant au revenu
	ligneASuppr = document.getElementById('typeRevenu'+nb);
	ligneASuppr2 = document.getElementById('montantRevenu'+nb);
	postHr = document.getElementById('hrRevenu'+nb);
	blocRevenus.removeChild(postHr);
	blocRevenus.removeChild(ligneASuppr);
	blocRevenus.removeChild(ligneASuppr2);
	if (nb==1){
		preHr = document.getElementById('hrRevenu'+(nb-1));
		blocRevenus.removeChild(preHr);
	}
	
	//on décrémente le nombre de revenus
	document.forms['ouverture'].elements['nbRevenusAffiches'].value=--nb;
	
	//on supprime la ligne du bouton supprimer
	ligneBtSuppr = document.getElementById('supprRevenu');
	if (ligneBtSuppr){
		blocRevenus.removeChild(ligneBtSuppr);
	}
	
	//si le nouveau nombre est à 2, on ajoute le bouton d'ajout
	if (nb==2){
		ajouterBoutonAjoutRevenu(nomListeTypes, classSelect, classInput);
	}
	
	//si il y a au moins un revenu affiché, on réécrit le bouton de suppression
	if (document.forms['ouverture'].elements['nbRevenusAffiches'].value >0){
		ligneBtSuppr = creerBoutonSuppressionRevenu(nomListeTypes, classSelect, classInput, document.forms['ouverture'].elements['nbRevenusAffiches'].value);
		ligneBtAjout = document.getElementById('ajoutRevenu');
		blocRevenus.insertBefore(ligneBtSuppr, ligneBtAjout);
	}
}

/**
 * Fonction qui ajoute un champ de saisie de credit sur la page budget
 */
function ajouterChampCredit(listTypes, nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbCreditsAffiches'].value;
	if (nb<3){
		++nb;
		//on récupère l'élément "père" auquel on veut ajouter une ligne
		blocCharges = document.getElementById('blocCharges');
		
		//on crée le paragraphe et ses éléments
		postHr = creerSeparationHr('hrCredit'+nb);
		ligneType = creerLigneSelect('typeCredit'+nb, 'natureAutreCredit'+nb, 'natureAutreCredit'+nb, classSelect, '', 'Choisissez un type de crédit', listTypes, false, 'Type de crédit', true);
		ligneMontant = creerLigneInput('montantCredit'+nb, 'montantAutreCredit'+nb, 'montantAutreCredit'+nb, 'Montant net', 'text', '0', true );
		ligneDate = creerLigneDateMmAaaa('dateCredit'+nb, 'moisDateAutreCredit'+nb, 'moisDateAutreCredit'+nb, 'anneeDateAutreCredit'+nb, 'anneeDateAutreCredit'+nb, 'Jusque', true, true);
		
		//on met à jour la ligne de suppression de credit
		ligneAjout = document.getElementById('ajoutCredit');
		ligneSuppr = document.getElementById('supprCredit');
		if (ligneSuppr){
			blocCharges.removeChild(ligneSuppr);
		}
		ligneSuppr = creerBoutonSuppressionCredit(nomListeTypes, classSelect, classInput, nb)
		blocCharges.insertBefore(ligneSuppr, ligneAjout);
		
		//on place les éléments dans l'arbre
		ligneSuppr = document.getElementById('supprCredit');
		blocCharges.insertBefore(postHr, ligneSuppr);
		blocCharges.insertBefore(ligneDate, postHr);
		blocCharges.insertBefore(ligneMontant, ligneDate);
		blocCharges.insertBefore(ligneType, ligneMontant);
		if (nb==1){
			preHr = creerSeparationHr('hrCredit'+(nb-1));
			blocCharges.insertBefore(preHr, ligneType);
		}
		
		if(nb==3){
			blocCharges.removeChild(ligneAjout);
		}
		
		document.forms['ouverture'].elements['nbCreditsAffiches'].value=nb;
	}
}

/**
 * Fonction qui ajoute le bouton d'ajout de credit sur la page budget
 */
function ajouterBoutonAjoutCredit(nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbCreditsAffiches'].value;
	if (nb<3){
		ligne = creerParagraphe('ajoutCredit','text-aligner-input');
		
		
		image = creerImage('ajouterCredit', '../../../ressources/commun/img/boutons/bouton-vert-clair-ajouter-c.png', '130', '19', 'Ajouter un crédit');
		lien = creerLien("javascript:ajouterChampCredit("+nomListeTypes+",'"+nomListeTypes+"','"+classSelect+"','"+classInput+"');");
		lien.appendChild(image);
		
		ligne.appendChild(lien);
		ligneBas = document.getElementById('blocCharges').lastChild;
		while (ligneBas.nodeType!=1){
			ligneBas = ligneBas.previousSibling;
		}
		document.getElementById('blocCharges').insertBefore(ligne, ligneBas);
	}
}

/**
 * Fonction qui crée le bouton d'ajout de credit sur la page budget
 */
function creerBoutonSuppressionCredit(nomListeTypes, classSelect, classInput, nb){
	ligne = creerParagraphe('supprCredit','text-aligner-input');
	
	image = creerImage('supprimerCredit'+nb, '../../../ressources/commun/img/boutons/bouton-vert-clair-sup-c.png', '130', '19', 'Supprimer un crédit');
	lien = creerLien("javascript:supprimerChampCredit('"+nomListeTypes+"', '"+classSelect+"', '"+classInput+"');");
	lien.appendChild(image);
	ligne.appendChild(lien);
	
	return ligne;
}

/**
 *
 */
function ajouterBoutonSuppressionCredit(nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbCreditsAffiches'].value;
	if (nb>0){		
		ligneBtSuppr = creerBoutonSuppressionCredit(nomListeTypes, classSelect, classInput, nb);
				
		blocCharges = document.getElementById('blocCharges');
		if (nb<3){
			ligneBtAjout = document.getElementById('ajoutCredit');
			blocCharges.insertBefore(ligneBtSuppr, ligneBtAjout);
		} else {
			ligneBas = blocCharges.lastChild;
			while (ligneBas.nodeType!=1){
				ligneBas = ligneBas.previousSibling;
			}
			blocCharges.insertBefore(ligneBtSuppr, ligneBas);
		}
	}
}

/**
 * Fonction qui supprime un champ de saisie de credit sur la page budget
 */
function supprimerChampCredit(nomListeTypes, classSelect, classInput){
	var nb = document.forms['ouverture'].elements['nbCreditsAffiches'].value;
	blocCharges = document.getElementById('blocCharges');
	
	//on supprime les lignes
	postHr = document.getElementById('hrCredit'+nb);
	ligneASuppr = document.getElementById('typeCredit'+nb);
	ligneASuppr2 = document.getElementById('montantCredit'+nb);
	ligneASuppr3 = document.getElementById('dateCredit'+nb);
	blocCharges.removeChild(postHr);
	blocCharges.removeChild(ligneASuppr);
	blocCharges.removeChild(ligneASuppr2);
	blocCharges.removeChild(ligneASuppr3);
	if (nb==1){
		preHr = document.getElementById('hrCredit'+(nb-1));
		blocCharges.removeChild(preHr);
	}
	
	//on décrémente
	document.forms['ouverture'].elements['nbCreditsAffiches'].value=--nb;
	
	//on supprime la ligne du bouton supprimer
	ligneBtSuppr = document.getElementById('supprCredit');
	if (ligneBtSuppr){
		blocCharges.removeChild(ligneBtSuppr);
	}
	
	//si le nouveau nombre est à 2, on ajoute le bouton d'ajout
	if (nb==2){
		ajouterBoutonAjoutCredit(nomListeTypes, classSelect, classInput);
	}
	
	//si il y a au moins un credit affiché, on réécrit le bouton de suppression
	if (document.forms['ouverture'].elements['nbCreditsAffiches'].value >0){
		ligneBtSuppr = creerBoutonSuppressionCredit(nomListeTypes, classSelect, classInput, document.forms['ouverture'].elements['nbCreditsAffiches'].value);
		ligneBtAjout = document.getElementById('ajoutCredit');
		blocCharges.insertBefore(ligneBtSuppr, ligneBtAjout);
	}
}

/**
 * Permer de supprimer le champ de saisie du nom de jeune fille
 */
function supprimerNomJf(prsNo){
	var blocEtatCivil;
	var ligneNomJf;
	if (prsNo == 1){
		blocEtatCivil = document.getElementById('blocEtatCivil')
		ligneNomJf = document.getElementById('ligneNomJfTitulaire');
	} else {
		blocEtatCivil = document.getElementById('blocCotitulaire')
		ligneNomJf = document.getElementById('ligneNomJfCotitulaire');
	}
	if (ligneNomJf){
		blocEtatCivil.removeChild(ligneNomJf);
	}
}

/**
 * Permet d'ajouter le champ de saisie du nom de jaune fille
 */
function ajouterNomJf(prsNo, nomEtIdChamp){
	var blocEtatCivil;
	var ligneNomJf;
	var lignePrenom;
	if (prsNo == 1){
		blocEtatCivil = document.getElementById('blocEtatCivil')
		ligneNomJf = document.getElementById('ligneNomJfTitulaire');
		lignePrenom = document.getElementById('lignePrenomTitulaire');
	} else {
		blocEtatCivil = document.getElementById('blocCotitulaire')
		ligneNomJf = document.getElementById('ligneNomJfCotitulaire');
		lignePrenom = document.getElementById('lignePrenomCotitulaire');
	}

	if (!ligneNomJf){
		if (prsNo == 1){
			ligneNomJfId = 'ligneNomJfTitulaire';
		} else {
			ligneNomJfId = 'ligneNomJfCotitulaire';
		}
		ligneNomJf = creerLigneInput(ligneNomJfId, nomEtIdChamp, nomEtIdChamp, 'Nom de jeune fille', 'text', '', true );
		
		blocEtatCivil.insertBefore(ligneNomJf,lignePrenom.nextSibling);
	}
	
}

/**
 * Supprime les champs de saisie de la date de situation familiale
 */
function supprimerDateSituationFamiliale(){
	var blocSituationFamiliale = document.getElementById('blocSituationFamiliale')
	var ligneDateSituationFamiliale = document.getElementById('ligneDateSituationFamiliale');
	
	if (ligneDateSituationFamiliale){
		blocSituationFamiliale.removeChild(ligneDateSituationFamiliale);
	}
}

/**
 * Ajoute les champs de saisie de la date de situation familiale
 */
function ajouterDateSituationFamiliale(nomEtIdChampMois, nomEtIdChampAnnee){
	var blocSituationFamiliale = document.getElementById('blocSituationFamiliale')
	var ligneDSF = document.getElementById('ligneDateSituationFamiliale');
	var ligneNbEnfants = document.getElementById('ligneNbEnfants');
	
	if (!ligneDSF){
		ligneDSF = creerLigneDateMmAaaa('ligneDateSituationFamiliale', nomEtIdChampMois, nomEtIdChampMois, nomEtIdChampAnnee, nomEtIdChampAnnee, 'Depuis', true);
	
		blocSituationFamiliale.insertBefore(ligneDSF,ligneNbEnfants);
	}
}

/**
 * Gere l'affichage des champs de saisie de la date de situation familiale
 */
function gererAffichageSituationFamiliale(nomEtIdChampMois, nomEtIdChampAnnee){
	if (document.forms['ouverture'].elements['situationFamiliale'].value=='1'){
		supprimerDateSituationFamiliale();
	} else {
		ajouterDateSituationFamiliale(nomEtIdChampMois, nomEtIdChampAnnee);
	}
}

/**
 *
 */
function gererAffichageBlocCoEmp(){
	var hasCoEmp = false;
	document.forms['ouverture'].elements['ignoreValidation'].value = 1; 
	document.forms['ouverture'].submit();	
}

/**
 *
 */
function choixProfession(formulaire, champProfession, champSecteur, champTypeContrat, prsNo) {
  
  if (document.forms[formulaire].elements[champProfession].selectedIndex < 0) return;
  if (p[document.forms[formulaire].elements[champTypeContrat].selectedIndex]) {
	  liste = (p[document.forms[formulaire].elements[champTypeContrat].selectedIndex])[2];
	  if (document.forms[formulaire].elements[champProfession].selectedIndex > 0
			  	&& liste[document.forms[formulaire].elements[champProfession].selectedIndex-1]) {
	    	liste = (liste[document.forms[formulaire].elements[champProfession].selectedIndex-1])[2];
	  } else {
    		liste = new Array(0);
	  }
  } else {
	  liste = new Array(0);
  }

  // supprime le contenu de la liste des secteurs
  for(i=0; i<document.forms[formulaire].elements[champSecteur].options.length;) {
    document.forms[formulaire].elements[champSecteur].options[i] = null;
  }
  document.forms[formulaire].elements[champSecteur].options[document.forms[formulaire].elements[champSecteur].options.length] = new Option("Choisissez", "");
      
  // ajoute les nouveaux secteurs
  for(i=0; i<liste.length; i++) {
    document.forms[formulaire].elements[champSecteur].options[document.forms[formulaire].elements[champSecteur].options.length] = new Option(liste[i][1], liste[i][0]);
  }
}

/**
 *
 */
function choixTypeContrat(formulaire, champProfession, champSecteur, champTypeContrat, prsNo) {
  
  coherenceChamps(formulaire, champTypeContrat, prsNo);

  if (document.forms[formulaire].elements[champTypeContrat].selectedIndex < 0) return;
  if (document.forms[formulaire].elements[champTypeContrat].selectedIndex > 0) {
  	if (p[document.forms[formulaire].elements[champTypeContrat].selectedIndex]) {
	    liste = (p[document.forms[formulaire].elements[champTypeContrat].selectedIndex])[2];
	} else {
		liste = new Array(0);
	}
  } else {
    liste = new Array(0);
  }

  if (document.forms[formulaire].elements[champProfession]){
	  // supprime le contenu de la liste des professions
	  for(i=0; i<document.forms[formulaire].elements[champProfession].options.length;) {
	    document.forms[formulaire].elements[champProfession].options[i] = null;
	  }
	  document.forms[formulaire].elements[champProfession].options[document.forms[formulaire].elements[champProfession].options.length] = new Option("Choisissez", "");
  }

  if (document.forms[formulaire].elements[champSecteur]){
	  // supprime le contenu de la liste des secteurs
	  for(i=0; i<document.forms[formulaire].elements[champSecteur].options.length;) {
	    document.forms[formulaire].elements[champSecteur].options[i] = null;
	  }
	  document.forms[formulaire].elements[champSecteur].options[document.forms[formulaire].elements[champSecteur].options.length] = new Option("Choisissez", "");
  }
  
  if (document.forms[formulaire].elements[champProfession]){
	  // ajoute les nouvelles professions
	  for(i=0; i<liste.length; i++) {
	    document.forms[formulaire].elements[champProfession].options[document.forms[formulaire].elements[champProfession].options.length] = new Option(liste[i][1], liste[i][0]);
	  }
  }

}

function masquerProfession(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById('sousBlocProf'+prs);
	ligne = document.getElementById('ligneProfession'+prs);
	if (ligne && bloc){
		bloc.removeChild(ligne);
	}
}

function afficherProfession(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById('sousBlocProf'+prs);
	liste = new Array(0);
	ligne = document.getElementById('ligneProfession'+prs);
	if (!ligne){
		onchange = "choixProfession('ouverture', 'profession"+prs+"\", 'secteurActivite"+prs+"', 'typeContrat"+prs+"', "+prsNo+");";
		ligne = creerLigneSelect('ligneProfession'+prs, 'profession'+prs, 'profession'+prs, null, '', 'Choisissez', liste, false, 'Statut professionnel', true, onchange);
		ligneTC = document.getElementById('ligneContrat'+prs);
		next = ligneTC.nextSibling;
		bloc.insertBefore(ligne, next);
	}	
}

function masquerSecteur(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById('sousBlocProf'+prs);
	ligne = document.getElementById('ligneSecteur'+prs);
	if (ligne && bloc){
		bloc.removeChild(ligne);
	}
}

function afficherSecteur(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById('sousBlocProf'+prs);
	liste = new Array(0);
	ligne = document.getElementById('ligneSecteur'+prs);
	if (!ligne){
		ligne = creerLigneSelect('ligneSecteur'+prs, 'secteurActivite'+prs, 'secteurActivite'+prs, null, '', 'Choisissez', liste, false, "Secteur d'activité", true);
		lignedebut = document.getElementById('ligneDateDebut'+prs);
		bloc.insertBefore(ligne, lignedebut);
	}
}

function masquerDateFin(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById('sousBlocProf'+prs);
	ligne = document.getElementById('ligneDateFin'+prs);
	if (ligne && bloc){
		bloc.removeChild(ligne);
	}
}

function afficherDateFin(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById('sousBlocProf'+prs);
	var ligneDF = document.getElementById('ligneDateFin'+prs);
	
	if (!ligneDF){
		ligneDF = creerLigneDateMmAaaa('ligneDateFin'+prs, 'moisFinProfessionnel'+prs, 'moisFinProfessionnel'+prs, 'anneeFinProfessionnel'+prs, 'anneeFinProfessionnel'+prs, 'Jusque', true, true);
	
		lignebas = bloc.lastChild;
		while (lignebas.nodeType !=1){
			lignebas = lignebas.previousSibling;
		}
		bloc.insertBefore(ligneDF, lignebas);
	}
}

function masquerEmployeur(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	nomZone = (prsNo == 1 ? 'blocProf'+prs:'blocCoEmp');
	zone = document.getElementById(nomZone);
	bloc = document.getElementById('blocEmployeur'+prs);
	if (zone && bloc){
		zone.removeChild(bloc);
	}
}

function afficherEmployeur(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	nomZone = (prsNo == 1 ? 'blocProf'+prs:'blocCoEmp');
	zone = document.getElementById(nomZone);
	bloc = document.getElementById('blocEmployeur'+prs);
	if (!bloc){
		bloc = creerFieldset('blocEmployeur'+prs);
		legend = creerLegend("Coordonnées de l'employeur");
		ligneNom = creerLigneInput('', 'raisonSocialeEmployeur'+prs, 'raisonSocialeEmployeur'+prs, 'Nom', 'text', '', false, 'zone-de-texte' );
		ligneTel = creerLigneInput('', 'telephoneEmployeur'+prs, 'telephoneEmployeur'+prs, 'Téléphone', 'text', '', false, 'zone-de-texte' );
		ligneDpt = creerLigneSelect('', 'departementEmployeur'+prs, 'departementEmployeur'+prs, '', '', 'Précisez le département', listeDpts, false, 'Département', false);
		ligneVille = creerLigneInput('', 'communeEmployeur'+prs, 'communeEmployeur'+prs, 'Ville', 'text', '', false, 'zone-de-texte' );
		
		bloc.appendChild(legend);
		bloc.appendChild(ligneNom);
		bloc.appendChild(ligneTel);
		bloc.appendChild(ligneDpt);
		bloc.appendChild(ligneVille);
		
		zone.appendChild(bloc);
	}
	
}

function afficherDepartementDeNaissance(prsNo, listeDepts){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById((prsNo == 1 ? 'blocEtatCivil':'blocCotitulaire'));
	var ligneDptN = document.getElementById('ligneDeptNais'+prs);
	
	if (!ligneDptN){
		ligneDptN = creerLigneSelect('ligneDeptNais'+prs, 'departementNaissance'+prs, 'departementNaissance'+prs, null, '', "Précisez le département", listeDepts, false, 'Département de naissance', true);
	
		ligneVN = document.getElementById('ligneVilleNaiss'+prs);
		
		bloc.insertBefore(ligneDptN, ligneVN);
	}
	
}

function masquerDepartementDeNaissance(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	bloc = document.getElementById((prsNo == 1 ? 'blocEtatCivil':'blocCotitulaire'));
	var ligneDptN = document.getElementById('ligneDeptNais'+prs);
	
	if (ligneDptN){
		bloc.removeChild(ligneDptN);
	}
	
}

function gererAffichageDepartementNaisance(prsNo){
	prs = prsNo == 1 ? 'Titulaire':'Cotitulaire';
	nomChamp = 'paysNaissance'+prs;
	if (document.forms['ouverture'].elements[nomChamp].value=='FR'){
		afficherDepartementDeNaissance(prsNo, listeDepartements);
	} else {
		masquerDepartementDeNaissance(prsNo);
	}
}
	

/*****************************************
fonctions autres
******************************************/
var hrefJustifs;
function afficherListeJustifs(lienId, width, height){

	//alert('ouvrirPopup');
	lien = document.getElementById(lienId);
	href = lien.getAttributeNode('href');
	valHref = href.nodeValue;
	if (valHref != 'javascript:void(0)'){
		hrefJustifs = valHref;
	} else {
		valHref = hrefJustifs;
	}
	lien.removeAttributeNode(href);
	
	href = document.createAttribute('href');
	href.nodeValue='javascript:void(0)';
	lien.setAttributeNode(href);
	
	target = lien.getAttributeNode('target');
	if (target){
		lien.removeAttributeNode(target);
	}
	
	window.open(valHref,'justifs', 'menubar=no,toolbar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);
	
}