//form validation
//languages:
//0: deutsch
//1: italiano
//2: english

//TEXTE
error_message = new Array(); //viene creato l'array
error_message[0]="Achtung, nicht alle Felder wurden korrekt ausgefuellt";
error_message[1]="Attenzione, non tutti i campi sono stati compilati in maniera corretta";
error_message[2]="Warning, not all fields were filled out correctly";

privacy_message = new Array(); //viene creato l'array
privacy_message[0]="Achtung, stimmen Sie bitte den Bedingungen zur Privacy zu.";
privacy_message[1]="Attenzione, per continuare e neccessario accettare le condizioni privacy";
privacy_message[2]="Warning, to continue please accept the privacy terms";

date_message = new Array(); //viene creato l'array
date_message[0]="Waehlen Sie bitte ein korrektes Datum aus.";
date_message[1]="Prege seleionare una data corretta";
date_message[2]="Plese select a correct date";



//show and hide privacy terms
function setDisplay(id){
	try{
  	var elem;
  	elem = document.getElementById(id);
  	if (elem != null){
    	if (elem.style.display == "none"){
    		elem.style.display = "block";
    		return;
    	}
    	else{
      	elem.style.display = "none";
      	return;
    	}
  	}
	}
	catch(e){
	}
}

//show and hide privacy terms
function setDisplay(id){
	try{
  	var elem;
  	elem = document.getElementById(id);
  	if (elem != null){
    	if (elem.style.display == "none"){
    		elem.style.display = "block";
    		return;
    	}
    	else{
      	elem.style.display = "none";
      	return;
    	}
  	}
	}
	catch(e){
	}
}





function validateFormOnSubmit(theForm, language) {
//alert("validate");
  var reason = "";

  if(theForm.bedingungen.checked != true){
    alert(privacy_message[language]);
    return false;
  } 
  
  //check if date has been modified
	/*if(theForm.date_has_been_modified.value == "0"){
	 alert(date_message[language]);
	 return false;
  }  
  */
  reason += validateEmpty(theForm.name);
  reason += validateEmail(theForm.email);
  	      
  //reason += validateEmpty(theForm.anfrage_formular_adults);  
  //reason += validateEmpty(theForm.bedingungen);
  //alert(reason);
      //return false;
  if (reason != ""){
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.border = '1px solid red'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.border = '1px solid green';
    }
    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.border = '1px solid red';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.border = '1px solid red';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.border = '1px solid red';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.border = '1px solid green';
    }
    return error;
}

