// Script de getion du catalogue de l'arquebuse
// Declaration des variables
var LARGEUR=6;

var v_catalogue;
v_catalogue = new Array();

//Corps des fonctions
//creeCatalogue : cree le catalogue a partir des articles
function creeCatalogue() {
  var i=0;
  // La syntaxe est la suivante :  
  //                                 
  // v_catalogue[i]=new article(  
  //    "NomArticle",       
  // <- Nom de l'article apparaissant dans le catalogue  
  //    "cheminFiche.html", // <- Chemin du fichier decrivant l'article entre "" ou "" sinon  
  //    prixUnitaire); i++;  // <- prix unitaire de l'article  
  // Exemples :  
  //   Pour un fichier .jpg :  
  //v_catalogue[i]=new article("article 2", "scan/logo.jpg",30); i++;
  //   Pour un fichier htm ou html :
  //  v_catalogue[i]=new article("Munitions 9mm", "tableaux.htm",10); i++;
  v_catalogue[i]=new article(
    "Munitions",
	"",
	-1);  i++;
  v_catalogue[i]=new article(prix_unit
    "38 Spécial FMJ Sellier Bellot<BR>50 cart",
	"fiches_techniques/ft38.htm",
	Nous Consulter);  i++;
  v_catalogue[i]=new article(
    "9para FMJ Sellier Bellot En PROMOTION Quantité limitée<BR>50 cart",
	"fiches_techniques/ft9.htm",
	Nous Consulter);  i++;
  v_catalogue[i]=new article(
    "357Mag FMJ Sellier Bellot<BR>50 cart",
	"fiches_techniques/ft357.htm",
	Nous Consulter); i++;
  v_catalogue[i]=new article(
    "45Acp FMJ Sellier Bellot<BR>50 cart",
	"fiches_techniques/ft45.htm",
	Nous Consulter); i++;
  v_catalogue[i]=new article(
    "44Mag FMJ<BR>50 cart",
	"fiches_techniques/ft44.htm",
	25); i++;
  v_catalogue[i]=new article(
    "22LR CCI Blazer<BR>500 cart",
    "fiches_techniques/ft22Blazer.htm",
	21); i++;
  v_catalogue[i]=new article(
    "22LR CCI Blazer<BR>1000 cart",
	"fiches_techniques/ft22Blazer.htm",
	40); i++;
  v_catalogue[i]=new article(
    "22LR CCI Blazer<BR>5000 cart",
	"fiches_techniques/ft22Blazer.htm",
	185); i++;
  v_catalogue[i]=new article(
    "Armes",
	"",
	-1); i++;
  v_catalogue[i]=new article(
    "CZ 75TS<BR>PA<BR>40 SW",
	"",
	1265); i++;
  v_catalogue[i]=new article(
    "SMITH & WESSON mod 22 spécial TAR<BR>Revolver<BR>45 Acp",
	"",
	990); i++;
  v_catalogue[i]=new article(
    "SMITH & WESSON mod 1911 Pro Serie<BR>PA<BR>45 Acp",
	"",
	1245); i++;
  v_catalogue[i]=new article(
    "PROMOTION TANFOGLIO NEUFS<BR>PA<BR>9 para",
	"",
	480); i++;
  v_catalogue[i]=new article(
    "SMITH & WESSON 686<BR>Revolver<BR>357 Mag",
	"scan/sw 686.jpg",
	1080); i++;
  v_catalogue[i]=new article(
    "NORINCO<BR>Fusil<BR>calibre 12",
	"",
	555); i++;
  v_catalogue[i]=new article(
    "VZ858<BR>Carabine<BR>calibre 7,62x39",
	"",
	340);  i++;
  v_catalogue[i]=new article(
    "SAFEGOM<BR>revolver<BR>avec 2 barillets",
	"scan/SAFEGOM.jpg",
	215); i++;
  v_catalogue[i]=new article(
    "FLASH BALL 7ème catégorie<BR>arme de défense<BR>2 coups",
	"scan/flash.jpg",
	430); i++;
  v_catalogue[i]=new article(
    "Divers",
	"",
	-1); i++;
  v_catalogue[i]=new article(
    "GC 27<BR>revolver de défense",
	"scan/gc 27.jpg",
	105); i++;
  v_catalogue[i]=new article(
    "GC 54<BR>revolver de défense",
 	"scan/.jpg",
	151); i++;
  v_catalogue[i]=new article(
    "TONFA<BR>nouvelle forme",
	"scan/TONFA2.jpg",
	27); i++;
  v_catalogue[i]=new article(
    "MASQUE A GAZ COMPLET NBC<BR>nouvelle fabrication",
	"scan/masque gaz.jpg",
	249); i++;
  v_catalogue[i]=new article(
    "Promo",
	"",
	-1); i++;
  v_catalogue[i]=new article(
    "douilles tirées une fois calibre 38 <BR>2000 cart, frais de port compris",
	"",
	20); i++;
 }
//article : constructeur de chaque article du catalogue
function article(intitule, fiche, prix_unit) {
  this.intitule    = intitule;
  this.prix_unit   = prix_unit;
  if(fiche != "")  
  {
    this.fiche_dispo = "<A TARGET=_blank "+
	"HREF=\""+fiche+"\">"+
	"<img src='scan/fich_tech.jpg'></A>";
  } else {
    this.fiche_dispo = "Pas de Fiche";
  }
}
//incQte et decQte : fonctions de gestion des quantites d'articles
function incQte(ind) {
  j=eval(2*ind);
  document.catalogue.elements[j].value=
    parseInt(document.catalogue.elements[j].value)+1;
  calcCmde();
}
function decQte(ind) {
  j=eval(2*ind);
  if(eval(document.catalogue.elements[j].value)>0)
    document.catalogue.elements[j].value =
  	  parseInt(document.catalogue.elements[j].value)-1;
  else
    document.catalogue.elements[j].value = 0;
  calcCmde();
}
// calcCmde : fonction recalculant le total de la commande
function calcCmde() {
  val_total_franc = 0.;
  val_total_euro = 0.;
  val_synthese = "\n\t";
  char_separateur = "<BR>......................... ";
  i=0;
  // + 7 elements de saisie d'adresse
  // + 2 elements de selection livraison
  // + 1 bouton de soumission
  // + 1 variable cachee de synthese de commande
  // = 11 -1 car demarre de 0
  //alert(v_catalogue.length);
  longueur = eval(document.catalogue.elements.length-10);
  //alert(longueur);
  nb_articles = eval((longueur-3)/2);
  //alert(nb_articles);
  // Le tableau contient 2 var par article + 3 var dt 1 cachee a la fin
  //    total , total_euro, synthese (cachee) 
  while(i<nb_articles)
  {
    offset=eval(2*i);
    nbArticle   = document.catalogue.elements[offset].value;
    prixArticle = v_catalogue[i].prix_unit;
    document.catalogue.elements[offset+1].value =
  	  eval(nbArticle*prixArticle);
    val_total_euro += eval(document.forms[0].elements[offset+1].value);
    if(nbArticle>0)
  	  val_synthese += '=> '+v_catalogue[i].intitule + char_separateur +
	  nbArticle + " x " +
	  prixArticle + "<BR>";
    i++;
  }
  val_total_franc = parseInt(val_total_euro*6.55957);
//  val_total_franc = eval(val_total_euro/100);
  document.forms[0].elements[longueur-3].value = val_total_franc;
  document.forms[0].elements[longueur-2].value = val_total_euro;
  document.forms[0].elements[longueur-1].value = val_synthese +
  "Total en EUROS  : " + val_total_euro + " EUROS<BR>" +
  "Total en FRANCS : " + val_total_franc + " FRANCS <BR>";
}
// Chargement du catalogue
creeCatalogue();
// Creation dynamique du tableau
document.write('<CENTER>');
document.write('<TABLE CELLPADDING=2 border=1 bordercolor="#FFFFFF" bgcolor="LightGrey">');
document.write('<TR bgcolor=#FFA500>');
document.write('<TD ALIGN=CENTER ><B>Article</B></TD>');
document.write('<TD ALIGN=CENTER ><B>Prix</B><BR><B>Unitaire</B></TD>');
document.write('<TD ALIGN=CENTER ><B>Fiche<BR> Technique</B></TD>');
document.write('<TD ALIGN=CENTER ><B>Quantit&eacute; </B></TD>');
document.write('<TD ALIGN=CENTER ><B>Total</B></TD>');
document.write('</TR>');
i=0;
while(i<v_catalogue.length) 
{
    if(v_catalogue[i].prix_unit<0)  
    {
     // Cas d'un titre de rubrique    
	    document.write(
		  '<TR bgcolor=#FFA500><TD COLSPAN=5><STRONG>'+
		  v_catalogue[i].intitule+'</STRONG>'+
		  '  <input type="hidden" name="qte_'+i+'" size='+LARGEUR +
		  '  value="0" READONLY> '+
		  '  <input type="hidden" name="tot_'+i+'" '+
		  '   size=' + eval(LARGEUR+2) +
		  '   align="right" value="0" READONLY></TD>'+
		  '</TR>'
        );
        i++;
    }  else {
 // cas nominal d'un article en vente
      document.write(
	    '<TR><TD><STRONG>'+
	    v_catalogue[i].intitule+'</STRONG></TD>'
      );
      document.write(
	    '<TD align="right">'+
	    v_catalogue[i].prix_unit+' € </TD>'
      );
      document.write(
	    '<TD align="center">'+
	    v_catalogue[i].fiche_dispo+'</TD>'
      );
      document.write(
	    '<TD bgcolor=#FFA500 align="center" valign="middle">'+
	    '  <IMG src="scan/plus.jpg"  OnMouseUp=incQte('+i+')>'+
	    '  <IMG src="scan/moins.jpg" onMouseUp=decQte('+i+')>'+
	    '  <input type="text" name="qte_'+i+'" size='+LARGEUR +
	    '  value="0" READONLY> '+
	    '</TD>'
      );
      document.write(
	    '<TD align="center">'+
	    '  <input type="text" name="tot_'+i+'" '+
	    '   size=' + eval(LARGEUR+2) +
	    '   align="right" bgcolor="yellow" value="0" READONLY></TD>'+
	    '</TR>'
      );
      i++;
    }
}
document.write(
    '<TR Border=0>'+
    '<TD BgColor=#FFA500 colspan=4><STRONG>Total de la commande en <Font Color=#FF0000>FRANCS</FONT></STRONG></TD>'+
    '<TD BgColor=#FF0000    align="center">'+
    '  <input type="text" name="total" size='+eval(LARGEUR+2)+' READONLY ></TD>'+
    '</TR>'
	);
document.write(
    '<TR Border=0>'+
    '<TD BgColor=#FFA500 colspan=4>'+
    '<STRONG>Total de la commande en <Font Color=#FF0000>EUROS</FONT></STRONG></TD>'+
    '<TD BgColor=#FF0000 align="center">'+
    '<input type="text" name="total_euro" READONLY size='+eval(LARGEUR+2)+
    '></TD></TR>'
	);
document.write('</TR></TABLE></CENTER>');
// fin du script
