function submitForm(formName, actionName) {
    top.document.forms[formName].action = actionName;
    top.document.forms[formName].submit();
}

function changeClassRib() {

		var banque = document.getElementById('Banque');
		var agence = document.getElementById('Agence');
		var compte = document.getElementById('Compte');
		var cleRib = document.getElementById('cleRIB');

		var classBanque = banque.className;
		var classAgence = agence.className;
		var classCompte = compte.className;
		var classCleRib = cleRib.className;
		
		var index = classBanque.lastIndexOf('INPUTOBLIGATOIRE');

	if( ORBEON.xforms.Document.getValue("ribTotal") == '0' ) {
		//pas de rib de saisie : class INPUT
		if(  index != -1 ) {
			//ils sont en INPUTOBLIGATOIRE
			classBanque = classBanque.replace('INPUTOBLIGATOIRE', 'INPUT');
			classAgence = classAgence.replace('INPUTOBLIGATOIRE', 'INPUT');
			classCompte = classCompte.replace('INPUTOBLIGATOIRE', 'INPUT');
			classCleRib = classCleRib.replace('INPUTOBLIGATOIRE', 'INPUT');
			
			banque.className = classBanque;
			agence.className = classAgence;
			compte.className = classCompte;
			cleRib.className = classCleRib;			
		} 
	} else {
		//un rib est saisie : class INPUTOBLIGATOIRE
		if( index == -1 ) {
			//ils sont en INPUT
			classBanque = classBanque.replace('INPUT', 'INPUTOBLIGATOIRE');
			classAgence = classAgence.replace('INPUT', 'INPUTOBLIGATOIRE');
			classCompte = classCompte.replace('INPUT', 'INPUTOBLIGATOIRE');
			classCleRib = classCleRib.replace('INPUT', 'INPUTOBLIGATOIRE');
			
			banque.className = classBanque;
			agence.className = classAgence;
			compte.className = classCompte;
			cleRib.className = classCleRib;		
		}
	}
} 


function masqAffichElem(elem, val) {

	var oBalise = document.getElementById(elem);
    	
	oBalise.style.display = val;
}


function hideShow(obj) {
    if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf["MSIE 5"] != -1)
    {
    	var oBalise = document.getElementById(obj);
    	
        if (oBalise.style.display == "none")
             oBalise.style.display = "";
        else
             oBalise.style.display = "none";
    }
}


function rendreObligatoireRIB(select, champ) {

	var valeur = select.options[select.selectedIndex].value;
	if( valeur != "pas_rib" && valeur != "" && valeur != "null" ) {
		champ.className ="FORMINPUTOBLIGATOIRE";
		
	} else {
		champ.className ="FORMINPUT";
		champ.value="";
		
		
	}
}

function checkFormatRIBTiers(idchamp) {

	var champ = document.getElementById(idchamp);
	var select = document.getElementById('typeRIB');
	
	var valeur = select.options[select.selectedIndex].value;

	if( valeur == "RIB" ) {
		//format rib fr
		formats(champ, "Information du RIB", "99999 99999 XXXXXXXXXXX 99");
		return checkFormat(champ);
	} 

	return true;
}

function completeZeroGauche(champ, taille) {

	if( champ.value != '' ) {
		var iTailleChamp = (""+ champ.value).length;
		var sComble = "";
		
		for( var j = iTailleChamp; j<taille; j++) {
			sComble += "0";
		}
	
		champ.value = sComble + champ.value;
	}
}

function rendreObligCorres(corres) {

	if( corres == "president" ) {
		if( document.getElementById('nomPres').value != "" ) {
			document.getElementById('prenomPres').className ="FORMINPUTOBLIGATOIRE";
			document.getElementById('codePostPres').className ="FORMINPUTOBLIGATOIRE";
			document.getElementById('villePres').className ="FORMINPUTOBLIGATOIRE";
		} else {
			document.getElementById('prenomPres').className ="FORMINPUT";
			document.getElementById('codePostPres').className ="FORMINPUT";
			document.getElementById('villePres').className ="FORMINPUT";
		}
	} else {
		if( corres == "secretaire" ) {
			if( document.getElementById('nomSecr').value != "" ) {
				document.getElementById('prenomSecr').className ="FORMINPUTOBLIGATOIRE";
				document.getElementById('codePostSecr').className ="FORMINPUTOBLIGATOIRE";
				document.getElementById('villeSecr').className ="FORMINPUTOBLIGATOIRE";
			} else {
				document.getElementById('prenomSecr').className ="FORMINPUT";
				document.getElementById('codePostSecr').className ="FORMINPUT";
				document.getElementById('villeSecr').className ="FORMINPUT";
			}
		} else {
			if( corres == "tresorier" ) {
				if( document.getElementById('nomTres').value != "" ) {
					document.getElementById('prenomTres').className ="FORMINPUTOBLIGATOIRE";
					document.getElementById('codePostTres').className ="FORMINPUTOBLIGATOIRE";
					document.getElementById('villeTres').className ="FORMINPUTOBLIGATOIRE";
				} else {
					document.getElementById('prenomTres').className ="FORMINPUT";
					document.getElementById('codePostTres').className ="FORMINPUT";
					document.getElementById('villeTres').className ="FORMINPUT";
				}
			}
		}
	}		
}


function clickStatJur() {

	if (document.getElementById("autreStatutJur").checked == true) {
		//on affiche le champ préciser
		masqAffichElem('precisStatJur', '');
	} else {
		//on masque le champ préciser
		masqAffichElem('precisStatJur', 'none');
		//et on le vide
		document.getElementById("precisStatJur").value="";
	}
	
	if (document.getElementById("statAsso").checked == true) {
		//on met le type de tiers à PMPRIV
		document.getElementById("typeTiers").value = "2";
	} else {
		//on met le type de tiers à PMPUB
		document.getElementById("typeTiers").value = "3";
	}

}

function clickPubMajor() {

	if (document.getElementById("pubAutre").checked == true) {
		//on affiche le champ préciser
		masqAffichElem('autrePub', '');
	} else {
		//on masque le champ préciser
		masqAffichElem('autrePub', 'none');
		//et on le vide
		document.getElementById("autrePub").value="";
	}

}

function clickPubTot() {

	if (document.getElementById("pubTot100").checked == true) {
		//on affiche le champ environ
		masqAffichElem('envPlus100', '');
		document.getElementById("boolPlus100").value="true";
	} else {
		//on masque le champ environ
		masqAffichElem('envPlus100', 'none');
		//et on le vide
		document.getElementById("envPlus100").value="";
		document.getElementById("boolPlus100").value="false";
	}

}


function initNomDispositif(nomDispo) {

	var nomDispositif = document.getElementById("nomDispositif").value;
	
	if( nomDispositif != ("Formulaire " + nomDispo)) {
		document.getElementById("nomDispositif").value="Formulaire " + nomDispo;
		document.getElementById("aChargerDem").value="1";
	}
}

/*
Calcul des totaux pour l'association d'enseignement de la musique
*/
function calculTotal() {
	
	var totDir = document.getElementById("totalDir");
	var totEns = document.getElementById("totalEnsei");
	var totSal = document.getElementById("totalSal");
	var totCharg = document.getElementById("totalCharg");
	var totTotal = document.getElementById("totalTotal");
		
	totDir.value = mettreMontantAuFormatFinancier(arrondirSansBugJS(mettreMontantAuFormatCalcul(document.getElementById("salDire").value) + mettreMontantAuFormatCalcul(document.getElementById("chargDir").value)));
	totEns.value = mettreMontantAuFormatFinancier(arrondirSansBugJS(mettreMontantAuFormatCalcul(document.getElementById("salEnsei").value) + mettreMontantAuFormatCalcul(document.getElementById("chargEnsei").value)));
	totSal.value = mettreMontantAuFormatFinancier(arrondirSansBugJS(mettreMontantAuFormatCalcul(document.getElementById("salEnsei").value) + mettreMontantAuFormatCalcul(document.getElementById("salDire").value)));
	totCharg.value = mettreMontantAuFormatFinancier(arrondirSansBugJS(mettreMontantAuFormatCalcul(document.getElementById("chargDir").value) + mettreMontantAuFormatCalcul(document.getElementById("chargEnsei").value)));
	totTotal.value = mettreMontantAuFormatFinancier(arrondirSansBugJS(mettreMontantAuFormatCalcul(document.getElementById("totalSal").value) + mettreMontantAuFormatCalcul(document.getElementById("totalCharg").value)));

}

//on remplace les accents de toutes sorte pour les villes
function remplacerAccent(champ) {

	var val = champ.value;
	var codeAscii;
	
	for (var i=0; i<val.length; i++) {

		codeAscii = val.charCodeAt(i);
		switch(codeAscii) {
			case 233:
				val = val.replace(String.fromCharCode(233),"e");
				break;
			
			case 232:
				val = val.replace(String.fromCharCode(232),"e");
				break;

			case 224:
				val = val.replace(String.fromCharCode(224),"a");
				break;
			
			case 231:
				val = val.replace(String.fromCharCode(231),"c");
				break;
			
			case 251:
				val = val.replace(String.fromCharCode(251),"u");
				break;
			
			case 252:
				val = val.replace(String.fromCharCode(252),"u");
				break;
				
			case 238:
				val = val.replace(String.fromCharCode(238),"i");
				break;
			
			case 239:			
				val = val.replace(String.fromCharCode(239),"i");
				break;
			
			default:
				break;
		}
	}
	
	val = val.toUpperCase();
		
	champ.value = val;
}


//pour formater les montants doubles en formatCalcul
function formatMontant(oMt) {

	document.getElementById(oMt).value = mettreMontantAuFormatCalcul(document.getElementById(oMt).value);
	
	//s'il y en a plusieurs
	for (var i=1; i<formatMontant.arguments.length; i++) {
		document.getElementById(formatMontant.arguments[i]).value = mettreMontantAuFormatCalcul(document.getElementById(formatMontant.arguments[i]).value);
    }

}


//---------------------------------------------------------------------------
//enleverEspacesMontant(psMontant)
//
//Méthode permettant d'enlever les espaces d'un financier,
//à utiliser lors de l'entree ds un champ montant modifiable
//---------------------------------------------------------------------------
function enleverEspacesMontant(oChamp) {
	
	alert('enleverEspacesMontant');
	var val = oChamp.value;
	var psMontant = "";
	for (var i=0; i<val.length; i++) {
		if (val.charAt(i) != '\u00A0' && val.charAt(i) != ' ') {
			psMontant += val.charAt(i);
		}
	}	
	alert(psMontant);
	return psMontant;
	//oChamp.value = psMontant;
}

//---------------------------------------------------------------------------
//mettreChampAuFormatFinancier(oChamp)
//
//Méthode permettant de mettre la valeur du champ au format financier
//---------------------------------------------------------------------------
function mettreChampAuFormatFinancier(oChamp) {

	oChamp.value = mettreMontantAuFormatFinancier(oChamp.value);
}

//---------------------------------------------------------------------------
//mettreMontantAuFormatCalcul(psMontant)
//
//Méthode permettant de mettre au format numérique un montant. On remplace 
//	notamment la virgule par un point. Retourne la valeur formatée
//		psMontant : Valeur du champ nom formaté
// argument bonus : si on veut une autre valeur par défaut que la chaîne vide
//---------------------------------------------------------------------------
function mettreMontantAuFormatCalcul(psMontant) {

	//suppression des espaces
	var val = ""+psMontant;
	var psMontant = "";
	if (mettreMontantAuFormatCalcul.arguments[1] != null && val == "") {
		psMontant = "" + mettreMontantAuFormatCalcul.arguments[1];
	}
	for (var i=0; i<val.length; i++) {
		if (val.charAt(i) != '\u00A0' && val.charAt(i) != ' ') {
			psMontant += val.charAt(i);
		}
	}

	//remplacement de la virgule
	if (psMontant.lastIndexOf(',') > -1) {
		// On a une, et une seule virgule => on la remplace par un point
		psMontant = psMontant.replace(",",".");
	}
	//alert("mettreMontantAuFormatCalcul retourné 3/4 : "+ parseFloat(psMontant));
	return Math.round(parseFloat(psMontant) *100)/100;
}

//---------------------------------------------------------------------------
//mettreMontantAuFormatFinancier(psMontant)
//
//Méthode permettant de mettre au format d'affichage un montant tappé par le user.  
//	On remplace notamment le point par une virgule. Retourne la valeur formatée
//		psMontant : Valeur du champ nom formaté
//---------------------------------------------------------------------------
function mettreMontantAuFormatFinancier(psMontantOrig) {
	
	if (psMontantOrig.toString() == 'NaN') {
		return "";
	}
	
	//alert("mettreMontantAuFormatFinancier 1/5 : "+ psMontantOrig);
	var sChaineAvantPoint, sChaineApresPoint;
	var psMontant = mettreMontantAuFormatCalcul(psMontantOrig);
	psMontant = ""+psMontant;
	//alert("mettreMontantAuFormatFinancier mis en calcul 2/5 : "+ psMontant);
	// On ne transforme pas le montant s'il comprend des lettres
	var val = psMontant;
	val = val.replace(/\./, "");
	if (val.charAt(0) == "-" || val.charAt(0) == "+") {
		val = val.slice(1)
	}
	for (var i=0; i<val.length; i++) {
//		alert("on teste le caractère "+val.charAt(i));
		if (!isDigit(val.charAt(i))) {
	//		alert("NaaaaaaN");
			return psMontantOrig;
		}
	}

	var iIndexOfPoint = psMontant.lastIndexOf('.');
	if (iIndexOfPoint != -1) {
		// On a un . on le remplace par une ,
		sChaineAvantPoint = psMontant.substring(0, iIndexOfPoint);
		sChaineApresPoint = psMontant.substring(iIndexOfPoint + 1);
	} else {
		sChaineAvantPoint = psMontant;
		sChaineApresPoint = "00";			
	}
//	alert("index du point 3/5 : "+iIndexOfPoint+" sChaineAvantPoint : "+sChaineAvantPoint+" sChaineApresPoint : "+sChaineApresPoint);
	// Ajout des espaces pr séparer les milliers
	var i = 0;
	var iNbChiffresEnTete = sChaineAvantPoint.length % 3;
	var psMontantEspace = sChaineAvantPoint.substring(i, i+iNbChiffresEnTete);
	i += iNbChiffresEnTete;

	while (sChaineAvantPoint.substring(i, i+3) != "") {
		psMontantEspace += " " + sChaineAvantPoint.substring(i, i+3);
		i += 3;
	}
	if (psMontantEspace == "") psMontantEspace=0;
	if (sChaineApresPoint.length == 1) sChaineApresPoint += "0";
//	alert("mettreMontantAuFormatFinancier psMontantEspace 4/5 : "+ psMontantEspace);
	psMontant = psMontantEspace + "," + sChaineApresPoint;
//	alert("mettreMontantAuFormatFinancier retourné 5/5 : "+ psMontant);	
	if (psMontant.substring(0,1) == " ") {
		psMontant = psMontant.substring(1);
	}
	return psMontant;
}

/*
* Javascript ne sait pas compter !!
* 286.40 * 100 = 28639.99999999996
* Pour palier à ce problème, la méthode d'arrondi simple round(n*100)/100 ne suffit pas.
* La méthode ci-dessous permet de récupérer un nombre au format financier et de valeur correcte.
* Ainsi arrondirSansBugJS(286.40*100) retourne 28640
*/	
function arrondirSansBugJS(montant){
	return Math.round(Math.round(montant*10000)/100)/100;
}

/*------------------------------------------------------------------
Fonction :		isDigit
--------------------------------------------------------------------
Nature :		private
Arguments :		Aucun
Resultat :		Aucun
Description :	Retourne true si le caratère est un chiffre
-------------------------------------------------------------------*/
function isDigit(digit) {
	// Tous les caratères accéptés
	var charOk = "0123456789";
	return !(charOk.indexOf(digit) == -1)
}


/*------------------------------------------------------------------
Fonction :      isAlpha
--------------------------------------------------------------------
Nature :        private
Arguments :     Aucun
Resultat :      Aucun
Description :   Retourne true si le caratère est une lettre
-------------------------------------------------------------------*/
function isAlpha(alpha) {
    // Tous les caratères accéptés
    var charOk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
    return !(charOk.indexOf(alpha) == -1);
}

/*------------------------------------------------------------------
Fonction :      formats
--------------------------------------------------------------------
Nature :        public
Arguments :     form = champ 1
                    + Libellé du champ 1
                    + Format du champ 1
                    + ...
Resultat :      aucun
Description :   Positionne l'attribut format au format spécifié
                pour tous les champs spécifiés
                Le format peut être quelconque, les caractères réservés sont :
                    X : qui signifie "n'importe quel caractère"
                    A : qui signifie "uniquement une lettre"
                    9 : qui signifie "uniquement un chiffre"
                    @ : qui signifie "uniquement une lettre ou un chiffre"
-------------------------------------------------------------------*/
function formats(champ, nom, format) {
    var txt = champ;
    
    txt.nomLong = nom;
	txt.format = format;
}

/*------------------------------------------------------------------
Fonction :      checkFormat
--------------------------------------------------------------------
Nature :        private
Arguments :     txt = Zone du formulaire concerné
Resultat :      Boolean
Description :   Retourne false si le champ n'a pas le bon format ou la bonne longueur
-------------------------------------------------------------------*/
function checkFormat(txt) {
    var val = txt.value;

    // S'il est vide, c'est ok
    if (val.length == 0) {
        return true;
    }
    // Si la longueur n'est pas bonne, pas la peine d'aller plus loin
    if (val.length != txt.format.length) {
    	alert("Vous devez saisir le champ " + txt.nomLong + " avec le format suivant: " + txt.format);
    	//txt.focus();
    	//txt.select();
        return false;
    }
    // Vérifie chaque caractère en fonction de son format
    for (var i=0; i<val.length; i++) {
        var fchar = txt.format.charAt(i);
        var vchar = val.charAt(i);
        switch(fchar) {
            case "A" :
                if (!isAlpha(vchar)) {
                	alert("Vous devez saisir le champ " + txt.nomLong + " avec le format suivant: " + txt.format);
                	//txt.focus();
    				//txt.select();
                    return false;
                }
                break;
            case "9" :
                if (!isDigit(vchar)) {
					alert("Vous devez saisir le champ " + txt.nomLong + " avec le format suivant: " + txt.format);
					//txt.focus();
    				//txt.select();
                    return false;
                }
                break;
            case "@" :
                if (!isDigit(vchar) && !isAlpha(vchar)) {
                    alert("Vous devez saisir le champ " + txt.nomLong + " avec le format suivant: " + txt.format);
                    //txt.focus();
    				//txt.select();
                    return false;
                }
                break;
            case "X" :
                break;
            default :
                if (fchar != vchar) {
                    alert("Vous devez saisir le champ " + txt.nomLong + " avec le format suivant: " + txt.format);
                    //txt.focus();
    				//txt.select();
                    return false;
                }
        }
    }
    return true;
}




function oncl(event) {
    var target = event == null ? window.event.srcElement : event.target;

    // Only hide/show when clicked on -/+
    if (target.parentNode.className != 'x') {
        return null;
    }

    while (target.className != 'cd' && target.parentNode) {
        target = target.parentNode;
    }

    if (target.className == 'cd') {
        // Toggle all internal DIVs
        var child = target.firstChild;
        while (child) {
            if (child.className == 'rd' || child.className == 'cd' || child.className == 'id' || child.className == 'c') {
                // Toggle visibility of all relevant children
                hideShow(child);
            } else if (child.className == 'x' && child.firstChild) {
                // Toggle +/-
                var textNode = child.firstChild;
                var value = textNode.nodeValue;
                if (value.indexOf('-') != -1)
                    textNode.nodeValue = value.substring(0, value.indexOf('-')) + '+' + value.substring(value.indexOf('-') + 1);
                else
                    textNode.nodeValue = value.substring(0, value.indexOf('+')) + '-' + value.substring(value.indexOf('+') + 1);
            }

            child = child.nextSibling;
        }
    }
}

function initialize() {
    document.onclick = oncl;
}
// MEDD 26/12/2007
function checkMSA(val) {
	
	if( val == 'false') {
		hideShowMSA('none');
	} else {
		hideShowMSA('');
	}	
}
function hideShowMSA(val) {
	masqAffichElem('numAffMSA', val);	
}
// FIN MEDD
/*
MEDD-19/03/2008: Extranet - demande de connexion
Début modification
*/
function checkRefBancaire(val) {
	if( val == 'RIB' ) {
		hideShowRIB('');
		hideShowRIBEtranger('none');
		hideShowIBAN('none');
	} else if (val == 'RIBEtranger') {
		hideShowRIB('none');
		hideShowRIBEtranger('');
		hideShowIBAN('none');
	}else{
		hideShowRIB('none');
		hideShowRIBEtranger('none');
		hideShowIBAN('');	
	}				
}

function hideShowRIB(val) {
   
	masqAffichElem('numRib', val);
	
}

function hideShowRIBEtranger(val) {

	masqAffichElem('numRibEtranger', val);	
    
}

function hideShowIBAN(val) {

	masqAffichElem('numIban', val);
	masqAffichElem('numBic', val);
    
}
/*
Fin modification
MEDD-19/03/2008: Extranet - demande de connexion
*/
