var initImages = 
	new Class(
	{
		initialize:
			function()
			{
				path = _static_img + "/images/fields/";
				ext  = (window.ie6 ? "gif" : "png");
		
				this.images = new Array(new Array(), new Array());
		
				for (var i = 0; i < 4; i ++)
				{
					this.images[0][i] = new Image();
					this.images[1][i] = new Image();
				}
				
				this.images[0][0].src = path + "bt_in_submit." + ext;
				this.images[0][1].src = path + "bt_in_submit_on." + ext;
		
				this.images[1][0].src = path + "bt_in_submit_bordered." + ext;
				this.images[1][1].src = path + "bt_in_submit_on_bordered." + ext;
		
				if (_site == "be" && _page == "index.php")
				{
					path = _static_img + "/images/search/";
		
					this.mapImages = new Array();
				}
				
				if (_site == "fr" && _page == "resultat.php")
				{
					
					path = _static_img + "/images/common/";
		
					this.images = new Array();
					this.images[0] = new Image();
					this.images[1] = new Image();
					this.images[2] = new Image();
		
					this.images[0].src = path + "trie_off.gif";
					this.images[1].src = path + "trie_asc.gif";
					this.images[2].src = path + "trie_desc.gif";
				}
			}
	}
);

// forms
var agForm =
	new Class(
	{
		initialize:
			function(item, index, FormImages, withoutWrapping)
			{
				this.id = index;
				this.form = $(item);
				
				this.fieldsSelect = new Array();
				this.fieldsText = new Array();
				this.fieldsTextarea = new Array();
				this.linkgosearch = new Array();
				this.withoutWrapping = withoutWrapping;

				this.preload(FormImages);

				items = this.form.getElementsBySelector("input[type=text], input[type=password]");

				for (var i = 0; i < items.length; i ++)
				{ 
					this.fieldsText.push(new agText(this, items[i], i, this.freeT9, this.images, this.withoutWrapping));
				}

				items = this.form.getElements("textarea");

				for (var i = 0; i < items.length; i ++)
				{ 
					this.fieldsTextarea.push(new agTextarea(this, items[i], i, this.withoutWrapping));
				}

				items = this.form.getElements("select");

				for (var i = 0; i < items.length; i ++)
				{ 
					this.fieldsSelect.push(new agSelect(this, items[i], i, this.withoutWrapping));
				}
				
				items = this.form.getElementsBySelector("a[id=label_go_search]");
				for (var i = 0; i < items.length; i ++)
				{ 
					this.linkgosearch.push(new agLinkGo(this, items[i], i, this.withoutWrapping));
				}
				
				this.link = {};

				this.mapSearchable = false;
			},

		preload:
			function(FormImages)
			{
				this.images = FormImages.images;
				this.mapImages = FormImages.mapImages;
			},

		hasSelectOpen:
			function()
			{
				for (var i = 0; i < this.fieldsSelect.length; i ++)
				{
					if (this.fieldsSelect[i].isOpen() == true)
					{
						return i;
					}
				}

				return -1;
			},

		displaceSelect:
			function(index)
			{
				this.fieldsSelect[index].displaceList();
			},

		closeSelect:
			function(index)
			{
				this.fieldsSelect[index].hideList(true);
			},

		hasSuggesterOpen:
			function()
			{
				for (var i = 0; i < this.fieldsText.length; i ++)
				{
					if (this.fieldsText[i].isOpen() == true)
					{
						return i;
					}
				}

				return -1;
			},

		displaceSuggester:
			function(index)
			{
				this.fieldsText[index].displaceList();
			},
		
		closeSuggester:
			function(index)
			{
				this.fieldsText[index].hideList();
			},

		submit:
			function()
			{
				this.form.submit();
			},

		enableMapSearch:
			function()
			{
				if (this.mapSearchable == false)
				{
					/*$("map_go_search").setProperty("src", this.mapImages[0].src);

					// be homepage -> call of map_search() function
					this.link = new Element("a", { "id" : "map_link_search", "class" : "rollOver", "href" : "javascript:map_search()" }).injectBefore($("map_go_search"));

					this.link.adopt($("map_go_search"));

					new agLink(this.link, agLinks.length);

					this.mapSearchable = true;*/
				}
			},

		disableMapSearch:
			function()
			{
				if (this.mapSearchable == true)
				{
					this.link = $("map_link_search");

					img = $("map_go_search").clone();

					img.setProperty("src", this.mapImages[2].src);
					img.injectBefore(this.link);

					this.link.remove();
					
					this.mapSearchable = false;
				}
			}
	});

//Pour la gestion du focus sur le link GO SEARCH
var agLinkGo =
	new Class(
	{
		initialize:
			function(form, item, index)
			{
				item.addEvent("focus", function() { closeSelect(); closeSuggester();}.bind(this));
			}
	}
);

// text and password fields
var agText =
	new Class(
	{
		initialize:
			function(form, item, index, freeT9, images, withoutWrapping)
			{
				this.images = images;
				this.withoutWrapping = withoutWrapping;
				
				parameters	= item.getProperty("class").split("/");
				if (parameters[0] == "skip")
				{
					return;
				}
				
				if(item.hasClass('agTextStatic'))
				{
					if(parameters[3])
					{
						var par3 = parameters[3].split(' ');
						parameters[3] = par3[0];
					}
					else
					{
						parameters[3] = '';
					}
				}

				if(item.hasClass('autocomplete'))
				{
					parameters[4] = 1;
					parameters[3] = 'DEFAULT_LOCALITY';
				}
				if(isNaN(parameters[0].toInt()))
				{
					parameters[0] = item.getStyle('width').toInt();
					parameters[5] = parameters[0];
				}

				this.id				= index;
				this.parent			= form;
				this.item			= item;
				this.freeT9 		= 0;

				this.width			= parameters[0].toInt();
				this.bordered		= (parameters[1] == 1 ? true : false);
				this.button			= (parameters[2] == 1 ? true : false);
				this.defaut			= parameters[3];
				this.hasSuggester	= (parameters[4] >= 1 ? true : false);
				this.freeT9			= (parameters[4] == 2 ? 1 : 0);  //T9 ouvert
				this.widthSuggester = parameters[5];
				this.decalSuggester = parameters[6];
				//this.decalSuggester = parameters[6];
				this.SubmitOnSelect = (parameters[7] == 1 ? "true" : "false");
				//this.item.value = this.SubmitOnSelect;
				this.cntlocname		= 0;
				this.transform();
					
			},

		transform:
			function()
			{
					
				this.item.removeProperty("style");
	
				this.item.setStyle("width", (this.width - 8 - 8 - (this.button ? 35 : 0)) + "px");
				
				if(!this.withoutWrapping)
				{
					this.item.removeProperty("class");
					
					this.item.addClass("agText" + (this.bordered ? "Bordered" : ""));
					this.item.setStyle("paddingBottom", 4 + "px");
					
					var classcl = "agTextCorner agTextCornerLeft" + (this.bordered ? "Border" : "");
					var classcr = "agTextCorner agTextCornerRight" + (this.bordered ? "Border" : "");
					
					var cl = new Element("div", { "class" : classcl }).injectBefore(this.item);
					var cr = new Element("div", { "class" : classcr }).injectAfter(this.item);
				}
				else
				{
					var parent = this.item.parentNode;
					var cl = $(parent).getElement('.agTextCornerLeft');
					var cr = $(parent).getElement('.agTextCornerRight');
				}
				
				if (this.button)
				{
					link = new Element("a");
					image = new Element("img", { "id" : this.item.id + "_img_" + this.id, "src" : this.images[(this.bordered ? 1 : 0)][0].src, "class" : "agTextButton" });

					image.addEvent("mouseover", function() { this.switchImage(1); }.bind(this));
					image.addEvent("mouseout", function() { this.switchImage(0); }.bind(this));

					link.addEvent("click", function() { this.parent.submit(); }.bind(this));
					link.adopt(image).injectBefore(cr);
				}

				if (this.hasSuggester)
				{
					id = this.item.getProperty("id");
					//console.log('id = ' + id);
					id = id.substr(id.length - 1, 1);
					id = isNaN(id) ? "" : id;

					name 		= this.item.getProperty("id");
					tab_name 	= name.split("_");
					//alert("NAME: "+tab_name[0]);
					
					if (tab_name[0]!="loc")
					{
						//alert("NAME: "+tab_name[0]);
						this.sugg_hidn = $(tab_name[0]+"_key" + id);
						//alert("ID : "+this.sugg_hidn.id);
					}
					else
					{
						//console.log($("loc_key" + id));
						this.sugg_hidn = $("loc_key" + id);
						this.cntlocname = this.cntlocname+1;
					}

					if (this.widthSuggester > 0)
					{
						this.suggester = new agSuggester(this.item, this.widthSuggester, this.sugg_hidn, this.decalSuggester, this.SubmitOnSelect, this.freeT9);
					}
					else
					{
						this.suggester = new agSuggester(this.item, this.width, this.sugg_hidn, this.decalSuggester, this.SubmitOnSelect, 0);
					}

					if (this.defaut == "")
					{
						this.defaut = this.item.value;

						this.suggester.setDefault(this.sugg_hidn.value, this.item.value);
					}
				}

			
				if (this.defaut)
				{
					this.item.addEvent("click", function() { this.clearField(); }.bind(this));
					this.item.addEvent("blur", function() { this.resetField(); }.bind(this));
					//New OFO
					//this.item.addEvent('blur', function(e) { e.cancelBubble = true; closeSelect(); closeSuggester(); this.resetField()}.bind(this));
					//this.item.addEvent("focus", function() { closeSelect(); closeSuggester(); }.bind(this));
					this.item.addEvent('focus', function(e) { e.cancelBubble = true; closeSelect(); closeSuggester(); this.clearField(); }.bind(this));					
					
					/* Si mode T9 ouvert alors si texte <> "" alors bouton search = actif */
					if (this.hasSuggester && this.freeT9==1)
					{
						//this.item.addEvent("keyup", function() { this.noT9validcontent() }.bind(this));
					}
				}

				if (this.item.getProperty("id"))
				{
					if (this.item.getProperty("id").substr(0, 8) == "loc_name")
					{
						this.item.disabled = false;
					}
				}
				if (this.item.getProperty("tabIndex"))
				{
					//New OFO
					this.item.tabIndex= this.item.getProperty("tabIndex");
					
				}				
				
			},
		
		noT9validcontent:
			function ()
			{
				/* En mode T9 ouvert on vérifi le contenu afin de pouvoir activer le bouton "Search" */
				//alert("Valid content:"+this.item.value);
				if (this.hasSuggester){
					if (this.item.value.trim() != "" && this.item.value.trim() != LABEL[this.defaut])
					{
						this.suggester.enableSearch();
					}
					else
					{
						this.suggester.disableSearch();
					} 
				}
			},
			
		clearField:
			function()
			{
				//$('menu').setHTML("clear");
				//console.log(LABEL[this.defaut]);
				//console.log(this.item.value.trim());
				if ((this.hasSuggester || this.item.value.trim() == LABEL[this.defaut]))
				{
					if (this.freeT9==1)
					{
						if (this.item.value.trim() == LABEL[this.defaut])
						{
							this.item.value = "";
						}
					}
					else
					{
						this.item.value = "";
					}
				}
			},

		resetField:
			function()
			{
				
				currnt = this.item.value.trim();

				if (this.hasSuggester)
				{
					this.suggester.cancel();
					
					if (this.freeT9==0)
					{
						defaut = this.suggester.getValue();
	
						if (defaut == "")
						{
							defaut = LABEL[this.defaut];			
						}
	
						if (currnt == "" || currnt != defaut)
						{
						//	$('menu').setHTML("reset 1-2 : "+currnt);
							this.item.value = defaut;					
						}
					}
					else
					{
						/* T9 Ouvert */
						if (currnt == "")
						{
							defaut = LABEL[this.defaut];
	
							this.item.value = defaut;
						}					
					}
				}
				else
				{
					if (currnt == "")
					{
						defaut = LABEL[this.defaut];

						this.item.value = defaut;
					}
				}
			},

		switchImage:
			function(mode)
			{
				if (mode == 1)
				{
					$(this.item.id + "_img_" + this.id).setProperty("src", this.images[(this.bordered ? 1 : 0)][1].src);
				}
				else
				{
					$(this.item.id + "_img_" + this.id).setProperty("src", this.images[(this.bordered ? 1 : 0)][0].src);
				}
			},

		hideList:
			function()
			{
				this.suggester.clearResults();
			},

		displaceList:
			function()
			{
				
			},

		isOpen:
			function()
			{
				if (this.hasSuggester)
				{
					return this.suggester.isOpen();
				}
			}
	});

// textarea fields
var agTextarea = 
	new Class(
	{
		initialize:
			function(form, item, index, withoutWrapping)
			{
				parameters	= item.getProperty("class").split("/");
				this.withoutWrapping = withoutWrapping;
				
				if (parameters[0] == "skip")
				{
					return;
				}

				if(parameters[0] === undefined || isNaN(parameters[0].toInt()))
				{
					parameters[0] = item.getStyle('height').toInt();
				}
				if(parameters[1] === undefined || isNaN(parameters[1].toInt()))
				{
					parameters[1] = item.getStyle('width').toInt();
				}
				
				this.id			= index;
				this.parent		= form;
				this.item		= item;
				this.height		= parameters[0].toInt();
				this.width		= parameters[1].toInt();
				this.bordered	= (parameters[2] == 1 ? true : false);
				this.defaut		= parameters[3];

				this.transform();
			},

		transform:
			function()
			{
				h = this.height;
				w = this.width;
				
				this.item.removeProperty("class");
				this.item.removeProperty("style");
				
				this.item.setStyle("height", (h - 5 - 5) + "px");
				this.item.setStyle("width", (w - 8 - 8) + "px");
				
				if(!this.withoutWrapping)
				{
					this.area = new Element("div", { "class" : "agTextarea" + (this.bordered ? "Bordered" : "") }).injectBefore(this.item); //.adopt(this.item);
					this.area.setStyle("height", h + "px");
					this.area.setStyle("width", w + "px");
				}
				else
				{
					this.area = this.item.parentNode.parentNode;
					this.area.setStyle("height", h + "px");
					this.area.setStyle("width", w + "px");
				}
				
				if (this.defaut)
				{
					this.item.addEvent("click", function() { this.clearField() }.bind(this));
					this.item.addEvent("blur", function() { this.resetField() }.bind(this));
					//New OFO
					this.item.addEvent('focus', function(e) { e.cancelBubble = true; closeSelect(); closeSuggester();}.bind(this));
										
				}

				wl = Math.ceil(w / 2);
				wr = w - wl;
				
				if(!this.withoutWrapping)
				{
					ctl = new Element("div", { "class" : "topLeft", "styles" : { "height" : 5 + "px", "width" : wl + "px" }});
					ctr = new Element("div", { "class" : "topRight", "styles" : { "height" : 5 + "px", "width" : wr + "px" }});
					cl = new Element("div", { "class" : "left", "styles" : { "height" : (h - 5 - 5) + "px", "width" : 8 + "px" }});
					cr = new Element("div", { "class" : "right", "styles" : { "height" : (h - 5 - 5) + "px", "width" : w - 8 + "px" }});
					cbl = new Element("div", { "class" : "bottomLeft", "styles" : { "height" : 5 + "px", "width" : wl + "px" }});
					cbr = new Element("div", { "class" : "bottomRight", "styles" : { "height" : 5 + "px", "width" : wr + "px" }});
	
					cr.adopt(this.item);
	
					this.area.adopt(ctl);
					this.area.adopt(ctr);
					this.area.adopt(cl);
					this.area.adopt(cr);
					this.area.adopt(cbl);
					this.area.adopt(cbr);
				}
				else
				{
					ctl = this.area.getElement('.topLeft');
					ctl.setStyles({ "height" : 5 + "px", "width" : wl + "px" });
					ctr = this.area.getElement('.topRight');
					ctr.setStyles({ "height" : 5 + "px", "width" : wr + "px" });
					cl = this.area.getElement('.left');
					cl.setStyles({ "height" : (h - 5 - 5) + "px", "width" : 8 + "px" });
					cr = this.area.getElement('.right');
					cr.setStyles({ "height" : (h - 5 - 5) + "px", "width" : w - 8 + "px" });
					cbl = this.area.getElement('.bottomLeft');
					cbl.setStyles({ "height" : 5 + "px", "width" : wl + "px" });
					cbr = this.area.getElement('.bottomRight');
					cbr.setStyles({ "height" : 5 + "px", "width" : wr + "px" });
				}
			},

		clearField:
			function()
			{
				if (this.item.value.trim() == LABEL[this.defaut])
				{
					this.item.value = "";
				}
			},

		resetField:
			function()
			{
				if (this.item.value.trim() == "")
				{
					this.item.value = LABEL[this.defaut];
				}
			}
	});

// select fields
var agSelect =
	new Class(
	{
		initialize:
			function(form, item, index, withoutWrapping)
			{
				parameters	= item.getProperty("class").split("/");
				this.withoutWrapping = withoutWrapping;
				
				if (parameters[0] == "skip")
				{
					return;
				}

				if(isNaN(parameters[0].toInt()))
				{
					parameters[0] = item.getStyle('width').toInt();
				}
				
				if(item.hasClass('addBorder'))
				{
					parameters[1] = 1;
				}
				
				this.id			= index;
				this.parent		= form;
				this.item		= item;

				this.width		= parameters[0].toInt();
				this.bordered	= (parameters[1] == 1 ? true : false);
				this.multiple	= (parameters[2] == 1 ? true : false);

				if (this.multiple)
				{
					this.def	= parameters[3];
				}
				else
				{
					this.range	= (parameters[3] == 1 || parameters[3] == 2 ? parameters[3].toInt() : 0);
					this.min	= ((parameters.length >= 5) ? parameters[4] : "");
					this.max	= ((parameters.length >= 6) ? parameters[5] : "");
					this.unit	= ((parameters.length >= 7) ? parameters[6] : "");
				}

				this.children	= item.getChildren();
				this.count		= this.children.length;
				this.height		= (this.count >= 20 ? 20 : this.count) * 18;

				this.transform();
			},

		transform:
			function()
			{
				this.buildArea();
				this.buildList();
				this.buildShadow();

				if (this.range > 0)
				{
					this.buildRange();
				}

				this.hideList();

				this.item.setStyle("display", "none");
			},

		buildArea:
			function()
			{
				this.area = new Element("div", { "class" : "agSelect" + (this.bordered ? "Bordered" : ""), "id" : "agSelect" + this.id }).injectBefore(this.item);
				this.area.setStyle("height", 21 + "px");
				this.area.setStyle("width", this.width + "px");

				this.area.adopt(new Element("div", { "class" : "left" }));

				this.label = new Element("div", { "class" : "center", "tabIndex" : 5 });
				
				//this.label.addEvent('focus', function(e) {e.cancelBubble = true; closeSelect(); closeSuggester(); this.showList()}.bind(this));
				//this.label.addEvent('blur', function(e) {this.hideList()}.bind(this));
				
				this.label.setStyle("height", 21 + "px");
				this.label.setStyle("width", (this.width - 8 - 21) + "px");

				this.area.adopt(this.label);
				this.area.adopt(new Element("div", { "class" : "right" }));

				//this.area.event.stopPropagation();
				//this.area.event.stop();

				if (window.ie)
				{
					this.area.addEvent ("click", function(e) {  e.cancelBubble = true; closeSelect(); closeSuggester(); this.showList() }.bind(this));
					this.label.addEvent("click", function(e) {  e.cancelBubble = true; closeSelect(); closeSuggester(); this.showList() }.bind(this));
					//this.label.addEvent("click", function(e) { this.label.fireEvent('focus', this.label)});

					
					//New OFO
					//this.area.addEvent('focus', function(e) { e.cancelBubble = true; $('menu').setHTML("FOCUS AREA: "+ this.id); closeSelect(this.id); closeSuggester(); this.showList()}.bind(this));
					//this.label.addEvent('focus', function(e) {  $('menu').setHTML("FOCUS LABEL 2: "+this.id); closeSelect(); closeSuggester();this.showList();this.event.stop(); }.bind(this));
					//this.label.addEvent('focus', function(e) {  $('menu').setHTML("FOCUS LABEL 2: "+this.id); closeSelect(); closeSuggester();this.showList();e.stop();  }.bind(this));
					
					//this.area.addEvent('focus', function(e) { e.cancelBubble = true; closeSelect(); closeSuggester(); this.showList()}.bind(this));
					//this.area.addEvent('blur', function(e) { e.cancelBubble = true; this.hideList()}.bind(this));
				}
				else
				{
					//New OFO
					//this.area.addEvent('focus', function(e) {e.cancelBubble = true; closeSelect(); closeSuggester(); this.showList()}.bind(this));
					this.label.addEvent('focus', function(e) {e.cancelBubble = true; closeSelect(); closeSuggester(); this.showList()}.bind(this));
					
					this.area.addEvent("click", function(e) {e.cancelBubble = true; closeSelect(); closeSuggester(); this.showList() }.bind(this));
					//New OFO
					//this.area.addEvent('blur', function(e) { e.cancelBubble = true; closeSelect(); closeSuggester(); this.hideList()}.bind(this));					
				}

				this.label.setText(this.getLabels());
			},

		buildList:
			function()
			{
				
				this.options = new Element("div", { "class" : "agOptions", "id" : "agOptions" + this.id });
				this.options.setStyle("height", this.height + "px");
				this.options.setStyle("width", (this.width - 3 - 3) + "px");
				//this.sort_orders[0] == 0;
				
				for (var i = 0; i < this.count; i ++)
				{
					link = new Element("a");

					link.setText(this.children[i].getText());
					link.setProperty("id", "option_" + this.parent.id + "_" + this.id + "_" + i);

					if (this.item.onchange)
					{
						link.setProperty("href", "javascript:agForms[" + this.parent.id + "].fieldsSelect[" + this.id + "].setLabel(" + i + "); " + this.item.onchange + (this.item.onchange.toString().indexOf("onchange") >= 0 ? "; onchange();" : "; anonymous();"));
					}
					else
					{
						link.setProperty("href", "javascript:agForms[" + this.parent.id + "].fieldsSelect[" + this.id + "].setLabel(" + i + ")");
					}

					if (this.multiple)
					{
						link.setProperty("class", "chk" + (this.children[i].selected ? "On" : ""));
					}

					this.options.adopt(new Element("p").adopt(link));
				}

				if (this.multiple)
				{
					if (window.ie)
					{
						this.options.addEvent("click", function(e) { e.cancelBubble = true });
					}
					else
					{
						this.options.addEvent("click", function(e) { e.stopPropagation() });
					}
				}

				$$("body")[0].adopt(this.options);
			},

		buildRange:
			function()
			{
				this.custom = new Element("div", { "class" : "agOptions", "id" : "agCustom" + this.id });
				this.custom.setStyle("height", (window.ie6 ? 45 : 25) + "px");
				this.custom.setStyle("width", (this.width - 3 - 3) + "px");

				min = new Element("input", { "id" : this.item.getProperty("id") + "_min_", "type" : "text", "value" : this.min });
				max = new Element("input", { "id" : this.item.getProperty("id") + "_max_", "type" : "text", "value" : this.max });

				min.setStyle("width", (this.range == 1 ? 44 : 56) + "px");
				max.setStyle("width", (this.range == 1 ? 44 : 56) + "px");

				if (window.ie)
				{
					min.addEvent("click", function(e) { e.cancelBubble = true });
					max.addEvent("click", function(e) { e.cancelBubble = true });
				}
				else
				{
					min.addEvent("click", function(e) { e.stopPropagation() });
					max.addEvent("click", function(e) { e.stopPropagation() });
				}

				s = new Element("div");
				s.setStyle("width", 1 + "px");

				a = new Element("div");
				a.setText(LABEL["TO"]);

				if (this.range == 1)
				{
					o = new Element("div", { "class" : "go" });
					o.addEvent("click", function() { this.setRange() }.bind(this));
				}

				container = new Element("p");
				container.adopt(s);
				container.adopt(min);
				container.adopt(a);
				container.adopt(max);

				if (this.range == 1)
				{
					container.adopt(o);
				}

				this.custom.adopt(new Element("p", { "class" : "separator" }));
				this.custom.adopt(container);

				$$("body")[0].adopt(this.custom);
			},

		buildShadow:
			function()
			{
				h = this.options.getStyle("height").toInt() + 3 + 5 + (this.range > 0 ? (window.ie6 ? 45 : 25) : 0);
				w = this.width + 1 + 1;

				this.shadow = new Element("div", { "class" : "agShadow", "id" : "agShadow" + this.id }).injectBefore(this.options);
				this.shadow.setStyle("height", h + "px");
				this.shadow.setStyle("width", w + "px");

				ht = Math.ceil(h / 2);
				hb = h - ht;

				wl = Math.ceil(w / 2);
				wr = w - wl;

				ctl = new Element("div", { "class" : "topLeft", "styles" : { "height" : ht, "width" : wl }});
				ctr = new Element("div", { "class" : "topRight", "styles" : { "height" : ht, "width" : wr }});
				cbl = new Element("div", { "class" : "bottomLeft", "styles" : { "height" : hb, "width" : wl }});
				cbr = new Element("div", { "class" : "bottomRight", "styles" : { "height" : hb, "width" : wr }});

				this.shadow.adopt(ctl);
				this.shadow.adopt(ctr);
				this.shadow.adopt(cbl);
				this.shadow.adopt(cbr);
			},

		showList:
			function()
			{
				this.open = true;

				this.displaceList();
			},

		hideList:
			function(requested)
			{
				this.open = false;

				this.shadow.setStyle("display", "none");
				this.options.setStyle("display", "none");

				if (this.range > 0)
				{
					this.custom.setStyle("display", "none");

					if (this.range == 2)
					{
						id = this.item.getProperty("id");

						if ($(id + "_min_").value != "" || $(id + "_max_").value != "")
						{
							this.children[0].selected = true;

							this.min = $(id + "_min_").value.replace(new RegExp("[^0-9]","g"), "");
							this.max = $(id + "_max_").value.replace(new RegExp("[^0-9]","g"), "");

							$(id + "_min").value = this.min;
							$(id + "_max").value = this.max;

							this.label.setText(this.getLabels());
						}
					}
					// if (requested)
					else
					{
						id = this.item.getProperty("id");

						$(id + "_min_").value = this.min;
						$(id + "_max_").value = this.max;
					}
				}
			},

		displaceList:
			function()
			{
				this.shadow.setStyle("top", this.area.getTop() + "px");
				this.shadow.setStyle("left", (this.area.getLeft() - 1) + "px");
				this.shadow.setStyle("display", "block");

				this.options.setStyle("top", (this.area.getTop() + 3) + "px");
				this.options.setStyle("left", (this.area.getLeft() + 3) + "px");
				this.options.setStyle("display", "block");

				if (this.range > 0)
				{
					dimensions = this.options.getCoordinates();

					this.custom.setStyle("top", (dimensions.top + dimensions.height) + "px");
					this.custom.setStyle("left", (this.area.getLeft() + 3) + "px");
					this.custom.setStyle("display", "block");
				}
			},

		getSelected:
			function()
			{
				for (var i = 0; i < this.children.length; i ++)
				{
					if (this.children[i].selected)
					{
						return i;
					}
				}
			},

		getLabels:
			function()
			{
				if (this.range > 0 && (this.min > 0 || this.max > 0) && (this.getSelected() == 0))
				{
					if (this.min > 0 && this.max > 0)
					{
						return (LABEL["FROM"] + " " + n_f(this.min) + " " + LABEL["TO"] + " " + n_f(this.max) + " " + LABEL[this.unit]);
					}
					else
					{
						if (this.min > 0)
						{
							return (LABEL["AT_LEAST"] + " " + n_f(this.min) + " " + LABEL[this.unit]);
						}
						else
						{
							return (LABEL["LESS_THAN"] + " " + n_f(this.max) + " " + LABEL[this.unit]);
						}
					}
				}
				else
				{
					var selectedCount = 0;
					var selectedOptions = new Array();

					for (var i = 0; i < this.children.length; i ++)
					{
						if (this.children[i].selected)
						{
							selectedCount = selectedCount + 1;
							selectedOptions.push(this.children[i].getText());
						}
					}

					if (this.multiple)
					{
						if (selectedCount == 0)
						{
							return LABEL[this.def];
						}
						else
						{
							var label = selectedOptions.join(", ");

							if (label.length > 15)
							{
								return label.substr(0, 15) + "... (" + selectedCount + ")";
							}
							else
							{
								return label;
							}
						}
					}
					else
					{
						return selectedOptions.join(", ");
					}
				}
			},

		setLabel:
			function(index)
			{
				if (this.range > 0)
				{
					id = this.item.getProperty("id");

					this.min = "";
					this.max = "";

					$(id + "_min_").value = "";
					$(id + "_max_").value = "";

					$(id + "_min").value = "";
					$(id + "_max").value = "";
				}

				if (this.multiple)
				{
					$("option_" + this.parent.id + "_" + this.id + "_" + index).setProperty("class", (this.children[index].selected ? "chk" : "chkOn"));

					this.children[index].selected = !(this.children[index].selected);
					this.label.setText(this.getLabels());
				}
				else
				{
					this.children[index].selected = true;
					this.label.setText(this.children[index].getText());

					this.hideList(true);
				}
			},

		setRange:
			function()
			{
				id = this.item.getProperty("id");

				$(id + "_min").value = $(id + "_min_").value;
				$(id + "_max").value = $(id + "_max_").value;

				this.item.setProperty("value", "");

				this.parent.submit();
			},

		isOpen:
			function()
			{
				return this.open;
			}
	});

// tools
function closeSelect(idx)
{
	//alert(idx);
	//$('menu').setHTML("CLOSE SELECT :"+idx);
	for (var i = 0; i < agForms.length; i ++)
	{
		index = agForms[i].hasSelectOpen();

		if (index > -1)
		{
			agForms[i].closeSelect(index);
		}
	}
};

function closeSuggester()
{
	for (var i = 0; i < agForms.length; i ++)
	{
		index = agForms[i].hasSuggesterOpen();

		if (index > -1)
		{
			agForms[i].closeSuggester(index);
		}
	}
}

function displaceSelect()
{
	for (var i = 0; i < agForms.length; i ++)
	{
		index = agForms[i].hasSelectOpen();

		if (index > -1)
		{
			agForms[i].displaceSelect(index);
		}
	}
}

function displaceSuggester()
{
	for (var i = 0; i < agForms.length; i ++)
	{
		index = agForms[i].hasSuggesterOpen();

		if (index > -1)
		{
			agForms[i].displaceSuggester(index);
		}
	}
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
