var tabEtats = new Array();
var tabTarifs = new Array();
var tabKmActuel = new Array();
// var tabCC=new Array;
// var tabNCC=new Array;
// var tabECC=new Array;
function identifie_tarif() {
  debut = new Date();
  if (this.document.info.date_debut_new.value == "1") {
    debut.setFullYear(this.document.info.date_debut.value.substr(6, 4));
    debut.setMonth(this.document.info.date_debut.value.substr(3, 2) - 1);
    debut.setDate(this.document.info.date_debut.value.substr(0, 2));
  } else {
    debut.setFullYear(this.document.info.new_annee_debut.value);
    debut.setMonth(this.document.info.new_mois_debut.value - 1);
    debut.setDate(this.document.info.new_jour_debut.value);
  }
  debut.setHours(this.document.info.new_heure_debut.value);
  debut.setMinutes(this.document.info.new_minutes_debut.value);
  debut.setSeconds(0);
  fin = new Date();
  if (this.document.info.date_fin_new.value == "1") {
    fin.setFullYear(this.document.info.date_fin.value.substr(6, 4));
    fin.setMonth(this.document.info.date_fin.value.substr(3, 2) - 1);
    fin.setDate(this.document.info.date_fin.value.substr(0, 2));
  } else {
    fin.setFullYear(this.document.info.new_annee_fin.value);
    fin.setMonth(this.document.info.new_mois_fin.value - 1);
    fin.setDate(this.document.info.new_jour_fin.value);
  }
  fin.setHours(this.document.info.new_heure_fin.value);
  fin.setMinutes(this.document.info.new_minutes_fin.value);
  fin.setSeconds(0);
  dms = fin.getTime() - debut.getTime();
  if (dms < 0) {
    alert("date de debut plus grande que date de fin"); // >
    return;
  }
  ds = dms / 1000;
  dh = ds / 3600;
  dj = dh / 24;
  nbj = Math.ceil(dj - 2 / 24);
  switch (nbj) {
    case 1:
      this.document.info.new_tarif_type.value = 1;
      this.document.info.nb_jours.value = nbj;
      break;
    case 2:
      if (
        debut.getDay() == 6 &&
        debut.getHours() >= 11 &&
        debut.getHours() <= 13 &&
        fin.getDay() == 1 &&
        fin.getHours() >= 8 &&
        fin.getHours() <= 10
      ) {
        this.document.info.new_tarif_type.value = 7;
        this.document.info.nb_jours.value = 1;
      } else {
        this.document.info.new_tarif_type.value = 1;
        this.document.info.nb_jours.value = nbj;
      }
      break;
    case 3:
      if (
        debut.getDay() == 5 &&
        debut.getHours() >= 16 &&
        debut.getHours() <= 18 &&
        fin.getDay() == 1 &&
        fin.getHours() >= 8 &&
        fin.getHours() <= 10
      ) {
        this.document.info.new_tarif_type.value = 8;
        this.document.info.nb_jours.value = 1;
      } else {
        this.document.info.new_tarif_type.value = 2;
        this.document.info.nb_jours.value = nbj;
      }
      break;
    case 4:
      if (
        debut.getDay() == 4 &&
        debut.getHours() >= 8 &&
        debut.getHours() <= 10 &&
        fin.getDay() == 1 &&
        fin.getHours() >= 8 &&
        fin.getHours() <= 10
      ) {
        this.document.info.new_tarif_type.value = 9;
        this.document.info.nb_jours.value = 1;
      } else {
        this.document.info.new_tarif_type.value = 2;
        this.document.info.nb_jours.value = nbj;
      }
      break;
    case 5:
    case 6:
      this.document.info.new_tarif_type.value = 2;
      this.document.info.nb_jours.value = nbj;
      break;
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
      this.document.info.new_tarif_type.value = 3;
      this.document.info.nb_jours.value = nbj;
      break;
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
      this.document.info.new_tarif_type.value = 4;
      this.document.info.nb_jours.value = nbj;
      break;
    //    	this.document.info.new_tarif_type.value=5;
    // 	    this.document.info.nb_jours.value=nbj;
    //    	break;
    default:
      this.document.info.new_tarif_type.value = 6;
      this.document.info.nb_jours.value = deux_dec(nbj / 30);
      break;
  }
  modif_tarif();
}
function modif_vhc() {
  //tarif_actuel = this.document.info.new_vhc.selectedIndex;
  var NoChasis = this.document.info.new_vhc.value;
  //alert(NoChasis);
  //alert(commande_ajax('import_vhc_location.php?NoChasis='+NoChasis));
  commande_ajax_javascript("import_vhc_location.php?NoChasis=" + NoChasis);
  this.document.info.etat.value = document.etatActuel;
  this.document.info.KmDepart.value = document.kmActuel;
  //alert(document.etatActuel);
  identifie_tarif();
}
function modif_tarif() {
  this.document.info.tarif.value =
    document.tarifs[this.document.info.new_tarif_type.value - 1];
  modif_prix();
}
function modif_prix() {
  modif_nb();
}
function modif_nb() {
  this.document.info.total.value = deux_dec(
    this.document.info.tarif.value *
      this.document.info.nb_jours.value *
      ((100.0 + this.document.info.rabais.value * 1.0) / 100.0)
  );
}
function is_we(jour, mois, annee) {
  return 0;
}
function cc_client() {
  this.document.info.cc.value =
    tabCC[this.document.info.NoClient.selectedIndex];
  this.document.info.ncc.value =
    tabNCC[this.document.info.NoClient.selectedIndex];
  this.document.info.ecc1.value = tabECC[
    this.document.info.NoClient.selectedIndex
  ].substr(5, 2);
  this.document.info.ecc2.value = tabECC[
    this.document.info.NoClient.selectedIndex
  ].substr(0, 4);
  this.document.info.scc.value =
    tabSCC[this.document.info.NoClient.selectedIndex];
}
function change_caution() {
  if (
    this.document.info.caution.value == 0 ||
    this.document.info.caution.value == 5
  ) {
    this.document.info.ncc.style.visibility = "hidden";
    this.document.info.ecc1.style.visibility = "hidden";
    this.document.info.ecc2.style.visibility = "hidden";
    this.document.info.scc.style.visibility = "hidden";
  } else {
    this.document.info.ncc.style.visibility = "visible";
    this.document.info.ecc1.style.visibility = "visible";
    this.document.info.ecc2.style.visibility = "visible";
    this.document.info.scc.style.visibility = "visible";
  }
}
function valide_suppresion() {
  if (confirm("Voulez-vous vraiment supprimer cette location ?")) {
    this.document.infodel.submit();
  }
}
function copie_tot_cont() {
  this.document.info.total_cont.value = this.document.info.total.value;
  calcul_tot();
}
function deux_dec(nombre_in) {
  var nombre = new String(nombre_in);
  var out = new String("");
  var Taille = nombre.length;
  var Position = -1;
  if (Taille == 0) {
    out = "0.00";
  } else {
    for (cont = 0; cont < Taille; cont++) {
      if (nombre.charCodeAt(cont) == 46) {
        Position = cont;
      } else {
      }
    }
    var Diff = Taille - Position;
    if (Position == -1) {
      out = nombre + ".00";
    } else {
      if (Diff < 3) {
        out = nombre;
        for (cont = Diff; cont < 3; cont++) {
          out = out + "0";
        }
      } else {
        out = nombre.substring(0, Position + 3);
      }
    }
  }
  return out;
}
function calcul_tot() {
  this.document.info.total_fact.value = deux_dec(
    parseFloat(this.document.info.total_cont.value) +
      parseFloat(this.document.info.total_km.value) +
      parseFloat(this.document.info.total_frais.value)
  );
}
function calcule_rabais() {
  this.document.info.rabais.value =
    Math.round(
      (-1 +
        this.document.info.total.value /
          (this.document.info.tarif.value *
            this.document.info.nb_jours.value)) *
        10000
    ) / 100;
}
function recherche_client() {
  Nom = this.info.elements["Nom"].value;
  Prenom = this.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=1",
      "",
      "top=0, left=0, width=10, height=10, resizable=yes, toolbar=no, scrollbars=yes, status=no"
    );
  }
}
function go_facture(NoCommande, NoClient) {
  this.opener.location =
    "default.php?Type=4&Sous_Type=1&Etape=3&New=0&NoCommande=" +
    NoCommande +
    "&NoClient=" +
    NoClient;
  window.close();
}
function tab_date() {
  if (this.document.info.ecc1.value.length == 2) {
    this.document.info.ecc2.select();
  }
}
function check_client() {
  if (this.document.info.Nom.value.length == 0) {
    alert("Vous devez saisir le nom du client");
    return false;
  }
  if (this.document.info.Telephone.value.length == 0) {
    if (!confirm("vous n'avez pas saisi de no de téléphone")) {
      return false;
    }
  }
  return true;
}
function check_no_client() {
  if (this.document.info.NoClient.value.length == 0) {
    alert("Vous devez assigner un numéro de client");
    return false;
  }
  return true;
}
function check_emp_rdv() {
  if (this.document.info.EmployeRDV.value == 0) {
    alert("Vous devez sélectionner un employé pour le RDV");
    return false;
  }
  return true;
}
function check_emp_depart() {
  if (this.document.info.EmployeDepart.value == 0) {
    alert("Vous devez sélectionner un employé pour le départ");
    return false;
  }
  return true;
}
function check_caution() {
  if (
    this.document.info.caution.value > 0 &&
    this.document.info.caution.value != 5
  ) {
    if (this.document.info.caution.value == 3) {
      if (this.document.info.ncc.value.length != 15) {
        alert("Le numéro de carte amex doit avoir 15 chiffres");
        return false;
      }
    } else if (this.document.info.ncc.value.length != 16) {
      alert("Le numéro de carte de crédit doit avoir 16 chiffres");
      return false;
    }
    if (
      this.document.info.ecc1.value.length != 2 ||
      this.document.info.ecc2.value.length != 2
    ) {
      alert("L'expiration de la carte de crédit doit être saisie");
      return false;
    }
    if (this.document.info.scc.value.length != 3) {
      alert("Le numéro sécurité de carte de crédit doit avoir 3 chiffres");
      return false;
    }
  }
  return true;
}
function check_type_caution() {
  if (this.document.info.caution.value == 6) {
    alert("Vous devez choisir le type de caution");
    return false;
  }
  return true;
}
function check_intervalle() {
  debut = new Date();
  if (this.document.info.date_debut_new.value == "1") {
    debut.setFullYear(this.document.info.date_debut.value.substr(6, 4));
    debut.setMonth(this.document.info.date_debut.value.substr(3, 2) - 1);
    debut.setDate(this.document.info.date_debut.value.substr(0, 2));
  } else {
    debut.setFullYear(this.document.info.new_annee_debut.value);
    debut.setMonth(this.document.info.new_mois_debut.value - 1);
    debut.setDate(this.document.info.new_jour_debut.value);
  }
  debut.setHours(this.document.info.new_heure_debut.value);
  debut.setMinutes(this.document.info.new_minutes_debut.value);
  debut.setSeconds(0);
  fin = new Date();
  if (this.document.info.date_fin_new.value == "1") {
    fin.setFullYear(this.document.info.date_fin.value.substr(6, 4));
    fin.setMonth(this.document.info.date_fin.value.substr(3, 2) - 1);
    fin.setDate(this.document.info.date_fin.value.substr(0, 2));
  } else {
    fin.setFullYear(this.document.info.new_annee_fin.value);
    fin.setMonth(this.document.info.new_mois_fin.value - 1);
    fin.setDate(this.document.info.new_jour_fin.value);
  }
  fin.setHours(this.document.info.new_heure_fin.value);
  fin.setMinutes(this.document.info.new_minutes_fin.value);
  fin.setSeconds(0);
  dms = fin.getTime() - debut.getTime();
  if (dms < 0) {
    alert(
      "date de debut plus grande que date de fin " +
        debut.toGMTString() +
        " " +
        fin.toGMTString()
    ); // >
    return false;
  }
  return true;
}
function check_km_retour() {
  if (this.document.info.KmRetour.value == this.document.info.KmDepart.value) {
    if (!confirm("vous devez rentrer les kilomètres de retour")) {
      return false;
    }
  }
  return true;
}
function check_dispo(etape) {
  if (this.document.info.upd == "undefined" || this.document.info.upd == null) {
    varnew = "1";
  } else {
    varnew =
      "0&Start=" +
      this.document.info.Start.value +
      "&old_vhc=" +
      this.document.info.vhc.value;
  }
  new_vhc = this.document.info.new_vhc.value;
  if (etape == 0) {
    if (this.document.info.date_debut_new.value == "1") {
      debut = format_date_sql(
        this.document.info.date_debut.value.substr(6, 4),
        this.document.info.date_debut.value.substr(3, 2),
        this.document.info.date_debut.value.substr(0, 2),
        this.document.info.new_heure_debut.value,
        this.document.info.new_minutes_debut.value
      );
    } else {
      debut = format_date_sql(
        this.document.info.new_annee_debut.value,
        this.document.info.new_mois_debut.value,
        this.document.info.new_jour_debut.value,
        this.document.info.new_heure_debut.value,
        this.document.info.new_minutes_debut.value
      );
    }
  } else {
    debut = format_date_sql(
      this.document.info.de_annee.value,
      this.document.info.de_mois.value,
      this.document.info.de_jour.value,
      this.document.info.de_heure.value,
      this.document.info.de_minute.value
    );
  }
  if (etape < 2) {
    if (this.document.info.date_fin_new.value == "1") {
      fin = format_date_sql(
        this.document.info.date_fin.value.substr(6, 4),
        this.document.info.date_fin.value.substr(3, 2),
        this.document.info.date_fin.value.substr(0, 2),
        this.document.info.new_heure_fin.value,
        this.document.info.new_minutes_fin.value
      );
    } else {
      fin = format_date_sql(
        this.document.info.new_annee_fin.value,
        this.document.info.new_mois_fin.value,
        this.document.info.new_jour_fin.value,
        this.document.info.new_heure_fin.value,
        this.document.info.new_minutes_fin.value
      );
    }
  } else {
    fin = format_date_sql(
      this.document.info.re_annee.value,
      this.document.info.re_mois.value,
      this.document.info.re_jour.value,
      this.document.info.re_heure.value,
      this.document.info.re_minute.value
    );
  }
  //alert(new_vhc+" "+debut+" "+fin);
  result = commande_ajax(
    "recherche_dispo_vhc_loc.php?new=" +
      varnew +
      "&vhc=" +
      new_vhc +
      "&debut=" +
      debut +
      "&fin=" +
      fin
  );
  if (result.length != 0) {
    alert(result);
    return false;
  }
  return true;
}
function format_date_sql(annee, mois, jour, heure, minute) {
  if (annee.length != 4) {
    return "";
  }
  str = annee + "-";
  if (mois.length == 1) {
    str = str + "0";
  }
  str = str + mois + "-";
  if (jour.length == 1) {
    str = str + "0";
  }
  str = str + jour + " ";
  if (heure.length == 1) {
    str = str + "0";
  }
  str = str + heure + ":";
  if (minute.length == 1) {
    str = str + "0";
  }
  str = str + minute + ":00";
  return str;
}
function valider() {
  if (
    check_client() &&
    check_dispo(this.document.info.etape.value) &&
    check_intervalle() &&
    check_emp_rdv()
  ) {
    if (
      this.document.info.acompte == "undefined" ||
      this.document.info.acompte == null
    ) {
      acompte = 0;
    } else {
      acompte = this.document.info.acompte.value;
    }
    switch (this.document.info.etape.value) {
      case 0:
      case "0":
        if (acompte != 0) {
          if (!check_no_client()) {
            return;
          }
        }
        break;
      case 1:
      case "1":
        if (!check_emp_depart()) {
          return;
        }
        if (acompte != 0) {
          if (!check_no_client()) {
            return;
          }
        }
        break;
      case 2:
      case "2":
        if (check_no_client() && check_caution() && check_emp_depart()) {
        } else {
          return;
        }
        break;
      case 3:
      case "3":
        if (check_no_client() && check_emp_depart()) {
        } else {
          return;
        }
        break;
    }
    this.document.info.submit();
  }
}
function imprimer_contrat() {
  if (
    check_client() &&
    check_dispo(this.document.info.etape.value) &&
    check_intervalle() &&
    check_emp_rdv()
  ) {
    if (
      this.document.info.acompte == "undefined" ||
      this.document.info.acompte == null
    ) {
      acompte = 0;
    } else {
      acompte = this.document.info.acompte.value;
    }
    switch (this.document.info.etape.value) {
      case 0:
      case "0":
        if (acompte != 0) {
          if (!check_no_client()) {
            return;
          }
        }
        break;
      case 1:
      case "1":
        if (!check_emp_depart()) {
          return;
        }
        if (acompte != 0) {
          if (!check_no_client()) {
            return;
          }
        }
        break;
      case 2:
      case "2":
        if (check_no_client() && check_caution() && check_emp_depart()) {
        } else {
          return;
        }
        break;
      case 3:
      case "3":
        if (check_no_client() && check_emp_depart()) {
        } else {
          return;
        }
        break;
    }
    this.document.info.pri.value = 1;
    this.document.info.submit();
  }
}
function imprimer_contrat_pdf() {
  if (
    check_client() &&
    check_dispo(this.document.info.etape.value) &&
    check_intervalle() &&
    check_emp_rdv()
  ) {
    if (
      this.document.info.acompte == "undefined" ||
      this.document.info.acompte == null
    ) {
      acompte = 0;
    } else {
      acompte = this.document.info.acompte.value;
    }
    switch (this.document.info.etape.value) {
      case 0:
      case "0":
        if (acompte != 0) {
          if (!check_no_client()) {
            return;
          }
        }
        break;
      case 1:
      case "1":
        if (!check_emp_depart()) {
          return;
        }
        if (acompte != 0) {
          if (!check_no_client()) {
            return;
          }
        }
        break;
      case 2:
      case "2":
        if (check_no_client() && check_caution() && check_emp_depart()) {
        } else {
          return;
        }
        break;
      case 3:
      case "3":
        if (check_no_client() && check_emp_depart()) {
        } else {
          return;
        }
        break;
    }
    this.document.info.pri.value = 3;
    this.document.info.submit();
  }
}
function demarrer_imprimer_contrat_facture() {
  if (
    check_client() &&
    check_dispo(1) &&
    check_intervalle() &&
    check_no_client() &&
    check_caution() &&
    check_emp_rdv() &&
    check_emp_depart() &&
    check_type_caution()
  ) {
    this.document.info.etape.value = 1;
    this.document.info.pri.value = 2;
    this.document.info.fact.value = 1;
    this.document.info.submit();
  }
}
function demarrer_imprimer_contrat_pdf_facture() {
  if (
    check_client() &&
    check_dispo(1) &&
    check_intervalle() &&
    check_no_client() &&
    check_caution() &&
    check_emp_rdv() &&
    check_emp_depart() &&
    check_type_caution()
  ) {
    this.document.info.etape.value = 1;
    this.document.info.pri.value = 3;
    this.document.info.fact.value = 1;
    this.document.info.submit();
  }
}
function debut_pret() {
  if (
    check_client() &&
    check_dispo(1) &&
    check_intervalle() &&
    check_emp_rdv() &&
    check_emp_depart()
  ) {
    this.document.info.acompte.value = 0;
    this.document.info.caution.selectedIndex = 5;
    this.document.info.Pret.selectedIndex = 1;
    this.document.info.etape.value = 1;
    this.document.info.submit();
  }
}
function retour_sans_facture() {
  if (
    check_client() &&
    check_dispo(3) &&
    check_intervalle() &&
    check_no_client() &&
    check_km_retour() &&
    check_emp_rdv() &&
    check_emp_depart()
  ) {
    this.document.info.etape.value = 3;
    this.document.info.fact.value = 0;
    this.document.info.pri.value = 0;
    this.document.info.submit();
  }
}
function retour_avec_facture() {
  if (
    check_client() &&
    check_dispo(2) &&
    check_intervalle() &&
    check_no_client() &&
    check_km_retour() &&
    check_caution() &&
    check_emp_rdv() &&
    check_emp_depart()
  ) {
    this.document.info.etape.value = 2;
    this.document.info.fact.value = 1;
    this.document.info.pri.value = 0;
    this.document.info.submit();
  }
}
function retour_location() {
  if (
    check_client() &&
    check_dispo(2) &&
    check_intervalle() &&
    check_no_client() &&
    check_km_retour() &&
    check_caution() &&
    check_emp_rdv() &&
    check_emp_depart()
  ) {
    this.document.info.etape.value = 2;
    this.document.info.fact.value = 0;
    this.document.info.pri.value = 0;
    this.document.info.submit();
  }
}
function demarrer_imprimer_contrat() {
  if (
    check_client() &&
    check_dispo(1) &&
    check_intervalle() &&
    check_emp_rdv() &&
    check_emp_depart() &&
    check_type_caution()
  ) {
    if (
      this.document.info.acompte == "undefined" ||
      this.document.info.acompte == null
    ) {
      acompte = 0;
    } else {
      acompte = this.document.info.acompte.value;
    }
    if (acompte != 0) {
      if (!check_no_client()) {
        return;
      }
    }
    this.document.info.pri.value = 2;
    this.document.info.etape.value = 1;
    this.document.info.fact.value = 0;
    this.document.info.submit();
  }
}
function demarrer_imprimer_contrat_pdf() {
  if (
    check_client() &&
    check_dispo(1) &&
    check_intervalle() &&
    check_emp_rdv() &&
    check_emp_depart() &&
    check_type_caution()
  ) {
    if (
      this.document.info.acompte == "undefined" ||
      this.document.info.acompte == null
    ) {
      acompte = 0;
    } else {
      acompte = this.document.info.acompte.value;
    }
    if (acompte != 0) {
      if (!check_no_client()) {
        return;
      }
    }
    this.document.info.pri.value = 3;
    this.document.info.etape.value = 1;
    this.document.info.fact.value = 0;
    this.document.info.submit();
  }
}
function creer_facture() {
  if (
    check_client() &&
    check_dispo(this.document.info.etape.value) &&
    check_intervalle() &&
    check_no_client() &&
    check_caution() &&
    check_emp_rdv() &&
    check_emp_depart()
  ) {
    this.document.info.fact.value = 1;
    this.document.info.submit();
  }
}
$(document).ready(function ($) {
  $(".BtDecrypt").click(function () {
    var file = $(".KeyFile").prop("files")[0];
    var fileReader = new FileReader();
    fileReader.readAsText(file);
    fileReader.onload = function (e) {
      var enc = $(".EncText").val();
      $.ajax({
        url: "decrypt.php",
        data: { key: fileReader.result, enc: enc },
      }).done(function (data) {
        var resArr = data.split("-");
        if (resArr.length == 4) {
          $('input[name="ncc"]').val(resArr[0]);
          $('input[name="scc"]').val(resArr[1]);
          $('input[name="ecc1"]').val(resArr[2]);
          $('input[name="ecc2"]').val(resArr[3]);
        } else {
        }
      });
    };
  });
});
