var tableau_value = new Array();
var tableau_text = new Array();

function D2W_findObj(n, d) { //v3.0
  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=D2W_findObj(n,d.layers[i].document); return x;
}

function D2W_validateForm() { file://v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=D2W_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=D2W_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' doit contenir une adresse e-mail.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' doit contenir un nombre.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' doit contenir un nombre entre '+min+' et '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' est requis.\n'; }
  } if (errors) alert('Un des champs n\'as pas été rempli correctement');
  document.D2W_returnValue = (errors == '');
}

function D2W_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function resetDefaultValues(what) {
   document.Formd2w.reset();
   // Contenu.document.body.innerHTML = document.all('GFTextCopyContenu').document.forms['Formname'].Contenu.value;
}

function ChangeCategoryBody()  
{
	var i;
  var optionCounter2 = 0;
	var theDayElement = document.Formd2w.CategoryList;
	var optionCounter;
	for (optionCounter = 0; optionCounter < theDayElement.length;
	 optionCounter++)
	{
			// alert(document.Formd2w.CategoryListDb.options[optionCounter].selected == true);
			if (document.Formd2w.CategoryListDb.options[optionCounter].selected == true)
			{
				var selectedText = document.Formd2w.CategoryListDb.options[optionCounter].text;
				// alert(selectedText);
				var selectedValue = document.Formd2w.CategoryListDb.options[optionCounter].value;
				newoption = new Option(selectedText, selectedValue, false, false);
				document.Formd2w.CategoryChoice.options[optionCounter2] = newoption;
				optionCounter2++
			}
	}
	
	
	for (i=0;i<document.Formd2w.CategoryList.length;i++)
	{
		tableau_value[i] = document.Formd2w.CategoryList.options[i].value;
		tableau_text[i] = document.Formd2w.CategoryList.options[i].text;
	}
}

function SelectCategoryD2W(optionCounter)  
{
	var SelectedList = document.Formd2w.CategoryChoice;
	var optionCounter2 = SelectedList.length;

	var selectedText = document.Formd2w.CategoryList.options[optionCounter].text;
	document.Formd2w.CategoryList.options[optionCounter].selected = false;
	var selectedValue = document.Formd2w.CategoryList.options[optionCounter].value;
	newoption = new Option(selectedText, selectedValue, false, false);
	document.Formd2w.CategoryChoice.options[optionCounter2] = newoption;
}

function RemoveAll(list) {
	list.options.length=0;
}

function ModifyCatListe() {
	var i, strSelecteur, strLowTab, strLowSel;
	strSelecteur = document.Formd2w.selecteur.value
	strLowSel = strSelecteur.toLowerCase();
	RemoveAll(document.Formd2w.CategoryList);
	for (i=0;i<tableau_value.length;i++)
	{	strLowTab = tableau_text[i].toLowerCase();
		if (strLowTab.indexOf(strLowSel) >= 0)
		{
			newoption = new Option(tableau_text[i], tableau_value[i], false, false);
			document.Formd2w.CategoryList.options[document.Formd2w.CategoryList.length] = newoption;
		}
	}

	var strValues = "";
	var boxLength = document.Formd2w.CategoryChoice.length;
  var count = 0;
  if (boxLength != 0) {
    for (i = 0; i < boxLength; i++) {
      if (count == 0) {
        strValues = document.Formd2w.CategoryChoice.options[i].value;
      }
      else {
        strValues = strValues + "," + document.Formd2w.CategoryChoice.options[i].value;
      }
      count++;
    }
	}
	
	document.Formd2w.IdCategorie.value = strValues;
	
}


function ChangeCategory()  
{
	var boxLength = document.Formd2w.CategoryChoice.length;
	var selectedItem = document.Formd2w.CategoryList.selectedIndex;
	var selectedText = document.Formd2w.CategoryList.options[selectedItem].text;
	var selectedValue = document.Formd2w.CategoryList.options[selectedItem].value;
	var i;
	var isNew = true;
	if (boxLength != 0) {
		for (i = 0; i < boxLength; i++) {
			thisitem = document.Formd2w.CategoryChoice.options[i].text;
			if (thisitem == selectedText) {
				isNew = false;
				break;
      }
    }
	} 

	if (isNew) {
		newoption = new Option(selectedText, selectedValue, false, false);
		document.Formd2w.CategoryChoice.options[boxLength] = newoption;
	}
	
	document.Formd2w.CategoryList.selectedIndex=-1;

	var strValues = "";
	var boxLength = document.Formd2w.CategoryChoice.length;
  var count = 0;
  if (boxLength != 0) {
    for (i = 0; i < boxLength; i++) {
      if (count == 0) {
        strValues = document.Formd2w.CategoryChoice.options[i].value;
      }
      else {
        strValues = strValues + "," + document.Formd2w.CategoryChoice.options[i].value;
      }
      count++;
    }
	}
	
	document.Formd2w.IdCategorie.value = strValues;
	
}


function RemoveCategory() {
	var boxLength = document.Formd2w.CategoryChoice.length;
	arrSelected = new Array();
	var count = 0;
	for (i = 0; i < boxLength; i++)
		{
		if (document.Formd2w.CategoryChoice.options[i].selected) 
			{
			arrSelected[count] = document.Formd2w.CategoryChoice.options[i].value;
			}
		count++;
		}
	var x;
	for (i = 0; i < boxLength; i++)
		{
		for (x = 0; x < arrSelected.length; x++)
			{
			if (document.Formd2w.CategoryChoice.options[i].value == arrSelected[x])
				{
				document.Formd2w.CategoryChoice.options[i] = null;
   			}
			}
		boxLength = document.Formd2w.CategoryChoice.length;
   	}

	var strValues = "";
	var boxLength = document.Formd2w.CategoryChoice.length;
  var count = 0;
  if (boxLength != 0) {
    for (i = 0; i < boxLength; i++) {
      if (count == 0) {
        strValues = document.Formd2w.CategoryChoice.options[i].value;
      }
      else {
        strValues = strValues + "," + document.Formd2w.CategoryChoice.options[i].value;
      }
      count++;
    }
	}
	
	document.Formd2w.IdCategorie.value = strValues;

}

function InsertD2WCategory() {

	var strValues = "";
	var boxLength = document.Formd2w.CategoryChoice.length;
  var count = 0;
  if (boxLength != 0) {
    for (i = 0; i < boxLength; i++) {
      if (count == 0) {
        strValues = document.Formd2w.CategoryChoice.options[i].value;
      }
      else {
        strValues = strValues + "," + document.Formd2w.CategoryChoice.options[i].value;
      }
      count++;
    }
	}
	
	document.Formd2w.IdCategorie.value = strValues;
	
}

function SaveCategories() {
	
	var vIndexCategorie, vOptionCategorie, vIndexType, vOptionType;
	
	var strValues = "";
	var boxLength = document.Formd2w.CategoryChoice.length;
  var count = 0;
  if (boxLength != 0) {
    for (i = 0; i < boxLength; i++) {
      if (count == 0) {
        strValues = document.Formd2w.CategoryChoice.options[i].value;
      }
      else {
        strValues = strValues + "," + document.Formd2w.CategoryChoice.options[i].value;
      }
      count++;
    }
}

document.Formd2w.IdCategorie.value = strValues;
	
	vIndexCategorie = document.Formd2w.IdCategorie.selectedIndex;
	
}

function SubmitFormValid(){

document.Formd2w.ToValidation.value = 'True';
document.Formd2w.submit();

}

