// Checkt Felder bei Ticket-Anfrage

function checkRequest() {	
	var why = "";

	why += isEmpty(document.requestForm.name.value,'Name');
	why += isEmpty(document.requestForm.phone.value,'Telefon');
	
	why += checkEmail(document.requestForm.email.value,'first');
	why += checkEmail(document.requestForm.email2.value,'again');							
	
	why += compare(document.requestForm.email.value,document.requestForm.email2.value,'email');
		
    if (why != "") {
       alert(why);
       return false;
    }
	return true;	
}

// Checkt Passwortfelder beim Passwort ändern

function checkNewPassword() {	
	var why = "";
	
	why += checkPassword(document.pwForm.password_old.value,'first');
	why += checkPassword(document.pwForm.password.value,'first');
	why += checkPassword(document.pwForm.password2.value,'again');
	why += compare(document.pwForm.password.value,document.pwForm.password2.value,'pw');	
		
    if (why != "") {
       alert(why);
       return false;
    }
	return true;	
}

// Hauptfunktion, checkt alles beim Account-Erstellen

function checkWholeForm(type) {	
	var why = "";
	
	array_fields = new Array(document.accountForm.name.value,
							 document.accountForm.lastname.value,
							 document.accountForm.street.value,
							 document.accountForm.zip.value,
							 document.accountForm.city.value,
							 document.accountForm.phone.value
							);
							
	array_names = new Array('Vorname','Nachname','Straße','Postleitzahl','Stadt','Telefon');
	
	why += checkTitle('billing');
	
	for(var i=0;i<array_fields.length;i++) {
		why += isEmpty(array_fields[i],array_names[i]);
	}
	
	why += checkDropdown(document.accountForm.country.selectedIndex,'country');
	
	if(type == 'create') {
		why += checkDropdown(document.accountForm.day.selectedIndex,'day');
		why += checkDropdown(document.accountForm.month.selectedIndex,'month');
		why += checkDropdown(document.accountForm.year.selectedIndex,'year');
		why += checkEmail(document.accountForm.email.value,'first');
		why += checkEmail(document.accountForm.email2.value,'again');
		why += compare(document.accountForm.email.value,document.accountForm.email2.value,'email');
	}
	
	if(document.accountForm.ship_adr[0].checked) {
		why += checkTitle('shipping');
		
		array_fields_s = new Array(document.accountForm.name_s.value,
							 document.accountForm.lastname_s.value,
							 document.accountForm.street_s.value,
							 document.accountForm.zip_s.value,
							 document.accountForm.city_s.value,
							 document.accountForm.phone_s.value
							);
		
		for(var i=0;i<array_fields.length;i++) {
			why += isEmpty(array_fields_s[i],array_names[i]);
		}					
							
		why += checkDropdown(document.accountForm.country_s.selectedIndex,'country');
	}	
		
    if (why != "") {
       alert(why);
       return false;
    }
	return true;	
}

// checkt das Passwort

function checkPassword (strng,type) {
 	var error = "";
 	if (strng == "") {
    	if(type == 'first') {	
			error = "Bitte Passwort eingeben.\n";
		}
		else {
			error = "Bitte Passwort wiederholen.\n";
		}
	}	
	else {
		var illegalChars = /[\W_]/; // allow only letters and numbers
		if (strng.length < 6) {
			error = "Passwort muss mindestens 6 Zeichen enthalten.\n";
		}
		else if (illegalChars.test(strng)) {
			 error = "Passwort enthält unerlaubte Zeichen.\n";
		}
	}
	return error;	
}

// checkt Email

function checkEmail (strng,type) {
	var error="";
	if (strng == "") {
		if(type == "first")
		{
			error = "Bitte Email-Adresse eingeben.\n";
		}
		else
		{
			error = "Bitte Email-Adresse wiederholen.\n";
		}	
	}
	else {
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(strng))) { 
		   error = "Bitte gültige Email-Adresse eingeben.\n";
		}
		else {
	//test email for illegal characters
		   var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
			 if (strng.match(illegalChars)) {
			  error = "Email-Adresse enthält unerlaubte Zeichen.\n";
		   }
		}	
	}
	return error; 	  
}

// checkt Telefonnummer

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}

// checkt Textfelder ob leer

function isEmpty(strng,field) {
var error = "";
  if (strng.length == 0) {
     error = "Bitte "+field+" eingeben.\n"
  }
return error;	  
}

// was textbox altered

function compare(strng,strng2,type) {
	var error = ""; 
	if (strng != strng2) {
		if(type == 'email') {
			error = "Email-Adressen stimmen nicht überein.\n";
		}
		else {
			error = "Passwörter stimmen nicht überein.\n";
		}	
	}
	return error;
}

// checkt Radio-Buttons

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// checkt Anrede

function checkTitle(type) {
	var error = "";
	if(type == "billing") {
		if(!document.accountForm.title[0].checked && !document.accountForm.title[1].checked) {
			error = "Bitte Anrede wählen.\n";
		}
	} else {
		if(!document.accountForm.title_s[0].checked && !document.accountForm.title_s[1].checked) {
			error = "Bitte Anrede wählen.\n";
		}
	}					
	return error;
}		

// checkt Listen

function checkDropdown(choice,type) {
var error = "";
    if (choice == '') {
    	switch(type) {
			case "country":
			error = "Bitte Land wählen.\n";
			break;
			
			case "day":
			error = "Bitte Tag des Geburtsdatums wählen.\n";
			break;
			
			case "month":
			error = "Bitte Monat des Geburtsdatums wählen.\n";
			break;
			
			case "year":
			error = "Bitte Jahr des Geburtsdatums wählen.\n";
			break;
		}										
    }    
return error;
}
