MustSave = 0;
AsChange = 0;
var taberr = new Array();
function fenetre_find(Type, Sous_type, Extra) {
  Vwidth = 800;
  Vheight = 600;
  Vtop = (screen.height - Vheight) / 2;
  Vleft = (screen.width - Vwidth) / 2;
  window.open(
    "find.php?Type=" + Type + "&Sous_Type=" + Sous_type + Extra,
    "",
    "top=" +
      Vtop +
      ", left=" +
      Vleft +
      ", width=" +
      Vwidth +
      ", height=" +
      Vheight +
      ", resizable=yes, toolbar=no, scrollbars=yes, status=no"
  );
}
function ok_move() {
  if (MustSave == 1) {
    alert("Vous devez sauver les changements!");
    return 0;
  }
  if (AsChange == 1) {
    if (!confirm("Perdre chamgements?")) {
      return 0;
    }
  }
  return 1;
}
function set_default(Type, Sous_type) {
  if (MustSave == 1) {
    alert("Vous devez sauver les changements!");
    return;
  }
  if (AsChange == 1) {
    if (!confirm("Perdre chamgements?")) {
      return;
    }
  }
  texte = "Type = " + Type + " Sous_Type = " + Sous_type;
  document.location.href =
    "default.php?Type=" + Type + "&Sous_Type=" + Sous_type + "&NoVisu=0";
}
function set_default_plus(Type, Sous_type, ExtraParam) {
  if (MustSave == 1) {
    alert("Vous devez sauver les changements!");
    return;
  }
  if (AsChange == 1) {
    if (!confirm("Perdre chamgements?")) {
      return;
    }
  }
  texte = "Type = " + Type + " Sous_Type = " + Sous_type;
  document.location.href =
    "default.php?Type=" +
    Type +
    "&Sous_Type=" +
    Sous_type +
    "&NoVisu=0&" +
    ExtraParam;
}
function logout() {
  if (MustSave == 1) {
    alert("Vous devez sauver les changements!");
    return;
  }
  if (AsChange == 1) {
    if (!confirm("Perdre chamgements?")) {
      return;
    }
  }
  if (confirm("logout")) {
    document.location.href = "default.php?Logout=1";
  }
}
function commande_ajax_javascript(url) {
  var xhr_object = null;
  if (window.XMLHttpRequest)
    // Firefox
    xhr_object = new XMLHttpRequest();
  else if (window.ActiveXObject)
    // Internet Explorer
    xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  else {
    // XMLHttpRequest non supporté par le navigateur
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
    return;
  }
  xhr_object.open("GET", url, false);
  xhr_object.send(null);
  if (xhr_object.readyState == 4) {
    eval(xhr_object.responseText);
  }
}
function commande_ajax(url) {
  var xhr_object = null;
  if (window.XMLHttpRequest)
    // Firefox
    xhr_object = new XMLHttpRequest();
  else if (window.ActiveXObject)
    // Internet Explorer
    xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  else {
    // XMLHttpRequest non supporté par le navigateur
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
    return;
  }
  xhr_object.open("GET", url, false);
  xhr_object.send(null);
  if (xhr_object.readyState == 4) {
    return xhr_object.responseText;
  }
  return "";
}
function local_eval(script) {
  eval(script);
}
function test_change(iciform) {
  if (MustSave == 1) {
    alert("Vous devez sauver les changements!");
    return;
  }
  if (AsChange == 1) {
    if (!confirm("Perdre chamgements?")) {
      return;
    }
  }
  iciform.submit();
}
function submit_ok(iciform) {
  if (taberr.length == 0) {
    //alert("ok");
    iciform.submit();
  } else {
    alert("vous devez corriger toutes les erreurs");
  }
}
function test_vendeur() {
  if (this.document.info.Vendeur.value == 0) {
    alert("Vous devez entrer le nom du vendeur");
  } else {
    this.document.info.submit();
  }
}
function ajoute_err(iderr) {
  actuel = -1;
  for (cpt = 0; cpt < taberr.length; cpt++) {
    if (taberr[cpt] == iderr) {
      actuel = cpt;
    }
  }
  if (actuel == -1) {
    taberr.push(iderr);
  }
}
function supprime_err(iderr) {
  actuel = -1;
  for (cpt = 0; cpt < taberr.length; cpt++) {
    if (taberr[cpt] == iderr) {
      actuel = cpt;
    }
  }
  if (actuel != -1) {
    taberr.splice(actuel, 1);
  }
}
function test_nombre(val, iderr) {
  AsChange = 1;
  if (parseInt(val) != val && val.length > 0) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("format de nombre invalide");
    ajoute_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  }
}
function test_nombre_obligatoire(val, iderr) {
  AsChange = 1;
  if (parseInt(val) != val && val.length > 0) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("format de nombre invalide");
    ajoute_err(iderr);
  } else if (val == 0 || val.length == 0) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("valeur vide ou 0 invalide");
    ajoute_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  }
}
function test_obligatoire(val, iderr) {
  AsChange = 1;
  if (val.trim().length == 0) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("valeur vide invalide");
    ajoute_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  }
}
function test_matricule(val1, val2, val3, iderr) {
  AsChange = 1;
  if (
    (isNaN(val1) ||
      val1.length != 3 ||
      isNaN(val2) ||
      val2.length != 3 ||
      isNaN(val3) ||
      val3.length != 3) &&
    val1.length != 0 &&
    val2.length != 0 &&
    val3.length != 0
  ) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("no matricule invalide");
    ajoute_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  }
}
function test_date(jour, mois, annee, iderr) {
  AsChange = 1;
  document.getElementById(iderr).style.visibility = "hidden";
  supprime_err(iderr);
  if (
    (isNaN(jour) ||
      jour.length != 2 ||
      isNaN(mois) ||
      mois.length != 2 ||
      isNaN(annee) ||
      annee.length != 4) &&
    jour.length != 0 &&
    mois.length != 0 &&
    annee.length != 0
  ) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("format date invalide");
    ajoute_err(iderr);
    return;
  }
  var dt = new Date();
  dt.setFullYear(annee);
  dt.setDate(1);
  dt.setMonth(mois - 1);
  dt.setDate(jour);
  if (
    (jour != dt.getDate() ||
      mois != dt.getMonth() + 1 ||
      annee != dt.getFullYear()) &&
    jour.length != 0 &&
    mois.length != 0 &&
    annee.length != 0
  ) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("date invalide");
    ajoute_err(iderr);
    return;
  }
}
function test_date_new(date, iderr) {
  //alert(date);
  AsChange = 1;
  document.getElementById(iderr).style.visibility = "hidden";
  supprime_err(iderr);
  if (date == "") {
  } else {
    var reg = new RegExp("^[0-9]{2}.[0-9]{2}.[0-9]{4}$", "g");
    if (reg.test(date)) {
      jour = date.substr(0, 2);
      mois = date.substr(3, 2);
      annee = date.substr(6, 4);
      var dt = new Date();
      dt.setFullYear(annee);
      dt.setDate(1);
      dt.setMonth(mois - 1);
      dt.setDate(jour);
      if (
        (jour != dt.getDate() ||
          mois != dt.getMonth() + 1 ||
          annee != dt.getFullYear()) &&
        jour.length != 0 &&
        mois.length != 0 &&
        annee.length != 0
      ) {
        document.getElementById(iderr).style.visibility = "visible";
        alert("date invalide");
        ajoute_err(iderr);
        return;
      }
    } else {
      document.getElementById(iderr).style.visibility = "visible";
      alert("date invalide");
      ajoute_err(iderr);
      return;
    }
  }
  if (false) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("date invalide");
    ajoute_err(iderr);
    return;
  }
}
function test_heure(heure, minutes, iderr) {
  AsChange = 1;
  document.getElementById(iderr).style.visibility = "hidden";
  supprime_err(iderr);
  if (
    (isNaN(heure) ||
      heure.length != 2 ||
      isNaN(minutes) ||
      minutes.length != 2) &&
    heure.length != 0 &&
    minutes.length != 0
  ) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("format heure invalide");
    ajoute_err(iderr);
    return;
  }
  if (heure > 23 || heure < 0) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("heure invalide");
    ajoute_err(iderr);
    return;
  }
  if (minutes > 59 || minutes < 0) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("minutes invalide");
    ajoute_err(iderr);
    return;
  }
}
function test_telephone(val, iderr) {
  AsChange = 1;
  var reg = new RegExp("^[0-9/. ]*$", "g");
  if (reg.test(val) || val.length == 0) {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "visible";
    alert("format du no de telephone invalide");
    ajoute_err(iderr);
  }
}
function test_telephone_recherche(val, iderr) {
  AsChange = 1;
  var reg = new RegExp("^[0-9%+/. ]*$", "g");
  if (reg.test(val) || val.length == 0) {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "visible";
    alert("format du no de telephone invalide");
    ajoute_err(iderr);
  }
}
function test_email(val, iderr) {
  AsChange = 1;
  //var reg=new RegExp("^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$","g");
  var reg = new RegExp(
    "^[-a-zA-Z0-9_][-a-zA-Z0-9._]*@[a-zA-Z0-9-_]+.[a-zA-Z.-_]{1,}[a-zA-Z-_]+",
    "g"
  );
  if (reg.test(val) || val.length == 0) {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "visible";
    alert("adresse e-mail invalide");
    ajoute_err(iderr);
  }
}
function test_decimal(val, iderr) {
  AsChange = 1;
  if (parseFloat(val) != val && val.length > 0) {
    document.getElementById(iderr).style.visibility = "visible";
    alert("format de nombre invalide");
    ajoute_err(iderr);
  } else {
    document.getElementById(iderr).style.visibility = "hidden";
    supprime_err(iderr);
  }
}
function test_no_plaque(val, iderr) {
  // alert("test no plaque");
  AsChange = 1;
  if (val.length > 0) {
    if (
      val.charAt(0) >= "A" &&
      val.charAt(0) <= "Z" &&
      val.charAt(1) >= "A" &&
      val.charAt(1) <= "Z"
    ) {
      //  alert("Canton ok");
      document.getElementById(iderr).style.visibility = "hidden";
    } else {
      if (
        confirm("No de plaques : Manque lettres canton ! Valider quand même ?")
      ) {
        document.getElementById(iderr).style.visibility = "hidden";
      } else {
        document.getElementById(iderr).style.visibility = "visible";
      }
    }
  } else {
    document.getElementById(iderr).style.visibility = "hidden";
  }
  // alert(val[1]);
}
function cancel_quitance(NoCash) {
  if (confirm("Voulez-vous vraiment supprimer cette quittance ?")) {
    window.open(
      "cancel_quitance.php?NoCash=" + NoCash,
      "",
      "top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no"
    );
  }
}
function edit_quitance(NoCash) {
  window.open(
    "edit_quitance.php?NoCash=" + NoCash,
    "",
    "top=0, left=0, width=300, height=100, resizable=yes, toolbar=no, scrollbars=yes, status=no"
  );
}
function ouvre_recherche_article(Mode) {
  NoClient = document.forms["info"].elements["NoClient"].value;
  IsCommande = document.forms["info"].elements["IsCommande"].value;
  IsAchatMarchandise =
    document.forms["info"].elements["IsAchatMarchandise"].value;
  window.open(
    "recherche_article_find.php?NoClient=" +
      NoClient +
      "&Commande=" +
      IsCommande +
      "&AchatMarchandise=" +
      IsAchatMarchandise,
    "",
    "top=0, left=0, width=300, height=100, resizable=yes, toolbar=no, scrollbars=yes, status=no"
  );
}
function recherche_article_ajax(Mode) {
  NoArticle = document.forms["info"].elements["NoArticle"].value;
  NoClient = document.forms["info"].elements["NoClient"].value;
  IsCommande = document.forms["info"].elements["IsCommande"].value;
  IsAchatMarchandise =
    document.forms["info"].elements["IsAchatMarchandise"].value;
  commande_ajax_javascript(
    "recherche_article.php?NoArticle=" +
      NoArticle +
      "&NoClient=" +
      NoClient +
      "&Commande=" +
      IsCommande +
      "&AchatMarchandise=" +
      IsAchatMarchandise
  );
}
function ouvre_recherche_client(Mode) {}
function update_etat_vhc() {
  //alert(document.info.elements['new_vhc'].value);
  //alert(document.info.elements['EtatRetour'].value);
  alert(
    commande_ajax(
      "update_etat_vhc.php?NoChasis=" +
        document.info.elements["new_vhc"].value +
        "&etat=" +
        escape(document.info.elements["EtatRetour"].value)
    )
  );
}
function update_etat_depart_vhc() {
  //alert(document.info.elements['new_vhc'].value);
  //alert(document.info.elements['EtatRetour'].value);
  alert(
    commande_ajax(
      "update_etat_vhc.php?NoChasis=" +
        document.info.elements["new_vhc"].value +
        "&etat=" +
        escape(document.info.elements["etat"].value)
    )
  );
}
function recherche_client_ajax(Mode) {
  if (Mode == 5) {
    Nom = document.info.elements["Nom2"].value;
    Prenom = document.info.elements["Prenom2"].value;
  } else {
    Nom = document.forms["info"].elements["Nom"].value;
    Prenom = document.forms["info"].elements["Prenom"].value;
  }
  if (Nom.length == 0 && Prenom.length == 0) {
    alert("Vous devez rentrer un nom ou un prénom");
  } else {
    commande_ajax_javascript(
      "recherche_client.php?Nom=" +
        encodeURI(Nom) +
        "&Prenom=" +
        encodeURI(Prenom) +
        "&Mode=" +
        encodeURI(Mode)
    );
    //window.open('recherche_client_old.php?Nom='+Nom+'&Prenom='+Prenom+'&Mode='+Mode, '', 'top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no');
  }
}
function recherche_alerte_ajax(noclient, nochasis) {
  commande_ajax_javascript(
    "recherche_alerte.php?NoClient=" + noclient + "&NoChasis=" + nochasis
  );
}
function recherche_client_dialog(mode, form) {
  if (mode == 5) {
    Nom = document.forms[form].elements["Nom2"].value;
    Prenom = document.forms[form].elements["Prenom2"].value;
  } else {
    Nom = document.forms[form].elements["Nom"].value;
    Prenom = document.forms[form].elements["Prenom"].value;
  }
  if (Nom.length == 0 && Prenom.length == 0) {
    alert("Vous devez rentrer un nom ou un prénom");
  } else {
    var contenthtml = commande_ajax(
      "recherche_client_dialog.php?Nom=" +
        encodeURI(Nom) +
        "&Prenom=" +
        encodeURI(Prenom) +
        "&Mode=" +
        encodeURI(mode) +
        "&Form=" +
        encodeURI(form)
    );
    var dialog = $("#dialog");
    dialog.html(contenthtml);
    dialog.dialog("open");
    //window.open('recherche_client_old.php?Nom='+Nom+'&Prenom='+Prenom+'&Mode='+Mode, '', 'top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no');
  }
}
function recherche_client_dialog_affecte(noclient, mode, form) {
  commande_ajax_javascript(
    "recherche_client_dialog_affecte.php?NoClient=" +
      encodeURI(noclient) +
      "&Mode=" +
      encodeURI(mode) +
      "&Form=" +
      encodeURI(form)
  );
}
function recherche_client_dialog_nouveau(nom, prenom, mode, form) {
  //alert("nouveau_client");
  var dialog = $("#dialog");
  dialog.dialog("close");
  window.open(
    "recherche_client_dialog_create.php?Nom=" +
      encodeURI(nom) +
      "&Prenom=" +
      encodeURI(prenom) +
      "&Mode=" +
      mode +
      "&Form=" +
      form,
    "",
    "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
  );
  //commande_ajax_javascript('recherche_client_dialog_affecte.php?NoClient='+encodeURI(noclient)+'&Mode='+encodeURI(mode)+'&Form='+encodeURI(form));
}
function recherche_client(Mode) {
  if (Mode == 5) {
    Nom = document.info.elements["Nom2"].value;
    Prenom = document.info.elements["Prenom2"].value;
  } else {
    Nom = document.forms["info"].elements["Nom"].value;
    Prenom = document.forms["info"].elements["Prenom"].value;
  }
  if (Nom.length == 0 && Prenom.length == 0) {
    alert("Vous devez rentrer un nom ou un prénom");
  } else {
    window.open(
      "recherche_client_old.php?Nom=" +
        Nom +
        "&Prenom=" +
        Prenom +
        "&Mode=" +
        Mode,
      "",
      "top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no"
    );
  }
}
function trouve_nom(pos) {
  if (pos == 0) {
    No = document.info.elements["NoClient"].value;
  } else {
    No = document.info.elements["NoClient2"].value;
  }
  window.open(
    "trouve_nom.php?No=" + No + "&Pos=" + pos,
    "",
    "top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no"
  );
}
function go_trouve_nom(champ) {
  No = document.info.elements[champ].value;
  window.open(
    "trouve_nom.php?No=" + No + "&Champ=" + champ,
    "",
    "top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no"
  );
}
function efface_client(Pos) {
  if (Pos == 1) {
    document.info.NoClient.value = "";
    document.info.Nom.value = "";
    document.info.Prenom.value = "";
  } else if (Pos == 2) {
    document.info.NoClient2.value = "";
    document.info.Nom2.value = "";
    document.info.Prenom2.value = "";
  }
}
function recherche_vhc(Mode) {
  NoClient = document.info.elements["NoClient"].value;
  if (NoClient.length != 0) {
    commande_ajax_javascript(
      "recherche_vhc.php?NoClient=" + NoClient + "&Mode=" + Mode
    ); //, '', 'top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no');
  } else {
    //   alert('Vous devez en premier trouver le no de client');
    commande_ajax_javascript("recherche_vhc.php?NoClient=0&Mode=" + Mode); //, '', 'top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no');
  }
}
function ouvre_recherche_vhc(Mode) {}
function recherche_vhc_ajax(Mode, Force) {
  //alert("recherche_vhc_ajax");
  //return;
  if (Mode == 5) {
    NoClient = document.info.elements["NoClient2"].value;
  } else {
    NoClient = document.info.elements["NoClient"].value;
  }
  if (NoClient.length == 0) {
    alert("Vous devez choisir en premier un client");
  } else {
    commande_ajax_javascript(
      "recherche_vhc.php?NoClient=" +
        NoClient +
        "&Mode=" +
        Mode +
        "&Force=" +
        Force
    );
  }
}
function efface_vhc() {
  document.info.NoChasis.value = "";
  document.info.Marque.value = "";
  document.info.Model.value = "";
  document.info.Km.value = "";
  document.info.NoPlaque.value = "";
}
function test_return() {
  if (event.keyCode == 10) {
    window.event.keyCode = 9;
  }
  if (event.keyCode == 13) {
    window.event.keyCode = 9;
  }
}
function test_return_newfact(ev) {
  var code;
  var ff = false;
  if (ev) {
    ff = true;
    code = ev.which;
    event = ev;
  } else {
    code = event.keyCode;
  }
  if (code == 10 || code == 13) {
    if (!ff) windowevent.keyCode = 9;
    this.ignore_submit = true;
    return false;
  }
}
function test_submit() {
  //alert("test_submit");
  return false;
}
function test_return_client(mode) {
  if (this.event.keyCode == 10) {
    window.event.keyCode = 32;
    window.event.returnValue = false;
    recherche_client_ajax(mode);
  }
  if (this.event.keyCode == 13) {
    window.event.keyCode = 32;
    window.event.returnValue = false;
    recherche_client_ajax(mode);
  }
}
function test_return_client_dialog(mode, form) {
  if (this.event.keyCode == 10) {
    window.event.keyCode = 32;
    window.event.returnValue = false;
    recherche_client_dialog(mode, form);
  }
  if (this.event.keyCode == 13) {
    window.event.keyCode = 32;
    window.event.returnValue = false;
    recherche_client_dialog(mode, form);
  }
}
function annule_return() {
  if (event.keyCode == 10) {
    window.event.keyCode = 32;
  }
  if (event.keyCode == 13) {
    window.event.keyCode = 32;
  }
}
function valide_supprimer(NoCommande) {
  if (
    confirm(
      "Voulez-vous vraiment supprimer cette commande " +
        NoCommande +
        " avec tous les risques associés ?"
    )
  ) {
    window.location.href =
      "default.php?Type=4&Sous_Type=100&NoCommande=" + NoCommande;
  }
}
function auto_submit(iciform) {
  iciform.submit();
}
function auto_tab(icival, pos, nbcar) {
  // alert("auto_tab");
  var nom1 = icival + "[" + pos + "]";
  var nom2 = icival + "[" + (pos + 1) + "]";
  //	alert(nom1);
  //	alert(nom2);
  if (this.document.info.elements[nom1].value.length == nbcar) {
    //  	alert("tab");
    //  	this.document.info.elements[nom2].focus();
    this.document.info.elements[nom2].select();
  }
}
function npa_to_localite(no) {
  commande_ajax_javascript("npa2localite.php?NPA=" + no);
}
function go_ticket(iciform) {
  //alert("go");
  iciform.Ticket.value = 1;
  iciform.submit();
}
function go_quittance(iciform) {
  //alert("go");
  cancel_submit = true;
  iciform.submit();
}
function enter_quantite_newfact(ev) {
  var code;
  if (ev.which != null) {
    // cas pour ff
    code = ev.which;
  } // cas pour ie
  else {
    code = window.event.keyCode;
  }
  if (code == 10 || code == 13) {
    if (this.document.forms["info"].NoArticle.value == "") {
      alert("Manque numéro d'article");
    } else if (this.document.forms["info"].Description.value == "") {
      alert("Manque description");
    } else if (
      isNaN(this.document.forms["info"].Prix.value) ||
      this.document.forms["info"].Prix.value == 0
    ) {
      alert("Manque prix unitaire");
    } else if (
      isNaN(this.document.forms["info"].GM.value) ||
      this.document.forms["info"].GM.value == 0
    ) {
      alert("Groupe marchandise 0 interdit -> modifier article");
    } else if (
      isNaN(this.document.forms["info"].Quantite.value) ||
      this.document.forms["info"].Quantite.value == 0
    ) {
      alert("Manque quantité");
    } else {
      add_new_newfact(true);
    }
  }
}
function enter_quantite_commande_newfact(ev) {
  var code;
  if (ev.which != null) {
    // cas pour ff
    code = ev.which;
  } // cas pour ie
  else {
    code = window.event.keyCode;
  }
  if (code == 10 || code == 13) {
    //alert("coucou");
    var obj = document.getElementById("Destinataire");
    obj.focus();
    obj.select();
  }
}
function enter_quantite(ev) {
  var code;
  if (ev.which != null) {
    // cas pour ff
    code = ev.which;
  } // cas pour ie
  else {
    code = window.event.keyCode;
  }
  if (code == 10 || code == 13) {
    add_new(true);
  }
}
function dupliquer(iciform) {
  if (version_error == 2) {
    alert(
      "Impossible d'enregistrer car votre version de la commande n'est plus à jour"
    );
    return;
  }
  iciform.Duplique.value = 1;
  iciform.submit();
}
function go_encaisse(iciform) {
  if (version_error == 2) {
    alert(
      "Impossible d'enregistrer car votre version de la commande n'est plus à jour"
    );
    return;
  }
  iciform.Encaisse.value = 1;
  iciform.submit();
}
function go_imprime(iciform) {
  if (version_error == 2) {
    alert(
      "Impossible d'enregistrer car votre version de la commande n'est plus à jour"
    );
    return;
  }
  iciform.submit();
}
function transfert_vhc(mode) {
  pos = this.document.info.NoChasis.selectedIndex;
  if (mode == 1) {
    window.opener.document.info.NoChasis.value = TabNoChasis[pos];
    window.opener.document.info.Marque.value = TabMarque[pos];
    window.opener.document.info.Model.value = TabModel[pos];
    window.opener.document.info.Km.value = TabKm[pos];
    window.opener.document.info.NoPlaque.value = TabNoPlaque[pos];
    if (window.opener.document.info.NoClient.value.length == 0) {
      window.opener.document.info.NoClient.value = TabNoClient[pos];
      window.opener.document.info.Nom.value = TabNom[pos];
      window.opener.document.info.Prenom.value = TabPrenom[pos];
    }
  } else {
    window.opener.document.info.NoChasis.value = TabNoChasis[pos];
    window.opener.document.info.Vehicule.value =
      TabMarque[pos] + " " + TabModel[pos];
    window.opener.document.info.Km.value = TabKm[pos];
  }
  window.close();
}
function change_status(nocommande, icivalue) {
  //  alert(nocommande);
  //  alert(icivalue);
  var xhr_object = null;
  if (
    confirm(
      "Voulez-vous vraiment changer le niveau de rappel de cette facture ?"
    )
  ) {
    if (window.XMLHttpRequest)
      // Firefox
      xhr_object = new XMLHttpRequest();
    else if (window.ActiveXObject)
      xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else {
      // XMLHttpRequest non supporté par le navigateur
      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
      return;
    }
    xhr_object.open("POST", "stat_rappel.php", false);
    xhr_object.setRequestHeader(
      "Content-type",
      "application/x-www-form-urlencoded"
    );
    var data = "nocommande=" + nocommande + "&status=" + icivalue;
    xhr_object.send(data);
  }
  //  if(xhr_object.readyState == 4) alert(xhr_object.responseText);
  history.go(0);
}
function imprime_groupe(degre, type) {
  nb_elements = formprint.elements.length;
  for (cpt = 0; cpt < nb_elements; cpt++) {
    if (formprint.elements[cpt].id == "let" + type + degre) {
      if (formprint.elements[cpt].checked) {
        tmp_no = formprint.elements[cpt].name;
        tmp_no = tmp_no.substr(3, tmp_no.length - 3);
        window.open(
          "print_rappel.php?nocommande=" +
            tmp_no +
            "&Direct=1&NoClient=" +
            formprint.elements["cli" + tmp_no].value,
          "",
          "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
        );
      }
    }
    if (formprint.elements[cpt].id == "fac" + type + degre) {
      if (formprint.elements[cpt].checked) {
        tmp_no = formprint.elements[cpt].name;
        tmp_no = tmp_no.substr(3, tmp_no.length - 3);
        window.open(
          "print.php?Type=1&Sous_Type=1&NoCommande=" +
            tmp_no +
            "&NoArticle=0&NoClient=" +
            formprint.elements["cli" + tmp_no].value +
            "&Direct=1",
          "",
          "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
        );
      }
    }
  }
  //  formprint.degre.value=degre;
  //  formprint.type_commande.value=type;
  //  formprint.submit();
}
function imprime_groupe_pdf(degre, type) {
  nb_elements = formprint.elements.length;
  for (cpt = 0; cpt < nb_elements; cpt++) {
    if (formprint.elements[cpt].id == "let" + type + degre) {
      if (formprint.elements[cpt].checked) {
        tmp_no = formprint.elements[cpt].name;
        tmp_no = tmp_no.substr(3, tmp_no.length - 3);
        window.open(
          "print_rappel.php?nocommande=" +
            tmp_no +
            "&Direct=0&NoClient=" +
            formprint.elements["cli" + tmp_no].value,
          "",
          "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
        );
      }
    }
    if (formprint.elements[cpt].id == "fac" + type + degre) {
      if (formprint.elements[cpt].checked) {
        tmp_no = formprint.elements[cpt].name;
        tmp_no = tmp_no.substr(3, tmp_no.length - 3);
        window.open(
          "print.php?Type=1&Sous_Type=1&NoCommande=" +
            tmp_no +
            "&NoArticle=0&NoClient=" +
            formprint.elements["cli" + tmp_no].value +
            "&Direct=0",
          "",
          "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
        );
      }
    }
  }
  //  formprint.degre.value=degre;
  //  formprint.type_commande.value=type;
  //  formprint.submit();
}
function imprime_unique(nocommande) {
  if (formprint.elements["let" + nocommande] != null)
    if (formprint.elements["let" + nocommande].checked)
      window.open(
        "print_rappel.php?nocommande=" +
          nocommande +
          "&Direct=1&NoClient=" +
          formprint.elements["cli" + nocommande].value,
        "",
        "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
      );
  if (formprint.elements["fac" + nocommande].checked)
    window.open(
      "print_rappel.php?nocommande=" +
        nocommande +
        "&Direct=1&NoClient=" +
        formprint.elements["cli" + nocommande].value,
      "",
      "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
    );
}
function imprime_unique_pdf(nocommande) {
  if (formprint.elements["let" + nocommande] != null)
    if (formprint.elements["let" + nocommande].checked)
      window.open(
        "print_rappel.php?nocommande=" +
          nocommande +
          "&Direct=0&NoClient=" +
          formprint.elements["cli" + nocommande].value,
        "",
        "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
      );
  if (formprint.elements["fac" + nocommande].checked)
    window.open(
      "print.php?Type=1&Sous_Type=1&NoCommande=" +
        nocommande +
        "&NoArticle=0&NoClient=" +
        formprint.elements["cli" + nocommande].value +
        "&Direct=0",
      "",
      "top=0, left=0, width=800, height=600, resizable=yes, toolbar=no, scrollbars=yes, status=no"
    );
}
function set_checkbox(dom, degre, type, do_check) {
  nb_elements = formprint.elements.length;
  for (cpt = 0; cpt < nb_elements; cpt++) {
    if (formprint.elements[cpt].id == "" + dom + type + degre) {
      formprint.elements[cpt].checked = do_check;
    }
  }
}
function send_sms(no_atelier) {
  //commande = "https://myaccount.voipbuster.com/clx/sendsms.php?username=CentreHonda&password=hondacr&from=&to="+no_tel+"&text=votre+vehicule+est+pret";
  reponse = commande_ajax("sms.php?no_atelier=" + no_atelier);
  alert(reponse);
  if (reponse == "SMS envoyé avec succès !") {
    var obj = document.getElementById("SMS_button");
    obj.style.backgroundColor = "#FF0000";
  }
}
function send_sms_client(no_telephone, id_option) {
  //alert(id_option);
  id_message = $(id_option).val();
  //alert(id_message);
  //return;
  reponse = commande_ajax(
    "smsclient.php?no_tel=" + no_telephone + "&id_message=" + id_message
  );
  alert(reponse);
  if (reponse == "SMS envoyé avec succès !") {
  }
}
function send_sms_anniversaire(no_telephone, no_client) {
  //alert("SMS ANNIVERSAIRE");
  //alert(id_message);
  //return;
  reponse = commande_ajax(
    "smsanniversaire.php?no_tel=" + no_telephone + "&no_client=" + no_client
  );
  alert(reponse);
  if (reponse == "SMS envoyé avec succès !") {
    window.location.reload(true);
  }
}
function send_sms_rappel_atelier(no_telephone, no_atelier, vehicule, date) {
  //alert("SMS ANNIVERSAIRE");
  //alert(id_message);
  //return;
  reponse = commande_ajax(
    "smsrappelatelier.php?no_tel=" +
      no_telephone +
      "&no_atelier=" +
      no_atelier +
      "&vehicule=" +
      vehicule +
      "&date=" +
      date
  );
  alert(reponse);
  if (reponse == "SMS envoyé avec succès !") {
    window.location.reload(true);
  }
}
function formkeydown(ev) {
  var code;
  var ff = false;
  if (ev) {
    ff = true;
    code = ev.which;
    event = ev;
  } else {
    code = event.keyCode;
  }
  if (code == 13) {
    document.forms[actif].submit();
  }
}

function init_rdv_atelier_drag_and_drop() {
  jQuery(".div_rdv").draggable({
    revert: "invalid",
    //helper: "clone",
    //cursor: "move"
  });
  //jQuery(".rdv_vide" ).droppable({
  //	accept: ".div_rdv",
  //	drop: function( event, ui ) {
  //$( this ).find( ".placeholder" ).remove();
  //var NoAtelier = jQuery("input.NoAtelier", ui.draggable.context.parentNode)[0].value;
  //var date = jQuery("input.date", this.parentNode)[0].value;
  //var employe = jQuery("input.employe", this.parentNode)[0].value;
  //alert(NoAtelier+" "+date+" "+employe);
  //ui.draggable.context.style.cssText="";
  //this.className="td_rdv";
  //this.innerHTML = ui.draggable.context.outerHTML;
  //ui.draggable.context.parentNode.className="td_rdv rdv_vide ui-droppable";
  //jQuery(this).append(ui.draggable.css({position: 'static'}));
  //this.parentNode.parentNode
  //ui.draggable.context.
  //ui.draggable.context.outerHTML = "";
  //jQuery(this).droppable("destroy");
  //jQuery.ajax({
  //	url: "moveAtelierAjax.php",
  //	data: { NoAtelier: NoAtelier, date: date, employe: employe },
  //	success: function(data) {
  //		//alert(data);
  //		location.reload();
  //	}
  //})
  //location.reload();
  //init_rdv_atelier_drag_and_drop();
  //alert(ui.draggable.context.outerHTML);
  //jQuery( this ).text( ui.draggable.text() ).appendTo( this );
  //	}
  //	});
  jQuery(".tr_jour, .tr_mecano, .tr_ligne_rdv").droppable({
    accept: ".div_rdv",
    drop: function (event, ui) {
      var NoAtelier = jQuery(
        "input.NoAtelier",
        ui.draggable.context.parentNode
      )[0].value;
      var NoSuite = jQuery("input.NoSuite", ui.draggable.context.parentNode)[0]
        .value;
      ui.draggable.context.style.cssText = "";
      var tmp_content = ui.draggable.context.outerHTML;
      ui.draggable.context.parentNode.className = "td_rdv rdv_vide";
      ui.draggable.context.outerHTML = "";
      var found_destination = false;

      var entete_row = this;
      var postfix_row = "communs";
      while (entete_row.classList[0] == "tr_ligne_rdv") {
        entete_row = entete_row.previousElementSibling;
      }
      var day_row = entete_row;
      while (day_row.classList[0] != "tr_jour") {
        day_row = day_row.previousElementSibling;
      }
      if (entete_row.classList[0] == "tr_mecano") {
        postfix_row = "macano";
      }

      var date = jQuery("input.date", entete_row)[0].value;
      var employe = jQuery("input.employe", entete_row)[0].value;
      var tmp_row = entete_row.nextElementSibling;
      while (!found_destination) {
        if (tmp_row.classList[0] == "tr_ligne_rdv") {
          if (tmp_row.cells[0].className == "td_rdv rdv_vide") {
            tmp_row.cells[0].className = "td_rdv";
            tmp_row.cells[0].innerHTML = tmp_content;
            found_destination = true;
          } else if (tmp_row.cells[1].className == "td_rdv rdv_vide") {
            tmp_row.cells[1].className = "td_rdv";
            tmp_row.cells[1].innerHTML = tmp_content;
            found_destination = true;
          } else if (tmp_row.cells[2].className == "td_rdv rdv_vide") {
            tmp_row.cells[2].className = "td_rdv";
            tmp_row.cells[2].innerHTML = tmp_content;
            found_destination = true;
          } else if (tmp_row.cells[3].className == "td_rdv rdv_vide") {
            tmp_row.cells[3].className = "td_rdv";
            tmp_row.cells[3].innerHTML = tmp_content;
            found_destination = true;
          } else if (tmp_row.cells[4].className == "td_rdv rdv_vide") {
            tmp_row.cells[4].className = "td_rdv";
            tmp_row.cells[4].innerHTML = tmp_content;
            found_destination = true;
          } else {
            tmp_row = tmp_row.nextElementSibling;
          }
        } else {
          var new_row = tmp_row.parentElement.insertRow(tmp_row.rowIndex);
          new_row.className = "tr_ligne_rdv rdv_" + postfix_row; // TODO mecano vs commun
          new_row.innerHTML =
            '<td class="td_rdv">' +
            tmp_content +
            '</td><td class="td_rdv rdv_vide"></td><td class="td_rdv rdv_vide"></td><td class="td_rdv rdv_vide"></td><td class="td_rdv rdv_vide"></td>';
          day_row.cells[3].rowSpan++;
          day_row.cells[4].rowSpan++;
          //alert("create_new_row");
          found_destination = true;
        }
        //found_destination = true;
      }
      init_rdv_atelier_drag_and_drop();
      var a = 0;
      //alert(NoAtelier + " " + date + " " + employe);

      jQuery.ajax({
        url: "moveAtelierAjax.php",
        data: {
          NoAtelier: NoAtelier,
          NoSuite: NoSuite,
          date: date,
          employe: employe,
        },
        success: function (data) {
          //alert(data);
          //location.reload();
        },
      });
    },
  });
}
function appel(tel, interne) {
  jQuery.ajax({
    url: "http://10.3.33.8/click-to-call.php?ext=" + interne + "&dst=" + tel,
  });
}
function appel_avec_formatage(champ, interne) {
  tel = jQuery("[name='" + champ + "']").val();
  tel = tel.replace(/ /g, "");
  if (tel.substring(0, 2) == "00") {
    appel(tel, interne);
  } else if (tel.substring(0, 1) == "0") {
    appel("0041" + tel.substring(1), interne);
  } else if (tel.substring(0, 1) == "+") {
    appel("00" + tel.substring(1), interne);
  }
}
function imprimer_entete_client(noclient) {
  jQuery.get("print_entete_client.php?NoClient=" + noclient);
}
function save_email_ajax(noclient) {
  var newemail = jQuery(".newemailajax").val();
  var reg = new RegExp(
    "^[-a-zA-Z0-9_][-a-zA-Z0-9._]*@[a-zA-Z0-9-_]+.[a-zA-Z.-_]{1,}[a-zA-Z-_]+",
    "g"
  );
  if (reg.test(newemail) && newemail.length != 0) {
    jQuery.get(
      "change_email.php?NoClient=" + noclient + "&newemail=" + newemail
    );
    jQuery(".alertemail").hide();
  } else {
    alert("adresse e-mail invalide");
  }
}
function ignore_alerte_email_ajax(noclient) {
  if (confirm("Voulez-vous vraiment ignorer définitivement cette alerte ?")) {
    jQuery.get("ignore_alert_email.php?NoClient=" + noclient);
    jQuery(".alertemail").hide();
  }
}
function change_adresse_courrier(noclient) {
  Vwidth = 800;
  Vheight = 600;
  Vtop = (screen.height - Vheight) / 2;
  Vleft = (screen.width - Vwidth) / 2;
  window.open(
    "fast.php?NoClient=" + noclient + "&Type=3&Sous_Type=2&Etape=2&NoVisu=0",
    "",
    "top=" +
      Vtop +
      ", left=" +
      Vleft +
      ", width=" +
      Vwidth +
      ", height=" +
      Vheight +
      ", resizable=yes, toolbar=no, scrollbars=yes, status=no"
  );
}
