// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
	return (str.length >= min)&&(str.length <= max);
}

// validate form
function validateForm(f,preCheck,theformfunction){
	var valid = true;
	var i,e,t,v,g,b,spantxt,spanid,spanelement,hiddenspan,revalidate,errorwarning;
	errorwarning = document.getElementById('errorwarning');
	
	for(i=1; i < f.elements.length; i++){
		e = f.elements[i];
			
		//add event & functions to form elements based on the formfucntion string
		if (theformfunction == 'contactValidation') revalidate = function(){contactValidation()};
		if (theformfunction == 'contactValidationDE') revalidate = function(){contactValidationDE()};
		if (theformfunction == 'contactValidationES') revalidate = function(){contactValidationES()};
		if (theformfunction == 'bookingValidation') revalidate = function(){bookingValidation()};
		if (theformfunction == 'bookingValidationDE') revalidate = function(){bookingValidationDE()};
		if (theformfunction == 'bookingValidationES') revalidate = function(){bookingValidationES()};
		if (e.type == 'text' || e.type == 'password' || e.type == 'textarea'){e.onkeyup = revalidate};
		if (e.nodeName.toLowerCase() == "select"){e.onchange = revalidate};
		if (e.type == 'radio' || e.type == 'checkbox'){e.onclick = revalidate};
		if (e.optional) continue;

		t = e.type;
		v = e.value;
		g = e.id + "L";

		if(document.getElementById(g)) b = document.getElementById(g);

		spanid = e.id + "m";
		spanelement = document.createElement('span');
		spanelement.id = spanid;
		spanelement.className = "errortxt"
		if (!document.getElementById(spanid)) e.parentNode.appendChild(spanelement);
		hiddenspan = document.getElementById(spanid);

		if(t == 'text' || t == 'password' || t == 'textarea'){
			if(isEmpty(v)){
				valid = false;
				b.className = "errorLabel";
				hiddenspan.style.display = 'block';
				if (theformfunction == 'contactValidation' || theformfunction == 'bookingValidation') hiddenspan.innerHTML = 'Required Information';
				if (theformfunction == 'contactValidationDE' || theformfunction == 'bookingValidationDE') hiddenspan.innerHTML = 'Benötigte Informationen';
				if (theformfunction == 'contactValidationES' || theformfunction == 'bookingValidationES') hiddenspan.innerHTML = 'Información necesaria';
				continue;
			}else{
				hiddenspan.style.display = 'none';
				hiddenspan.innerHTML = '';
				b.className = "fixedLabel"
			}

			if(e.isEmail){
				if(!isEmail(v)){
					valid = false;
					b.className = "errorLabel";
					hiddenspan.style.display = 'block';
					if (theformfunction == 'contactValidation' || theformfunction == 'bookingValidation') hiddenspan.innerHTML = 'Invalid Email Format';
					if (theformfunction == 'contactValidationDE' || theformfunction == 'bookingValidationDE') hiddenspan.innerHTML = 'Inkorrektes Email Format';
					if (theformfunction == 'contactValidationES' || theformfunction == 'bookingValidationES') hiddenspan.innerHTML = 'Formato de correo electrónico inválido';
					continue;
				}else{
					hiddenspan.style.display = 'none';
					hiddenspan.innerHTML = '';
					b.className = "fixedLabel"
				}
			}
			
			if(e.isLengthBetween != null){
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if(!isLengthBetween(v,min,max)){
					valid = false;
					b.className = "errorLabel";
					hiddenspan.style.display = 'block';
					hiddenspan.innerHTML = 'Invalid Number of Characters';
					continue;
				}else{
					hiddenspan.style.display = 'none';
					hiddenspan.innerHTML = '';
					b.className = "fixedLabel"
				}
			}
		}
		
		if(t.indexOf('select') != -1){
			if(e.options[e.selectedIndex].value == 'noselection'){
				valid = false;
				b.className = "errorLabel";
				hiddenspan.style.display = 'block';
				if (theformfunction == 'contactValidation' || theformfunction == 'bookingValidation') hiddenspan.innerHTML = 'Required Information';
				if (theformfunction == 'contactValidationDE' || theformfunction == 'bookingValidationDE') hiddenspan.innerHTML = 'Benötigte Informationen';
				if (theformfunction == 'contactValidationES' || theformfunction == 'bookingValidationES') hiddenspan.innerHTML = 'Información necesaria';
				continue;
			}else{
				hiddenspan.style.display = 'none';
				hiddenspan.innerHTML = '';
				b.className = "fixedLabel"
			}
		}
	}
	if(preCheck == false){valid = false};
	if(preCheck == false || valid == false){
			errorwarning.style.display = 'block';
			(window.location.hash == '#errorwarning') ? null : window.location.hash = 'errorwarning';
		}else{
			errorwarning.style.display = 'none'
		};
	return valid;
}

