
/* =============================================================================
   No Spam
   ========================================================================== */

function noSpam() {
    if (document.getElementById) {
		var at = "@";
	  	var links = document.getElementsByTagName('a');
	  
	  	for (var i = 0; i < links.length; i++) {
		  	var linkElem = links[i];
			
		  	if (linkElem.className == 'escape') {
		  		var mail = linkElem.firstChild; var domain = linkElem.lastChild;
		  		mail.nextSibling.firstChild.innerHTML = at;
		  		linkElem.href = "mailto:" + mail.data + at + domain.data;
		  	}
			
	  	} // End for
	  
    } // End if
}

window.onload = noSpam;


/* =============================================================================
   Input Placeholder Text
   ========================================================================== */
   
/*

Notes:

Add the following classes to text inputs or textareas to get the 
desired behaviour:

auto-select
	Will pre-populate the input with the title attribute and select the
	text when it receives focus.

auto-clear
	Will pre-populate the input with the title attribute and clear the 
	text when it receives focus. Note: if auto-select and auto-clear 
	are set, auto-select takes precedence.

populate
	Will just populate the input with the title attribute.

NB: A class name of "placeholder" is set on form inputs that have 
placeholder text in them. This allows you to style the inputs 
differently when there is user input versus placeholder text, e.g.
input.placeholder,
textarea.placeholder{
color: #777;
}



Potential additions:

Add in handling of default text if an initial value is detected (line 
60, line 93, etc) using something like:
if (!el.defaultValue) continue;
el.onfocus = function() {
	if (this.value == this.defaultValue) this.value = "";
}
el.onblur = function() {
	if (this.value == "") this.value = this.defaultValue;
}


*/

function hasClass(el,cls) {
	return el.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(el,cls) {
    if (!this.hasClass(el,cls)) el.className += " "+cls;
}
function removeClass(el,cls) {
    if (hasClass(el,cls)) {
        var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
        el.className = el.className.replace(reg,' ');
    }
}
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function initFormPlaceholders() {

	if (!document.getElementsByTagName) return true;

	ourForms = document.getElementsByTagName('form');

	// go through each form
	var numForms = ourForms.length;
	for (var i=0;i<numForms;i++) {

		// go through each form element
		var numFormElements = ourForms[i].elements.length;
		for (var j=0;j<numFormElements;j++) {

			var el = ourForms[i].elements[j];

			// ignore submit buttons
			if (el.type == "submit") continue;

			// if we got a text type input or textarea
			if ((el.type == "text") || (el.type == "textarea")) {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = el.className;
				if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
					// only populate if empty
					if (el.value == '') {
					    addClass(el, "placeholder");
					    el.value = el.title;
					}
				}

				// add auto select if class contains auto-select
				// note: else if below so auto-select takes precedence (assuming select is better than clear)
				if (el.className.match('auto-select')) {
					el.onfocus = function () {
						if (this.value == this.title) {
						    removeClass(this, "placeholder");
						    this.select();
						}
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') {
						    this.value = this.title;
						}
						if (this.value == this.title) {
						    addClass(this, "placeholder");
						}
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}

				// add auto clear if class contains auto-clear
				else if (el.className.match('auto-clear')) {
					el.onfocus = function () {
						if (this.value == this.title) {
						    removeClass(this, "placeholder");
						    this.value = '';
						}
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') {
						    this.value = this.title;
						}
						if (this.value == this.title) {
						    addClass(this, "placeholder");
						}
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}

		}

	}

}

addLoadEvent(initFormPlaceholders);



/* =============================================================================
   Form Validation
   ========================================================================== */

function validateForm(formular) {
	var error = 0;
	var currentLabel = '';
	var currentField = '';
	var klasse = '';
	var newClass = '';
	
	// Inspect all of the document's labels ...
	for (var i = 0; i < document.getElementsByTagName("label").length; i++) {
		currentLabel = document.getElementsByTagName("label")[i];
		if (currentLabel.htmlFor) {
			currentField = document.getElementById(currentLabel.htmlFor);
		}
		klasse = currentLabel.className;
		newClass = currentLabel.className.replace(/ error/, '');
		
		// Check if the current label belongs to the form we want to validate
		if (currentLabel.form == formular && currentField) {

			// ...  and if it is required at all, or has to be a number, or an e-mail address.
				
				// Rueckruf exists: Mark phone number field as required
				if (currentField.name == 'Rueckruf') {
					if (currentField.checked == true) {
						document.getElementById('label-telefon').className += ' required rueckruf';
					} else {
						var newTelClass = document.getElementById('label-telefon').className.replace(/ required rueckruf/, '');
						document.getElementById('label-telefon').className = newTelClass;
					}
				}
				

				// required field (but neither numeric nor an e-mail)
				if (klasse.match(/required/)) {
					if (currentField.tagName == 'SELECT') {
						if (currentField.childNodes[1].selected) {
							currentLabel.className = newClass;
							currentLabel.className += ' error';
							error = 1;
						} else {
							currentLabel.className = newClass;
						}
					} else if (currentField.type == 'checkbox') {
						if (currentField.checked == false) {
							currentLabel.className = newClass;
							currentLabel.className += ' error';
							error = 1;
						} else {
							currentLabel.className = newClass;
						}
					} else {
						if (currentField.value == '') {
							currentLabel.className = newClass;
							currentLabel.className += ' error';
							error = 1;
						} else {
							currentLabel.className = newClass;
						}
					}
					
				}
								
				// numeric field
				if (klasse.match(/number/)) {
					var numeric = isNumber(currentField);
					if (!numeric && !klasse.match(/required/) && currentField.value != '') {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						currentLabel.className = newClass;
					}
					if (!numeric && klasse.match(/required/)) {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						if (error == 0) {
							currentLabel.className = newClass;
						}
					}
				}
				
				// e-mail address
				if (klasse.match(/mail/)) {
					var valid = isMailValid(currentField);
					if (!valid && currentField.value != '') {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						currentLabel.className = newClass;
					}
					if (!valid && klasse.match(/required/)) {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						error = 1;
					} else {
						if (error == 0) {
							currentLabel.className = newClass;
						}
					}
				}


		}
		
	} // end for

	
	// Return TRUE and proceed sending the form if no errors occured.
	// Return FALSE in case of errors and display the error message,
	// then focus on the ID of the element containing the error message.
	
	// (The window.location.href call comes in handy if the page containing
	//	the form is as high as serveral screens,
	//  but can be safely removed without preventing the script to function properly).
	if (error === 0) {
		return true;
	} else {
		var errorbox = document.getElementById("fehlermeldung");
		errorbox.style.display = 'block';
		errorbox.innerHTML = '<strong>Beim Abschicken sind Fehler aufgetreten.</strong> Sie haben eventuell nicht alle Pflichtfelder ausgef&uuml;llt. Die falsch ausgef&uuml;llten Punkte sind <strong class="error">rot</strong> gekennzeichnet.<br /><a href="javascript:;" id="errorclose">Diese Meldung schlie&szlig;en</a>';
		window.location.href = "#fehlermeldung";
		document.getElementById("errorclose").onclick = function (event) { errorbox.style.display = 'none'; return false; }
		return false;
	}
		
}



// Additional functions for numeric and e-mail validation
function isNumber(field) {
	var returnvar = (isNaN(parseInt(field.value)) == true) ? false : true;
	return returnvar;
}

function isMailValid(field) {
	var returnvar = (field.value.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/)) ? true : false;
	return returnvar;
}
