/**
 * This file has functions that allow the EditableGrid to work.
 */
var editGridFocussedField;
var editGridFocussedRow;
var editGridRowRefreshed = new Object();

function gotoEditGrid() {
	var newUrl = location.href;
	if (newUrl.indexOf("edit=true") != -1)
		return;
	
	var pos = newUrl.indexOf("#");
	if (pos != -1)
		newUrl = newUrl.substring(0, pos);
	
	if (newUrl.indexOf("?") == -1)
		newUrl += "?";
	else
		newUrl += "&";
	navigate(newUrl + "edit=true");
}

function showEditGridInputs(row, gridCell, showLabel) {
	if (!row) return;
	if (gridCell)
	{
		var id = getEnclosingElem(gridCell, "div").id.replace(/^.*_(\d+)$/, "$1");
		if (!editGridRowRefreshed[id + "_" + row]) {
			if (eval("window.gridEditRowUpdate_" + id))
				eval("window.gridEditRowUpdate_" + id)(row);
			else
				gridEditRowUpdate(row);
			editGridRowRefreshed[id + "_" + row] = true;
		}
	}
	
	var labels = getElementsByClass("labelgroup_" + row, null, "div");
	var fields = getElementsByClass("fieldgroup_" + row, null, "div");
	
	var labelStyle = "none";
	var fieldStyle = "block";
	if (showLabel) {
		labelStyle = "block";
		fieldStyle = "none";
	}
	
	for (var i = 0; i < labels.length; i++) {
		labels[i].style.display = labelStyle;
		fields[i].style.display = fieldStyle;
	}
	
	if (gridCell) {
		var elem = getInputFromParent(gridCell/*.parentNode*/);
		if (elem && editGridFocussedRow != row) {
			try {
				if (elem.focus)
					elem.focus();
				if (elem.select)
					elem.select();
			} catch (e) {}
		}
	}
}

function gridMoveCell(event, field) {
	if (getKey(event) == 38 || getKey(event) == 40) { // Up or Down
		var id = field.id;
		var fieldId = id.replace(/_\d+(_display)?$/, "");
		var row = id.replace(/formfield_\d+_(\d+)(_display)?/, "$1");
		field.onblur();
		if (getKey(event) == 38) 	// Up
			row--;
		else
			row++;					// Down

		var newField = document.getElementById(fieldId + "_" + row);
		if (newField == null)
			return false;

		var fieldDiv = newField.parentNode;
		// Get fieldgroup div
		while (!/^fieldgroup/.test(fieldDiv.className))
			fieldDiv = fieldDiv.parentNode;
		
		if (findPreviousHTMLNode(fieldDiv.previousSibling).parentNode.onclick)
			findPreviousHTMLNode(fieldDiv.previousSibling).parentNode.onclick();
		
		return false;
	}
	return true;
}

/**
 * Selects the first input element withing the parent node.
 * @param parentElement
 * @return
 */
function getInputFromParent(parentElement) {
	if (parentElement == null) return false;

	var elem = parentElement.firstChild;
	while (elem != null)
	{
		var isInput;
		try {
			isInput = elem && elem.tagName && 
			           ((elem.tagName.toLowerCase() == "input" && elem.type != "hidden")
						|| elem.tagName.toLowerCase() == "select"
						|| elem.tagName.toLowerCase() == "textarea");
		} catch (e) {
			isInput = false;
		}
		if (isInput)
		{
			return elem;
		}
		else 
		{
			var e = getInputFromParent(elem);
			if (e)
				return e;
		}
		elem = elem.nextSibling;
	}
	return false;
}

/**
 * Calls the doHideGridEditRow method with a slight delay to make sure that the next input field can get focus.
 * @param row The row which should he hidden.
 */
function hideGridEditRow(row) {
	setTimeout("doHideGridEditRow("+row+")", 50);
}
/**
 * Checks if the row has changes or is still focussed. If the row is not changed and doesn't have focus it will be hidden again.
 * @param row The row which might get hidden.
 */
function doHideGridEditRow(row) {
	var fields = getElementsByClass("fieldgroup_" + row, null, "div");
	
	var isChanged = false;
	var isHidden = true;
	
	for (var i = 0; i < fields.length; i++) {
		var theField = getInputFromParent(fields[i]);
		
		// The row is still focussed.
		if (theField == editGridFocussedField)
		{
			isHidden = false;
		}
		
		// A field has been changed.
		if (theField.defaultValue != undefined)
		{
			if (theField.value != theField.defaultValue)
			{
				isHidden = false;
				isChanged = true;
				break;
			}
		}
	}
	
	// The field can't be found the first click.
	var changedField = document.getElementById("rowChanged_" + row);
	if (changedField != null)
		changedField.value = isChanged ? "1" : "0";

	if (isHidden)
		showEditGridInputs(row, null, true);
}