// Trier les clients
function triCli(lienordre) {
	$.ajax({
		type: "GET",
		url: "ajax/tri_cli.php",
		data: { ordre: lienordre },
		async: false,
		success: function(echo){
			_error = "off";
			$('ul#cli_list').html(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

// Charger les clients
function getClients(field_province,field_commune) {
	$.ajax({
		type: "GET",
		url: "ajax/get_clients.php",
		data: { prov: $(field_province).val(), commu: $(field_commune).val() },
		async: false,
		success: function(echo){
			_error = "off";
			$("select[@name=client_search]").html(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

// Charger les communes
function getCommunes(field_province) {
	$.ajax({
		type: "GET",
		url: "ajax/get_communes.php",
		data: { province: $(field_province).val() },
		async: false,
		success: function(echo){
			_error = "off";
			$("select[@name=commune_client]").html(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

// Charger le texte de l'annonce
function getTexte(field_id) {
	$.ajax({
		type: "GET",
		url: "ajax/get_texte.php",
		data: { id: $(field_id).val() },
		async: false,
		success: function(echo){
			_error = "off";
			$("input[@name=texte]").val(echo);
		}/*,
		error: function(echo){
			_error = "on";
			alert("Error - " + echo);
		}*/
	});	
}

$(document).ready(function() {

	// Tester si un champs est numérique
	$('input.numeric').keyup(function(){
		
		if( typeof $(document)[0].selection != 'undefined' ) {
			
			// Insertion du code de formatage
			var range = $(document)[0].selection.createRange();
			
			// on récupère la taille d'origine du texte
			var origi_length	= $(this).val().length;
			
			var range 			= $(this)[0].createTextRange();
			range.moveToBookmark($(document)[0].selection.createRange().getBookmark());
			range.moveEnd('character', $(this).val().length);
			
			var start			= $(this).val().length - range.text.length;
			
			var range 			= $(this)[0].createTextRange();
			range.moveToBookmark( $(document)[0].selection.createRange().getBookmark() );
			range.moveStart( 'character', -$(this).val().length );
			
			var end				= range.text.length;
			
			if( end < start )
				end = start;
			
			$(this).val( $(this).val().replace(/[^0-9]/ig,"") );
				
			// on récupère la taille du texte
			var dif				= origi_length - $(this).val().length;
			
			var range 			= $(this)[0].createTextRange();
			range.moveStart( 'character', start - dif );
			range.moveEnd( 'character', -$(this).val().length + end - dif );
			range.select();
		
		} else if (typeof $(this)[0].selectionStart != 'undefined') {
			
			// on récupère la position du curseur
			var start			= $(this)[0].selectionStart;
			var end				= $(this)[0].selectionEnd;
			
			// on récupère la taille d'origine du texte
			var origi_length	= $(this).val().length;
			
			// on retire les retours à la ligne
			$(this).val( $(this).val().replace(/[^0-9]/ig,"") );
			
			// on récupère la taille du texte
			var dif				= origi_length - $(this).val().length;
			
			// si il y a une différence c'est qu'on a retiré un retour a la ligne
			if( dif > 0 )
				dif = 1;
		
			// on repositione le curseur
			$(this)[0].selectionStart	= start - dif;
			$(this)[0].selectionEnd		= end - dif;
		
		}
		
	});
	
	// Sélectionner le client lors d'un clic sur un client
	/*$('ul#cli_list li').click(function() {
		if (($("select[@name=province_client]")[0]).selectedIndex != 0) {
			($("select[@name=province_client]")[0]).selectedIndex = 0;
			($("select[@name=commune_client]")[0]).selectedIndex = 0;
			getClients($('select[name="province_client"]'),$('select[name="commune_client"]'));
		}
		$('select[name="client_search"] option[value="' + $(this).attr('title') + '"]').attr('selected', 'selected');
	});*/
	
	// Scroller les clients vers le bas
	$("#scroll_bottom_cli").click(function() {
		var divOffset = $("#cli_list").offset().top;
		var pOffset = $("#cli_list li:eq(20)").offset().top;
		var pScroll = pOffset - divOffset;
		$("#cli_list_content").animate({scrollTop: "+=" + pScroll + "px"}, 1000, "easeinout");
	});
	
	// Scroller les clients vers le haut
	$("#scroll_top_cli").click(function() {
		var divOffset = $("#cli_list").offset().top;
		var pOffset = $("#cli_list li:eq(20)").offset().top;
		var pScroll = pOffset - divOffset;
		$("#cli_list_content").animate({scrollTop: "-=" + pScroll + "px"}, 1000, "easeinout");
	});
	
	// Afficher les communes et les clients
	$("select[@name=province_client]").change(function(){
		getCommunes(this);	
		($("select[@name=commune_client]")[0]).selectedIndex = 0;
		getClients(this,$("select[@name=commune_client] option:selected"));	
		($("select[@name=client_search]")[0]).selectedIndex = 0;		
	});
	
	// Afficher les clients
	$("select[@name=commune_client]").change(function(){
		getClients($("select[@name=province_client] option:selected"),this);	
		($("select[@name=client_search]")[0]).selectedIndex = 0;		
	});
	
	// Envoyer le formulaire de recherche par client
	$("#recherche_button_client").click(function(){
		$("input[@name=province_client_txt]").val($("select[@name=province_client] option:selected").text());
		$("input[@name=commune_client_txt]").val($("select[@name=commune_client] option:selected").text());
		$("input[@name=client_search_txt]").val($("select[@name=client_search] option:selected").text());
		$("form[@name=form_recherche_client]").submit();	
	});
	
	// Afficher les méthodes de tri d'affichage des clients
	$("#clients ul#cli_opt li#cli_afficher a:eq(0)").click(function(){
		$("#clients ul#cli_opt li#cli_afficher ul").toggle();
	});	
	
	// Changer le tri d'affichage des clients
	$("#clients ul#cli_opt li#cli_afficher ul li").click(function(){
		
		$("#clients ul#cli_opt li#cli_afficher ul li#cli_afficher_selected")
			
			// On ajoute le lien
			.html( '<a href="#" onclick="triCli(\'' + $('#clients ul#cli_opt li#cli_afficher ul li#cli_afficher_selected').attr('title') + '\'); return false;">' + $('#clients ul#cli_opt li#cli_afficher ul li#cli_afficher_selected').html() + '</a>' )
			
			// On retire l'id
			.removeAttr("id");
		
		$(this)
			
			// on rejoute l'id
			.attr("id", "cli_afficher_selected")
			
			// on change le contenu de la balise li
			.html($(this)
				// par le contenu de la balise enfant a
				.children("a").html()
			);
			
			
		// Sélectionner la rubrique lors d'un clic sur une catégorie
		$('ul#cat_list li').click(function() {
			$('select[name="rubrique"] option[value="' + $(this).attr('title') + '"]').attr('selected', 'selected');
			getProduits($('select[name="rubrique"]'));
			($("select[@name=produit]")[0]).selectedIndex = 0;
		});
		
	});
	
	// Rendre impossible la sélection des clients
	$("input[@name=part]").click(function() {
		
		if ($(this)[0].checked) {
			$("select[@name=client_search]").attr("disabled","disabled");
		} else {
			$("select[@name=client_search]").removeAttr("disabled");
		}
		
	});
	
	// Envoyer le formulaire d'accès direct
	$("#recherche_accesdirect").click(function() {
		if ($('input[@name="petiteannid"]').val() != "") {
			getTexte($('input[name="petiteannid"]'));
			$("form[@name=form_accesdirect]").submit();	
		}
	});

});
