//============================================================
// customized_fields.js
//============================================================

function findObj(n, d)
{
	//MM_findObj() v4.01
	
	var p,i,x;
	if (!d) d=document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length) {
    	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if (!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x=d.getElementById(n);
	return x;
}


//------
// Move options between two selections
function doMoveOptions(fbox, tbox)
{
	
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;

	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}

	var fLength = 0;
	var tLength = arrTbox.length;

	for (i = 0; i < fbox.options.length; i++)
	{
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		}
		else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
		}
	}

	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;

	for (c = 0; c < arrFbox.length; c++)
	{
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}

	for (c = 0; c < arrTbox.length; c++)
	{
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
	}
}

//------
// Move options between two selections
function doMoveOptions_Damien(fbox, tbox)
{
	
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	
	var monSelect = document.getElementById('ListeGamme');
	var valueOption = monSelect.options[monSelect.selectedIndex].value;
	
	
// si j'ai déjà des données dans le TO, je les stocke dans arrTbox
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}

	var fLength = 0;
	var tLength = arrTbox.length;

	for (i = 0; i < fbox.options.length; i++)
	{
		if (fbox.options[i].value.indexOf("|") != -1)
			arrLookup[fbox.options[i].text] = fbox.options[i].value
		else
			arrLookup[fbox.options[i].text] = fbox.options[i].value + "|" + valueOption;  // ajout pour prendre en compte la catégorie
		
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
			
		}
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
	}

	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;

	for (c = 0; c < arrFbox.length; c++)
	{
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}

	for (c = 0; c < arrTbox.length; c++)
	{
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
	}
}

//------
// Move options between two selections
function doDelete_Damien(tbox)
{
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;

	var tLength = arrTbox.length;

	for (i = 0; i < tbox.options.length; i++)
	{
		if (tbox.options[i].selected && tbox.options[i].value != "") {}
		else{
			arrLookup[tbox.options[i].text] = tbox.options[i].value;
			arrTbox[tLength] = tbox.options[i].text;
			tLength++;
			}
	}

	arrTbox.sort();
	tbox.length = 0;
	var c;

	for (c = 0; c < arrTbox.length; c++)
	{
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
	}
}

/*
function doValidateDoubleSelects(form)
{
	var nbelts = form.elements.length;
	for (var i=0; i<nbelts; i++)
		if (form.elements[i].isDoubleSelect)
			doValidateOptions(form.elements[i]);
}

function doValidateOptions(box)
{
	if (box.length > 0) {
		for (i = 0; i < box.options.length; i++) {
			box.options[i].selected = true;
		}
	}
}*/

/*
function matchPatternSelectOptionWithBlank(pattern, vArray, selection)
{
	//call method
	matchPatternSelectOption(pattern, vArray, selection);

	//add blank option
	var size = selection.length;
	var tab = new Array(size);
	for (var i = 0; i < size; i++) {
		tab[i] = new Array(listObj.options[i].value, listObj.options[i].text);
	}
	selection.options[0] = new Option("", "")
	for (var i = 0; i < size; i++) {
		var buffer = tab[i];
		var str = buffer[1];
		var val = buffer[0];
		selection.options[i+1] = new Option(str, val)
	}
}

//------
// Generates a filtered selection from an array
// and a pattern string.
function matchPatternSelectOption(pattern, vArray, selection)
{
	//replacing empty pattern with *
	if (pattern == '') pattern = '*';
	
	//--- Check pattern validity
	var wcchars = new Array(")", "(", "/", "\\", "+", "^", ".", ",", ";");

	for (var i = 0; i < wcchars.length; i++)
	{
		var token = wcchars[i];
		if (pattern.indexOf(token) != -1) {
			return alert("This pattern is not valid.");
		}
	}

	var vmode = 0;
	var vtemp = pattern;
	var bwildchars = false;
	var istartwc = pattern.indexOf("*");
	var iendwc = pattern.lastIndexOf("*");

	if ( (iendwc > 0) || (istartwc == 0) )
	{
		bwildchars = true;

		if (iendwc > 0)
		{
			vmode = 1;
			vtemp = vtemp.substring(0, iendwc);
		}
		if (istartwc == 0)
		{
			vmode = (vmode == 0)?2:3;
			vtemp = vtemp.substring(1, vtemp.length);
		}
		if ( ((vtemp.indexOf("*")) >= 0) && (vtemp.length > 1) ) {
			return alert("This pattern is not valid.");
		}
	}
	//-----

	var strrx = "";
	if (bwildchars)
	{
		if (vmode == 1) {
			strrx = '/^' + vtemp + '/i';
		}
		else if (vmode == 2) {
			strrx = '/' + vtemp + '$/i';
		}
		else if (vmode == 3) {
			strrx = '/' + vtemp + '/i';
		}
	}
	else if (vtemp != '') {
		strrx = '/' + vtemp + '/i';
	}
	else {
		strrx = '/^a/i';
	}

	var re = eval(strrx);

	var newOpt;
	var listObj = selection;

	try
	{
		while (listObj.options.length) {
			listObj.options.remove(0);
		}

		var idx = 0;
		for (var i=0; i < vArray.length; i++)
		{
			var buffer = vArray[i];
			var str = buffer[1];
			var val = buffer[0];

			// ---> Specific
			var strcmp1 = str;
			var idxof = str.indexOf('-');
			if (idxof > 0) {
				strcmp1 = str.substring(idxof+2, str.length);
			}

			var strcmp2 = strcmp1;
			var idxof2 = str.indexOf('(');
			var idxof3 = str.indexOf(')');
			if ( (idxof2 >= 0) && (idxof3 > 0) ) {
				strcmp2 = str.substring(idxof2+1, idxof3);
			}
			//--->

			//if (re.test(str))
			if ( (re.test(strcmp1)) || (re.test(strcmp2)) || val == '') // specific
			{
				listObj.options[idx] = new Option(str, val);
				idx++;
			}
		}

		if (listObj.options.length == 0) {
			listObj.options[0] = new Option("", "");
		}
	}
	catch (E) {}
}
*/