///////////////////////////////////////OPEN IN NEW WINDOW

/**
* ouovre le lien dans une nouvelle fenêtre
*
*/
function openInNewWindow ( e ) {
	
	Event . stop ( e ) ;
	
	var href = Event . element ( e ) . href ;
	
	if ( href == null ) {
		
		var href = Event . element ( e ) . up ( ) . href ;
	}
	
	window . open ( href ) ;
}

/**
* initialise l'ouverture dans une autre fenêtre
*
*/
function initOpenInNewWindow ( ) {
	
	$$ ( '.openInNewWindow' ) . each ( function  ( e ) { Event . observe ( $ ( e ) , 'click', openInNewWindow ) } ) ;
}

///////////////////////////////////////AUTOCOMPLETERS

/**
 * initialise l'autocompleter selection user
 * 
 */
function initAutocompleterUserSelection () {
	
	if ( $ ( 'autocompleteUserSelection' ) ) {
		var offset = $ ( 'autocompleteUserSelection' ) . viewportOffset ( )  ;
		
		$ ( 'autocompleteIndicatorDiv' ) . setStyle ( {
  			'position' : 'absolute',
  			'left': offset . left + 384 + 'px',
  			'top': offset . top + 'px'
		} ) ;
		
		new Ajax . Autocompleter ( 'autocompleteUserSelection', 'autocompleteDiv', 'selectionuserautocompleter', { 
			indicator : 'autocompleteIndicatorDiv',
			afterUpdateElement : function ( text, li ) {
				$ ( 'id' ) . value = li . id ;
			}
		} ) ;
	}
}

/**
 * initialise l'autocompleter selection admin
 * 
 */
function initAutocompleterAdminSelection () {
	
	if ( $ ( 'autocompleteAdminSelection' ) ) {
		var offset = $ ( 'autocompleteAdminSelection' ) . viewportOffset ( )  ;
		
		$ ( 'autocompleteIndicatorDiv' ) . setStyle ( {
  			'position' : 'absolute',
  			'left': offset . left + 384 + 'px',
  			'top': offset . top + 'px'
		} ) ;
		
		new Ajax . Autocompleter ( 'autocompleteAdminSelection', 'autocompleteDiv', 'selectionadminautocompleter', { 
			indicator : 'autocompleteIndicatorDiv',
			afterUpdateElement : function ( text, li ) {
				$ ( 'id' ) . value = li . id ;
			}
		} ) ;
	}
}

/**
 * initialise l'autocompleter selection identite
 * 
 */
function initAutocompleterIdentitySelection () {
	
	if ( $ ( 'autocompleteIdentitySelection' ) ) {
		var offset = $ ( 'autocompleteIdentitySelection' ) . viewportOffset ( )  ;
		
		$ ( 'autocompleteIndicatorDiv' ) . setStyle ( {
  			'position' : 'absolute',
  			'left': offset . left + 384 + 'px',
  			'top': offset . top + 'px'
		} ) ;
		
		new Ajax . Autocompleter ( 'autocompleteIdentitySelection', 'autocompleteDiv', 'selectionautocompleter', { 
			indicator : 'autocompleteIndicatorDiv',
			afterUpdateElement : function ( text, li ) {
				$ ( 'id' ) . value = li . id ;
			}
		} ) ;
	}
}

/**
 * initialise l'autocompleter text ville
 * 
 */
function initAutocompleterVilleText () {
	
	if ( $ ( 'VilleText' ) ) {
		var offset = $ ( 'VilleText' ) . viewportOffset ( )  ;
		
		$ ( 'autocompleteIndicatorDiv' ) . setStyle ( {
  			'position' : 'absolute',
  			'left': offset . left + 384 + 'px',
  			'top': offset . top + 'px'
		} ) ;
		
		new Ajax . Autocompleter ( 'VilleText', 'autocompleteDiv', 'villeautocompleter', { 
			minChars: 2, 
			indicator : 'autocompleteIndicatorDiv',
			afterUpdateElement : function ( text, li ) {
				$ ( 'Ville' ) . value = li . id ;
				zipUpdate ( li . id ) ;
			}
		} ) ;
	}
}

/**
 * met à jour  le code postal suivant l'id de la ville
 * 
 * @param integer id
 */
function zipUpdate ( id ) {
	
	var data = $H ( { 'idVille' : id } ) . toQueryString ( ) ;
	
	new Ajax . Request ( 'zipupdate', {
		postBody : data,
		onComplete : function ( requester ) {
			$ ( 'CodePostal' ) . value = requester . responseText ;
		}
	});
}

///////////////////////////////////////IDENTITY FORM DISPLAY

/**
 * form identite : affiche l'onglet général au départ puis observe les clicks sur les autres onglets 
 * 
 */
function initIdentityFormDisplay ( ) {
	
	if ( $ ( 'formIdentity' ) ) {
	
		firstNoDisplay ( '.subFieldset' ) ;
		
		$ ( 'fieldset-Register' ) . removeClassName ( 'noDisplay' ) ;

		var formPart = $ ( 'formPart' ) . value ;
		
		var arrayToDisplay ;
		
		if ( formPart != '' ) {
			arrayToDisplay = formPart . gsub ( 'menu-identity', '' ) ;
		}
		else {
			arrayToDisplay = 'General' ;
		}
		
		var identityArray = setIdentityArray ( ) ;
		identityArray [ arrayToDisplay ] . each ( function ( e ) { $ ( e )  . removeClassName ( 'noDisplay' ) } ) ;
		
		$ ( 'menu-identity' + arrayToDisplay ) . addClassName ( 'active' ) ;
		
		$$ ( '.ajaxSubMenu' ) . each ( function ( e ) {
			Event . observe ( $ ( e ) , 'click', identityPartsDisplay ) ;
		} ) ;
	}
}

/**
* retourne le tableau identityArray
*
*/
function setIdentityArray ( ) {
	
	var general = new Array ( ) ;
	general [ 0 ] = 'fieldset-EtatCivil' ;
	general [ 1 ] = 'fieldset-Logement' ;
	general [ 2 ] = 'fieldset-Famille' ;
							
	var socioPro = new Array ( ) ;
	socioPro [ 0 ] = 'fieldset-Etudes' ; 
	socioPro [ 1 ] = 'fieldset-SocioPro' ;
							
	var divers = new Array ( ) ;
	divers [ 0 ] = 'fieldset-Locomotion' ;
	
	var identityArray = new Array ( ) ;
	identityArray . General = general ;
	identityArray . SocioPro = socioPro ;
	identityArray . Divers = divers ;
	
	return identityArray ; 
}

/**
 * change d'onglet sur le form identite
 * 
 * @param HTML element e
 */
function identityPartsDisplay ( e ) {
	
	var identityArray = setIdentityArray ( ) ;
	
	Event . stop ( e ) ;
		
	firstNoDisplay ( '.subFieldset' ) ;
	$ ( 'fieldset-Register' ) . removeClassName ( 'noDisplay' ) ;
	
	$$ ( '.active' ) . each ( function ( e ) { $ ( e )  . removeClassName ( 'active' ) } ) ;
	
	var elementAction = Event . element ( e ) . id ;
	var arrayToDisplay = elementAction . gsub ( 'menu-identity', '' ) ;
	
	identityArray [ arrayToDisplay ] . each ( function ( e ) { $ ( e )  . removeClassName ( 'noDisplay' ) } ) ;
	
	$ ( elementAction ) . addClassName ( 'active' ) ;
	
	$( 'formPart' ) . value = elementAction ;
}

/**
 * général : enlève la class noDisplay
 * 
 * @param string cssrule
 */
function firstNoDisplay ( cssrule ) {
	
	$$ ( cssrule ) . each ( function  ( e ) { $ ( e ) . addClassName ( 'noDisplay' ) ; } ) ;
}

///////////////////////////////////////DISABLE ELEMENTS

/**
* disable des éléments de form
*
*/
function initDisableFormElements ( ) {
	
	$$ ( '.disabledElement' ) . each ( function ( e ) { 
			
		$ ( e ) . disable ( ) ;
		$ ( e ) . setStyle ( { backgroundColor: '#a73737' } ) ;
	} ) ;
		
	if ($ ( 'login' ) ) {
		
		if ($('login').disabled) {
			
			var value=$('currentLogin').value;
			$('login').value=value;
		}
	}

}

///////////////////////////////////////PERIODICITEMAX TOGGLE

/**
 * initPeriodiciteMaxToggle
 * 
 */
 function initPeriodiciteMaxToggle ( ) {
 	
 	if ( $ ( 'divPeriodiciteMax' ) ) {
 	
 		var accompagnement = $F ( 'Accompagnement' ) ;
 	
 		if ( accompagnement == null ) {
 			
 			$ ( 'divPeriodiciteMax' ) . hide ( ) ;
 		}
 		
 		Event . observe ( $ ( 'Accompagnement' ), 'click', function ( e ) { $ ( 'divPeriodiciteMax' ) . toggle ( ) } )
 	}
 }

///////////////////////////////////////OBSERVERS

/**
 * observateurs d'événements
 */
Event.observe ( window, 'load', initPeriodiciteMaxToggle ) ; 
Event.observe ( window, 'load', initAutocompleterAdminSelection ) ; 
Event.observe ( window, 'load', initAutocompleterUserSelection ) ; 
Event.observe ( window, 'load', initAutocompleterIdentitySelection ) ; 
Event.observe ( window, 'load', initAutocompleterVilleText ) ; 
Event.observe ( window, 'load', initIdentityFormDisplay, false ) ; 
Event.observe ( window, 'load', initOpenInNewWindow, false ) ; 
Event.observe ( window, 'load', initDisableFormElements ) ; 
