// newsletter form
function validateRecomandaFormOnSubmit(theForm) {
  var reason = "";
  reason += validateEml(theForm.Expeditor,'Cine recomanda');
  reason += validateEml(theForm.Destinatar,'Cui recomanzi?');
  if (reason != "") { alert("Urmatoarele campuri trebuie corectate:\n\n" + reason); return false; }
  return true;
}

// newsletter form
function validateNewsletterFormOnSubmit(theForm) {
  var reason = "";
  reason += validateEml(theForm.EmailAbonat,'EmailAbonat');
  if (reason != "") { alert("Urmatoarele campuri trebuie corectate:\n\n" + reason); return false; }
  return true;
}

// contact form
function validateContactFormOnSubmit(theForm) {
  var reason = "";
  reason += validateNum(theForm.Num,'Nume');
  reason += validateNum(theForm.Pre,'Prenume');
  reason += validateTel(theForm.Tel,'Telefon');
  reason += validateEml(theForm.Eml,'Email');
  reason += validateEmpty(theForm.Com,'Mesaj');

  reason += validateNum(theForm.code,'Verificare cod');
  if (reason != "") { alert("Urmatoarele campuri trebuie corectate:\n\n" + reason); return false; }
  return true;
}

// rezervari form
function validateFormOnSubmit(theForm) {
  var reason = "";
  reason += validateNum(theForm.Num,'Nume');
  reason += validateNum(theForm.Pre,'Prenume');
  reason += validateTel(theForm.Tel,'Telefon');
  reason += validateEml(theForm.Eml,'Email');
  reason += validateEmpty(theForm.Serviciu,'Serviciu');
  reason += validateEmpty(theForm.RezData,'Data');
  reason += validateEmpty(theForm.Ora,'Ora');
  //reason += validateNum(theForm.Com,'Comentariu');
  reason += validateNum(theForm.code,'Verificare cod');
  reason += validateChB(theForm.Agree,'Termeni si Conditii');
  //reason += validateEml(theForm.Eml,'Email');
  //reason += validatePassword(theForm.pwd);
  //reason += validateEmpty(theForm.from);
  if (reason != "") { alert("Urmatoarele campuri trebuie corectate:\n\n" + reason); return false; }
  return true;
}

function validateChB(fld,camp){
    var error = "";
    if (fld.checked == 0) {
        error = "CITESTE TERMENII SI CONDITIILE DE UTILIZARE DIN SITE.\n"
    } else {
        fld.checked = 0;
    }
    return error;
}

function validateEmpty(fld,camp) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "Nu ai completat campul "+camp+".\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateNum(fld,camp) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Nu ai completat campul " + camp + "\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        fld.style.background = 'Yellow';
        error = "Campul "+camp+" trebuie sa fie intre 5 si 20 de caractere.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow';
        error = "Campul "+camp+" contine caractere ilegale.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateTel(fld,camp) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   if (fld.value == "") {
        error = "Nu ai completat campul " + camp + ".\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "Campul " + camp + " contine caractere ilegale.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "Campul " + camp + " nu are lungimea corespunzatoare. (maxim 12)\n";
        fld.style.background = 'Yellow';
    }else{
        fld.style.background = 'White';
    }
    return error;
}

function trim(s){
  return s.replace(/^\s+|\s+$/, '');
}

function validateEml(fld,camp) {
    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 = "Nu ai completat campul "+camp+".\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Adresa de "+camp+" nu este corecta.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Adresa de "+camp+" contine caractere ilegale.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

