/*
* 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_theme(){
	$(document).ready(function(){						   
		$('.loading1').css("display","block");
		$('.loading2').css("display","block");
		$('.loading3').css("display","block");
		
		$.ajax({
			type: "POST",
			url: 'proxy_theme.php',
			data: {what:'combotheme'},
			success: function(msg){
				if(msg.length>0){
					$('.loading1').css("display","none");
					if(document.all) $('#id_theme').empty().append(msg);
					else $('#id_theme').empty().append(msg).selectOptions();
				}	
				//charge du combo DEPART lors de l'affichage de la page en fonction de la destination (pays)
				
				$.ajax({
							type: "POST",
							url: 'proxy_theme.php',
							data: {what: 'combodestination', theme:$('#id_theme').val() },
							success: function(msg){
								if(msg.length>0)
								{
									$('.loading2').css("display","none");
									if(document.all) $('#destinationtheme').empty().append(msg);
									else $('#destinationtheme').empty().append(msg).selectOptions();
								}
					$.ajax({
						type: "POST",
						url: 'proxy_theme.php',
						data: {what: 'combovoyage',  dest:$('#destinationtheme').val(),theme:$('#id_theme').val()},
						success: function(msg){
							if(msg.length>0)
							{
								$('.loading3').css("display","none");
								if(document.all) $('#typevoyage').empty().append(msg);
								else $('#typevoyage').empty().append(msg).selectOptions();
							}
						 },
									error: function(msg){//Erreur s'il y a un probleme su le combo depart
										//alert(msg);
										$("#typevoyage").attr("disabled","true");
										//$("#cbdate").attr("disabled","true");
										  }
				  					 });
					
			     				},
								error: function(msg){
									$("#destinationtheme").attr("disabled","true");
									}
							});
				
				//Si on change la combo destination:Champ modifié==>Combo depart,Combo date,Combo jour
				$('#id_theme').change( 
					function(theme)
					{
						
						$('#destinationtheme').empty();
						$('.loading2').css("display","block");
						$.ajax({
									type: "POST",
									url: 'proxy_theme.php',
									data: {what: 'combodestination', theme:$('#id_theme').val() },
									success: function(msg){
										if(msg.length>0)
										{
											$('#typevoyage').empty();
											$('.loading2').css("display","none");
											$('.loading3').css("display","block");
											if(document.all) $('#destinationtheme').empty().append(msg);
											else $('#destinationtheme').empty().append(msg).selectOptions();
										}
						$.ajax({
							type: "POST",
							url: 'proxy_theme.php',
							data: { what: 'combovoyage', dest: $('#destinationtheme').val(),theme:$('#id_theme').val() },
							success: function(msg){
								if(msg.length>0)
								{
									$('.loading3').css("display","none");
									if(document.all) $('#typevoyage').empty().append(msg);
									else $('#typevoyage').empty().append(msg).selectOptions();
								}
							},
									error:function(msg){
										$("#typevoyage").attr("disabled","true");
										}
									});
									
						  		},
								error: function(msg){
									$("#destinationtheme").attr("disabled","true");									
								}					
							});
					} );
				
				
				//Si on change la combo de depart: Champ modifié ==>Combo date,Combo jour
				$('#destinationtheme').change(
					function(dest)
					{
						 $('#typevoyage').empty();
						$('.loading3').css("display","block");
						
						$.ajax({
								type: "POST",
								url: 'proxy_theme.php',
								data: { what: 'combovoyage', dest: $('#destinationtheme').val(),theme:$('#id_theme').val() },
								success: function(msg){
									if(msg.length>0)
									{
										$('.loading3').css("display","none");
										if(document.all) $('#typevoyage').empty().append(msg);
										else $('#typevoyage').empty().append(msg).selectOptions();
									}
														
						  		},
							error: function(msg){
									$("#typevoyage").attr("disabled","true");
								}					
							});
					} );
			
			},
			error: function(msg){
				$("#destinationtheme").attr("disabled","true");
				$("#id_theme").attr("disabled","true");
				$("#typevoyage").attr("disabled","true");
			}
			   
		});
		
		$('#typevoyage').ajaxStop(function(){ 
				$('.loading1').css("display","none");
				$('.loading2').css("display","none");
				$('.loading3').css("display","none");
				
		});
	
	
			
	});//***
} 
	


//Moteur theme liste
function moteur_theme_liste(id_theme)
{
	$(document).ready(function(){
						   
		$('.loading1').css("display","block");
		$('.loading2').css("display","block");
		$('.loading3').css("display","block");
		//$('.ok_too').css("display","none");
		
		$.ajax({
			type: "POST",
			url: 'proxy_theme.php',
			data: {what:'combodestinationtheme',id_theme:id_theme},
			success: function(msg){
				if(msg.length>0)
				{
					$('.loading1').css("display","none");
					if(document.all) $('#destinationthemeliste').empty().append(msg);
					else $('#destinationthemeliste').empty().append(msg).selectOptions();
				}
	
				//charge du combo DEPART lors de l'affichage de la page en fonction de la destination (pays)
				
				$.ajax({
							type: "POST",
							url: 'proxy_theme.php',
							data: {what: 'combotypevoyageliste', dest:$('#destinationthemeliste').val(),id_theme:id_theme },
							success: function(msg){
								if(msg.length>0)
								{
									$('.loading2').css("display","none");
									if(document.all) $('#typevoyage').empty().append(msg);
									else $('#typevoyage').empty().append(msg).selectOptions();
								}
					$.ajax({
						type: "POST",
						url: 'proxy_theme.php',
						data: {what: 'combodatedepartliste',  dest:$('#destinationthemeliste').val(),typevoyage:$('#typevoyage').val(),id_theme:id_theme},
						success: function(msg){
							if(msg.length>0)
							{
								$('.loading3').css("display","none");
								if(document.all) $('#datedepartliste').empty().append(msg);
								else $('#datedepartliste').empty().append(msg).selectOptions();
							}
						 },
									error: function(msg){//Erreur s'il y a un probleme su le combo depart
										//alert(msg);
										$("#datedepartliste").attr("disabled","true");
										//$("#cbdate").attr("disabled","true");
										  }
				  					 });
					
			     				},
								error: function(msg){//Erreur s'il y a un probleme su le combo date 
									//alert(msg);
									//$("#depart").attr("disabled","true");
									$("#typevoyage").attr("disabled","true");
									}
							});
				
				//Si on change la combo destination:Champ modifié==>Combo depart,Combo date,Combo jour
				$('#destinationthemeliste').change( 
					function(theme)
					{
						 $('#typevoyage').empty();
						// $('#datedepartliste').empty();
						$('.loading2').css("display","block");	
						//$('.loading3').css("display","block");
						
						$.ajax({
									type: "POST",
									url: 'proxy_theme.php',
									data: {what: 'combotypevoyageliste', dest:$('#destinationthemeliste').val(),id_theme:id_theme },
									success: function(msg){
										if(msg.length>0)
										{
											 $('#datedepartliste').empty();
											$('.loading2').css("display","none");
											$('.loading3').css("display","block");
											if(document.all) $('#typevoyage').empty().append(msg);
											else $('#typevoyage').empty().append(msg).selectOptions();
										}
						$.ajax({
							type: "POST",
							url: 'proxy_theme.php',
							data: { what: 'combodatedepartliste', dest:$('#destinationthemeliste').val(),typevoyage:$('#typevoyage').val(),id_theme:id_theme },
							success: function(msg){
								if(msg.length>0)
								{
									$('.loading3').css("display","none");
									if(document.all) $('#datedepartliste').empty().append(msg);
									else $('#datedepartliste').empty().append(msg).selectOptions();
								}
							},
									error:function(msg){
										//alert('erreur 333');
										$("#datedepartliste").attr("disabled","true");
									//	$("#cbdate").attr("disabled","true");
										}
									});
									
						  		},
								error: function(msg){
									//alert('erreur 333');
								//	$("#destination").attr("disabled","true");
									//$("#depart").attr("disabled","true");
									$("#typevoyage").attr("disabled","true");
									//$("#jour").attr("disabled","true");
								}					
							});
					} );
				
				
				//Si on change la combo de depart: Champ modifié ==>Combo date,Combo jour
				$('#typevoyage').change(
					function(dest)
					{
						$('#datedepartliste').empty();
						$('.loading3').css("display","block");
						
						$.ajax({
								type: "POST",
								url: 'proxy_theme.php',
								data: { what: 'combodatedepartliste', dest:$('#destinationthemeliste').val(),typevoyage:$('#typevoyage').val(),id_theme:id_theme },
								success: function(msg){
									if(msg.length>0)
									{
										$('.loading3').css("display","none");
										if(document.all) $('#datedepartliste').empty().append(msg);
										else $('#datedepartliste').empty().append(msg).selectOptions();
									}
														
						  		},
							error: function(msg){
									//alert('erreur 333');
									//$("#destination").attr("disabled","true");
									//$("#cbdate").attr("disabled","true");
									$("#datedepartliste").attr("disabled","true");
									//$("#jour").attr("disabled","true");
								}					
							});
					} );
			

			},
			error: function(msg){
				//alert('erreur 333');
				$("#destinationthemeliste").attr("disabled","true");
				$("#typevoyage").attr("disabled","true");
				$("#datedepartliste").attr("disabled","true");
				//$("#jour").attr("disabled","true");
			}
			   
		});
		
		$('#datedepartliste').ajaxStop(function(){ 
				
				//$('.ok_too').css("display","block");
				$('.loading1').css("display","none");
				$('.loading2').css("display","none");
				$('.loading3').css("display","none");
				
		});
	
	
			
	});//***
} 


