﻿// JScript File
/************************************************************************************* 
-  JScriptCommPageFunctions. This file contains JavaScript functions that are common 
-       to all pages.
**************************************************************************************/
/* *********************************************************************************** 
-  Commom Variables.  The following list of variables
-
************************************************************************************ */                   
        var ScrRes = screen.width + "x" + screen.height;  //Screen resolution
        var DocLocation = document.location;
        var Register = null;
        var CallFunct;
        var CommonUtil = null;
        var TID;
        var BMSIs_PageValid;  //Page validator flag
        var RegUserFunct;
/* *********************************************************************************** 
- End Commom Variables.
************************************************************************************ */                   
/************************************************************************************* 
-  Related Jscript files   
-       JScriptObservations.
-       JScriptDYNSurveyObjects.
-       JScriptNavObjects.
**************************************************************************************/
/* *********************************************************************************** 
-  Commom Page Functions:  pageClicked
-  Parameters: thisEvent -The event fired. 
-              thisObj - The parent object. 
-  Purpose: Setup for Observations recording for clicked elements.  Will capture any
-           page click. 
-  Date:  10/04/2006
-  Mods:
-     
************************************************************************************ */
function pageClicked(thisEvent,thisObj)	  
      {   
      
        _ErrHandler = CommonUtil.GetErrRef();
      //Check to see that we can get to our session and our
      //AJAX objects.
      if(!_ErrHandler.GetAjaxStatus())
        {
            return;
        }
      if(!_ErrHandler.GetSessionStatus())
        {
            _ErrHandler.HandelSessionErr(null);
            return;
        }  
      //Get browser specific Utility object... 
        var objFact = CommonUtil.GetObsDomUtilRef();
        var Util = objFact.CreateUtilObject();                  
        var IncFound = false; 
        var curEvent;
        var IncEvent;
        var MetaArray;
        var ObsCode;
        var ObsrObj;
        var Pos = 0;
        var DynObs;
        var SurveyObj;  
        var _ErrHandler;           //Error handler.
        var _AjaxProxy;            //Pointer to Server Ajax Object
        var _FormValidation;
        var _IsValidation = false;
        var idx = -1;
        var ele;
               
        //Get the event clicked. 
        curEvent = Util.GetEevent(thisEvent);

          if(isObservations)
            {
                document.getElementById(maxHField).value = maxTries;
                clearTimeout(TID);
                TimeOutEncountered(true);
            }        
         
          //Check for validation on this page.  This check done only 
        //for a submit.
        if((curEvent != null) && (curEvent != ""))
         { 
            ele = document.getElementById(curEvent);
            if(ele.type == "submit")
              {
                _IsValidation = true;
              }
              else
              {
                ele = curEvent.toLowerCase();
                idx = ele.indexOf("submit");
                if(idx > -1)
                    {
                        _IsValidation = true;
                    }
               }
            
            
            if(_IsValidation)
              {
                //Get browser specific Utility object... 
               var DOMobjFact = CommonUtil.GetDomUtilRef();   
               //var DOMobjFact = new DOMObjFactory();
               //This object provides browser specific services.  
               _DOMUtil = DOMobjFact.CreateDOMUtilObject();
                _FormValidation = new FormValidation(); 
                if(!(_FormValidation.getFormElements())) 
                  {
                      BMSIs_PageValid = false;  
                     _DOMUtil.CancelEvent(thisEvent); 
                     return;
                  }
                  else
                  {
                    //This is a registration type activity
                    
                    BMSIs_PageValid = true;
                  }
              }
          }    
        
        
        /*********************************************************************
        - Check to see if we should process a survey.  If we should instantiate
        - the Survey Manager DYNSurveyObjectManager.  Call the GetIsProceed 
        - method which will:
        - (1) Check to see if this event is of interest to the survey
        - (2) If this is a Survey related event, perform edits or clear
        -     the survey.
        - (3) Suspend processing if edits do not pass.   
        *********************************************************************/
         if(isSurvey)
            {
                SurveyObj = new DYNSurveyObjectManager(thisEvent);
                if(SurveyObj.GetIsProceed())
                  {
                    SurveyObj.ProcessEvent(false);
                    //Edits No Good stop now - event is cancelled.
                    if(!SurveyObj.GetIsProceed())
                    {                                
                        return;
                    }
                  }
            }
        if(IncludeList != null  && curEvent !=null)
        {                   
        //See if we want to record the event.. Is it included in the list??
            for(var i = 0; i < IncludeList.length; i++)
            {   
                  if(Util.FindID(IncludeList[i], curEvent))
                  {
                    IncEvent = IncludeList[i];
                    IncFound = true;
                    //Extract the observation code
                    ObsCode = Util.GitObservationID(IncEvent)
                    break;
                  }   

             }
         }
         else
         {
             if(curEvent != null)
             {
               ObsCode = Util.GitObservationID(curEvent);
             }
             else
             {
                ObsCode = null;
             }
             if(ObsCode != null)
               {
                IncFound = true;
               }
         }  
        
            if(IncFound)
            { 
                //We made it record this one...  
                try
                {  
                    //check page validation
                    _ErrHandler.PageValidation();
                    //If the page has failed validation, do not record observation....
                    if(_ErrHandler.GetPageValidStatus())
                    {              
                        //If this event gets recorded then do it
                        //Get an instance of the observation
                        DynObs = new DYNObservation(ObsCode, curEvent, exit_method, null, null, false);
                        
                        //Record the observation
                        DynObs.PutObservation();
                    }
                       
                }
                catch(e)
                {
                    alert("Error -" +  e);
                }    
            
            }
             
            //If there is a survey and edits passed, commit the data.
            if(isSurvey)
            {
                if(SurveyObj.GetIsProceed())
                  {
                    SurveyObj.ProcessEvent(true);
                  }  
                
            }
            
            
            //If this is a registration event and all edits passed,
            // call registerd function to commit the data.
           if((RegUserFunct != null) && (BMSIs_PageValid == true))
            {
                var regfunct = RegUserFunct.split('|');
                
                for(var i = 0; i < regfunct.length; i++)
                  {
                     try
                       {           
                         eval(regfunct[i]);   
                       }
                       catch(e)  
                       {
                       
                       }           
                  }
           
            }             
                        
      }
 /* *********************************************************************************** 
-  Commom Page Functions:  pageInit()
-  Parameters: None. 
-  Purpose: Setup for Observations for time on site, pop a window that asks for 
-           user age and residency confirmation.
-  Date:  5/19/2006
-  Mods:
-     
************************************************************************************ */         
function pageInit()
  {       
      //Instantiate common utility class
      CommonUtil = new CommonFunct();
      _ErrHandler = CommonUtil.GetErrRef();
      //Check to see that we can get to our session and our
      //AJAX objects.
      if(!_ErrHandler.GetAjaxStatus())
        {
            return;
        }
      //Check to see if we have session   
      if(!_ErrHandler.GetSessionStatus())
        {
            _ErrHandler.HandelSessionErr(null);
            return;
        }  
      //Get browser specific Utility object... 
        var objFact = CommonUtil.GetDomUtilRef();
        var NavObj;
        var MsgEle;
        var MsgTxt;
        var MsgArr;
        var DynObs;
        var IsClientInital;
        var APPageIsValid;         //Application page valid flag object.  
        var _ErrHandler;           //Error handler.
        var _AjaxProxy = AjaxProxy;            //Pointer to Server Ajax Object
        
       
      //Pop the US resident + 18 ys window if a function exists...
        if(popFunct != "")
            {
                //popFunct is the name of the JS function to execute
                //Do it Dynamically.... 
                eval(popFunct);
            } 
      //Set up time on site logging
      //First see if we observe anything...
      if(isObservations)
      {
          document.getElementById(maxHField).value = maxTries;
          TimeOutEncountered(true);    
      }
      
    
    //Should we substitute images??
    if(isImgSub == 'true')
        {   
            var ImgSub = new ImgSwap(formId);
            ImgSub.replaceImage(imgSubList);
            
        } 
   //Check for navigation
     if(isNav)
       {
            NavObj = new NavObject()
            NavObj.SetTextMsg(formId);
            if(NavObj.IsChangeText())
              {
                NavObj.ChangeTextMsg();              
              
              }
       }
   //Do  initial client side observations    
   //First see if we observe anything...    
   if(isObservations)
   {
      
   //Check for any initial client logging
      IsClientInital = _AjaxProxy.ClientGetIsInit().value
      if(IsClientInital)
      {
        //Get A list of initial observations fired on the client
        var InitList = _AjaxProxy.GetInitClientObservations();
        var ObsrId; 
         for(var i = 0; i < InitList.value.length; i++)
            {
                 var ObsrId = InitList.value[i];
                 //If this event gets recorded then do it
                //Get an instance of the observation
                DynObs = new DYNObservation(ObsrId, null, exit_method, null, null, false);
                
                //Record the observation
                DynObs.PutObservation();
            }
             
        //We're done with the initial client observations - make sure that they
        //are not repeated.  
        
        _AjaxProxy.ClientSetIsInit(false);
        
        //Now check for cookies - Specifically the cookie for this appliaction 
        var CookieNamz = "visitor" + appId;
        
        if(CommonUtil.CanHazCookiez())
         {
            //Look for application specific cookie
            if(!CommonUtil.IsCookiez(CookieNamz))
              {
                //We found no cookie make one and write initial visit observation
                _AjaxProxy.ClientWriteCookie(appId,userId,thisURL);
              }
         }
        else
         {
            //Cookies not enabled in browser write cookie not enabled observation
            _AjaxProxy.ClientWriteNoCookies(thisURL);
         }
        
      }  
    } 
    
    //See if any local processing is to be done on page init
    if(Register != null)
    {
        var reg = Register.split('|');
        for(var i = 0; i < reg.length; i++)
          {
             try
               {          
                    eval(reg[i]);
               }
               catch(e)  
               {
               
               }             
          }
        
            
    }  
    
    if(CallFunct != null)
    {
        var callfunct = CallFunct.split('|');
        for(var i = 0; i < callfunct.length; i++)
          {
             try
               {           
                 eval(callfunct[i]);   
               }
               catch(e)  
               {
               
               }           
          }
         
            
            
        
    }
    
   
               
  } 
/* *********************************************************************************** 
-  Commom Page Functions:  TimeOutEncountered()
-  Parameters: None. 
-  Purpose: Observations for (total) time on site.  This sets a timer based upon
-           parameterized inputs and records time intervals based upon parameters. 
-  Date:  5/19/2006
-  Mods:
-     
************************************************************************************ */           
function TimeOutEncountered(isTimeoutOnly)
    {
        var iTriesCnt = document.getElementById(maxHField).value;  
        var imilliSeconds = logSeconds * 1000; 
        var iseconds; 
        var iResultid;
        
        if (iTriesCnt > 0)
           {
           //If this is not the first time set the timer for the next time
            TID = setTimeout("TimeOutEncountered(false)",imilliSeconds);   
            
                if(!isTimeoutOnly)
                {
                 
                    iseconds = imilliSeconds / 1000
                       if (iTriesCnt != maxTries)
                       { 
                       //Instantiate a session end observation object and update...  
                            DynObs = new DYNObservation(SessionEndKey, "None", exit_method);
                            DynObs.UpdateObservation();                 
                          
                       }	
                    //Decrement number of tries when we reach zero do not record.  	
                    iTriesCnt--; 
                    document.getElementById(maxHField).value = iTriesCnt;	
                }
           }            
         
    }                     
/* End Commom Page Functions *************************************************************/

