SearchFormEmp = function(options)
{
	this.options = options;
	this.defaultValues = {};
	if(typeof this.options.defaultValues != "undefined")
	{
		this.defaultValues = this.options.defaultValues;
	}
	this.geo = {};
	this.jours = {};
	
	this.form = document.forms["sf.form"];
	
	if(Util.Component.exists("sf.region"))
	{
		this.region = new SearchForm.Select({id: "sf.region"});
	}
	else
	{
		this.region = null;
	}
	if(Util.Component.exists("sf.departement"))
	{
		this.departement = new SearchForm.Select({id: "sf.departement"});
	}
	else
	{
		this.departement = null;
	}
	if(Util.Component.exists("sf.ville"))
	{
		this.ville = new SearchForm.Select({id: "sf.ville"});
	}
	else
	{
		this.ville = null;
	}
	if(Util.Component.exists("sf.dateDebut"))
	{
		this.dateDebut = new SearchForm.Select({id: "sf.dateDebut"});
	}
	if(Util.Component.exists("sf.dateFin"))
	{
		this.dateFin = new SearchForm.Select({id: "sf.dateFin"});
	}
	//champ date type promovac
	if(Util.Component.exists("sf.dateText"))
	{
		this.dateText = new Util.Component({id: "sf.dateText"});
	}
	else
	{
		this.dateText = null;
	}
	if(Util.Component.exists("sf.capacite"))
	{
		this.capacite = new SearchForm.Select({id: "sf.capacite"});
	}
	else
	{
		this.capacite = null;
	}
	this.themes = [];
	//themes
	for(var i = 1; i < 10; i++)
	{
		if(Util.Component.exists("sf.theme."+i))
		{
			this.themes[i] = new Util.Component({id: "sf.theme."+i});
		}
	}
	
	this.init();
};

SearchFormEmp.prototype = {
		
	init: function()
	{
		this.setHistory();
	},
	
	setGeo: function()
	{
		var http = new Util.HttpReader({
			url: "/ajax/sf.emp.selects.php",
			params: {
				type: "geo",
				siteId: this.options.siteId
			},
			method: "post",
			handler: function(response)
			{				
				this.geo = eval("("+response+")");
				this.initSelectsGeo();
				if(this.history.region_id && this.region)
				{
					this.region.setValue(this.history.region_id);
				}
				
				if(this.history.dep_id && this.departement)
				{
					this.departement.setValue(this.history.dep_id);
				}
				
				if(this.history.ville_id && this.ville)
				{
					this.ville.setValue(this.history.ville_id);
				}
			},
			scope: this
		});
		
		http.load();
	},
	
	setDate: function()
	{
		var http = new Util.HttpReader({
			url: "/ajax/sf.emp.selects.php",
			params: {
				type: "date"
			},
			method: "post",
			handler: function(response)
			{
				this.jours = eval('('+response+')');

				this.dateDebut.set(this.jours);
				this.dateFin.set(this.jours);
				this.dateDebut.on("change", function(){
					var jours = {};
					
					for(var i in this.jours)
					{
						if(parseInt(i.replace("-","").replace("-","")) > parseInt(this.dateDebut.getValue().replace("-","").replace("-","")))
						{
							jours[i] = this.jours[i];
						}
					}
					
					this.dateFin.set(jours);
				}, this);
				if(this.history.date_d)
				{
					this.dateDebut.setValue(this.history.date_d);
				}
				
				if(this.history.date_f)
				{
					this.dateFin.setValue(this.history.date_f);
				}
			},
			scope: this
		});
		
		http.load();
	},
	
	setCapacite: function()
	{
		var http = new Util.HttpReader({
			url: "/ajax/sf.emp.selects.php",
			params: {
				type: "nbpax"
			},
			method: "post",
			handler: function(response)
			{
				var cap = eval('('+response+')');
				this.capacite.set(cap);
				if(this.history.ages)
				{
					this.capacite.setValue(this.history.ages.length);
					this.setAgesFields();
					
					for( var i in this.history.ages)
					{
						var id = parseInt(i)+1;						
						var input = new Util.Component({id: "sf.age."+id});						
						input.addAttribute("value", this.history.ages[i]);
					}
				}
				else
				{
					this.history.ages = [];
				}
				
				this.capacite.on("change", function()
						{
							this.setAgesFields();
						}, this);
			},
			scope: this
		});
		
		http.load();
	},
	
	setAgesFields: function()
	{
		var div = new Util.Component({id: "sf.ages"});
		
		for(var i = 0; i < this.capacite.getValue(); i++)
		{
			id = parseInt(i)+1;			
			if(Util.Component.exists("sf.age."+id))
			{
				var input = new Util.Component({id: "sf.age."+id});
				if(typeof this.history.ages == "undefined"){
					this.history.ages = {};
				}
				this.history.ages[i] = input.getAttribute("value");
			}
		}
		
		div.clear();
		for(var i = 0; i < this.capacite.getValue(); i++)
		{
			id = parseInt(i)+1;
			div.putElement({
				tag: "label",
				content: "Date de naissance "+id+" (jj/mm/aaaa)"
			});
			
			var value = "";
			if(this.history.ages)
			{
				if(this.history.ages[i])
				{
					value = this.history.ages[i];
				}
			}
			div.putElement({
				tag: "input",
				attributes: {
					type: "text",
					name: "ages[]",
					id: "sf.age."+id,
					value: value
				}
			});
		}
	},
	
	setDuree: function()
	{
		if(Util.Component.exists("sf.duree.1"))
		{
			var input = new Util.Component({id: "sf.duree.1"});
			input.on("click", function(){
				this.date.set(this.jours[6]);
			}, this);
			if(!this.history.nuite_id)
			{
				input.addAttribute("checked", "1");
			}
		}
		
		if(Util.Component.exists("sf.duree.2"))
		{
			var input = new Util.Component({id: "sf.duree.2"});
			input.on("click", function(){
				this.date.set(this.jours[5]);
			}, this);
			if(this.history.nuite_id)
			{
				if(this.history.nuite_id == "1:6")
				{
					input.addAttribute("checked", "1");
				}
			}
		}
	},
	
	initSelectsGeo: function()
	{
		if(this.region){
			this.region.add("", "Toutes les régions");
		}
		if(this.departement){
			this.departement.add("", "Tous les départements");
		}
		if(this.ville){
			this.ville.add("", "Toutes les villes");
		}
		var rId = "";
		if(this.history.region_id && this.region)
		{
			this.region.setValue(this.history.region_id);
			rId = this.history.region_id;
		}
		var dId = "";
		if(this.history.dep_id && this.departement)
		{
			this.departement.setValue(this.history.dep_id);
			dId = this.history.dep_id;
		}
		
		for(var p in this.geo)
		{
			
			var regions = this.geo[p].regions;
			if(this.region){
				this.region.addGroup(this.geo[p].label);
			}
			
			var tmpDep = [];
			var tmpVille = [];
			if(rId == "")
			{
				if(this.departement){
					this.departement.addGroup(this.geo[p].label);
				}
				if(this.ville){
					this.ville.addGroup(this.geo[p].label);
				}
			}

			for(var r in regions)
			{
				if(this.region){
					this.region.add(r, regions[r].label);
				}
				var departements = regions[r].departements;

				if(rId != "" && rId == r)
				{
					if(this.departement){
						this.departement.addGroup(this.geo[p].label);
					}
					if(this.ville){
						this.ville.addGroup(this.geo[p].label);
					}
				}
				for(var d in departements)
				{
					if(d && this.departement)
					{
						if(rId == r || rId == "")
						{
							tmpDep[tmpDep.length] = departements[d].label+"#"+d;	
						}
					}
										
					var villes = departements[d].villes;
					
					for(var v in villes)
					{						
						if(!in_array(tmpVille, villes[v].label+"#"+v))
						{
							if((rId == r || rId == "") )
							{								
								tmpVille[tmpVille.length] = villes[v].label+"#"+v;
							}
						}
					}
				}
			}
			
			if(this.departement){
				
				tmpDep.sort();
				
				for(var i in tmpDep)
				{	
					if(typeof tmpDep[i] == "string")
					{
						var code = tmpDep[i].split("#");
						this.departement.add(code[1], code[0]);
					}
				}
				
			}
			if(this.ville){
				tmpVille.sort();
				for(var i in tmpVille)
				{	
					var code = tmpVille[i].split("#");
					this.ville.add(code[1], code[0]);
				}
			}
		}
		if(this.region){
		this.region.on("change", function(){
			
			var rId =this.region.getValue();
			
			if(this.departement){
				this.departement.clear();
				this.departement.add("", "Tous les départements");
				this.departement.enable();
			}
			if(this.ville){
				this.ville.clear();
				this.ville.add("", "Toutes les villes");
				this.ville.enable();
			}
			for(var p in this.geo)
			{
				
				var regions = this.geo[p].regions;
				if(rId == "")
				{
					if(this.departement){
						this.departement.addGroup(this.geo[p].label);
					}
					if(this.ville){
						this.ville.addGroup(this.geo[p].label);
					}
				}
				var tmpDep = [];
				var tmpVille = [];
				for(var r in regions)
				{
					
					if(r == rId || rId == "")
					{
						if(rId != "") 
						{
							if(this.departement){
								this.departement.addGroup(this.geo[p].label);
							}
							if(this.ville){
								this.ville.addGroup(this.geo[p].label);
							}
						}
						var departements = regions[r].departements;
						
						for(var d in departements)
						{
							
							if(d && this.departement)
							{
								tmpDep[tmpDep.length] = departements[d].label+"#"+d;
								//this.departement.add(d, departements[d].label);
							}
							else
							{
								if(rId != "" && this.departement)
								{
									this.departement.clear();
									this.departement.add("","Pas de département");
									this.departement.disable();
								}
							}
							
							var villes = departements[d].villes;
							for(var v in villes)
							{
								if(!in_array(tmpVille, villes[v].label+"#"+v))
								{
									tmpVille[tmpVille.length] = villes[v].label+"#"+v;
								}
							}
						}
					}
				}
				if(this.departement){
					tmpDep.sort();
					for(var i in tmpDep)
					{	
						var code = tmpDep[i].split("#");
						this.departement.add(code[1], code[0]);
					}
				}
				if(this.ville){
					tmpVille.sort();
					for(var i in tmpVille)
					{	
						var code = tmpVille[i].split("#");
						this.ville.add(code[1], code[0]);
					}
				}
			}
			
			
		}, this);
		}
		
		if(this.departement){
		this.departement.on("change", function(){
			var dId =this.departement.getValue();
			if(this.ville){
				this.ville.clear();
				this.ville.add("", "Toutes les villes");
				this.ville.enable();
			}
			for(var p in this.geo)
			{
				var regions = this.geo[p].regions;
				if(dId == "" && this.ville)
				{
					this.ville.addGroup(this.geo[p].label);
				}
				var tmpVille = [];
				for(var r in regions)
				{
					var departements = regions[r].departements;
					for(var d in departements)
					{
						if(dId == d || dId == "")
						{
							if(dId == d && this.region)
							{
								this.region.setValue(r);
							}
							if(dId != "" && this.ville) 
							{
								this.ville.addGroup(this.geo[p].label);
							}
							var villes = departements[d].villes;
							for(var v in villes)
							{
								if(!in_array(tmpVille, villes[v].label+"#"+v))
								{
									tmpVille[tmpVille.length] = villes[v].label+"#"+v;
								}
							}
						}
					}
				}
				if(this.ville){
					tmpVille.sort();
					for(var i in tmpVille)
					{	
						var code = tmpVille[i].split("#");
						this.ville.add(code[1], code[0]);
					}
				}
			}
		}, this);
		}
		if(this.ville){
		this.ville.on("change", function(){
			var vId = this.ville.getValue();
			if(this.departement){
				this.departement.enable();
			}
			for(var p in this.geo)
			{
				var regions = this.geo[p].regions;

				for(var r in regions)
				{
					var departements = regions[r].departements;
					for(var d in departements)
					{
						if(d)
						{
							var villes = departements[d].villes;
							for(var v in villes)
							{							
								if(vId == v && vId != "")
								{
									if(this.region){
										this.region.setValue(r);
									}
									if(this.departement){									
										this.departement.setValue(d);
									}
								}
							}
						}
					}
				}
			}
		}, this);
		}
	},
	
	setHistory: function()
	{
		var http = new Util.HttpReader({
			url: "/ajax/sf.emp.selects.php",
			params: {
				getParams: "1"
			},
			method: "post",
			handler: function(response)
			{				
				this.history = eval('('+response+')');
				if(!this.history)
				{
					this.history = {};
				}
				for(var k in this.defaultValues)
				{
					this.history[k] = this.defaultValues[k];
				}
				
				this.setGeo();
				if(Util.Component.exists("sf.dateDebut") && Util.Component.exists("sf.dateFin"))
				{
					this.setDate();					
				}
				
				if(Util.Component.exists("sf.dateText"))
				{
					if(this.history.date)
					{
						this.dateText.getElement().value = this.history.date;
					}
				}
				if(Util.Component.exists("sf.capacite"))
				{
					this.setCapacite();
				}
				
				this.setDuree();	
				
				if(this.history.categories)
				{
					var cats = this.history.categories;

					for(var i in cats)
					{
						id = cats[i];
						var s = new Util.Component({id: "sf.categorie."+id});
						s.getElement().checked = 1;
					}
				}
				
				if(this.history.supplements)
				{
					var supp = this.history.supplements;

					for(var i in supp)
					{
						id = supp[i];
						var s = new Util.Component({id: "sf.supp."+id});
						s.getElement().checked = 1;
					}
				}
				
			},
			scope: this
		});

		
		http.load();
	},
	
	submit: function()
	{
		var test = true;
		var textes = [];
		if(this.capacite.getValue() == "0")
		{
			alert("Veuillez saisir le nombre de personnes");
		}
		else
		{
			for(var i = 0; i < this.capacite.getValue(); i++)
			{
				id = parseInt(i)+1;	
				var input = new Util.Component({id: "sf.age."+id});
				input.setStyle("border","");
				var val = input.getAttribute("value");
				var reg=new RegExp("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$","g");
				if(!reg.test(val))
				{
					test = false;
					input.setStyle("border","2px #FF0000 solid");
				}
			}
			
			if(test == false)
			{
				alert("Veuillez remplir les dates de naissances");
			}
			else
			{
				this.form.submit();
			}
		}
	},
	
	reset: function()
	{
		this.form.reset();
		this.setAgesFields();
	}
};

