var modifierprofile = {
	nbtags: 0,
	num: 0,
	todel: "",
	interestdel: {},
	interestadd: {},

	setlocalisation: function (texte, li)
	{
		var gF = document.forms['global'];
		gF.elements['city_id'].value		= li.childNodes[3].firstChild.nodeValue;
		gF.elements['localisation'].value	= li.childNodes[0].firstChild.nodeValue 
							+ ',' + li.childNodes[1].firstChild.nodeValue 
							+ ',' + li.childNodes[2].firstChild.nodeValue;
		gF.elements['city_name'].value		= li.childNodes[0].firstChild.nodeValue;
	},

	onLeavingLocalisation: function (texte, li)
	{
		var gF = document.forms['global'];
		if(texte != gF.elements['city_name'].value)
		{
			gF.elements['city_name'].value = texte;
			gF.elements['city_id'].value = '';
		}
	},

	delinterest: function (i)
	{
		if(this.interestadd[i])
		{
			delete(this.interestadd[i]);
		}
		else
		{
			this.interestdel[i]=i;
		}
		document.forms['interests'].elements['interest'+i].setAttribute('onclick',"modifierprofile.addinterest('"+i+"')");
	},

	addinterest: function (i)
	{
		if(this.interestdel[i])
		{
			delete(this.interestdel[i]);
		}
		else
		{
			this.interestadd[i]=i;
		}
		document.forms['interests'].elements['interest'+i].setAttribute('onclick',"modifierprofile.delinterest('"+i+"')");
	},

	submitinterests: function()
	{
		var chaine="";
		for (var v in this.interestdel) {
			if(chaine!="")
				chaine += ", ";
			chaine += v;
		}
		document.forms['interests'].elements['intereststodel'].value = chaine;
		chaine="";
		for (var v in this.interestadd) {
			if(chaine!="")
				chaine += ", ";
			chaine += v;
		}
		this.interestadd = {};
		this.interestdel = {};
		document.forms['interests'].elements['intereststoadd'].value = chaine;
	//	document.forms['interests'].submit();
	},

	setnbtags: function (value)
	{
		if(value>=0)
			this.nbtags=value;
	},

	getnbtags: function ()
	{
		return this.nbtags;
	},

	del: function (i)
	{
		var ligne = $('lignetag'+i);
		ligne.hide();
		new Ajax.Request('/Member/ajaxDeleteATag/'+i,
			{
				requestHeaders:['X-Groux-Ajax', '1', 'Content-Type','application/x-www-form-urlencoded'],
				onComplete: function(req) {
					$('info').update('Un tag a été supprimé.');
					$('info').highlight();
					ligne.parentNode.removeChild(ligne);
				}
			}
		);
		this.nbtags--;
	},

	ajouttag: function ()
	{
		var tag_name = $('newtag').value;
		if((tag_name=='')||(tag_name==null)) return false;
		$('addtagfield').hide();

		var ok=true;

		$$('#tagsblock .lignetag > .nom_tag').each(function(e){
			if(e.textContent==tag_name)
			{
				ok=false;
			}
		});	

		if(ok==false)
		{
			$('info').update('Vous avez déjà ce tag.');
			$('info').highlight();
			$('newtag').setValue('');
			$('addtagfield').show();
			return false;
		}

		new Ajax.Request('/Member/ajaxAddATag/'+encodeURIComponent(tag_name),
			{
				requestHeaders:['X-Groux-Ajax', '1', 'Content-Type','application/x-www-form-urlencoded'],
				onComplete: function(req) {
					var tag_id = req.responseText;
					if(tag_id>0)
					{
						$('info').update('Un tag a été ajouté.');
						$('info').highlight();
						$('tagsblock').appendChild(
							Builder.node('div',
								{
									id:'lignetag'+tag_id,
									className:'lignetag'
								},
								[
									Builder.node('input',
										{
											type:'button',
											value:'x',
											onclick: 'modifierprofile.del(' + tag_id + ')'
										}
									),
									Builder.node('span',
										{
											className:'nom_tag'
										}
										,
										[
											tag_name
										]
									),
									Builder.node('br')
								]
							)
						);
					}
					$('newtag').setValue('');
					$('addtagfield').show();
				}
			}
		);
		return true;						
	},

	age2selectable: function ()
	{
		$('chooseage2').show();
		$('conseilage').hide();
	},

	age2selectablegroup: function ()
	{
		$('chooseage2').show();
		$('conseilage').show();
	},

	age2unselectable: function ()
	{
		$('chooseage2').hide();
		$('conseilage').hide();
	},

	changeMemberType: function (el, controllerUrl, currentValue_orientation) 
	{
		var memberType = parseInt($(el).value);

		if (memberType<3)
			this.age2unselectable();
		else
		{
			if(memberType<6)
				this.age2selectable();
			else
				this.age2selectablegroup();
		}
		
		var url = controllerUrl + memberType + '/' + currentValue_orientation;

		new Ajax.Request( url, {
			method: 'POST',
			requestHeaders: ['X-Groux-Ajax','1'],
			onSuccess: function(trans){
				var trucsAutorises = eval(trans.responseText);
				$('conteneurOrientationForm').update(trucsAutorises);
				
				$('orientation').selectedIndex = parseInt($('orientation').getAttribute('si'));
				
				//$('orientation').options[parseInt($('orientation').selectedIndex)].selected = true;
				//$('conteneurOrientationForm').update($('orientation').selectedIndex);
			}
		});
	}
};

