﻿// JScript File
/************************************************************************************* 
-  JScriptObservations  This file contains JavaScript functions and objects that 
-       facilitate Observations
**************************************************************************************/
/************************************************************************************* 
-  Related Jscript files   
-       JScriptObservationUtilities.  This is REQUIRED in order to have observations function.
-       JScriptCommPageFunctions.  This is REQUIRED in order to have observations function.
**************************************************************************************/
/*DynObservation Classes *****************************************************************/ 
/* *********************************************************************************** 
-  Class: DYNObsMetaData   
-  Purpose: Records Observation Metadata.  This class represents MetaData and methods that 
-           encapsulate one item of metadata for an observation.  There may be more than
-           one metadata object for an observation or no metadata.  In the case of
-           multiple metadata, these objects are put in a collection.  
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
function DYNObsMetaData()
{
// Public Function Declarations ////////////////////////
     this.PutObservationData = PutObservationData;
     this.UpdateObservationData = UpdateObservationData;
// Public Property Declarations ////////////////////////
// Property Setters
     this.SetCollDescVarName = SetCollDescVarName; 
     function SetCollDescVarName(value)
        {
            _CollDescVarName = value;
        }
        
     this.SetCollDescAttrName = SetCollDescAttrName;    
     function SetCollDescAttrName(value)
        {
            _CollDescAttrName = value;
        } 
        
     this.SetObjectID = SetObjectID;    
     function SetObjectID(value)
        {
            _ObjectID = value;
        }      
     this.SetthisURL = SetthisURL;    
     function SetthisURL(value)
        {
            _thisURL = value;
        }
     this.SetObsrCode = SetObsrCode;    
     function SetObsrCode(value)
        {
            _ObsrCode = value;
        }
     this.SetObsData = SetObsData;    
     function SetObsData(value)
        {
            _ObsData = value;
        } 
     this.SetisFlash = SetisFlash;    
     function SetisFlash(value)
        {
            _isFlash = value;
        }                                                        
// Private Variable Declarations ////////////////////////
     var _CollDescVarName = null;
     var _CollDescAttrName = null; 
     var _ObjectID = null;
     var _thisURL = null;
     var _ObsrCode = null;
     var _ObsData = null;
     var _isFlash = null;
////////////////////////////////////////////////////////
// Public Functions ////////////////////////////////////
/* *********************************************************************************** 
-  Function: PutObservationData   
-  Purpose:  Record data for an observation.
-  Parameters: Pos - the position in the object array that this object occupies.  
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
    function PutObservationData(Pos)
        { 
          //If it is a Flash site request we already have the data
           if(!_isFlash)
             { 
                var ObsData = GetObsData(_ObjectID, _CollDescVarName, _CollDescAttrName);
                try
               {          
                    AjaxProxy.ClientPutObservationData(_ObsrCode,ObsData,Pos);
               }
               catch(e)  
               {
                    alert("e");
               }          
                
             }
             else
             {
                AjaxProxy.ClientPutObservationData(_ObsrCode,_ObsData,Pos);
             }
           
        }
/* *********************************************************************************** 
-  Function: UpdateObservationData   
-  Purpose:  UpDate data for an observation.
-  Parameters: Pos - the position in the object array that this object occupies.  
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
    function UpdateObservationData(Pos)
        { 
           //If it is a Flash site request we already have the data
            if(!_isFlash)
             {  
                var ObsData = GetObsData(_ObjectID, _CollDescVarName, _CollDescAttrName);
                AjaxProxy.ClientUpdateObservationData(_ObsrCode,ObsData,Pos);
             }
             else
             {
                AjaxProxy.ClientUpdateObservationData(_ObsrCode,ObsData,Pos);
             }   
           
        }        

// Private Functions ////////////////////////////////////
/* *********************************************************************************** 
-  Function: GetObsData   
-  Purpose: Get the actual data for this metadata obhject.  We obtain this data 
-           dynamically based upon the input metadata passed.    
-  Parameters: ObjId - The ID of the client side object from which we wish to extract
-              data.  This could be an actual object or a variable.
-              VarName - Variable name or attribute name.
-              AttrName - Indicator that describes the source of the data  i.e. a variable 
-              or an actual attribute. 
-  Date:  5/15/2006
-  Mods:
-     
************************************************************************************ */        
    function GetObsData(ObjId, VarName, AttrName)
       {
            
            var ObsData;
            var CurObj = null;
            
            //See if we have a client object 
            if(ObjId != null)
            {
                CurObj = document.getElementById(ObjId);
            }
            //Look at the passed metadata to determine the source of the data that 
            //we wish to obtain.
            switch(VarName)
            {
                 case "attr":
                      if(CurObj != null)
                      {
                        ObsData = eval("CurObj." + AttrName);
                      }
                    break;
             }
             switch(AttrName)
                {
                //This is a Javascript var
                 case "property":
                   ObsData = eval(VarName);
                 
                 break;
                 //This is an attribute of a client tag named in VarName. 
                 case "attr":
                      if(CurObj != null)
                      { 
                        ObsData = eval("CurObj." + VarName);
                      }
                    break; 
                 
                }    
           return ObsData;
       
       }    
        
} 
//*End DYNObsMetaData Class ************************************************************/  
/* *********************************************************************************** 
-  Class: DYNObservation   
-  Purpose: Records Observations.  This class represents MetaData and methods that 
-           encapsulate an observation.  
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
function DYNObservation(ObsrCode, CurEvent, thisURL, ParmNames, ParmValues, isFlash)
{
// Constructor
     this.Init = Init(ObsrCode, CurEvent, ParmNames, ParmValues, isFlash);
      _thisURL = thisURL;
      _ObsrCode = ObsrCode;
// Public Function Declarations ////////////////////////
     this.PutObservation = PutObservation;
     this.UpdateObservation = UpdateObservation;

// Public Property Declarations ////////////////////////

                        
// Private Variable Declarations ////////////////////////
     var _ObsrCode;
     var _MetaArray;
     var _thisURL;
      
////////////////////////////////////////////////////////
// Public Functions ////////////////////////////////////
/* *********************************************************************************** 
-  Function: Init  
-  Purpose: Constructor sets up any metadata for this observation.  If this request
-           is from a Flash site, we already have the data in the ParmNames/ParmValues
-           list.  We initialize the metadata objects differently      
-  Parameters: ObsCode - Observation Code that represents this observation. 
-              CurEvent - The client event ID that triggered this action.  
-              ParmNames - A list of data value names.  
-              ParmValues - A list of data values associated with ParmNames.  
-              isFlash - Is this a request from a Flash site.  
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
    function Init(ObsCode, curEvent, ParmNames, ParmValues, isFlash)
        {             
           //Get a utility object compatible with this browser 
           var objFact = new BrObjFactory();
           var Util = objFact.CreateUtilObject();
           //Populate the Metadata collection for this observation 
           //depending on the type of request Flash/non Flash 
           if(!isFlash)
             {
                _MetaArray = Util.MetaObjectFactory(ObsCode, curEvent);
             }
           else
             {
                if(ParmNames != null)
                {
                    _MetaArray = Util.FlashMetaObjectFactory(ObsCode, ParmNames, ParmValues);
                }
             }  

        }
/* *********************************************************************************** 
-  Function: PutObservation  
-  Purpose: Records an Observation. 
-  Parameters: ObsCode - Observation Code that represents this observation. 
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
    function PutObservation()
        { 
          //Record the observation   
          AjaxProxy.ClientPutObservation(_ObsrCode, _thisURL);
          //Now record the metadata for the observation
          if(_MetaArray != null)
          {
            PutObsMeta();
          }  
                
        } 
/* *********************************************************************************** 
-  Function: UpdateObservation  
-  Purpose: Updates an Observation. 
-  Parameters: ObsCode - Observation Code that represents this observation. 
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
    function UpdateObservation()
        { 
          //Update the observation 
          AjaxProxy.ClientUpdateObservation(_ObsrCode, _thisURL);
         //Now record the metadata for the observation
         if(_MetaArray != null)
          {
            UpdateObsMeta()
          }  
                
        }             
// Private Functions ////////////////////////////////////
/* *********************************************************************************** 
-  Function: PutObsMeta  
-  Purpose: Records Observation Metadata.  Iterates thru the metadata object collection 
-           and calls the correct method to record the data. 
-  Parameters: None. 
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
    function PutObsMeta()
        { 
            //Iterate thru the list of Observation Metadata objects and executes
            //the PutObservationData method on each MetaData object.  
            var MtaDataObj;
            var Pos = 0;
            if(_MetaArray.length > 0)
                {
                     for (var i = 0; i < _MetaArray.length; i++)
                        {
                            MtaDataObj = _MetaArray[i]
                            MtaDataObj.PutObservationData(Pos);
                            Pos ++;
                                
                        }   

                }          
                
        } 
/* *********************************************************************************** 
-  Function: UpdateObsMeta  
-  Purpose: Updates Observation Metadata.  Iterates thru the metadata object collection 
-           and calls the correct method to record the data. 
-  Parameters: None. 
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
    function UpdateObsMeta()
        { 
            //Iterate thru the list of Observation Metadata objects and executes
            //the PutObservationData method on each MetaData object.  
            var MtaDataObj;
            var Pos = 0;
            if(_MetaArray.length > 0)
                {
                     for (var i = 0; i < _MetaArray.length; i++)
                        {
                            MtaDataObj = _MetaArray[i]
                            MtaDataObj.UpdateObservationData(Pos);
                            Pos ++;
                                
                        }   

                }          
                
        }                                                 

} 
//*End DYNObservation Class ************************************************************/  
//*End DYNObservation Classes *************************************************************/                                                                */  


