function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateFornavn(theForm.a_fornavn);
 	reason += validateEfternavn(theForm.b_efternavn);
	reason += validateAdresse(theForm.c_adresse);
	reason += validatePostnr(theForm.d_postnr);
	reason += validateBy(theForm.e_by);
  reason += validatePhone(theForm.f_phone);
	reason += validateEmail(theForm.g_email);
	reason += validateAntal(theForm.i_antal_gavekort);
	reason += validateVaerdi(theForm.j_vaerdi);
	reason += validateAktiv(theForm.k_aktiv_fra_dato);

      
  if (reason != "") {
    alert("Einige Felder sind nicht ausgefüllt:\n" + reason);
    return false;
  }
  return true;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Ein Feld ist nicht ausgefüllt.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateFornavn(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie den Vornamen an.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 25)) {
        fld.style.background = 'Yellow'; 
        error = "Der Vorname hat die verkehrte Länge.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateEfternavn(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie den Nachnamen an.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 25)) {
        fld.style.background = 'Yellow'; 
        error = "Der Nachname hat die verkehrte Länge.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAdresse(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie die Adresse an-Strasse und Hausnr.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePostnr(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie die Postnummer an.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 8)) {
        fld.style.background = 'Yellow'; 
        error = "Die Postnummer ist falsch.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateBy(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie den Ort an.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 25)) {
        fld.style.background = 'Yellow'; 
        error = "Der Ortsname ist verkehrt.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Bitte geben Sie Ihre Emailadresse an.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Die Emailadresse ist ungültig.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Die Emailadresse enthält nicht gültige Symbole.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Bitte geben Sie Ihre Telefonnummer an.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "Die Telefonummer enthält nicht gültige Symbole.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
function validateAntal(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie die Anzahl der Gutscheine an.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateVaerdi(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie den Wert des Gutscheines an.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateAktiv(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Bitte geben Sie an, ab wann der Gutschein gültig sein soll.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
