
//pour connaitre la position d'un élément
jQuery.fn.extend({
	findPos : function() {
		obj = $(this).get(0);
		var curleft = obj.offsetLeft || 0;
		var curtop = obj.offsetTop || 0;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
		return {x:curleft,y:curtop};
	}
});

/*
* Selects an option by value
*
* @name     selectOptions
* @author   Mathias Bank (http://www.mathias-bank.de)
* @param    value specifies, which options should be selected
* @example  jQuery("#myselect").selectOptions("val1");
*
*/
jQuery.fn.selectOptions = function(value) {
	
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;

			// get number of options
			var optionsLength = this.options.length;

			if(value)
			{
				for(var i = 0; i<optionsLength; i++) {
					if (this.options[i].value == value) {
						this.options[i].selected = true;
					};
				}
			}
			else
			{
				this.options[0].selected = true;
			}
		}
	)	
	return this;
}

function moteur_home(id_ville)
{
	$(document).ready(function(){
		  	
		
		//Charge combo dest MHO
		$.ajax({
			type: "POST",
			url: 'moteur.php',
			data: {what:'combodestination',id_ville:id_ville},
			success: function(msg){
				if(msg.length>0)
				{
					if(document.all) $('#destination').empty().append(msg);
					else $('#destination').empty().append(msg).selectOptions(id_ville);
				}
	
					$.ajax({
								type: "POST",
								url: 'moteur.php',
								data: {what: 'combotype', destination:$('#destination').val()},
								success: function(msg){
									if(msg.length>0)
									{
										if(document.all) $('#type').empty().append(msg);
										else $('#type').empty().append(msg).selectOptions();
									}
									
					$.ajax({
						type: "POST",
						url: 'moteur.php',
						data: {what: 'combocategorie',  destination:$('#destination').val(),type:$('#type').val()},
						success: function(msg){
							if(msg.length>0)
							{
								if(document.all) $('#categorie').empty().append(msg);
								else $('#categorie').empty().append(msg).selectOptions();
							}
					 },
									error: function(msg){//Erreur s'il y a un probleme su le combo date
										//alert(msg);
										$("#categorie").attr("disabled","true");
										//$("#cbdate").attr("disabled","true");
										  }
				  					 });
					
			     				},
								error: function(msg){//Erreur s'il y a un probleme su le combo depart 
									//alert(msg);
									$("#type").attr("disabled","true");
									//$("#depart").attr("disabled","true");
									}
							});
					
			
				
				//Si on change la combo destination:Champ modifié==>Combo depart,Combo date,Combo jour
				$('#destination').change( 
					function(destination)
					{
											
						$.ajax({
									type: "POST",
									url: 'moteur.php',
									data: {what: 'combotype', destination:$('#destination').val()},
									success: function(msg){
										if(msg.length>0)
										{
											if(document.all) $('#type').empty().append(msg);
											else $('#type').empty().append(msg).selectOptions();
										}
						$.ajax({
							type: "POST",
							url: 'moteur.php',
							data: { what: 'combocategorie', destination:$('#destination').val(),type:$('#type').val() },
							success: function(msg){
								if(msg.length>0)
								{
									if(document.all) $('#categorie').empty().append(msg);
									else $('#categorie').empty().append(msg).selectOptions();
								}
							},
									error:function(msg){
										$("#categorie").attr("disabled","true");
										}
									});
									
						  		},
								error: function(msg){
									
									$("#type").attr("disabled","true");
								
								}					
							});
					} );
				

				$('#type').change(
					function(type)
					{
							$.ajax({
								type: "POST",
								url: 'moteur.php',
								data: { what: 'combocategorie', destination: $('#destination').val(),type:$('#type').val()},
								success: function(msg){
									if(msg.length>0)
									{
										if(document.all) $('#categorie').empty().append(msg);
										else $('#categorie').empty().append(msg).selectOptions();
									}
												
						  		},
							error: function(msg){
									
									$("#type").attr("disabled","true");
									
								}					
							});
					} );
			
			},
			error: function(msg){
				
				$("#destination").attr("disabled","true");
				$("#type").attr("disabled","true");
				$("#categorie").attr("disabled","true");
				
			}
			   
		  });
			
	});
} 
	
	



