﻿function validateForm(theForm){
    var error = "";
        error += checkNom(theForm.nom.value);
        error += checkSociete(theForm.societe.value);
        error += checkEmail(theForm.email.value);
    if (error != "") {
       alert(error);
       return false;
    }
    return true;
}
function validateFormEstimate(theForm){
    var error = "";
        error += checkNom(theForm.nom.value);
        error += checkPrenom(theForm.prenom.value);
        error += checkMinChars(theForm.billingAddress.value, 'Adresse de facturation');
        error += checkEmail(theForm.email.value);
        error += checkMinChars(theForm.partType.value, 'Type de partie commune', 2);
        error += checkMinChars(theForm.partArea.value, 'Superficie de la partie commune', 1);
        error += checkMinChars(theForm.mainParcelArea.value, 'Superficie du lot principal', 1);
        error += checkMinChars(theForm.parcelsNb.value, 'Nombre de lots dans la copropriété', 1);
        error += checkChecked($(theForm).find('input[name="carrezAttest"]:checked').val(), 'Attestation loi Carrez');
    if (error != "") {
       alert(error);
       return false;
    }
    return true;
}
function checkNom(strng) {
    error = "";
    if (strng == "") {
        error+= "Veuillez entrer votre nom.\n";
    }else{
        if (strng.length < 3) {
        error += "Votre nom doit comporter au moins 3 caractères.\n";
        }
        //replace spaces with underscores
        strng2 = strng.replace(/ /,"_");
        // \W = "anything except letters, numbers and underscores"
        var illegalChars = /\W/;
        if (illegalChars.test(strng2)) {
           error = "Votre nom contient des caractères interdits.\n";
        }
    }
    return error;
}
function checkPrenom(strng) {
    error = "";
    if (strng == "") {
        error+= "Veuillez entrer votre prénom.\n";
    }else{
        if (strng.length < 3) {
        error += "Votre prénom doit comporter au moins 3 caractères.\n";
        }
        //replace spaces with underscores
        strng2 = strng.replace(/ /,"_");
        // \W = "anything except letters, numbers and underscores"
        var illegalChars = /\W/;
        if (illegalChars.test(strng2)) {
           error = "Votre prénom contient des caractères interdits.\n";
        }
    }
    return error;
}
function checkSociete(strng){
    error = "";
    if (strng == "") {
        error+= "Veuillez entrer le nom de votre Société.\n";
    }else
    if (strng.length < 3) {
    error += "Le nom de votre Société doit comporter au moins 3 caractères.\n";
    }
    return error;
}
function checkMinChars(strng, fieldname, charsNb) {
    charsNb = charsNb || 3;
    error = "";
    if (strng == "") {
        error+= "Veuillez renseigner le champ '" + fieldname + "'.\n";
    }else
    if (strng.length < charsNb) {
    error += "Le champ '" + fieldname + "' doit comporter au moins " + charsNb + " caractère(s).\n";
    }
    return error;
}
function checkChecked(value, fieldname) {
    error = "";
    if (value === undefined) {
        error += "Veuillez choisir une option pour le champ '" + fieldname + "'.\n";
    }
    return error;
}
function checkEmail(strng){
    error="";
    if(strng==""){
        error="Veuillez entrer une adresse email.\n";
    }else{
        //replace allowed chars with underscores
        strng2 = strng.replace(/[@.-]/g, '_')
        // \W = "anything except letters, numbers and underscores"
        var illegalChars = /\W/;
        if (illegalChars.test(strng2)) {
           error += "Votre adresse email contient des caractères interdits.\n";
        }
        var emailFilter=/^.+@.+\..{2,4}$/;
        if (!(emailFilter.test(strng))) {
               error += "Veuillez entrer une adresse email valide.\n";
        }
    }
    return error;
}

