sortitems = 0;  // Automatically sort items within lists? (1 or 0)
isearch=0;

var ligne = "";
var col = "";

var IE4 = (document.all) ? true : false;
var NS4 = (document.layers) ? true : false;

function selectAll(box)
{
 if (box) {
	if (box.options.length>0) {
		for(var i=0; i<box.options.length; i++) {
				box.options[i].selected=true;
		}
	}
 }
}

function chercheTransfert(box,searchstring,direction)
{
  if (box.options.length>0) {
	if (isearch>box.options.length-1) isearch=0;
	if (isearch<0) isearch=box.options.length-1;
	if (direction==1) {
		for(var i=isearch; i<box.options.length; i++) {
		//alert(i);
		  if (box.options[i].value.indexOf(searchstring,0)!=-1 || box.options[i].text.indexOf(searchstring,0)!=-1) {
				box.options[i].selected=true;
				isearch=i+1;
				break;
			};
		}
	} else {
		for(var i=isearch; i>=0; i--) {
		//alert(i);
		  if (box.options[i].value.indexOf(searchstring,0)!=-1 || box.options[i].text.indexOf(searchstring,0)!=-1) {
				box.options[i].selected=true;
				isearch=i-1;
				break;
			};
		}
	}
  }
}

function move(fbox,tbox) {
	isearch=0;
	BumpUp(tbox);
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			var no = new Option();
			no.value = fbox.options[i].value;
			no.text = fbox.options[i].text;
			tbox.options[tbox.options.length] = no;
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		}
	}
	//BumpUp(fbox);
	if (sortitems) SortD(tbox);
	return true;
}

function movetobox(val,lib,sbox) {
	tbox = eval(sbox);
	
	found = 0;
	for (var i=0; i<tbox.options.length; i++) {
		if (tbox.options[i].text==lib) { found++; };
	}
	
			if (found>0) { var no = new Option(lib,val+"_"+found); }
			else { var no = new Option(lib,val); };
			
			//no.value = val;
			//no.text = lib;
			tbox.options[tbox.options.length] = no;
			tbox.options[tbox.options.length-1].selected = true;
	
	return true;
}

function removefrombox(fbox)
{
	isearch=0;
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			//fbox.options[i].value = "";
			//fbox.options[i].text = "";
			fbox.options[i] = null;
		}
	}
	BumpUp(fbox);
	return true;
}

function BumpUp(box)  {
	for(var i=0; i<box.options.length; i++) {
		if(box.options[i].value == "")  {
			for(var j=i; j<box.options.length-1; j++)  {
				box.options[j].value = box.options[j+1].value;
				box.options[j].text = box.options[j+1].text;
			}
			var ln = i;
			break;
		}
	}
	if(ln < box.options.length)  {
	box.options.length -= 1;
	//BumpUp(box);
   }
}

function SortD(box)  {
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<box.options.length; i++)  {
		temp_opts[i] = box.options[i];
	}
	for(var x=0; x<temp_opts.length-1; x++)  {
		for(var y=(x+1); y<temp_opts.length; y++)  {
			if(temp_opts[x].text > temp_opts[y].text)  {
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
			}
		}
	}
	for(var i=0; i<box.options.length; i++)  {
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
	}
}

/*********** Aide popup *************************/
function displayHelp(mess)
{
	if (mess.length>0) {
	s = '<table bgcolor="#000000" cellpadding="0" cellspacing="2"><tr><td>'
	s += '<table bgcolor="#ECFC00" cellpadding="5" cellspacing="0"><tr><td width="100%" class="normal">'+mess
	s += '</td></tr></table>'
	s += '</td></tr></table>'
	} else { s = mess; }
	posX=0;
	posY=0;
	if (document.layers) // Netscape
	{
		document.layers.help.document.write(s);
		document.layers.help.document.close();
	} else //IE
	{
		document.all.help.innerHTML = s;
	}
}

/********************* petit rollover *********************/

function roll(objimg,src)
{
	objimg.src=src;
}

/********************** Fonctions diverses ******************/

function remplirArticle(oldArticle)
{
	location='modifyArticle.asp?remplir=ok&oldarticle='+oldArticle;
}

function remplirCateg(oldcateg)
{
	location='modifyCateg.asp?remplir=ok&oldcateg='+oldcateg;
}

function remplirFamille(oldFamille)
{
	location='modifyFamille.asp?remplir=ok&oldfamille='+oldFamille;
}

function remplirModele(oldModele)
{
	location='modifyModele.asp?remplir=ok&oldModele='+oldModele;
}

function checkInteger(obj)
{
	var myval = new String(obj.value)
	if (!myval.match('^[0-9]+$')) obj.value="";
}

function preview(url)
{
	return window.open(url,'','toolbar=no,location=no,personalbar=no,status=yes,menubar=no,dependent=yes,resizable=yes,width=350,height=500');
}

function initLog()
{
	if (NS4) {
        for (i=0; i<document.layers.length; i++) {
            whichEl = document.layers[i];
            if (whichEl.id.indexOf("itemlog") != -1) {
													 whichEl.visibility = "hide";
						}
        }
        arrange();
    }
    else {
        divColl = document.all.tags("DIV");
        for (i=0; i<divColl.length; i++) {
            whichEl = divColl(i);
            if (whichEl.className == "itemlog") whichEl.style.display = "none";
        }
    }
}

function changeLog(el)
{
	if (NS4) {
        for (i=0; i<document.layers.length; i++) {
            whichEl = document.layers[i];
            if (whichEl.id.indexOf(el) != -1) {
								if (whichEl.visibility == "hide") { whichEl.visibility="show"; }
								else { whichEl.visibility = "hide" };
						}
        }
        arrange();
    }
    else {
        divColl = document.all.tags("DIV");
        for (i=0; i<divColl.length; i++) {
            whichEl = divColl(i); 
            if (whichEl.id == el) { 
				if (whichEl.style.display == "none") { whichEl.style.display = "block"; }
				else { whichEl.style.display = "none"; };
            }
        }
    }	
}

function search()
{
	return window.open('search.asp','','toolbar=no,location=no,personalbar=no,status=yes,menubar=no,dependent=yes,resizable=yes,width=400,height=150');
}

function recalculetarif(myitem)
{
	s = new String(myitem.name); 
	i = s.lastIndexOf('_',s.length);
	ttarifbase = s.split('_');
	//tarifbase = 'tarifbase_'+ttarifbase[1];	
	tarifbase = 'tarifbase';
	tarif = 'tarif_'+ttarifbase[1];	
	
	objTarifBase = eval('myitem.form.'+tarifbase);
	//pivotname = tarif+'_'+objTarifBase.options[objTarifBase.selectedIndex].value;
	pivotname = tarif+'_'+objTarifBase.value;
	
	if (s==pivotname) {	
		myForm = myitem.form
		for (i=0; i<myForm.elements.length; i++) {
			el = myForm.elements[i];
			elname = new String(el.name);
			if (elname.substring(0,6)=='tarif_'&&elname!=myitem.name) {
				
				tcoef = elname.split('_');
				coefname = 'coef_'+tcoef[1]+'_'+tcoef[2];
				coef = eval('myForm.'+coefname).value;
				
				tauxname = 'taux_'+tcoef[1]+'_'+tcoef[2];
				taux = eval('myForm.'+tauxname).value;
				
				r = parseFloat(myitem.value) * parseFloat(coef);
				r = r / parseFloat(taux);
				
				//on formate le résultat pour n'avoir que 2 décimales
				rs = new String(r);
				p = rs.indexOf('.',0);
				el.value = rs.substring(0,p+3);
			}
		}
	}
}

function imprime()
{
	window.open('imprimeCaddie.asp','','toolbar=yes,location=no,personalbar=no,status=yes,menubar=no,dependent=yes,resizable=yes,width=650,height=400');
}


/********************************************************************
  Popup pour le tableau de choix des fichiers des pièces détachées
********************************************************************/

function changeRepeater(el,myLigne,myCol)
{
	ligne = myLigne;
	col = myCol;
}

function changeIt(formName,myCol)
{
	el = eval(formName+".fic_"+ligne+"_"+myCol);
	elref = eval("el.form.fic_"+ligne+"_"+col);
	el.selectedIndex = elref.selectedIndex;
}

function changeCol(num)
{
	colimg = eval("document.colimg_"+num);
	divColl = document.all.tags("DIV");
    for (i=0; i<divColl.length; i++) {
      col = divColl(i); 
      if (col.id=="col_"+num) {            
		if (col.style.display == 'none') {
			colimg.src = '/robotcoupe/images/minus.gif';
			col.style.display = 'block';
		} else {
			colimg.src = '/robotcoupe/images/plus.gif';
			col.style.display = 'none';
		};
	  }
    }
}