function addClass(element, classname){
	if (element != null) {
		element.className += ' ' + classname;
	}
} 


function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
return true;
}

function unCheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
return true;
}


function disableSelection(element) {
	if (typeof element.onselectstart !== 'undefined') {
		element.onselectstart = function() { return false; };
	} else if (typeof element.style.MozUserSelect !== 'undefined') {
		element.style.MozUserSelect = 'none';
	} else {
		element.onmousedown = function() { return false; };
	}
}

window.onload = function() {
	var i = 0;
	var labels = document.getElementsByTagName('span');
	for (i = 0; i < labels.length; i++) {
		disableSelection(labels[i]);
	}
};
	

function radioCheck(theCheckBox){
	theCheckBox = document.getElementById(theCheckBox);
	var checked = theCheckBox.checked;
	if (checked === true){
		theCheckBox.checked = false;
	}
	else {
		theCheckBox.checked = true; // checkbox action
	}
}

function findLabel(el) {
	var idVal = el.id;
	var i = 0;
	var labels = document.getElementsByTagName('label');
	for(i = 0; i < labels.length; i++ ) {
		if (labels[i].htmlFor === idVal){
			var fulllabel = labels[i].innerHTML;
			fulllabel = fulllabel.split("\n");
			return fulllabel[0];
		}
	}
}

function isChecked(theCheckBox) {
	if (theCheckBox.checked === false){
		alert ('Die AGB müssen akzeptiert werden.');
		theCheckBox.focus();
		return false;
	}
	return true;
}
	
function isNotEmpty(strfield) {
	var fieldValue = strfield.value;
	if (fieldValue === ""){
		var fieldName = findLabel(strfield);
		alert("\""+fieldName+"\" ist ein erforderliches Feld.\n\nBitte füllen Sie alle erforderlichen Felder aus\nund versuchen Sie es erneut.");
		strfield.focus();
		return false;
	}
	return true;
}



function isValidEmail(strEmail) {
	var str = strEmail.value;
	if (str.indexOf(".") > 2 && str.indexOf("@") > 0){
		return true;
	}
	else {
		alert("Bitte geben Sie eine gültige E-Mail Adresse ein.");
		strEmail.focus();
		return false;
	}
}

function isValidPasswd(strPasswd) {
	var pwCount = strPasswd.value.length;
	if (pwCount > 5){
		return true;
	}
	else {
		alert("Das Passwort ist zu kurz.\nEs muss mindestens 6 Zeichen enthalten.");
		strPasswd.focus();
		return false;
	}
}

function checkKaufort(theForm) {
	if (theForm.usertype.value == 2){
		if (theForm.kaufort.value === ""){
			alert("Sie haben als Berechtigung \"Käufer\" ausgewählt, bitte geben Sie an, wo sie den Musikatlas gekauft haben.");
			theForm.kaufort.focus();
			return false;
		}
	}
	return true;
}

function confirmData(theForm){
	if (confirm("Sind Sie sicher, die Bestellung mit den angegebenen Daten durchführen zu wollen?")){
		return true;
	}
	return false;
}


function checkOrder(theForm){
	if (isNotEmpty(theForm.email)){
		if (isNotEmpty(theForm.passwd)){
			if (isNotEmpty(theForm.fullname)){
				if (isNotEmpty(theForm.address)){
					if (isNotEmpty(theForm.plz)){
						if (isNotEmpty(theForm.ort)){
							if (isValidPasswd(theForm.passwd)){
								if (isChecked(theForm.agb)){
									if (isValidEmail(theForm.email)){
										if (confirm("Sind Sie sicher, die Bestellung mit den angegebenen Daten durchführen zu wollen?")){
											return true;
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}

function checkRegister(theForm){
	if (isNotEmpty(theForm.email)){
		if (isNotEmpty(theForm.passwd)){
			if (isNotEmpty(theForm.fullname)){
				if (isNotEmpty(theForm.address)){
					if (isNotEmpty(theForm.plz)){
						if (isNotEmpty(theForm.ort)){
							if (isValidPasswd(theForm.passwd)){
								if (checkKaufort(theForm)){
									if (isChecked(theForm.agb)){
										if (isValidEmail(theForm.email)){
											if (confirm("Sind Sie sicher, die Registrierung mit den angegebenen Daten durchführen zu wollen?")){
												return true;
											}
										}
									}
								}
							}			
						}
					}
				}
			}
		}
	}
	return false;
}



