﻿// Code source sous licence GNU GPL : http://robloche.free.fr modifié et amélioré pour l'iphone par http://www.celka.com
// L'objectif de la licence GNU GPL, selon ses créateurs est de garantir à l'utilisateur les droits suivants (appelés libertés) sur un programme informatique :
//  1. la liberté d'exécuter le logiciel, pour n'importe quel usage ;
//  2. la liberté d'étudier le fonctionnement d'un programme et de l'adapter à ses besoins, ce qui passe par l'accès aux codes sources ;
//  3. la liberté de redistribuer des copies ;
//  4. la liberté d'améliorer le programme et de rendre publiques les modifications afin que l'ensemble de la communauté en bénéficie.
//  Le présent code est régit lui aussi par la licence GNU GPL

function List() {
	// Attributs
	this.nb            = 0;      // nombres d'éléments
	this.selection     = [];     // liste triée des indices des éléments sélectionnés
	this.multi         = true;   // sélection multiple permise (true) ou non (false)
	this.selection_ptr = -1;     // pointeur de parcours de la sélection

	// Fonctions membres
	this.isMultipleSelectionEnabled = List_isMultipleSelectionEnabled;
	this.setMultipleSelection       = List_setMultipleSelection;
	
	this.addItem                    = List_addItem;
	this.deleteItem                 = List_deleteItem;
	this.deleteSelection            = List_deleteSelection;
	this.isSelectionEmpty           = List_isSelectionEmpty;
	this.isInSelection              = List_isInSelection;
	this.selectIndex                = List_selectIndex;
	this.deselectIndex              = List_deselectIndex;

	this.getSelectionLength         = List_getSelectionLength;
	this.getFirstSelectedIndex      = List_getFirstSelectedIndex;
	this.getLastSelectedIndex       = List_getLastSelectedIndex;
	
	// Fonctions de parcours de la sélection
	this.initSelectionPtr           = List_initSelectionPtr;
	this.getCurrentSelectionPtr     = List_getCurrentSelectionPtr;
	this.getCurrentSelectionIndex   = List_getCurrentSelectionIndex;
	this.setCurrentSelectionIndex   = List_setCurrentSelectionIndex;
	this.nextSelectionPtr           = List_nextSelectionPtr;
	this.isAtSelectionEnd           = List_isAtSelectionEnd;
	this.getItem                    = List_getItem;

	this.getLength                  = List_getLength;
	this.clearList                  = List_clearList;
	this.selectAll                  = List_selectAll;
	this.selectNone                 = List_selectNone;
	this.invertSelection            = List_invertSelection;
}

// Retourne true si la sélection multiple est autorisée, false sinon
function List_isMultipleSelectionEnabled() {
	return this.multi;
}

// Autorise (true) ou interdit (false) la sélection multiple
function List_setMultipleSelection(flag) {
	this.multi = flag;
}

// Retourne la taille de la sélection
function List_getSelectionLength() {
	return this.selection.length;
}

// Désélectionne tous les éléments de la liste
function List_selectNone() {
	this.selection = [];
}


// sélectionne tous les éléments de la liste
function List_selectAll() {
	this.selection = [];
	for(var i=0; i<this.getLength(); ++i) this.selection.push(i);
}

// Inverse la sélection
function List_invertSelection() {
	var tmp = [];
	var j   = 0;
	for(var i=0; i<this.getLength(); ++i) {
		if(this.selection[j] == i) {
			++j;
			continue;
		}
		tmp.push(i);
	}
	this.selection = tmp;
}

// Retourne true si la sélection est vide, false sinon
function List_isSelectionEmpty() {
	return this.getSelectionLength() == 0;
}

// Retourne true si i est dans la sélection
function List_isInSelection(i) {
	if(this.isSelectionEmpty()) return false;
	this.initSelectionPtr();
	while(!this.isAtSelectionEnd()) {
		if(this.getCurrentSelectionIndex() == i)
			return true;
		if(this.getCurrentSelectionIndex() > i)
			return false;
		this.nextSelectionPtr();
	}
	return false;
}

// Ajoute i à la sélection
function List_selectIndex(i) {
	if(!this.isMultipleSelectionEnabled() || this.isSelectionEmpty()) this.selection = [i];
	
	else if(this.selection[this.getSelectionLength()-1] < i) this.selection.push(i);
	else {
		var j = 0;
		while(j<this.getSelectionLength() && this.selection[j]<i) ++j;
		for(var k=this.getSelectionLength(); k>j; --k)
			this.selection[k] = this.selection[k-1];
		this.selection[j] = i;		
	}
}

// Retire i de la sélection
function List_deselectIndex(i) {
	this.initSelectionPtr();
	while(!this.isAtSelectionEnd()) {
		if(this.getCurrentSelectionIndex() == i) {
			this.selection.splice(this.getCurrentSelectionPtr(), 1);
			return;
		}
		this.nextSelectionPtr();
	}
}

// Retourne le premier élément de la sélection
function List_getFirstSelectedIndex() {
	return this.selection[0];
}

// Retourne le dernier élément de la sélection
function List_getLastSelectedIndex() {
	return this.selection[this.getSelectionLength()-1];
}

// Positionne le pointeur de parcours sur la sélection au début de celle-ci
function List_initSelectionPtr() {
	this.selection_ptr = 0;
}

// Retourne l'indice de l'élément point? par le pointeur sur la sélection
function List_getCurrentSelectionPtr() {
	return this.selection_ptr;
}

// Retourne l'élément point? par le pointeur sur la sélection
function List_getCurrentSelectionIndex() {
	return this.selection[this.selection_ptr];
}

// Affecte i à l'indice point? dans la sélection par selection_ptr
function List_setCurrentSelectionIndex(i) {
	this.selection[this.selection_ptr] = i;
}

// Avance le pointeur sur la sélection d'un indice
function List_nextSelectionPtr() {
	this.selection_ptr++;
}

// Retourne true si le pointeur sur la sélection est arrivé à la fin
function List_isAtSelectionEnd() {
	return this.selection_ptr == this.getSelectionLength();
}

// Retourne l'élément d'indice i
function List_getItem(i) {
	return this[i];
}

// Retourne la taille de la liste
function List_getLength() {
	return this.nb;
}

// Supprime tous les éléments de la liste
function List_clearList() {
	while(this.getLength() > 0)
		delete(this[--this.nb]);
	this.selectNone();
}

// Supprime l'élément d'incide i de la liste
function List_deleteItem(i) {
	delete(this[i]);
	for(var j=i; j<this.getLength()-1; ++j)
		this[j] = this[j+1];
	this.nb--;
}

// Supprime l'élément sélectionné de la liste et retourne true
// ou retourne false si rien n'a été fait
function List_deleteSelection() {
	if(this.isSelectionEmpty()) return false;

	this.selection.reverse();
	this.initSelectionPtr();
	while(!this.isAtSelectionEnd()) {
		this.deleteItem(this.getCurrentSelectionIndex());
		this.nextSelectionPtr();
	}
	this.selectNone();
	return true;
}



// Ajoute l'élément passé en paramètre à la fin de la liste
function List_addItem(i) {
	this[this.getLength()] = i;
	this.nb++;
}

// Fonction langues

function updatelanguage(){
   if(document.getElementById("MgFound1")){document.getElementById("MgFound1").innerHTML=MgFound1;}
   if(document.getElementById("MgFound2")){document.getElementById("MgFound2").innerHTML=MgFound2;}
   if(document.getElementById("MgSelected")){document.getElementById("MgSelected").innerHTML=MgSelected;}
   if(document.getElementById("MgItem")){document.getElementById("MgItem").innerHTML=MgItem;}
   if(document.getElementById("MgPrice")){document.getElementById("MgPrice").innerHTML=MgPrice;}      
   if(document.getElementById("MgAdd")){document.getElementById("MgAdd").value=MgAdd;}
   if(document.getElementById("MgModify")){document.getElementById("MgModify").value=MgModify;}
   if(document.getElementById("MgList")){document.getElementById("MgList").innerHTML=MgList;}
   if(document.getElementById("MgInfo")){document.getElementById("MgInfo").innerHTML='<a href=' +MgInfoLink+ '>' +MgInfo+ '</a>';}
   if(document.getElementById("MgOptions")){document.getElementById("MgOptions").innerHTML=MgOptions;}
   if(document.getElementById("OngletA")) {document.getElementById("OngletA").innerHTML=TxtOngletA;}
   if(document.getElementById("OngletB")){document.getElementById("OngletB").innerHTML=TxtOngletB;}
   if(document.getElementById("OngletC")){document.getElementById("OngletC").innerHTML=TxtOngletC;}
   if(document.getElementById("MgDeselectAll")){document.getElementById("MgDeselectAll").value=MgDeselectAll;}
   if(document.getElementById("MgClearList")){document.getElementById("MgClearList").value=MgClearList;}
   if(document.getElementById("MgResetPrice")) {document.getElementById("MgResetPrice").value=MgResetPrice;}
   if(document.getElementById("MgTotal")) {document.getElementById("MgTotal").innerHTML=MgTotal;}
   if(document.getElementById("MgInfoContent")) {document.getElementById("MgInfoContent").innerHTML=MgInfoContent;}
   
   if (Edt=="oui") {if(document.getElementById("dInitImg")) {document.getElementById("dInitImg").innerHTML=MgDone;}}
   else if (Edt=="non" || Edt=="nontemp") {if(document.getElementById("dInitImg")) {document.getElementById("dInitImg").innerHTML=MgEdit;}}

}



function changeLanguage(glangue){
 iCartLanguage=glangue;
 choiceLanguage();
 saveList();
}


function choiceLanguage() {

    if (FirstLoad=="oui") {
	var f4=GetCookie("PrefLangue");
	if (f4!=="vide"){
	     iCartLanguage = f4;
	};
	}

  if (iCartLanguage=="FR"){     
    MgModified = "modifié!";
    MgAdded = "ajouté!";
    MgFound1 = "Nombre d'éléments trouvés : ";
    MgFound2 = " sur ";
    MgSelected ="Produit sélectionné : ";    
    MgList = "Ma Liste";
    MgItem = "Produit";
    MgPrice = "Prix";
    MgAlertItem = "Erreur : Le champ 'Produit' ne doit pas être vide";
    MgInfo = "Plus d'infos sur iCart";
    MgAdd = "Ajouter";
    MgModify = "Modifier";
    MgOptions= "Options ";
    MgDeselectAll = "Tout désélectionner";
    MgClearList = "Vider la liste";
    MgResetPrice = "Remise à zéro des prix";
    MgTotal = "Total : ";
    MgLanguage = "EN";
    MgEdit = "Editer";
    MgDone = "OK";
    MgInfoLink = "info_FR.html";
    TxtOngletA = "Liste 1";
    TxtOngletB = "Liste 2";
    TxtOngletC = "Liste 3";
    MgInfoContent = "Liste vide";
    }
    if (iCartLanguage=="EN"){
    MgModified = "modified!";
    MgAdded = "added!";
    MgFound1 = "Number of items found : ";
    MgFound2 = " of ";
    MgSelected ="Item selected : ";    
    MgList = "My List";
    MgItem = "Product name";
    MgPrice = "Price";
    MgAlertItem = "Error : Field 'Product name' is empty";
    MgInfo = "More info about iCart";
    MgAdd = "Add";
    MgModify = "Modify";
    MgOptions= "Options ";
    MgDeselectAll = "Deselect all";
    MgClearList = "Clear list";
    MgResetPrice = "Reset prices";
    MgTotal = "Total : ";    
    MgLanguage = "FR";
    MgEdit = "Edit";
    MgDone = "Done";
    MgInfoLink = "info_EN.html";
    TxtOngletA = "List 1";
    TxtOngletB = "List 2";
    TxtOngletC = "List 3";
    MgInfoContent = "List is empty";
    }
    if (iCartLanguage=="D"){
    MgModified = "Geändert!";
    MgAdded = "Hinzugefügt!";
    MgFound1 = "Anzahl der Einträge gefunden: ";
    MgFound2 = " von ";
    MgSelected ="Produkt ausgewählt: ";    
    MgList = "Meine Liste";
    MgItem = "Produktname";
    MgPrice = "Preis";
    MgAlertItem = "Fehler: Das Feld 'Produktname' ist leer";
    MgInfo = "Mehr Infos über iCart";
    MgAdd = "Hinzufügen";
    MgModify = "Ändern";
    MgOptions= "Optionen ";
    MgDeselectAll = "Abwählen alle";
    MgClearList = "Löschen der Liste";
    MgResetPrice = "Nullstellen der Preise";
    MgTotal = "Gesamtbetrag : ";    
    MgLanguage = "D";
    MgEdit = " Bearb.";
    MgDone = "Fertig";
    MgInfoLink = "info_D.html";
    TxtOngletA = "Liste 1";
    TxtOngletB = "Liste 2";
    TxtOngletC = "Liste 3";
    MgInfoContent = "Liste ist leer";
    }
    if (iCartLanguage=="ES"){
    MgModified = "Modificado !";
    MgAdded =  "añadido !" ;
    MgFound1 = "Numero de elementos encontrados : ";
    MgFound2 = " de "; 
    MgSelected = "producto Seleccionado : ";
    MgList = "Mi Lista";
    MgItem = "Producto";
    MgPrice = "Precio";
    MgAlertItem = "ERROR:  El campo 'Producto' no debe estar vacio";
    MgInfo = "Mas informacion sobre iCart";
    MgAdd = "Añadir";
    MgModify = "Modificar";
    MgOptions=  "Opciones ";
    MgDeselectAll = "Deseleccionar todo";
    MgClearList = "Desocupar la lista";
    MgResetPrice = "Reinicializar los precios";
    MgTotal =  "Total : ";
    MgLanguage = "ES";
    MgEdit =  "Editar"; 
    MgDone = "OK";
    MgInfoLink = "info_ES.html"; 
    TxtOngletA = "Lista 1";
    TxtOngletB = "Lista 2";
    TxtOngletC = "Lista 3";
    MgInfoContent = "Lista vacia";
    }
    updatelanguage();

}

