//*****************************************************************************
// APOM.js ActivPortal Object Model
// 
// Fichier qui contient la definition de SQL Portal Services Component.
// 
//*****************************************************************************



	
	function WebPartDef(webPartID,webPartQualifier,HTMLObject,parentDash,HelpLink)
	{
		
		var _properties = new PropertiesCollection();
		var _parentDash = parentDash;
		
		this.HelpLink = HelpLink;
		this.WebPartID = webPartID;
		this.WebPartQualifier = webPartQualifier;
		this.HTMLObject = HTMLObject;
		
		this.properties = _properties;		
		this.setVisible = SetVisible;
		this.setCaption = SetCaption;
		this.getCaption = GetCaption;
		this.save = Save;
		
		function Save()
		{
			var strFrm;
			var strPropsValue
			var i;
			
			if( _parentDash == null )
				return false;

			if( typeof(_parentDash.frame.document.all.__FRM_WPsave) == "undefined"  )
			{
				strFrm = "<FORM id='__FRM_WPsave' action='" + _parentDash.urlStore + "' ><INPUT TYPE='TEXT' NAME='DashboardID' VALUE='' ><INPUT TYPE='TEXT' NAME='WebPartID' VALUE='' ><INPUT TYPE='TEXT' NAME='WebPartProps' VALUE=''></FORM>";
				_parentDash.frame.document.write(strFrm);
			}

			strPropsValue = '';
			for( i=0; i < _properties.Count; i++ )
			{
				var prop;

				prop = _properties.Item(i);
				strPropsValue = strPropsValue + '<' + prop.propertyName + '>' + prop.value + '</' + prop.propertyName + '>';
			}
			//window.clipboardData.setData("Text",strPropsValue);

			_parentDash.frame.document.all.__FRM_WPsave.DashboardID.value = _parentDash.DashboardID;
			_parentDash.frame.document.all.__FRM_WPsave.WebPartID.value = this.WebPartID;
			_parentDash.frame.document.all.__FRM_WPsave.WebPartProps.value = strPropsValue;

			_properties.Clear();

			_parentDash.frame.document.all.__FRM_WPsave.submit();
		}
		
		function SetVisible(fValue)
		{
			HTMLObject.style.display = (fValue)?'':'none';	
		}
		
		function SetCaption(sCaption)
		{
			getElByID('WebPartCaption' + HTMLObject.id).innerHTML = sCaption;
		}
		
		function GetCaption()
		{
			return getElByID('WebPartCaption' + HTMLObject.id).innerText;
		}
			
		
		
	}

	function DashboardParamDef(Name,Value)
	{
		this.Name = Name;
		this.Value = Value;
	}

	
	function DashboardDef(DBID, HTMLObj,urlStore)
	{

		var _listParam = new Collection();
		var _listWebPart = new Collection();
		var _properties = new PropertiesCollection();

		var _DashboardID = DBID;
		var _HTMLObject = HTMLObj;


		this.urlStore = urlStore;
		this.WebPart = WebPart;
		this.AddWebPart = AddWebPart;
		this.RemoveWebPart = RemoveWebPart;
		this.FindWebPart = FindWebPart;
		this.properties = _properties;
		this.DashboardID = DBID;
		this.frame = null;
		this.init = init;
		this.ParameterCount = 0;
		
		this.Parameters = Parameters;
		this.AddParameter = AddParameter;
		this.GetParametersQueryString = GetParametersQueryString;
		
		
		function AddParameter(Name,Value)
		{
			var Index;
			var NewParameter;
			
			
			
			NewParameter = _listParam.FindByName(Name);
			
			if (NewParameter != null)
				return NewParameter
			
			
			NewParameter = new DashboardParamDef(Name,Value);
			
			
			_listParam.Add(NewParameter);
			this.ParameterCount = _listParam.Count();
		
			return NewParameter;			
		}
		
		function Parameters(Index)
		{
			if (isNaN(Index))
			{
				return _listParam.FindByName(Index);
			}
			else
			{
				return _listParam.Item(Index);
			}
		}
		
		function GetParametersQueryString()
		{
			var i=0;
			var strQuery = '';
			var oParameter;
			
			for(i=0;i<this.ParameterCount;i++)
			{
				oParameter = Parameters(i);
				if( strQuery != '') strQuery = strQuery + '&';
				strQuery = strQuery + 'DBParamName=' + oParameter.Name
				strQuery = strQuery + '&DBParamValue=' + oParameter.Value
			}
			return strQuery;
		}				
		
		
		function FindWebPart(Index)
		{
			if (isNaN(Index))
			{
				var i;

				for( i=0; i<_listWebPart.Count(); i++ )
				{
					
					if( _listWebPart.Item(i).WebPartQualifier == Index )
						return i;
				}
				return -1;
			}
			else
			{
				if( Index < 0 || Index >= _listWebPart.Count() )
					return -1;
				else
					return Index;
			}
		}

		function WebPart(Index)
		{
			var PartIndex;
			PartIndex = FindWebPart(Index);
			if (PartIndex >= 0)
				return _listWebPart.Item(PartIndex);
			else
				return null;
		}

		function AddWebPart(WebPartID,WebPartQualifier, HTMLPart,HelpLink)
		{
			var PartIndex;
			var NewPart;
			
			
			
			PartIndex = FindWebPart(WebPartQualifier);
			
			if (PartIndex >= 0)
				return _listWebPart.Item(PartIndex);
			
			
			NewPart = new WebPartDef(WebPartID,WebPartQualifier,HTMLPart,this,HelpLink);
			
			
			_listWebPart.Add(NewPart);
			this.Count = _listWebPart.Count();
			
		
			return NewPart;
		}

		function RemoveWebPart(Index)
		{
			_WebPartList.Remove(FindWebPart(Index));
			this.Count = _WebPartList.Count();
		}


		function init()
		{
			document.write("<IFRAME ID='__ISave' src='blank.html' style='display:none;' ></IFRAME>"); //
			this.frame = document.getElementById("__ISave")
		}

	}

	//**************MAIN******************
	
	var Dashboard = null;
	
	
	function InitDashboard(DBID, HTMLObj,urlStore)
	{
		Dashboard = new DashboardDef(DBID,HTMLObj,urlStore);
		Dashboard.init();
		
		RegisterEvent('www.activportal.mc:Browser','onload',LoadParameters);
		RegisterAvailableInfos('www.activportal.mc:Dashboard','GetParameter',Dashboard.Parameters);
	}
	
	function LoadParameters()
	{
		var iCount = 0;
		
		if( Dashboard == null ) return;
		
		for(iCount=0;iCount<Dashboard.ParameterCount;iCount++)
			RaiseEvent('www.activportal.mc:Dashboard','SetParameter',Dashboard.Parameters(iCount));
	}
	
	
	function GetCurrentDashboard()
	{
		return Dashboard;	
	}
	
	function AddDashboardParameter(ParamName,ParamValue)
	{
		if( Dashboard == null ) return;
		Dashboard.AddParameter(ParamName,ParamValue);
	}
	
	function RegisterWebPart( strWebPartID, strWPQ, WebPartObject,IsFixedLayout,HelpLink  ) 
	{ 
	
		if( typeof(Dashboard) == "object" )
		{
			if(!IsFixedLayout)
			{
				var objWP = WebPartObject;
				var objBody = eval('WebPart' + strWPQ);
				
				var state = objBody.style.display
				objBody.style.display = '';
				var cxOffset = objWP.offsetWidth - objBody.offsetWidth;
				
				objBody.style.width = objBody.offsetWidth - cxOffset;
				objBody.style.display = state;
				objBody = null;
			}
			return Dashboard.AddWebPart(strWebPartID, strWPQ, WebPartObject,HelpLink);
		}
	}
	
	function MinimizeRestoreWebPart(WPQualifier)
	{
	
		var varProp;
		
		var part = Dashboard.WebPart(WPQualifier)
		if( part != null )
		{
			var partElem = getElByID('WebPart' + WPQualifier);
	
			if (partElem.style.display == "none")
			{
				
				varProp = part.properties.Add("FrameState");
				varProp.value = "1";
				part.save();
				
				partElem.style.display = "";
		
				//alert(part.webPartID);
				//var obj = eval('TR' + part.webPartID);
				
				getElByID('_ImgState' + WPQualifier).src = "pictures/minus.gif";
				getElByID('_ImgState' + WPQualifier).title = MinimizeTip_Text;
				
				RaiseEvent('www.activportal.mc:Webpart','Maximize',part);
			}
			else
			{
				varProp = part.properties.Add("FrameState");
				varProp.value = "0";
				part.save();
		
				partElem.style.display = "none";
				getElByID('_ImgState' + WPQualifier).src = "pictures/plus.gif";
				getElByID('_ImgState' + WPQualifier).title = ExpandTip_Text;
				RaiseEvent('www.activportal.mc:Webpart','Minimize',part);
			}
	
		}	
		
	}
	
	
	function DisplayWebPart(WPQualifier,fVisible)
	{
		var wp = Dashboard.WebPart(WPQualifier)
		if( wp != null )
		{
			try
			{
				wp.setVisible(fVisible);
				var partElem = getElByID('WebPart' + WPQualifier);
				if( fVisible )
				{
					if( partElem.style.display == "none")
					{	
						partElem.style.display = "";
						getElByID('_ImgState' + WPQualifier).src = "pictures/minus.gif";
						getElByID('_ImgState' + WPQualifier).title = MinimizeTip_Text;
					}
				}
				else
				{ 
					if( partElem.style.display == "")
					{	
						partElem.style.display = "none";
						getElByID('_ImgState' + WPQualifier).src = "pictures/plus.gif";
						getElByID('_ImgState' + WPQualifier).title = ExpandTip_Text;
					}
				}
			}
			catch(e)
			{ }
		}
		
			
	}
	
	
	function SetCaptionWebPart(WPQualifier,sCaption)
	{
		var wp = Dashboard.WebPart(WPQualifier)

		if( wp != null )
		{
			try
			{
				wp.setCaption(sCaption);
			}
			catch(e)
			{
			}
		}
	}	
	
	function GetCaptionWebPart(WPQualifier)
	{
		var wp = Dashboard.WebPart(WPQualifier)
		if( wp != null )
		{
			try
			{
				return wp.getCaption();
			}
			catch(e)
			{
			}
			return "";
		}		
	}
	
	function GetWebPart(WPQualifier)
	{
		return Dashboard.WebPart(WPQualifier)
	}
	
	function GetHelpWebPart(WPQualifier)
	{
		var wp = Dashboard.WebPart(WPQualifier)
		if( wp != null )
		{
			try
			{
				return wp.HelpLink;
			}
			catch(e)
			{
			}
			return "";
		}				
	}
	
	 					