var firAbtPosRegExp = /^([-_\.,!?\/\\()\w\d\s\ß\ä\ü\ö]*)?$/i;
var nameVornameRegExp = /^(\s*|\s*([^-._!"§$%&\(\)=?\/\d<>]+[-.]{0,2}\s*)+\s*)$/i;
var strasseRegExp = /^\s*[a-zA-Z\s\ß\ä\ü\ö]+(-?[a-zA-Z\s\ß\ä\ü\ö]+)*\.?\s*$/i;
var nrRegExp = /^(\s*[0-9]+\s*(1+\/+[2-9]+)*\s*[a-zA-Z]?\s*(-?\s*[0-9]+\s*(1+\/+[2-9]+)*\s*[a-zA-Z]?)*\s*)?$/i;
var plzRegExp = /^(\s*\w*\s*-?\s*(\w*\s*\d*\s*)+)?$/i;
var ortRegExp = /^(\s*[-_.\/a-zA-Z\s\ß\ä\ü\ö]+\s*)?$/i;
var telFaxRegExp = /^(\s*(\+?\(?[\d]+\)?)?(\s*[-_\.\s\/]?\s*\(?[\d]+\)?)*\s*)?$/i;
var emailRegExp = /^\s*(("[\w-\s\ß\ä\ü\ö]+")|([\w-\ß\ä\ü\ö]+(?:\.[\w-\ß\ä\ü\ö]+)*)|("[\w-\s\ß\ä\ü\ö]+")([\w-\ß\ä\ü\ö]+(?:\.[\w-\ß\ä\ü\ö]+)*))(@((?:[\w-\ß\ä\ü\ö]+\.)*\w[\w-\ß\ä\ü\ö]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)\s*$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?\s*$)/i;
var textRegExp = /^([^<>]*)*$/i;

var errors = new Array(13);

$(document).ready(function(){
	for (var i = 0; i < errors.length; i++) {
		errors[i] = false;
	}
	
	/* firma check */
    $('#firma').blur(function(){
        var inputFirmaValue = document.contactform.firma.value;
        checkInput(inputFirmaValue, firAbtPosRegExp, "#firmaLabel", 0);
    });
	/* name check */
    $('#name').blur(function(){
        var inputNameValue = document.contactform.name.value;
        checkInput(inputNameValue, nameVornameRegExp, "#nameLabel", 1);
    });
	/* vorname check */
    $('#vorname').blur(function(){
        var inputVornameValue = document.contactform.vorname.value;
        checkInput(inputVornameValue, nameVornameRegExp, "#vornameLabel", 2);
    });
	/* position check */
    $('#position').blur(function(){
        var inputPositionValue = document.contactform.position.value;
        checkInput(inputPositionValue, firAbtPosRegExp, "#positionLabel", 3);
    });
	/* abteilung check */
    $('#abteilung').blur(function(){
        var inputAbteilungValue = document.contactform.abteilung.value;
        checkInput(inputAbteilungValue, firAbtPosRegExp, "#abteilungLabel", 4);
    });
	/* strassen check */
    $('#strasse').blur(function(){
        var inputStrasseValue = document.contactform.strasse.value;
        checkInput(inputStrasseValue, strasseRegExp, "#strasseLabel", 5);
    });
	/* nr check */
    $('#nr').blur(function(){
        var inputNrValue = document.contactform.nr.value;
        checkInput(inputNrValue, nrRegExp, "#nrLabel", 6);
    });
	/* plz check */
    $('#plz').blur(function(){
        var inputPlzValue = document.contactform.plz.value;
        checkInput(inputPlzValue, plzRegExp, "#plzLabel", 7);
    });
	/* ort check */
    $('#ort').blur(function(){
        var inputOrtValue = document.contactform.ort.value;
        checkInput(inputOrtValue, ortRegExp, "#ortLabel", 8);
    });
	/* tel check */
    $('#tel').blur(function(){
        var inputTelefonValue = document.contactform.tel.value;
        checkInput(inputTelefonValue, telFaxRegExp, "#telLabel", 9);
    });
	/* fax check */
    $('#fax').blur(function(){
        var inputFaxValue = document.contactform.fax.value;
        checkInput(inputFaxValue, telFaxRegExp, "#faxLabel", 10);
    });
    /* email check */
    $('#email').blur(function(){
        var inputEmailValue = document.contactform.email.value;
        checkInput(inputEmailValue, emailRegExp, "#emailLabel", 11);
    });
	/* ihrText check */
    $('#ihrText').blur(function(){
        var inputTextValue = document.contactform.ihrText.value;
        checkInput(inputTextValue, textRegExp, "#ihrTextLabel", 12);
    });
	
	$('#submitBtn').bind('mouseenter', submitOver)
		.bind('mouseleave', submitOut)
		.bind('mousedown', submitDown)
		.bind('mouseup', submitOver);
		
    $('#cform').submit(function(){
		if (errors[0] == true
			|| errors[1] == true
			|| errors[2] == true
			|| errors[3] == true
			|| errors[4] == true
			|| errors[5] == true
			|| errors[6] == true
			|| errors[7] == true
			|| errors[8] == true
			|| errors[9] == true
			|| errors[10] == true
			|| errors[11] == true
			|| errors[12] == true) {
				$('#errorContainer').show();
				return false;
		} else {
			$('#errorContainer').hide();
			return true;
		}
	});
});


var checkInput = function(inputFieldValue, regExp, errId, index){
    if (inputFieldValue.search(regExp) == -1 && inputFieldValue != "") {
        $(errId).removeClass('nonErrorColor').addClass('errorColor');
		errors[index] = true;
    }
    else {
        $(errId).removeClass('errorColor').addClass('nonErrorColor');
		errors[index] = false;
    }  
	if(inputFieldValue == "") {
			$(errId).removeClass('nonErrorColor').remove('errorColor');
	}
}

var submitDown = function(){
    $(this).css("background-position", "0 -38px");
}
var submitOver = function(){
    $(this).css("background-position", "0 -19px");
}
var submitOut = function() {
	$(this).css("background-position", "0 0");
}
