/*
 * ALLGEMEINE FUNTIONEN
 */

// Icon hinzufügen
function icon(icon) {
	if (icon == 'error') {
		return '<img src="server/images/icons/exclamation.png" alt="Ung&uuml;ltig!" class="icon" /> ';
	} else if (icon == 'warning') {
		return '<img src="server/images/icons/error.png" alt="Warnung!" class="icon" /> ';
	} else if (icon == 'ok') {
		return '<img src="server/images/icons/tick.png" alt="Gültig!" class="icon" /> ';
	}
}

// HTML-Elemente ein und ausblenden
function showAndHide(show,hide) {
	if (typeof show == 'object') {
		for (var i = 0; i < show.length; ++i) {
			document.getElementById(show[i]).style.display = 'block';
		}
	} else if (show.length > 0) {
		document.getElementById(show).style.display = 'block';
	}
	if (typeof hide == 'object') {
		for (var i = 0; i < hide.length; ++i) {
			document.getElementById(hide[i]).style.display = 'none';
		}
	} else if (hide.length > 0) {
		document.getElementById(hide).style.display = 'none';
	}
}

// Nicht sichtbare HTML-Elemente aus Seite entfernen
function deleteHidden(elements) {
	if (typeof elements == 'object') {
		for (var i = 0; i < elements.length; ++i) {
			if (document.getElementById(elements[i]).style.display == 'none') {
				document.getElementById(elements[i]).innerHTML = '';
			}
		}
	} else if (elements.length > 0) {
		if (document.getElementById(elements).style.display == 'none') {
			document.getElementById(elements).innerHTML = '';
		}
	}
	return true;
}
	

/*
 * KONKRETE FUNTIONEN
 */


// Sicherheits-Dialog beim Löschen
function confirmDelete (element) {
	return confirm("Möchten Sie den Datensatz '"+element+"' wirklich löschen?");
}

// Passwort
function check_password() {
	var input = document.getElementById('password').value;
	var output = document.getElementById('passwordIndicator');
	if (input.length == 0) {
		output.innerHTML = '';
	} else if (input.length > 5) {
		strength = password_strength(input)
		if (strength > 2) {
			output.innerHTML = icon('ok');
		} else {
			output.innerHTML = icon('warning');
		}
		output.innerHTML += "St&auml;rke: " + Math.min(strength*100/4,100) + " %";
	} else {
		output.innerHTML = icon('error') + "zu kurz!";
	}
}

// Passwort Stärke
function password_strength(input) {
	var strength = 0; // higher is better
	if (input.match(/[^A-Za-z0-9]/g))
		strength++;
	if (input.match(/[0-9]/g))
		strength++;
	if (input.match(/[A-Z]/g))
		strength++;
	if (input.match(/[a-z]/g))
		strength++;
	if (input.length > 11)
		strength++;
	return strength;
}

// Kontrollpasswort
function check_passwordcheck() {
	var input = document.getElementById('password').value;
	var check = document.getElementById('passwordcheck').value;
	var output = document.getElementById('passwordcheckIndicator');
	if (input.length == 0 && check.length == 0) {
		output.innerHTML = '';
	} else if (input == check) {
		output.innerHTML = icon('ok');
	} else {
		output.innerHTML = icon('error') + "Passw&ouml;rter stimmen nicht &uuml;berein!";
	}
}


/*
 *	HILFSFUNKTIONEN
 */

function regex(expr,pattern) {
	var result = expr.match(pattern);
	if (typeof result == 'object') {
		r = '';
		for (var i = 0; i < result.length; ++i) {
			r += result[i];
		}
		result = r;
	}
	if (result == expr) {
		return true;
	} else {
		return false;
	}
}

function is_integer(input) {
	return regex(input,/d*/g);
}

function is_money(input) {
	return regex(input,/d*[,]?d{0,2}/g);
}

function has_length(input,len) {
	if (input.length < len)
		return false;
	return true;
}

function is_same(a,b) {
	if (a != b)
		return false;
	return true;
}