SearchForm = function(options)
{
	this.options = options;
	this.defaultValues = {};
	if(typeof this.options.defaultValues != "undefined")
	{
		this.defaultValues = this.options.defaultValues;
	}
	this.geo = {};
	this.jours = {};
	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.date"))
	{
		this.date = new SearchForm.Select({id: "sf.date"});
	}
	//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();
};

SearchForm.prototype = {
		
	init: function()
	{
		this.setHistory();
	},
	
	setGeo: function()
	{
		var http = new Util.HttpReader({
			url: "/ajax/sf.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.selects.php",
			params: {
				type: "date"
			},
			method: "post",
			handler: function(response)
			{
				this.jours = eval('('+response+')');
				var dow = 6;
				if(this.history.nuite_id)
				{
					if(this.history.nuite_id == "1:6")
					{
						dow = 5;
					}
				}
				this.date.set(this.jours[dow]);
				if(this.history.date)
				{
					this.date.setValue(this.history.date);
				}
			},
			scope: this
		});
		
		http.load();
	},
	
	setCapacite: function()
	{
		var http = new Util.HttpReader({
			url: "/ajax/sf.selects.php",
			params: {
				type: "capacite"
			},
			method: "post",
			handler: function(response)
			{
				var cap = eval('('+response+')');
				this.capacite.set(cap);
				if(this.history.capacite_id)
				{
					this.capacite.setValue(this.history.capacite_id);
				}
			},
			scope: this
		});
		
		http.load();
	},
	
	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.selects.php",
			params: {
				getParams: "1"
			},
			method: "post",
			handler: function(response)
			{				
				this.history = eval('('+response+')');
				for(var k in this.defaultValues)
				{
					this.history[k] = this.defaultValues[k];
				}
				
				this.setGeo();
				if(Util.Component.exists("sf.date"))
				{
					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.theme_res)
				{
					var themes = this.history.theme_res.split("-");

					for(var i=0; i < themes.length; i++)
					{
						//this.themes[themes[i]].addAttribute("checked", "1");
						//pour octopode
						this.themes[themes[i]].getElement().click();
					}
				}
				
			},
			scope: this
		});

		
		http.load();
	}
	
};

SearchForm.Select = function(options)
{
	this.currentGroup = null;
	this.options = options;
	this.element = null;
	this.init();
};

SearchForm.Select.prototype = {
	
	init: function()
	{
		this.element = new Util.Component({id: this.options.id});
	},
	
	addGroup: function(label)
	{
		this.currentGroup = new Util.Component({tag: "optgroup", attributes: {label: label}});
		this.element.append(this.currentGroup.getElement());
	},
	
	add: function(key, value)
	{
		if(this.currentGroup == null)
		{
			var option = new Util.Component({tag: "option", content: value, attributes: {value: key}});
			this.element.append(option.getElement());
		}
		else
		{
			var option = new Util.Component({tag: "option", content: value, attributes: {value: key}});
			this.currentGroup.append(option.getElement());
		}
	},
	
	clear: function()
	{
		this.element.clear();
		this.currentGroup = null;
	},
		
	set: function(data)
	{
		this.clear();
						
		for(var i in data)
		{
			this.add(i, data[i]);
		}

	},
	
	getValue: function()
	{
		return this.element.getAttribute("value");
	},
	
	setValue: function(v)
	{
		//this.element.addAttribute("value", v);
		/*for(var opt in this.element.getElement().options)
		{
			if(this.element.getElement().options[opt].value == v)
			{
				//alert(this.element.getElement().options[opt].value);
				this.element.getElement().options[opt].selected=true;
			}
			else
			{
				this.element.getElement().options[opt].selected=false;
			}
		}*/
		this.element.getElement().value = v;
	},
	
	disable: function()
	{
		this.element.addAttribute("disabled", "true");
	},
	
	enable: function()
	{
		this.element.removeAttribute("disabled");
	},
		
	on: function(event, fct, scope)
	{
		this.element.on(event,fct,scope);
	}
};

function in_array(tab, p_val) {
    for(var i = 0, l = tab.length; i < l; i++) {
        if(tab[i] == p_val) {
            rowid = i;
            return true;
        }
    }
    return false;
}
