function turn_red(id) {
  document.getElementById(id).style.border = 'solid 1px #FE0000';
  document.getElementById(id).style.background = '#FFEAE7';
  document.getElementById(id).focus();
}

function turn_normal(id) {
  document.getElementById(id).style.border = 'solid 1px #000000';
  document.getElementById(id).style.background = '#FFFFFF';
}

function check_email(id) {
  var emailStr = document.getElementById(id).value;
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

  var matchArray=emailStr.match(emailPat)
  if (matchArray==null)
    return false;

  var user=matchArray[1]
  var domain=matchArray[2]

  if (user.match(userPat)==null)
    return false;

  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255)
        return false;
    }
    return true;
  }

  var domainArray=domain.match(domainPat)
  if (domainArray==null)
    return false;

  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length
  if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4 || len<2)
    return false;

  return true;
}

function checkform() {

  // Premierement, recuperer la totalite des objets
  var nb_piece = document.getElementById('nb_piece').value;
  var loc1 = document.getElementById('loc1').value;
  var loc2 = document.getElementById('loc2').value;
  var loc3 = document.getElementById('loc3').value;
  var loc4 = document.getElementById('loc4').value;
  var nom = document.getElementById('nom').value;
  var prenom = document.getElementById('prenom').value;
  var email = document.getElementById('email').value;

  // Remettre les champs niquel
  turn_normal('nb_piece');
  turn_normal('loc1');
  turn_normal('nom');
  turn_normal('prenom');
  turn_normal('email');
  
  // Verification du nombre de piece minimum
  var filter = /^([0-9]{1,2})+$/;
  if ((!filter.test(nb_piece) || nb_piece.length == 0 || nb_piece.length > 2 || nb_piece == '0' || nb_piece == '00')) {
    alert('Le nombre de pieces est incorrect ou non renseigne.\nMerci de corriger');
    turn_red('nb_piece');
    return;
  }

  // Verification qu'au moins une localisation soit remplie
  if (loc1.length == 0 && loc2.length == 0 && loc3.length == 0 && loc4.length == 0)
  {
    alert('Les localisations semblent incorrectes.\nVous devez remplir au moins une localisation\nMerci de corriger');
    turn_red('loc1');
    return;
  }

  // Verification du nom du contact
  if (nom.length == 0 || nom.length > 64) {
    alert('Le nom semble etre incorrect ou non renseigne.\nMerci de corriger');
    turn_red('nom');
    return;
  }

  // Verification du prenom du contact
  if (prenom.length == 0 || prenom.length > 64) {
    alert('Le prenom semble etre incorrect ou non renseigne.\nMerci de corriger');
    turn_red('prenom');
    return;
  }

  if (!check_email('email')) {
    alert('L\'email semble etre incorrect ou non renseigne.\nMerci de corriger');
    turn_red('email');
    return;
  }

  document.notif.submit();
}

