var dom = (document.getElementsByTagName) ? true : false;
var ie5 = (document.getElementsByTagName && document.all) ? true : false;

var Category1Array = new Array();
var Category2Array = new Array();
var Category3Array = new Array();
Category1Array[0] = new Category(-1,"Toutes les cat&eacute;gories 1",0,-1);
Category2Array[0] = new Category(-1,"Toutes les cat&eacute;gories 2",0,-1);	
Category3Array[0] = new Category(-1,"Toutes les cat&eacute;gories 3",0,-1);
var CountCategoryArray = new Array();
function CountCategory(distributorid,cntdistr,fundtypeid,cntfundtype,fundmaintypeid)
{
	this.distributorid    = distributorid;
	this.cntdistr  = cntdistr;
	this.fundtypeid = fundtypeid;
	this.cntfundtype = cntfundtype;
	this.fundmaintypeid = fundmaintypeid;
}
function CountSearchResults(distributor,fundtypeslist)
{
	var cnt_results=0;
	if (fundtypeslist == ",")
	//only search on distributor
	{
	//alert("totales: " + CountCategoryArray.length);
		for(var i=1;i< CountCategoryArray.length;i++)
		{
			if(CountCategoryArray[i].distributorid == distributor)
			{
				//alert(distributor + " found!!!");
				cnt_results = CountCategoryArray[i].cntdistr
				break;
			}
		}
	}
	else
	{
		for(var i=1;i< CountCategoryArray.length;i++)
		{
			if((CountCategoryArray[i].distributorid == distributor || distributor == 0)&& (fundtypeslist.indexOf("," + CountCategoryArray[i].fundtypeid + ",") > -1 || fundtypeslist.indexOf("," + CountCategoryArray[i].fundmaintypeid + ",") > -1 ))
			{
				//alert(distributor + " " + fundtypeslist + " found!!!");
				//alert(CountCategoryArray[i].distributorid + ";" + fundtypeslist + ":" + CountCategoryArray[i].cntfundtype);
				cnt_results = cnt_results + CountCategoryArray[i].cntfundtype;
			}
		}
		
	}
	//alert(cnt_results);
	return cnt_results;
}

// Create Category object	
function Category(Id, Name, ParentId,Children)
{
	this.Id    = Id;
	this.Name  = Name;
	this.ParentId = ParentId;
	this.Children = Children;
}
function RemoveFromList(obj)
{
	//the first element of the list is ----------------------------- : don't allow removal, or the layout becomes awful
	if (obj.selectedIndex == 0) 
	{
		return;
	}
	obj.options[obj.selectedIndex]=null;
}

function AddToList(obj1,obj2,searchlist)
{
	var selectedindex;
	var ot;
	//alert(obj1.selectedIndex);
	//var prefixtext;
	var addtext="";
	if (obj1.selectedIndex==0)
	{
		selectedindex=obj2.selectedIndex;
		ot=obj2.options;
		//prefixtext="Toutes les cat&eacute;gories de ";
		addtext = ot[selectedindex].text;
	}
	else
	{
		selectedindex=obj1.selectedIndex;
		ot=obj1.options;
		addtext = obj2.options[obj2.selectedIndex].text + " / " + ot[selectedindex].text;
		//prefixtext="";
	}
	if (selectedindex==0)
	{
		//alert("Veuillez faire un ou plusieurs choix");
		return;
	}
	//alert (prefixtext);
	//alert(ot[obj1.selectedIndex].text);
	var listcnt = searchlist.length;
	//alert(listcnt);
	for (var i=0; i < listcnt;i++) 
	{
		// if this item is already in the searchlist, don't add it again
		// if item is empty,don't add it
		if (ot[selectedindex].value==searchlist[i].value || ot[selectedindex].value=="")
		{
			return;
		}
	}
	searchlist[listcnt] = new Option(addtext,ot[selectedindex].value,false,false);
	//searchlist[listcnt] = new Option(ot[selectedindex].text,ot[selectedindex].value,false,false);
	//AddToList(document.f.cat2_1.options[document.f.cat2_1.selectedIndex].text);
}
function Addcat()
{
	var catsubobj = document.f.catsub;
	var catmainobj = document.f.catmain;
}

function Removecat()
{

}

function FillList(obj,destarr,Idx,sourcearr,currcolnr,TableNum)
{
	var parentId=sourcearr[Idx].Id
	var children=sourcearr[Idx].Children
  //if(jsver>1.0){
    var ot = obj.options;
    var os = destarr;
    var listcnt=1;
    ot.length = (children==-1)?1:children+1;//os.length;
	//alert(children);
	if (currcolnr==0 && Idx==0)
	{
		//document.f["cat2_" + TableNum].length=1;
		//document.f["cat3_" + TableNum].length=1;
		//document.f["cat2_" + TableNum].selectedIndex=0;
		//document.f["cat3_" + TableNum].selectedIndex=0;
	}
	else if(currcolnr==1 && Idx==0)
	{
		//document.f["cat3_" + TableNum].length=1;
		//document.f["cat3_" + TableNum].selectedIndex=0;
	}
	else
	{
	
	}
	/*if (ie5 || dom)
	{
			if (currcolnr==0)
			{
				if (Idx==0)
				{
					hideColumn(1,"TableSelect" + TableNum);
					//hideColumn(2,"TableSelect" + TableNum);
				}
				else
				{
					//hideColumn(2,"TableSelect" + TableNum);
					showColumn(1,"TableSelect" + TableNum);
				}
			}
			else if (currcolnr==1)
			{
				if (Idx==0)
				{
					hideColumn(2,"TableSelect" + TableNum);
				}
				else
				{
					showColumn(2,"TableSelect" + TableNum);
				}
			}

	}*/
	//alert(parentId);

    	for (var i=1; i<=os.length; i++)
	    {
		 // alert(os[i-1].Name);
		  //alert(parentId);
	      if (os[i-1].ParentId==parentId)
	      {	
			//alert(os[i-1].Id);
      		ot[listcnt] = new Option(os[i-1].Name,os[i-1].Id,false,false);
      		listcnt+=1;
      		if(listcnt==ot.length ) break;
	      }	
	    }
    //}
    ot[0].selected=true;
    ot.length = listcnt;
  //}	
}

function InitializeFundLists()
{
	FillMainCategories(document.f.catmain,Category1Array);
	FillMainCategories(document.f.catdistrib,Category3Array);
}

function FillMainCategories(obj,arr)
{
	var ot = obj.options;
	ot.length = arr.length;
	for (var i=1; i<=arr.length-1; i++)
	{
		ot[i] = new Option(arr[i].Name,arr[i].Id,false,false);
	}
	//document.f.catmain.selectedIndex =0;
	obj.selectedIndex =0;
}
function init()
{

	/*if (ie5 || dom)
	{ 
		for (var i=1; i<6;i++)
		{
			hideColumn(1,"TableSelect" + i);
			hideColumn(2,"TableSelect" + i);
		}
		
			//hideColumn(1,"TableSelect1" );
			//hideColumn(2,"TableSelect1");
		
	
	}*/
	
	
	FillMainCategories(document.f.cat1_1);
	document.f.cat1_1.selectedIndex=0;
	/*FillMainCategories(document.f.cat1_2);
	document.f.cat1_2.selectedIndex=0;
	FillMainCategories(document.f.cat1_3);
	document.f.cat1_3.selectedIndex=0;
	FillMainCategories(document.f.cat1_4);
	document.f.cat1_4.selectedIndex=0;
	FillMainCategories(document.f.cat1_5);
	document.f.cat1_5.selectedIndex=0;*/


}

										
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
