﻿function IsValidCastingTitle(title) {
    // Trim leading and trailing whitespace
    title = title.trim();
    
    // Check if the title length is between 20 and 100 characters
    const titleLength = title.length;
    if (titleLength < 20 || titleLength > 100) {
        return false;
    }

    // Define a regular expression pattern to validate the title
    // - Allows letters (including special characters), numbers (0-9)
    // - Allows spaces, periods, hyphens, and curly apostrophes
    //const regEx = /^[A-Za-zÆØÅæøåÖöÜüÄäÉéÈèÍíÓóÚúÁáÃãÐðÑñÇçÞþ0-9 .’\-]+$/;
	const regEx = /^[^<>']+$/;

    // Test if the title matches the pattern
    return regEx.test(title);
}



function IsValidCastingDescription(description) {
    // Trim leading and trailing whitespace
    description = description.trim();
    
    // Check if the description length is between 100 and 5000 characters
    const descriptionLength = description.length;
    if (descriptionLength < 50 || descriptionLength > 5000) {
        return false;
    }

    // Define a regular expression pattern to validate the description
    // - Allows all characters (including special characters, letters, numbers, etc.)
    // Since all inputs are allowed, no need for strict pattern matching
    const regEx = /^[\s\S]*$/;

    // Test if the description matches the pattern
    return regEx.test(description);
}




function IsValidEmail(email) {
    // Get the length of the email
    const emailLength = email.length;

    // Check if the email length is between 6 and 254 characters
    if (emailLength < 6 || emailLength > 254) {
        return false;
    }

    // Define a regular expression pattern to validate the email
    const regEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;

    // Test if the email matches the pattern
    return regEx.test(email);
}





function IsValidPhone(phoneNumber) {
    
    // Remove any spaces in the phone number
    phoneNumber = phoneNumber.replace(/\s+/g, '');
    
    // Check if the phone number length is between 7 and 17 characters
    const phoneLength = phoneNumber.length;
    if (phoneLength < 7 || phoneLength > 17) {
        return false;
    }

    // Define a regular expression pattern to validate only digits (0-9)
    const regEx = /^\d{7,17}$/;

    // Test if the phone number matches the pattern
    return regEx.test(phoneNumber);
}






function IsValidName(name) {
    // Trim leading and trailing whitespace
    name = name.trim();
    
    // Check if the name length is between 2 and 60 characters
    const nameLength = name.length;
    if (nameLength < 2 || nameLength > 60) {
        return false;
    }

    // Define a regular expression pattern to validate the name
    const regEx = /^[A-Za-zÆØÅæøåÖöÜüÄäÉéÈèÍíÓóÚúÁáÃãÐðÑñÇçÞþ .’\-]+$/;

    // Test if the name matches the pattern
    return regEx.test(name);
}









function validateAll() { 
    valid = true 
    err = "" 
	
	if (IsValidCastingTitle(document.form.headline.value)==false) err = err+"Overskrift (20-100 tegn, ingen specialtegn)\n"	  	
	if (IsValidCastingDescription(document.form.Contenthtml.value)==false) err = err+"Beskrivelse (50-5000 tegn)\n"	  	
	if (IsValidEmail(document.form.Email.value)==false) err = err+"E-mail ikke gyldig eller ikke udfyldt\n"	
	if (IsValidPhone(document.form.Phone.value)==false) err = err+"Mobil nummer (7-13 tegn - kun tal)\n"
	if (IsValidName(document.form.ContactPerson.value)==false) err = err+"Kontaktperson (2-60 tegn, ingen specialtegn)\n"	  	

//	if (IsValidNameEmptyOk(document.form.CompanyName.value)==false) err = err+"Firmanavn (2-60 tegn, ingen specialtegn)\n"	  	
	
	
	
	
	
//	if (IsValidPassword(document.form.kodeord.value)==false) err = err+"Kodeord (8-30 tegn, Min ét stort og ét lille bogstav, Min 1 tal)\n"
//	if (document.form.kodeord.value != document.form.kodeord1.value) err = err+"Kodeord ikke ens\n"
//    if (IsValidName(document.form.kontaktperson.value)==false) err = err+"Dit fulde navn (2-60 tegn, ingen specialtegn)\n"	  	

//	if (IsValidPhone(document.form.tlf.value)==false) err = err+"Mobil nummer (7-13 tegn - kun tal)\n"
//    if (IsValidCompanyName(document.form.firmanavn.value)==false) err = err+"Firmanavn (2-60 tegn, ingen specialtegn)\n"	  	
//    if (IsValidCVRNumber(document.form.se.value)==false) err = err+"CVR (Skal bestå af præcist 8 tal - Ellers efterlad tom)\n"	  	
//    if (IsValidAddress(document.form.adresse.value)==false) err = err+"Addresse (5-100 tegn, ingen specialtegn)\n"	 
//    if (IsValidZip(document.form.postnr.value)==false) err = err+"Post nummer (4-5 tegn - kun tal)\n"	 
//    if (IsValidCity(document.form.by.value)==false) err = err+"By (2-50 tegn, ingen specialtegn)\n"	
//    if (IsValidURLCaster(document.form.hjemmeside.value)==false) err = err+"Website (5-150 tegn, skal indeholde http)\n"	
//	if (IsValidDescriptionCaster(document.form.beskrivelse.value)==false) err = err+"Beskrivelse (100-5000 tegn, ingen specialtegn)\n"		
//	if (document.form.accept.checked==false) err = err+"Du mangler at acceptere vores handelsbetingelser\n"
  
	

    if (err.length!=0){ 
      valid=false 
      err = "Du mangler at oplyse følgende korrekt:\n\n"+err 
      alert(err) 
      } 
    return valid 
    } 	 
