var editing = false;

/* Feedback system */

function initEdit ()
{
	var initMsg = "You may make changes to this information and submit the changes for approval. Pressing \"OK\" will initiate editing mode.";
	
	if (window && window.confirm && !window.confirm(initMsg))
	{
		return false;
	}
	
	var mButton1 = document.createElement("input");
	mButton1.setAttribute("type", "button"); // (setAttribute for KHTML)
	mButton1.value = "Submit changes";
	mButton1.className = "submitchanges";
	addEvent(mButton1, "click", submitChanges); // (Presto, Trident)
	var mDiv = document.getElementsByTagName("div").item(0);
	mDiv.parentNode.insertBefore(mButton1, mDiv);
	
	var mButton2 = document.createElement("input");
	mButton2.setAttribute("type", "button"); // (setAttribute for KHTML)
	mButton2.value = "Submit changes";
	mButton2.className = "submitchanges";
	addEvent(mButton2, "click", submitChanges); // (Presto, Trident)
	var lHr = document.getElementsByTagName("hr");
	var mHr = lHr.item(lHr.length - 1);
	mHr.parentNode.insertBefore(mButton2, mHr);
	
	editing = true;
	
	if (window && window.alert)
	{
		window.alert("Editing mode has been initiated.\n\nClick on a value to bring up two input fields. The upper input field is the support rating and the lower input field is your explanation for the change.\n\nWhen you're finished, be sure to click the \"Submit changes\" button at the beginning or end of the page.");
	}
	
	return true;
}

function vC (obj) // Value clicked
{
	if ((editing || initEdit()) && obj.getElementsByTagName("input").length == 0)
	{
		obj.className += " editing";
		
		var mInput = document.createElement("input");
		mInput.type = "text";
		mInput.className = "infoedit";
		mInput.value = getTextContent(obj); // (KHTML, Presto, Trident)
		mInput.name = obj.parentNode.parentNode.id+"-"+getTextContent(obj.parentNode.getElementsByTagName("th").item(0)).split(/[^A-Za-z0-9_\.]/).join("_")+":"+obj.parentNode.parentNode.parentNode.getElementsByTagName("thead").item(0).getElementsByTagName("th").item(getCellIndex(obj)).id.split("-")[2]; // getCellIndex: (KHTML)
		for (var i=obj.childNodes.length-1; i >= 0; i--)
		{
			obj.removeChild(obj.childNodes.item(i));
		}
		obj.appendChild(mInput);
		
		var mTextArea = document.createElement("textarea");
		mTextArea.value = obj.title != "" ? obj.title : (obj.id != "" && titles[obj.id.split("-")[1]].length > 0) ? titles[obj.id.split("-")[1]].join("; ") : "Add the details / explanation here";
		if (mTextArea.childNodes.length == 0)
		{
			mTextArea.appendChild(document.createTextNode(mTextArea.value)); // (KHTML)
		}
		obj.appendChild(mTextArea);
		
		obj.title = "";
		
		removeEvent(obj, "mousemove", bubblePosition);
		removeEvent(obj, "mouseout", bubbleOff);
		removeEvent(obj, "mouseover", bubbleOn);
		
		bubbleOff();
		
		mInput.focus();
	}
}

function submitChanges ()
{
	var mForm = document.createElement("form");
	mForm.action = "/browser-info-feedback";
	mForm.method = "post";
	
	var lEdits = document.getElementsByTagName("input");
	for (var i=0; i < lEdits.length; i++)
	{
		var mEdit = lEdits.item(i);
		if (mEdit.className == "infoedit")
		{
			var mInput = document.createElement("input");
			mInput.type = "hidden";
			mInput.name = mEdit.name;
			mInput.value = mEdit.value+";"+mEdit.nextSibling.value;
			mForm.appendChild(mInput);
		}
	}
	
	if (window && window.prompt)
	{
		var email = window.prompt("If you wish to allow the maintainer to contact you regarding your submission, please leave your e-mail address here.\n\nThis address will not be used for anything beyond what is needed to follow through with your request.", "");
		if (email == null)
		{
			if (window.alert)
			{
				window.alert("You have cancelled your submission request.\n\nIf this was a mistake, please click the \"Submit changes\" button again.");
			}
			return;
		}
		else if (email != "")
		{
			var mInput = document.createElement("input");
			mInput.type = "hidden";
			mInput.name = "email";
			mInput.value = email;
			mForm.appendChild(mInput);
		}
	}
	
	document.body.appendChild(mForm);
	mForm.submit();
	document.body.removeChild(mForm); // Prevent problems with the back button
}

/* Stock functions */

function addEvent (obj, e, f)
{
	if (obj.addEventListener) // Standard method
	{
		obj.addEventListener(e, f, false);
	}
	else if (obj.attachEvent) // Trident
	{
		obj.attachEvent("on"+e, f);
	}
}

function removeEvent (obj, e, f)
{
	if (obj.removeEventListener)
	{
		obj.removeEventListener(e, f, false);
	}
	else if (obj.detachEvent)
	{
		obj.detachEvent("on"+e, f);
	}
}

function getTextContent (obj)
{
	var content = "";
	if (obj.textContent) // Standard method
	{
		content = obj.textContent;
	}
	else // Fetch the text manually (KHTML, Presto, Trident)
	{
		var lChildren = obj.childNodes;
		for (var i=0; i < lChildren.length; i++)
		{
			var mChild = lChildren.item(i);
			if (mChild.hasChildNodes())
			{
				content += getTextContent(mChild);
			}
			else
			{
				content += mChild.data;
			}
		}
	}
	
	return content;
}

function getCellIndex (obj)
{
	if (obj.cellIndex != 0) // Standard attribute
	{
		return obj.cellIndex;
	}
	else // Determine manually (KHTML)
	{
		var cellIndex = 0;
		while (obj.previousSibling != undefined)
		{
			obj = obj.previousSibling;
			cellIndex++;
		}
		return cellIndex;
	}
}
