
/* Show-Hide objects */

function show(objectId) {
	document.getElementById(objectId).style.display = "";
}

function hide(objectId) {
	document.getElementById(objectId).style.display = "none";
}

function showHide(objectId) {
	var object = document.getElementById(objectId);
	object.style.display = (object.style.display == 'none') ? "" : "none";
}

function isHidden(objectId) {
	return (document.getElementById(objectId).style.display == "none");
}

/* Set focus */

function setFocus(objectId) {
	document.getElementById(objectId).focus();
}

/* Check checkboxes */

function check(checkBox) {
	if(typeof checkBox.length == 'number') {
		for(i = 0; i < checkBox.length; i++) {
			checkBox[i].checked = true; 
		}
	} else {
		checkBox.checked = true;
	}
}

/* Uncheck checkboxes */

function unCheck(checkBox) {
	if(typeof checkBox.length == 'number') {
		for(i = 0; i < checkBox.length; i++) {
			checkBox[i].checked = false; 
		}
	} else {
		checkBox.checked = false;
	}
}

/* Count checked checkboxes */

function countChecked(checkBox) {
	var checked = 0;
	if(typeof checkBox.length == 'number') {
		for(i = 0; i < checkBox.length; i++) {
			if(checkBox[i].checked) {
				checked++;
			}
		}
	} else {
		if(checkBox.checked) {
			checked++;
		}
	}
	return checked;
}

/* Convert string to number */

function stringToNumber(string, precision, decimalSeparator) {
	if(decimalSeparator != null) {
		string = string.replace(decimalSeparator, ".");
	}
	var number = parseFloat(string);
	if(!isNaN(number)) {
		if(precision > 0) {
			number = (Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision));
		} else {
			number = Math.round(number);
		}
		return number;
	}
	return null;
}

/* Round number */

function roundNumber(number, precision) {
	if(!isNaN(number)) {
		if(precision > 0) {
			number = (Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision));
		} else {
			number = Math.round(number);
		}
		return number;
	}
	return null;
}

/* Convert number to string */

function numberToString(number, precision, decimalSeparator) {
	if(!isNaN(number)) {
		number = roundNumber(number, precision);
		if(number != null) {
			if(precision > 0) {
				var string = number.toString();
				var commaPosition = string.lastIndexOf(".");
				if(commaPosition > -1) {
					var intPart = string.slice(0, commaPosition);
					var decimalPart = string.slice(commaPosition + 1);
					for(i = decimalPart.length; i < precision; i++) {
						decimalPart += "0";
					}
					string = intPart + "." + decimalPart;
				} else {
					string += ".";
					for(i = 0; i < precision; i++) {
						string += "0";
					}
				}
				if(decimalSeparator != null) {
					string = string.replace(".", decimalSeparator);
				}
			} else {
				string = number.toString();
			}
			return string;
		}
	}
	return null;
}

/* Get float from string */

function getFloat(stringValue, decimalSeparator, precision) {
	if(decimalSeparator != null) {
		stringValue = stringValue.replace(decimalSeparator, ".");
	}
	var floatValue = parseFloat(stringValue);
	if(!isNaN(floatValue)) {
		floatValue = (Math.round(floatValue * Math.pow(10, precision)) / Math.pow(10, precision));
		return floatValue;
	}
	return null;
}

/* Get int from string */

function getInt(stringValue) {
	var intValue = parseInt(stringValue);
	if(!isNaN(intValue)) {
		return intValue;
	}
	return null;
}

/* Check number field */

function checkNumberField(field, precision, decimalSeparator, minValue, maxValue, defaultValue) {
	var string = field.value;
	var number = stringToNumber(string, precision, decimalSeparator);
	if((number != null) && ((minValue == null) || (number >= minValue)) && ((maxValue == null) || (number <= maxValue))) {
		string = numberToString(number, precision, decimalSeparator);
	} else {
		if(defaultValue != null) {
			string = numberToString(defaultValue, precision, decimalSeparator);
		} else {
			string = new String("");
		}
	}
	field.value = string;
}
