// ------ Scrolling Layer ---------------------------------------------------------------------------------------------------
// ------ Initialisation des calques ------
//scrollInit(lName, posX, posY, tX, tY, indZ, lStyle, stepX, delay, innerText)
// lName		: new layer namer
// posX		: absolute left position of the new layer
// posY		: absolute top position of the new layer
// tX		: new layer width
// tY		: new layer height
// indZ 		: z-idex of the new layer
// lStyle		: CSS style of the new layer
// stepX		: shift definition for the new layer

// delay		: time between two shifts
// innerText	: displayed html in the new layer

function scrollInit(lName, posX, posY, tX, tY, indZ, lStyle, stepX, delay, innerText){
	// ------ Création des calques ------
	document.write('<DIV ID="'+lName+'A" STYLE="position:absolute; left:'+posX+'px; top:'+posY+'px; width:'+tX+'px; height:'+tY+'px; z-index:'+indZ+'; visibility: visible;" CLASS="'+lStyle+'">');
	document.write(innerText);
	document.write('</DIV>');	
	document.write('<DIV ID="'+lName+'B" STYLE="position:absolute; left:'+(posX+tX)+'px; top:'+posY+'px; width:'+tX+'px; height:'+tY+'px; z-index:'+indZ+'; visibility: visible;" CLASS="'+lStyle+'">');
	document.write(innerText);
	document.write('</DIV>');
	// ------ Appel de la fonction de scrolling ------
	scrolLay(lName+"A", stepX, delay);
	scrolLay(lName+"B", stepX, delay);
}

// ------ Scrolling des calques ------
//scrollInit(lName, posX, posY, tX, tY, indZ, lStyle, stepX, delay, innerText)
// lName		: new layer namer
// stepX		: shift definition for the new layer
// delay		: time between two shifts
function scrolLay(Lname, stepX, delay){
	X=document.getElementById(Lname).style.left;
	X=Number(X.slice(0,X.length-2));
	maxX=document.getElementById(Lname).style.width;
	maxX=Number(maxX.slice(0,maxX.length-2));
	minX=-1*maxX;
	if(X-stepX>minX){
		X=X-stepX;
	} else {
		X=maxX;
	}
	document.getElementById(Lname).style.left=X;
	setTimeout("scrolLay('"+Lname+"',"+stepX+","+delay+")",delay);
}

//----- Outils pour Formulaire --------- ©Supernick 2004---------------------------------//
//----- Vérifie si les champs du formulaire on bien été remplis -----//
// syntaxe : veriForm(nomForm, messBase, typChamp0, noChamp0, messChamp0, typChamp1, noChamp1, messChamp1....... typChampX, noChampX, messChampX)

// nomForm : nom du formulaire à vérifier
// mesBase : base du message d'alerte 
// typ champ : "s" = champ simple alphanumérique
//			"d" = champ simple numérique
// 		"p" = champ numérique positif
//			"i|" = interval dans lequel une valeur au moins doit être cochée
//			"i&" = interval dans lequel toutes les valeurs doivent être cochées pour le même message d'erreur
//			"c" = conditionnel : si premier champ rempli alors verifie si deuxieme champ rempli
// noChamp :	si "s" alors simple n° du champ
//			si "i|" alors n°debut-n°fin interval ex. : 20-30
//			si "i&" alors n°debut-n°fin interval ex. : 11-17
//			si "c" : alors n°premmier champ, si validé verifie n° deuxieme champ ex. : 20-30
// messChamp : message d'alerte pour le champ simple ou l'interval

function veriForm(){
	var noError=true;
	var nomForm=veriForm.arguments[0];
	var mesBase=veriForm.arguments[1];
	form=document.forms[nomForm];
	for(i=2;i<veriForm.arguments.length-2;i=i+3){
		var typChamp=veriForm.arguments[i];
		var messChamp=veriForm.arguments[i+2];		
		var selected=true;
		if(typChamp=="s" || typChamp=="d" || typChamp=="p"){
			var noChamp=veriForm.arguments[i+1];
			if(!form.elements[noChamp]){
				var val=document.all[veriForm.arguments[i+1]].value;
			} else {
				var val=form.elements[noChamp].value;
			}			
		} else {
			noChamp=veriForm.arguments[i+1];
			cutpos=noChamp.lastIndexOf("-");
			indexDeb=parseInt(noChamp.slice(0,cutpos));
			indexFin=parseInt(noChamp.slice(cutpos+1,noChamp.length));			
		}
		switch(typChamp){
			case "s":
				if(val==""){selected=false;}
				break;
			case "d":
				if(!checkNum(val)){selected=false;}
				break;
			case "p":
				if(val<=0){selected=false;}
				break;
			case "i|":
				var good=false;
				for(j=indexDeb;j<=indexFin;j++){
					if (form.elements[j].value!=""){good=true;}
					selected=good;
				}
				break;
			case "i&":			
				for(j=indexDeb;j<=indexFin;j++){
					if(form.elements[j].value==""){selected=false;}
				}
				break;
			case "c":
				if(form.elements[indexDeb].value!=""){
					if(form.elements[indexFin].value==""){selected=false;}
				}
				break;
		}
		if(!selected){
			mesBase+=messChamp+"\n";
			noError=false;
		}
	}
	if(!noError){
		alert(mesBase);
		return(noError);		
	} else {
		form.submit();
	}
}

// ---------------------------------------------------------------------------------
// ------ Calculs pour les formulaires de type bon de commande, factures etc. ------
// ---------------------------------------------------------------------------------
function calc_lignes(qte,pu,montant){ // Affiche dans montantXX le resultat de qteXX*puXX (si qte est numérique) 
	var tags=document.getElementsByTagName("input");
	var long=qte.length;
	for($i=0;$i<tags.length;$i++){
		var nom=tags[$i].name;
		var deb=nom.substring(0,long);
		var index=nom.substring(long,nom.length);
		if(deb==qte){
			if(!checkNum(tags[$i].value)){
				alert("ATTENTION, seules les valeurs numériques sont admises !");
				tags[$i].value=0;
			} else {
				document.all[montant+index].value=Number(document.all[qte+index].value*document.all[pu+index].value);
			}
		}
	}	
}

function calc_total(prefix,chmpTotal){	// Calcul le total de tous les champs dont le nom commence par prefix et affiche le resultat dans chmpTotal
	var tags=document.getElementsByTagName("input");
	var long=prefix.length;
	var total=0;
	for($i=0;$i<tags.length;$i++){
		var nom=tags[$i].name;
		var deb=nom.substring(0,long);
		if(nom==chmpTotal){champ=tags[$i];}
		if(deb==prefix){			
			if(!checkNum(tags[$i].value)){
				alert("ATTENTION, seules les valeurs numériques sont admises !");
				return false;
			} else {
				total+=Number(tags[$i].value);
			}
		}
	}
	champ.value=total;
}

function calc_limit(defaut,limite,valeur,dest){ //  Affiche dans dest defaut si valeur<limite sinon 0
	var taux=defaut;
	if(document.all[valeur].value>=limite){taux=0;}
	document.all[dest].value=taux;
}

function calc_add(dest,valeur){ // Affiche dans  dest, valeur additionnée de la valeur de tous les champs passés en paramètre à sa suite 
	var total=0;
	if(document.all[valeur].value>0){
		for($i=1;$i<calc_add.arguments.length;$i++){total=total+Number(document.all[calc_add.arguments[$i]].value);}
	}
	document.all[dest].value=total;
}

function checkNum(checkStr){ // Vérifie si une valeur est bien numérique
	var checkOK="0123456789-.";
	var allValid=true;
	for(k=0;k<checkStr.length;k++){
		ch=checkStr.charAt(k);
		for(j=0;j<checkOK.length;j++){if(ch==checkOK.charAt(j)){break;}}
		if(j==checkOK.length){
			allValid=false;
			break;
		}
	}
	return allValid;	
}

// ---------------------------------------------------------------------------------
// ------ Navigation  --------------------------------------------------------------
// ---------------------------------------------------------------------------------
function goTo(dest,target){ // Aller à (url et/ou ancre dans la page)
	if (!target){var target="pages";}
	if(window.top.frames[target]){
		var path=window.top.frames[target].location;
		path=path.toString();
		for (var i=0;i<path.length;i++){if(path.charAt(i)=="#"){path=path.slice(0,i);}}	
		if (dest.charAt(0)=="#"){path=path+dest;}	else {path=dest;}
		window.top.frames[target].location=path;
	} else {
		var path=document.location;
		path=path.toString();
		for (var i=0;i<path.length;i++){if(path.charAt(i)=="#"){path=path.slice(0,i);}}	
		if (dest.charAt(0)=="#"){path=path+dest;}	else {path=dest;}
		document.location=path;
	}
}