/**
 * Met à jour la liste des complexes lors d'un changement de région
 */
function registration_changeCounty() {
	//Si une région a été sélectionnée...
	if ("00000" != document.registration.regCountySelection.options[document.registration.regCountySelection.selectedIndex].value) {
		//Met à jour la liste des complexes et la dégrise
		//TODO mettre à jour la liste des complexes
		document.registration.regComplexSelection.disabled = false;
	}
	else {
		//Si aucune région n'est sélectionnée, remet le texte par défaut de la liste des complexes et la grise
		document.registration.regComplexSelection.options[0].selected = true;
		document.registration.regComplexSelection.disabled = true;
	}
}

/**
 * Enregistre le complexe préféré
 */
function registration_saveFavoriteComplex() {
	//Récupère le complexe sélectionné (nom, identifiant, numéro)
	var favCplxName = document.registration.regComplexSelection.options[document.registration.regComplexSelection.selectedIndex].text;
	var favCplxId = document.registration.regComplexSelection.options[document.registration.regComplexSelection.selectedIndex].value;
	//Si un complexe a été sélectionné...
	if ("00000" != favCplxId) {
		var favCplxNbrVar = parseInt(document.getElementById('favCplxNbr').innerHTML, 10);
		switch (favCplxNbrVar) {
			case 1:
				document.registration.regFavCplx1Name.value = favCplxName;
				document.registration.regFavCplx1Id.value = favCplxId;
				//Premier complexe mémorisé : mémorise également l'identifiant de la région
				var countyId = document.registration.regCountySelection.options[document.registration.regCountySelection.selectedIndex].value;
				document.registration.regCountyId.value = countyId;
				//Dégrise et coche la checkbox du service 'ugcscope'
				document.registration.regUgcscopeServiceChecked.disabled = false;
				document.registration.regUgcscopeServiceChecked.checked = true;
				//Met à jour le numéro du prochain complexe mémorisé
				document.registration.regFavouriteComplexesNbr.value = favCplxNbrVar;
				document.getElementById('favCplxNbr').innerHTML = favCplxNbrVar+1;
				break;
			case 2:
				if (document.registration.regFavCplx1Id.value != favCplxId) {
					document.registration.regFavCplx2Name.value = favCplxName;
					document.registration.regFavCplx2Id.value = favCplxId;
					//Met à jour le numéro du prochain complexe mémorisé
					document.registration.regFavouriteComplexesNbr.value = favCplxNbrVar;
					document.getElementById('favCplxNbr').innerHTML = favCplxNbrVar+1;
				}
				break;
			case 3:
				if ((document.registration.regFavCplx1Id.value != favCplxId) &&
						(document.registration.regFavCplx2Id.value != favCplxId)) {
					document.registration.regFavCplx3Name.value = favCplxName;
					document.registration.regFavCplx3Id.value = favCplxId;
					//Met à jour le numéro du prochain complexe mémorisé
					document.registration.regFavouriteComplexesNbr.value = favCplxNbrVar;
					document.getElementById('favCplxNbr').innerHTML = favCplxNbrVar+1;
				}
				break;
			case 4:
				if ((document.registration.regFavCplx1Id.value != favCplxId) &&
						(document.registration.regFavCplx2Id.value != favCplxId) &&
						(document.registration.regFavCplx3Id.value != favCplxId)) {
					document.registration.regFavCplx4Name.value = favCplxName;
					document.registration.regFavCplx4Id.value = favCplxId;
					//Met à jour le numéro du prochain complexe mémorisé
					document.registration.regFavouriteComplexesNbr.value = favCplxNbrVar;
					document.getElementById('favCplxNbr').innerHTML = favCplxNbrVar+1;
				}
				break;
			default:
				if ((document.registration.regFavCplx1Id.value != favCplxId) &&
						(document.registration.regFavCplx2Id.value != favCplxId) &&
						(document.registration.regFavCplx3Id.value != favCplxId) &&
						(document.registration.regFavCplx4Id.value != favCplxId)) {
					document.registration.regFavCplx5Name.value = favCplxName;
					document.registration.regFavCplx5Id.value = favCplxId;
					//Dernier complexe mémorisé : grise les listes de régions et de complexes
					document.registration.regCountySelection.options[0].selected = true;
					document.registration.regCountySelection.disabled = true;
					document.registration.regComplexSelection.options[0].selected = true;
					document.registration.regComplexSelection.disabled = true;
					//Met à jour le numéro du prochain complexe mémorisé
					document.registration.regFavouriteComplexesNbr.value = favCplxNbrVar;
					/*document.getElementById('favCplxNbr').innerHTML = favCplxNbrVar+1;*/
				}
				break;
		}
	}
}

/**
 * Supprime la liste des complexes préférés enregistrée
 */
function registration_deleteFavoriteComplex() {
	//Suppression de la région enregistrée
	document.registration.regCountyId.value = '';
	//Suppression de la liste des complexes préférés enregistrée (nom et identifiants)
	document.registration.regFavCplx1Name.value = '';
	document.registration.regFavCplx1Id.value = '';
	document.registration.regFavCplx2Name.value = '';
	document.registration.regFavCplx2Id.value = '';
	document.registration.regFavCplx3Name.value = '';
	document.registration.regFavCplx3Id.value = '';
	document.registration.regFavCplx4Name.value = '';
	document.registration.regFavCplx4Id.value = '';
	document.registration.regFavCplx5Name.value = '';
	document.registration.regFavCplx5Id.value = '';
	//Réinitialise le numéro du prochain complexe mémorisé
	document.registration.regFavouriteComplexesNbr.value = 0;
	document.getElementById('favCplxNbr').innerHTML = '1';
	//Dégrise la liste des régions
	document.registration.regCountySelection.disabled = false;
	//Décoche et grise la checkbox du service 'ugcscope'
	document.registration.regUgcscopeServiceChecked.checked = false;
	document.registration.regUgcscopeServiceChecked.disabled = true;
}

/**
 * Met à jour la checkbox du service 'les Plus ugc illimité' (grisé ou coché)
 * selon que le champs 'numéro de carte ugc illimité' est renseigné ou non
 */
function registration_checkUnlimitedUgcFreeService() {
	if ((null == document.registration.regUnlimitedUgcCardNumber.value) || 
			('' == document.registration.regUnlimitedUgcCardNumber.value) ||
			('99' == document.registration.regUnlimitedUgcCardNumber.value.substring(0,2))) {
		//Décoche et grise la checkbox du service 'les Plus ugc illimité'
		document.registration.regUnlimitedUgcServiceChecked.checked = false;
		document.registration.regUnlimitedUgcServiceChecked.disabled = true;
	} else {
		//Dégrise et coche la checkbox du service 'les Plus ugc illimité'
		document.registration.regUnlimitedUgcServiceChecked.disabled = false;
		document.registration.regUnlimitedUgcServiceChecked.checked = true;
	}
}
