var	currentRequestDepartement	= 0;

$(window).load(function(){
	if( $("input#departement") ) {
	
		$("input#departement").keyup(function(){
			if( $(this).attr("value").length == 2 ) {
				if( $(this).attr("value") == currentRequestDepartement ) {
					return;
				}
				
				currentRequestDepartement	= $(this).attr("value");
				
				var elem = $($("#spinner").html());
				$("#sites").append(elem);
				
				elem = $($("#spinner").html());
				$("#poles").append(elem);
				
				$.ajax({
					async: true,
					dataType: "html",
					type: "GET",
					url: "http://" + window.location.hostname + window.location.pathname + "?do=etablissements&departement=" + $(this).attr("value") + "&id_etablissement=" + $("input#id_etablissement_preselect").attr("value"),
					success: function(data, textStatus) {
						$("#etablissements").html(data);			
						
						$("#sites img").replaceWith("");
						$("#poles img").replaceWith("");
						
						if( data.match(/^<select/) !== null ) {
							addEventsSelectEtablissements();
							
							$("select#etablissement").trigger("change");
						} else {
							$.ajax({
								async: true,
								dataType: "html",
								type: "GET",
								url: "http://" + window.location.hostname + window.location.pathname + "?do=sites&id_etablissement=0",
								success: function(data, textStatus) {
									$("#sites").html(data);
								}
							});
							
							$.ajax({
								async: true,
								dataType: "html",
								type: "GET",
								url: "http://" + window.location.hostname + window.location.pathname + "?do=poles&id_etablissement=0",
								success: function(data, textStatus) {
									$("#poles").html(data);
								}
							});
						}
					}
				});
			}
		});
		
		addEventsSelectEtablissements();
	
	}
	
	if( $("select#mandats") ) {
	
		$("select#mandats").change(function(){
			var elem = $($("#spinner").html());
			$("#fct_asso_fonctions").append(elem);
			
			$("select#mandats option:selected").each(function(){
				$.ajax({
					async: true,
					dataType: "html",
					type: "GET",
					url: "http://" + window.location.hostname + window.location.pathname + "?do=fonctions&id_mandat=" + $(this).attr("value"),
					success: function(data, textStatus) {
						$("#fct_asso_fonctions").html(data);
					}
				});
			});
		});
	
	}
});

function addEventsSelectEtablissements() {
	if( $("select#etablissement") ) {
	
		$("select#etablissement").change(function(){
			var elem = $($("#spinner").html());
			$("#sites").append(elem);
			
			elem = $($("#spinner").html());
			$("#poles").append(elem);
			
			$("select#etablissement option:selected").each(function(){
				$.ajax({
					async: true,
					dataType: "html",
					type: "GET",
					url: "http://" + window.location.hostname + window.location.pathname + "?do=sites&id_etablissement=" + $(this).attr("value") + "&id_site=" + $("input#id_site_preselect").attr("value"),
					success: function(data, textStatus) {
						$("#sites").html(data);
					}
				});
				
				$.ajax({
					async: true,
					dataType: "html",
					type: "GET",
					url: "http://" + window.location.hostname + window.location.pathname + "?do=poles&id_etablissement=" + $(this).attr("value") + "&id_pole=" + $("input#id_pole_preselect").attr("value"),
					success: function(data, textStatus) {
						$("#poles").html(data);
					}
				});
			});
		});
	
	}
}
