Event.addBehavior({
  '#facts table td a.edit' : function(e){this.observe('click', function(event){Event.stop(event);factsEdit(this,Event.pointerX(event),Event.pointerY(event));},false);},
	'#facts-edit' : function(e){this.observe('submit',function(event){Event.stop(event);validateFactsEdit(this)},false);}
});




function factsEdit(element,x,y)
{
	var firstElementClass = element.ancestors()[2].className;
	if(firstElementClass.indexOf(" ")>0){firstElementClass=firstElementClass.replace(/ /g, ",");}
	
	var fetchList = new Array();
	var group=firstElementClass;//DEBUG//alert(group);
	//DEBUG//alert(group.split(",").length)
	for(g=0;g<group.split(",").length;g++)
	{
		switch(group.split(",")[g])
		{
			case "something": fetchList=["dob_row","action_row","button_row"];break;
			case "otherthing": fetchList.push("this_row","that_row");break;
			case "group_appearance": fetchList.push("this_row","that_row");break;
			case "group_personality": fetchList.push("this_row","that_row");break;
			case "group_background": fetchList.push("this_row","that_row");break;
			default: fetchList.push(group.split(",")[g]+"_row");
		}
	}
	fetchList.push("button_row","action_row");
	
  //DEBUG//alert(fetchList);
	var content = document.createElement('div');
	url=element.readAttribute("href");
	if(url.indexOf("?")>-1){url+="&content_type=xml&ajax=yes&group="+group;}else{url+="?content_type=xml&ajax=yes&group="+group;}
	//DEBUG//alert(url)
	new Ajax.Request(url,
	{
		method:'get',
		/*onFailure:function(transport){ajaxCursorEnd();},*/
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport)
		{
			//DEBUG//alert(transport.responseText);
			FormRows = transport.responseXML.getElementsByTagName("form")[1].getElementsByTagName("div");//Will have to change so it actually gets the right node and not just the first form
			//DEBUG//alert(FormRows.length);alert(fetchList.length);
			for(i=0;i<fetchList.length;i++)
			{
				for(j=0;j<FormRows.length;j++)
				{
					if(FormRows[j].getAttribute("id"))
					{
						if(fetchList[i]==FormRows[j].getAttribute("id"))
						{
							try{content.appendChild(FormRows[j]);}
						  catch(e){content.innerHTML+=serializeNode(FormRows[j]);}//if(Prototype.Browser.IE){content.innerHTML+=serializeNode(FormRows[j]);}else{content.appendChild(FormRows[j]);}
						}
					}
				}
			}
			//DEBUG//alert(i+":"+j+":"+k);
			var popupSpecs = {'name':'facts-edit','css':'box-facts-edit','action':'/action/editfacts.html?group='+group,'top':y-120,'left':x-120,'backdrop':'no','closeButton':'yes'};
			createPopup(content,popupSpecs);
		}
	});
}

function validateFactsEdit(factsEditForm){sendFactsEditForm(factsEditForm);}

function sendFactsEditForm(factsEditForm)
{
	url=factsEditForm.readAttribute("action");//"/ajax/response_editprofile.xml";//editForm.attributes.action.nodeValue;
	pars=factsEditForm.serialize();
	new Ajax.Request(url,{method:'post',postBody:pars,onCreate:function(){ajaxCursorStart();},onComplete:function(){ajaxCursorEnd();},onSuccess:function(transport){$("popupLayer").remove();refreshFacts();}});
	return false;
}

function refreshFacts()
{
	var url=window.location;
	url+="";
	if(url.indexOf("?")>-1){url+="&content_type=xml"}else{url+="?content_type=xml"}
	new Ajax.Request(url,{method:'post',onSuccess:function(transport){refreshById(transport.responseXML,"div","userdata")}});
}