function AMGTree(__ID, __AryContent, __TopMenu, __Width, __Heigth, __Top, __Left){
	this.Constructor			= AMGBaseObject;
	this.Constructor			(__ID, __AryContent, true);
	this.Width					= __Width;
	this.Height					= __Heigth;
	this.Position					= 'relative';
	this.Top						= __Top || 0;
	this.Left						= __Left || 0;	
	this.Style						= '';
	this.BgColor					= '';
	this.TopMenu				= __TopMenu || null;
	this.Prefix						= 'AMGTree_'+this.ID;
	this.Title						= 'Treeview';
	this.ShowTitle = true;
	this.TitleIcon				= '1.gif';
	this.FontFamily				= 'arial';
	this.FontSize				= 11;
	this.FontColor				= '#0E7DAC';
	this.BgImage				= '';	
	this.Count					= 0;
	this.ChildsDepth			= 1000;
	this.AryUsedSection			= new Array();
	this.PixPath				= '/pix/treeview/'+this.Style;
	this.Border					= 0;
	this.BorderColor			= '#dfdfdf';
	this.IsForm					= true;
	this.FormAllowMutiple		= true;
	this.FormType				= 'radio';
	this.FormSelectedItems		= null;
	this.SelectedItemsApplyChild= false;
	this.NotSelectableItems		= null;
	this.NotSelectableApplyChild= true;
	this.FormValidate			= true;
	this.ActionOnItem			= false;
	this.IsEditForm				= true;
	this.OnOffOnClick			= true;
	
	this.GetPath				= AMGTreeGetPath;
	this.Init					= AMGTreeInit;
	this.WriteHtml				= AMGTreeWriteHtml;
	this.BuildUsedMenu			= AMGTreeBuildUsedMenu;
	this.GetNode				= AMGTreeGetNodeHtml;
	this.GetOnClickHtml			= AMGTreeGetOnClickHtml;
	this.GetOnClickNoCountHtml	= AMGTreeGetOnClickNoCountHtml;
	this.NodeClick				= AMGTreeNodeClick;
	//this.GetPath				= AMGTreeGetPath;
	this.SwapImages				= AMGTreeSwapImages;
	this.SetStatus				= AMGTreeSetStatus;
	this.LoadAttribute			= AMGTreeLoadAttribute;
	this.UnselectAll			= AMGTreeUnselectAll;
	this.WriteToLayer			= AMGTreeWriteToLayer;
	this.OpenTree				= AMGTreeOpenTree;
	this.OpenTreeInit			= AMGTreeOpenTreeInit;
	this.ReBuild				= AMGTreeReBuild; 
	this.InsertNode				= AMGTreeInsertNode; 
	
	this.Init();
}

//************************************************************
function AMGTreeInit(){try{

}catch(e){handleClientError('AMGTreeInit', e);}}
//************************************************************
function AMGTreeLoadAttribute(){try{
	if (this.FormAllowMutiple)
		this.FormType= 'checkbox';
	this.TopInit		= this.Top;
	this.LeftInit		= this.Left;
	var iBuffer = 0;
	this.CloneContent();
	if (this.TopMenu == null) { 
		for (var i = 0 ; i < this.AryContent.length ; i++){
			if (!this.AryContent[i][0][0] || this.AryContent[i][0][0] == '0')	{
				this.TopMenu = this.AryContent[i][0][0];
				break;
			}
		}
	}
}catch(e){handleClientError('AMGTreeInit', e);}}
//************************************************************
//************************************************************
function AMGTreeGetPath(__sStyle){try{
	var bRetValue = __sStyle;
	if (__sStyle && ! (__sStyle.indexOf('http://') == 0 || __sStyle.indexOf('/') == 0) )
			bRetValue =  this.PixFolder + __sStyle;
	else
		bRetValue =  this.PixFolder;
	return checkFolderPath(bRetValue);

}catch(e){handleClientError('AMGTreeGetPath', e);}}
//************************************************************
function AMGTreeBuildUsedMenu(__vMenu, __sOrderPath, __iLevel, __SelectedStatusFromParent){try{
	var SelectedStatusFromParent = __SelectedStatusFromParent;
	for (var i = 0 ; i < this.AryContent.length ; i++){
		if (__vMenu == this.AryContent[i][0][0]){
			this.AryUsedSection[this.AryUsedSection.length] = new Array(i, __sOrderPath, __iLevel,  this.AryContent[i][0][0]);
			for (var j = 1 ; j < this.AryContent[i].length ; j++){
				if (ParamExists(__SelectedStatusFromParent)){
					this.AryContent[i][j][8] = !__SelectedStatusFromParent;
					this.AryContent[i][j][9] = __SelectedStatusFromParent;
				}
				else{
					if(this.FormSelectedItems && this.FormSelectedItems.indexOf(','+this.AryContent[i][j][0]+',') != -1){
						this.AryContent[i][j][8] = false;
						this.AryContent[i][j][9] = true;
						if (this.SelectedItemsApplyChild){
							SelectedStatusFromParent = true;
						}
					}
					else if(this.NotSelectableItems && this.NotSelectableItems.indexOf(','+this.AryContent[i][j][0]+',') != -1){
						this.AryContent[i][j][8] = true;
						this.AryContent[i][j][9] = false;
						if (this.NotSelectableApplyChild){
							SelectedStatusFromParent = false;
						}
					}
				}
				if (__iLevel < this.ChildsDepth	 &&  this.AryContent[i][j][6]){
					this.BuildUsedMenu(this.AryContent[i][j][0], __sOrderPath+'_'+this.AryContent[i][j][0], __iLevel+1, SelectedStatusFromParent);
				}
				SelectedStatusFromParent = __SelectedStatusFromParent;
			}
			break;
		}
	}
}catch(e){handleClientError('AMGTreeBuildUsedMenu', e);}}
//************************************************************

function AMGTreeReBuild(){//__AryContent){
	var divTree = getObject(this.Prefix);
	this.Count = 0;
	this.TopMenu = null;
	//this.AryContent = __AryContent;
	this.AryUsedSection = new Array();
	this.CheckIt();
	this.LoadAttribute();
	this.BuildUsedMenu(this.TopMenu, 'root', 0);
	divTree.innerHTML = this.GetNode(this.AryUsedSection[this.Count][0], true);;
}
function AMGTreeInsertNode(__ParentFolderID, __FolderId, __Array){
		var numberParent = 0;
		var CreateArbo = true;
		for(j = 0; j<this.AryContent.length; j++){
			if(this.AryContent[j][0][0] == __ParentFolderID){
				CreateArbo = false;
				numberParent = j;
				break;
			}
		}
		for(i = 0; i<this.AryContent.length; i++){
			for(j = 1; j<this.AryContent[i].length; j++){
				if(this.AryContent[i][j][0] == __ParentFolderID){
					this.AryContent[i][j][6]++;
					break;
				}
			}
		}
		///alert('0');
		if(CreateArbo){
			this.AryContent[this.AryContent.length] = [[__ParentFolderID, 150, 25, '_style1'], __Array]
		}else{
			this.AryContent[numberParent][this.AryContent[numberParent].length] = __Array;
		}
		//alert('4');
		this.ReBuild();
}

//************************************************************
function AMGTreeWriteHtml(){try{
	this.CheckIt();
	this.LoadAttribute();
	this.BuildUsedMenu(this.TopMenu, 'root', 0);
	var sBuffer = '';
	sBuffer += '<div id="'+this.Prefix+'" style="border:'+this.Border+'px solid '+this.BorderColor+';'+this.GetScrollStyle();
	if 	(this.Width || this.Height)
		sBuffer += 'overflow:auto;'
	if (this.Width	)
		sBuffer += 'width:'+this.Width+sPx+';';
	if (this.Height	)
		sBuffer += 'height:'+this.Height+sPx+';';
	if (this.BgColor	)
		sBuffer += 'background:'+this.BgColor+';';
	if (this.BgImage)
		sBuffer += 'background-image:url('+this.BgImage+');';
	if (this.Position == "absolute")
		sBuffer += 'position:absolute;top:'+this.Top+'px;left:'+this.Left+'px;';
	sBuffer += '">';
	sBuffer += '<table  cellpadding="0" cellspacing="0" border="0" width="100%">';
	sBuffer += '<tr>';
	sBuffer += '<td>';
	if (this.IsForm){
		sBuffer += '<a  href="javascript:'+this.ID+'.UnselectAll();" ><img src="'+this.GetPixPath(this.TitleIcon, this.PixPath)+'" border="0"><span style="COLOR:'+this.FontColor+'; FONT: '+this.FontSize+'px normal '+this.FontFamily+'; line-height:11px;height:15px;PADDING:0px; PADDING-LEFT:3px;"><b>'+this.Title+'</b></span></a>';
	}
	else if (this.ShowTitle){
		sBuffer += '<a  href="javascript:setDisplayOnOff(\''+this.Prefix+ '_root\');" ><img src="'+this.GetPixPath(this.TitleIcon, this.PixPath)+'" border="0"><span style="COLOR:'+this.FontColor+'; FONT: '+this.FontSize+'px normal '+this.FontFamily+'; line-height:11px;height:15px;PADDING:0px; PADDING-LEFT:3px;"><b>'+this.Title+'</b></span></a>';
	}
	else{
		sBuffer += '<img src="/1.gif" height="1">';
	}
	sBuffer += '</td>';
	sBuffer += '</tr>';
	sBuffer += '</table>';
	
	if (this.LoadDelayed){
		sBuffer += '<div id="'+this.Prefix+'ContentDiv"><center><img src="/pix/loading.gif"></CENTER></div>';
		sBuffer += '</div>';
		this.WriteToDocAfterLoad(sBuffer);
	}
	else{
		if (this.AryUsedSection.length > 0)
			sBuffer += this.GetNode(this.AryUsedSection[this.Count][0], true);
		sBuffer += '</div>';
		this.WriteToDoc(sBuffer);
	}
	//alert(sBuffer)
}catch(e){handleClientError('AMGTreeWriteHtml', e);}}

//************************************************************
function AMGTreeWriteToLayer (){
	var sBuffer = this.GetNode(this.AryUsedSection[this.Count][0], true);
	writeLayer(this.Prefix+'ContentDiv', sBuffer)
}
//************************************************************
function AMGTreeUnselectAll(){
	if(this.IsForm && this.FormType == 'radio'){
		DoRadioDeselectAll(eval('document.forms[0].fld'+this.ID));
	}
}
//************************************************************
function AMGTreeGetNodeHtml(__iIndex, __bRoot){try{
	var sBuffer		= '';
	var iLastNode	= this.AryContent[__iIndex].length-1;
	var iCount = this.Count;
	__bRoot				= (__bRoot)?'inline':'none';
	sBuffer += '<div id="'+ this.Prefix+ '_'+ this.AryUsedSection[this.Count][1] + '" style="visibility:'+sVisible+';display:'+__bRoot+';"><table border="0" cellpadding="0" cellspacing="0" style="padding:0px;margin:0px;">';
	//sBuffer += '<table border="0" cellpadding="0" cellspacing="0" style="padding:0px;margin:0px;">';
	//alert(this.AryContent)
	for (var j = 1 ; j < this.AryContent[__iIndex].length ; j++){
		sBuffer+= '<tr>';
		var sBranchType = (this.AryUsedSection[iCount][2] < this.ChildsDepth && this.AryContent[__iIndex][j][6]) ? 'close':'empty';
		if ( iLastNode != j){
			sBuffer += '<td style="background-image:url('+this.GetPixPath('branch.gif', this.PixPath)+');background-repeat:repeat-y;vertical-align:top;height:10px;padding:0px;padding-top:0px;" valign="top" background="'+this.GetPixPath('branch.gif', this.PixPath)+'"><img src="'+this.GetPixPath('branch_'+sBranchType+'.gif', this.PixPath) +'" width="20" height="20" border="0" id="'+this.Prefix+ '_'+ this.AryContent[__iIndex][j][0]+'Img1" onclick="'+this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, false)+'">';
			//sBuffer+= '<img src="'+this.GetPixPath('branch_'+sBranchType+'.gif', this.PixPath) +'" width="20" height="20" border="0" id="'+this.Prefix+ '_'+ this.AryContent[__iIndex][j][0]+'Img1" onclick="'+this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, false)+'">';
		}
		else{
			sBuffer += '<td style="vertical-align:top;padding:0px;padding-top:0px;" valign="top"><img src="'+this.GetPixPath('branch_last_'+sBranchType+'.gif', this.PixPath)+'" width="20" height="20" border="0" id="'+ this.Prefix+ '_'+ this.AryContent[__iIndex][j][0]+'Img1" onclick="'+this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, false)+'">';
			//sBuffer+= '<img src="'+this.GetPixPath('branch_last_'+sBranchType+'.gif', this.PixPath)+'" width="20" height="20" border="0" id="'+ this.Prefix+ '_'+ this.AryContent[__iIndex][j][0]+'Img1" onclick="'+this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, false)+'">';
		}	
		sBuffer += '</td><td style="vertical-align:top;padding:0px;" width="100%"><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" style="vertical-align:top;padding:0px;padding-top:1px;">';
		//sBuffer += '<td style="vertical-align:top;padding:0px;" width="100%">';
		//sBuffer +=	'<table border="0" cellpadding="0" cellspacing="0">';
		//sBuffer +=	'<tr>';
		//sBuffer +=	'<td valign="top" style="vertical-align:top;padding:0px;padding-top:1px;">';
		if (this.AryContent[__iIndex][j][1])
			sBuffer +=	'<img src="'+this.GetPixPath(this.AryContent[__iIndex][j][1], this.PixPath)+'" width="20" height="20" border="0" id="'+this.Prefix+ '_'+ this.AryContent[__iIndex][j][0]+'Img2" onclick="'+this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, false)+'"></td>';
		else
			sBuffer +=	'<img src="'+this.GetPixPath('folder_close.gif', this.PixPath)+'" width="20" height="20" border="0" id="'+this.Prefix+ '_'+ this.AryContent[__iIndex][j][0]+'Img2" onclick="'+this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, false)+'"></td>';
		if (this.IsForm){
				sBuffer +=	'<td valign="top" style="padding:0px;width:20px;">';
			if ( this.AryContent[__iIndex][j][8] != true){
				sBuffer +=	'<input type="'+this.FormType+'" name="fld'+this.ID+'" id="fld'+this.ID+this.AryContent[__iIndex][j][0]+'Field" value="'+this.AryContent[__iIndex][j][0]+'"';
				if ( this.AryContent[__iIndex][j][9] == true){
					//alert(this.ID + ' - ' + this.AryContent[__iIndex][j][0] + ' - ' + this.AryContent[__iIndex][j][9])
					sBuffer += ' checked="true"';
				}
				if ( this.FormValidate){
					sBuffer += ' validate="true" required="true"';
				}
				if(this.IsEditForm)
					sBuffer +=	 ' onClick="ChangeFormModifiedStat(true);">';
				else
					sBuffer +=	 '>';
				
			}
			else
				sBuffer +=	'&nbsp;';
		}
		sBuffer +=	'</td><td valign="top" style="vertical-align:top;padding-top:4px;">';
		//sBuffer +=	'<td valign="top" style="vertical-align:top;padding-top:4px;">';
		sBuffer +=	'<a href="javascript:';
		sBuffer +=	this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, this.ActionOnItem);
		sBuffer +=	'" ';
		sBuffer +=	' style="text-decoration:none;color:'+this.FontColor+'; font-size:'+this.FontSize+'px;font-weight:normal;font-family:'+this.FontFamily+'; line-height:11px;height:15px;PADDING:0px; PADDING-LEFT:3px;cursor:hand;" ';
		
		sBuffer +=	' ondblClick="'+ this.GetOnClickHtml(this.AryContent[__iIndex][j], this.Count, false) + ';"';
		
		sBuffer +=	' onMouseOver="return '+this.ID+'.SetStatus(\''+EscapeString(this.AryContent[__iIndex][j][5])+'\');"';
		sBuffer +=	' id="'+this.ID + 'BranchText'+ this.AryContent[__iIndex][j][0] +'"';
		sBuffer +=	'>';
		sBuffer +=	this.AryContent[__iIndex][j][2];
		sBuffer +=	'</a></td></tr></table>';
		//sBuffer +=	'</td>';
		//sBuffer +=	'</tr>';
		//sBuffer +=	'</table>';
		if (this.AryUsedSection[iCount][2] < this.ChildsDepth	 && this.AryContent[__iIndex][j][6]){
			sBuffer += this.GetNode(this.AryUsedSection[++this.Count][0], false);
			//alert('OK \n'+this.AryContent[__iIndex][j]);
		}else{
			//alert('KO \n'+this.AryContent[__iIndex][j]);	
		}
		sBuffer += '</td></tr>';
		//sBuffer += '</tr>';
	}
	sBuffer += '</table></div>';
	//sBuffer += '</div>';
	return sBuffer;
}catch(e){handleClientError('AMGTreeGetNodeHtml', e);}}
//************************************************************
function AMGTreeSetStatus(__sMessage){
	return setWindowStatus(__sMessage);
	
}
//************************************************************
function AMGTreeGetOnClickHtml(__aryItem, __iCount, __bAction){try{
	var sBuffer = '';
	if (!__bAction && __aryItem[6] && this.ChildsDepth > 0 && this.AryUsedSection[__iCount][2] <= this.ChildsDepth	 &&  this.AryUsedSection[__iCount+1][3] == __aryItem[0])
		sBuffer +=	this.ID+'.NodeClick(\''+this.AryUsedSection[__iCount+1][1] +'\', \''+ __aryItem[0] +'\');';
	if (__bAction && __aryItem[3])
			sBuffer +=	__aryItem[3]+';';
	return sBuffer;
}catch(e){handleClientError('AMGTreeGetOnClickHtml', e);return '';}}
//************************************************************
function AMGTreeGetOnClickNoCountHtml(__aryItem, __bAction){try{
	var sBuffer = '';
	if (__bAction && __aryItem[3])
			sBuffer +=	__aryItem[3]+';';
	return sBuffer;
}catch(e){handleClientError('AMGTreeGetOnClickHtml', e);return '';}}


//************************************************************
function AMGTreeNodeClick(__sChildID, __sID){
	setDisplayOnOff(this.Prefix+ '_'+ __sChildID);
	this.SwapImages(this.Prefix+ '_'+ __sID);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function AMGTreeSwapImages(__sID){try{
		var oImageBranch = getObject(__sID+'Img1');
		var sImageBranch = oImageBranch.src;
		if (sImageBranch.indexOf('close') != -1 || sImageBranch.indexOf('open') != -1){
			oImageBranch.src = (sImageBranch.indexOf('close')!= -1)?sImageBranch.replace('close', 'open'):sImageBranch.replace('open', 'close');
		}
		oImageBranch = getObject(__sID+'Img2');
		var sImageBranch = oImageBranch.src;
		if (sImageBranch.indexOf('close') != -1 || sImageBranch.indexOf('open') != -1){
			oImageBranch.src = (sImageBranch.indexOf('close')!= -1)?sImageBranch.replace('close', 'open'):sImageBranch.replace('open', 'close');
		}
}catch(e){handleClientError('AMGTreeSwapImages', e);}}


//************************************************************
function AMGTreeOpenTree(__TreeID,__Selected){try{
	
	for (var i = 0 ; i < this.AryContent.length ; i++){
		for (var j = 1 ; j <this.AryContent[i].length ; j++){
			if(this.AryContent[i][j][0] == __TreeID){
				//alert(__TreeID+' - '+this.AryContent[i][0][0]);
				this.OpenTree(this.AryContent[i][0][0],false);	
				//alert(this.AryUsedSection[i]);

				var oImageBranch = getObject(this.Prefix+ '_'+__TreeID+'Img1');
				if(oImageBranch!=null){
					var sImageBranch = oImageBranch.src;
					if (sImageBranch.indexOf('close') != -1){
						for (var k = 0 ; k< this.AryUsedSection.length ; k++){
							if(this.AryUsedSection[k][3] ==__TreeID){
								this.NodeClick(this.AryUsedSection[k][1],this.AryUsedSection[k][3]);
								break;
							}
						}
					}			
				}else{
					//alert('null');
				}
				break;
			}
		}
		/*
		if(__TreeID == this.AryContent[i][0][0]){	
			
			this.OpenTree(this.AryUsedSection[i][0],false);	
			var oImageBranch = getObject(this.Prefix+ '_'+this.AryUsedSection[i][3]+'Img1');
			var sImageBranch = oImageBranch.src;
			if (sImageBranch.indexOf('close') != -1){
				this.NodeClick(this.AryUsedSection[i][1],this.AryUsedSection[i][3]);
			}
		}*/
		
	}
	return this.GetPath(__TreeID);
	
}catch(e){handleClientError('AMGTreeOpenTree', e);return '';}}
//************************************************************
function AMGTreeOpenTreeInit(){try{
	if (this.FormSelectedItems){
		var ArySelected = this.FormSelectedItems.split(',');
		for (var i = 0; i< ArySelected.length; i++){
			if (ArySelected[i]){
				this.OpenTree(ArySelected[i], false);
			}
		}
	}
}catch(e){handleClientError('AMGTreeOpenTreeInit', e);return '';}}
//************************************************************
function AMGTreeGetPath(__TreeID){try{
	var path = '';
	var name = '';

	for (var i = 0 ; i < this.AryContent.length ; i++){
		for (var j = 1 ; j <this.AryContent[i].length ; j++){
			if(this.AryContent[i][j][0] == __TreeID){
				name = this.AryContent[i][j][2] ;
				path +=this.GetPath(this.AryContent[i][0][0]);
				path += '/' + name;
				break;
			}
		}			
	}
	return path;
	
}catch(e){handleClientError('AMGTreeGetOnClickHtml', e);return '';}}



