

//*************************************************
function table(nb_champs_max, nb_enregistrements_max)
{
this.nb_champs = 0 ;
this.champs = new Array(nb_champs_max) ;
this.ajouter_champ = table_ajouter_champ ;
this.nb_enregistrements = 0 ;
this.enregistrements = new Array(nb_enregistrements_max) ;
this.ajouter_enregistrement = table_ajouter_enregistrement ;
this.trier = table_trier ;

this.charger_fichier = table_charger_fichier ;
//fonction à l'usage spécifique des fichiers du pab 
this.charger_fichier_navire = table_charger_fichier_navire ;
this.charger_fichier_marees = table_charger_fichier_marees ;
}

//*************************************************
function table_ajouter_champ(nom, libelle, type_donnee, valeur_initiale, longueur)
{
var nouveau_champ ;

var nb_champs_max = this.champs.length ;
var nb_champs = this.nb_champs ;

if (nb_champs >= nb_champs_max) 
	return(null) ;

nouveau_champ = new champ(nom, libelle, type_donnee, valeur_initiale, longueur) ;
this.champs[nb_champs] = nouveau_champ ;
this.nb_champs = nb_champs + 1 ;

return(nouveau_champ) ;
}

//*************************************************
function table_ajouter_enregistrement()
{
var nouveau_enregistrement ;

var nb_enregistrements_max = this.enregistrements.length ;
var nb_enregistrements = this.nb_enregistrements ;

if (nb_enregistrements >= nb_enregistrements_max) 
	return(null) ;

nouveau_enregistrement = new enregistrement(this) ;
this.enregistrements[nb_enregistrements] = nouveau_enregistrement ;
this.nb_enregistrements = nb_enregistrements + 1 ;

return(nouveau_enregistrement) ;
}

//*************************************************
function champ(nom, libelle, type_donnee, valeur_initiale, longueur, position)
{
this.nom = nom ;
this.libelle = libelle ;
this.type_donnee = type_donnee ;
this.valeur = valeur_initiale ;
this.longueur = longueur ;
}

//*************************************************
// dans la page : var rs = ma_table.ajouter_enregistrement
// puis, rs.champ1 = valeur
function enregistrement(table)
{
var champ ;
var nb_champs = table.nb_champs ;
for ( i = 0 ; i < nb_champs ; i ++ )
	 {
	 champ = table.champs[i] ;
	 //debug("this." + champ.nom + " = '" + champ.valeur + "'") ;
	 eval("this." + champ.nom + " = '" + champ.valeur + "'") ;
	 }
}

//*************************************************
function table_trier(champ)
{
   var i, j = 0 ;
   var trieur = new Array(this.nb_enregistrements);
   var enregistrements = this.enregistrements ;
   // boucle sur les lignes
   for (i = 0 ; i < this.nb_enregistrements ; i ++ )
      // boucle sur les éléments d'une colonne
      for ( j = this.nb_enregistrements -1 ; j > i ; j -- )
         // si l'élément j est > à l'élément j-1, on les permute
         if ( enregistrements[j][champ] < enregistrements[j-1][champ] ) 
         {
            trieur = enregistrements[j] ;
            enregistrements[j] = enregistrements[j-1] ;
            enregistrements[j-1] = trieur ;
         }
}

//*************************************************
function table_charger_fichier(fichier)
{
var i, j, p, rs, champ ;
var longueur, valeur, chaine ;
var nb_enregistrements = fichier.length  ;
var nb_champs = this.nb_champs ;
for ( j = 1 ; j < nb_enregistrements ; j ++ )
	{
	chaine = fichier[j] ;
	rs = this.ajouter_enregistrement() ;
	p = 0
	for ( i = 0 ; i < nb_champs ; i ++ )
		{
		champ = this.champs[i] ;
		longueur = champ.longueur ;
		valeur = chaine.substring(p, (p + longueur)) ;
		eval ('rs.' + champ.nom + ' = "' + valeur + '"');
		p = p + longueur ;
		}
	}
}

//*************************************************
function table_charger_fichier_navire(fichier, article)
{
var i, j, p, rs, champ ;
var longueur, valeur, chaine ;
var nb_enregistrements = fichier.length  ;
var nb_champs = this.nb_champs ;

for ( j = 1 ; j < nb_enregistrements ; j ++ )
	{
	chaine = fichier[j] ;
	p = 0
	valeur = chaine.substring(8, 9) ; // teste l'article
	if (valeur == article )
		{	
		rs = this.ajouter_enregistrement() ;
		for ( i = 0 ; i < nb_champs ; i ++ )
			{
			champ = this.champs[i] ;
			longueur = champ.longueur ;
			valeur = chaine.substring(p, (p + longueur)) ;
			eval ('rs.' + champ.nom + ' = "' + valeur + '"');
			p = p + longueur ;	
			}
		}
	}
}

//*************************************************
function table_charger_fichier_marees(fichier)
{
var i, j, p, rs, champ ;
var longueur, valeur, chaine ;
var nb_enregistrements = fichier.length  ;
var nb_champs = this.nb_champs ;

for ( j = 1 ; j < nb_enregistrements ; j ++ )
	{
	chaine = fichier[j] ;
	p = 0
	rs = this.ajouter_enregistrement() ;
	for ( i = 0 ; i < nb_champs ; i ++ )
		{
		champ = this.champs[i] ;
		longueur = champ.longueur ;
		valeur = chaine.substring(p, (p + longueur)) ;
		eval ('rs.' + champ.nom + ' = "' + valeur + '"');
		p = p + longueur ;	
		}
	}
}

//*************************************************
function debug(message)
{
alert(message);
}
