/**
 * This file offers basic helper functions
 */

/*
 * get all input-elements whose id starts with <i>prefix</i>
 */
function getInputElemsStartingWithId(prefix) {
	return getElemsStartingWithId('input', prefix);
}

function getSelCheckBoxesStartingWith(prefix) {
	allChbxs = getInputElemsStartingWithId(prefix);
	ids = new Array();
	idx = 0;
	for (i = 0; i < allChbxs.length; i++) {
		if (allChbxs[i].checked) {
			id = allChbxs[i].id;
			ids[idx++] = id;
		}
	}
	return ids;
}

function getElemsStartingWithId(type, prefix) {
	inputEls = document.getElementsByTagName(type);
	allIds = new Array();
	inI = 0;
	for (i = 0; i < inputEls.length; i++) {
		idStr = inputEls.item(i).id;
		if (startsWith(idStr, prefix)) {
			allIds[inI++] = inputEls.item(i);
		}
	}
	return allIds;
}

/*
 * Check if a certain string starts with another string return: Boolean
 */
function startsWith(str, startStr) {
	if (str.length < startStr.length) {
		return false;
	}
	strFirstPart = str.substring(0, startStr.length);
	return (strFirstPart == startStr);
}

/*
 * Check if a certain string is a number Parameters: numberStr: The string to be
 * checked if is a number
 */
function isNumber(numberStr) {
	d = numberStr * 1;
	return (!isNaN(d));
}

/**
 * Check if a string starts with another string
 */
function startsWith(baseStr, lookupStr) {
	return (baseStr.indexOf(lookupStr) == 0);
}

/*
 * Parameters: radioBoxes: A group of radio boxes Return: + true: if one of
 * these radio boxes is checked + false: otherwise
 */
function isOneRadioBoxOn(radioBoxes) {
	for (i = 0; i < radioBoxes.length; i = i + 1) {
		if (radioBoxes[i].checked) {
			return true;
		}
	}
	return false;
}

/*
 * Some handy String-trim-functions from the selfhtml-forum Extends available
 * methods for class string
 * http://forum.de.selfhtml.org/archiv/2005/4/t106433/#m659975
 */

String.prototype.leftTrim = function() {
	return (this.replace(/^\s+/, ""));
};
String.prototype.rightTrim = function() {
	return (this.replace(/\s+$/, ""));
};
// kombiniert "leftTrim" und "rightTrim";
String.prototype.basicTrim = function() {
	return (this.replace(/\s+$/, "").replace(/^\s+/, ""));
};
// dampft leerzeichen(-sequenzen) innerhalb einer zeichenkette auf ein einzelnes
// "space" ein;
String.prototype.superTrim = function() {
	return (this.replace(/\s+/g, " ").replace(/\s+$/, "").replace(/^\s+/, ""));
};

// zugabe: entfernt alle leerzeichen aus einer zeichenkette;
String.prototype.removeWhiteSpaces = function() {
	return (this.replace(/\s+/g, ""));
};

/**
 * If checked is true, the element's class value is set to ifCheckedStyle, else
 * to ifUncheckedStyle.
 * 
 * @param checked
 * @param elementId
 * @param ifUncheckedStyle
 * @param ifCheckedStyle
 * @return
 */
function adjustVisibility(checked, elementId, ifUncheckedStyle, ifCheckedStyle) {
	if (checked) {
		document.getElementById(elementId)
				.setAttribute("class", ifCheckedStyle);
	} else {
		document.getElementById(elementId).setAttribute("class",
				ifUncheckedStyle);
	}
}

/**
 * Check if the number of checked inputs does not exceed the max allowed number.
 * This works for the CLSD_M normal.
 * 
 * @param containingElementId
 * @param type
 * @param maxSelected
 * @return
 */
function checkMaxSelected(containingElementId, type, maxSelected) {

	var count = 0;
	var elements = document.getElementById(containingElementId)
			.getElementsByTagName("input");
	for ( var i in elements) {
		if (elements[i].type == type && elements[i].checked) {
			if (++count > maxSelected) {
				return false;
			}
		}
	}
	return true;
}

/**
 * Check if the number of checked inputs does not exceed the max allowed number.
 * This works for the CLSD_M flipped.
 * 
 * @param colIndex
 *            index of column where box was checked
 * @param tableId
 *            ID of table that holds all the inputs
 * @param type
 *            The type of input, usually a checkbox, because others don't allow
 *            multiple entry by nature.
 * @param maxSelected
 *            How many picks the user may pick at the most.
 * @return
 */
function checkMaxSelectedFlip(colIndex, tableId, type, maxSelected) {

	var count = 0;
	var table = document.getElementById(tableId);
	var tds = table.getElementsByTagName("td");
	for ( var i in tds) {
		var td = tds[i];
		if (td.cellIndex == colIndex) {
			var inputs = td.getElementsByTagName("input");
			if (inputs.length > 0) {
				var input = inputs[0];
				if (input.type == type && input.checked) {
					if (++count > maxSelected) {
						return false;
					}
				}
			}
		}
	}
	return true;
}

function toggleAskReasonForClosedQ(table, checkedId, selectedState,
		unselectedState) {
	var rows = table.getElementsByTagName("tr");
	if (rows != null) {
		for (r in rows) {
			if (r < rows.length) { // should be that way, but somehow a bug
				// enforces this statement
				cols = rows[r].getElementsByTagName("td");
				input = cols[0].getElementsByTagName("input")[0];
				var id = input.id;
				var firstCol = cols[1];
				if (firstCol != null) {
					var spans = cols[1].getElementsByTagName("span");
					if (spans.length > 1) {
						var reasonStuff = spans[1];
						if (reasonStuff != null) {
							if (input.type == "radio") {
								if (id == checkedId)
									reasonStuff.className = selectedState;
								else {
									reasonStuff.className = unselectedState;
									if (reasonStuff
											.getElementsByTagName("input").length > 0) {
										reasonStuff
												.getElementsByTagName("input")[0].value = "";
									}
								}
							}
							if (input.type == "checkbox") {
								if (!input.checked) {
									reasonStuff.className = unselectedState;
									reasonStuff.getElementsByTagName("input")[0].value = "";
								} else {
									reasonStuff.className = selectedState;
								}
							}
						}
					}
				}
			}
		}
	}
}