﻿// JScript File  JScriptUtilities 
/************************************************************************************* 
-  JScriptUtilities  This file contains JavaScript functions and objects that 
-       are common in nature AND NOT BROWSER SPECIFIC.
**************************************************************************************/
/************************************************************************************* 
-  Related Jscript files   
- 
/* *********************************************************************************** 
-  Class: CommonFunct   
-  Purpose: This object will provide common page functionality.  In addition it will 
-           provide global references to common objects that will be needed for error, Dom
-           compatibility and other functions.  In this manner only ONE instance of 
-           common utility objects will exist.  
-  Date:  10/17/2007
-  Mods:
-     
************************************************************************************ */ 
function CommonFunct()
{
     // Constructor
   this.Init = Init();
  
  //Public Methods 
   this.CanHazCookiez = CanHazCookiez;
   this.IsCookiez = IzCookiez; 
   this.SubmitForm = SubmitForm;
  // Private Variable Declarations ////////////////////////
  
 
  // Property Getters
  //GetErrRef - Reference to the common error handler.
  this.GetErrRef = GetErrRef; 
  function GetErrRef()
    {
        return _ErrHandler;
    }
  //GetDomUtilRef - Reference to the common DOM compatibility object.
  this.GetDomUtilRef = GetDomUtilRef; 
  function GetDomUtilRef()
    {
        return _DomUtil;
    }  
 //GetObsDomUtilRef - Reference to the common observation DOM compatibility object.
  this.GetObsDomUtilRef = GetObsDomUtilRef; 
  function GetObsDomUtilRef()
    {
        return _ObsDomUtil;
    }     
  
  //Objects
  var _ErrHandler;
  var _DomUtil; 
  var _ObsDomUtil;    
////////////////////////////////////////////////////////
// Public Functions ////////////////////////////////////

/* *********************************************************************************** 
-  Function: Init  
-  Purpose: Constructor sets up any references to common objects.  
-             
-  Parameters: None. 
-  Date:  10/17/2007
-  Mods:
-     
************************************************************************************ */  
  function Init()
    {
      //Instantiate error handler, common Dom compat objects
       _ErrHandler = new CommonErrorHandler();
       _DomUtil = new DOMObjFactory(_ErrHandler);
       _ObsDomUtil  = new BrObjFactory(_ErrHandler);
        //Call method to extend any native JavaScript objects
       ExtendObjects();     
    }
/* *********************************************************************************** 
-  Function: CanHazCookiez  
-  Purpose: Determine if user has disabled cookies on the browser.  
-             
-  Parameters: None. 
-  Date:  04/14/2008
-  Mods:
-     
************************************************************************************ */  
  function SubmitForm(formId)
    {
        theform = document.forms[formId];
        theform.submit();
        
    }        
/* *********************************************************************************** 
-  Function: CanHazCookiez  
-  Purpose: Determine if user has disabled cookies on the browser.  
-             
-  Parameters: None. 
-  Date:  04/14/2008
-  Mods:
-     
************************************************************************************ */  
  function CanHazCookiez()
    {
        if(navigator.cookieEnabled == true)
        {
           return true;
        }
        else
        {
            return false;
        }
        
    }    
/* *********************************************************************************** 
-  Function: IzCookiez  
-  Purpose: Determine if the application cookie exists on the local machine.  This looks
-           for a specific cookie for a specific application.
-             
-  Parameters: cookieNamz.  The name of the application cookie. 
-  Date:  04/14/2008
-  Mods:
-     
************************************************************************************ */  
  function IzCookiez(cookieNamz)
    {
            var arg = cookieNamz + "=";
            var arlen = arg.length;
            var clen = document.cookie.length;
            var i = 0;
            
            while (i < clen)
            {
                var j = i + arlen;
                if(document.cookie.substring(i, j) == arg)
                  {
                    //Found us some cookiez - Chocolate Chipz!!!!
                    return true;
                  
                  }
                i = document.cookie.indexOf( " ", i) + 1;
                if(i == 0)
                  {
                    break;
                  }
 
            }
            //No Haz cookiez
            return false;        
    }     
}
    
/*End Init Classes *************************************************************/ 
////////////////////////////////////////////////////////
// Private Functions ///////////////////////////////////
    
/* *********************************************************************************** 
-  Function: ExtendObjects  
-  Purpose: This Extends any JavaScript native or Platform specific objects.   
-             
-  Parameters: None. 
-  Date:  10/17/2007
-  Mods:
-     
************************************************************************************ */  
  function ExtendObjects()
    {
        //This will fix the deficient string object in JavaScript by
        //adding trim functions to string  
        // ========================================
        //        STRING FUNCTION EXTENTIONS
        // ========================================
        // ========================================
        //            TRIM FUNCTIONS
        // ========================================
        String.prototype.trim = function() 
            {                
	            return this.replace(/^\s+|\s+$/g,"");
            }
       
        String.prototype.ltrim = function() {
	        return this.replace(/^\s+/,"");
            }
        String.prototype.rtrim = function() {
	        return this.replace(/\s+$/,"");
            }



        // ========================================
        //        ARRAY FUNCTION EXTENTIONS
        // ========================================
        // ========================================
        //          INDEXOF FUNCTIONS
        // ========================================
        // ===========================================================================
        // Function indexOfString - Find the position of the given string in an array. 
        // Parameters: inval - String to loacte in the array.    
        //             begin - Begin position in the array zero based.
        // ===========================================================================
        Array.prototype.indexOfString = function( inval, begin ) 
        {
         for( var i = + begin || 0, l = this.length; i < l; i++ ) 
           {
              if(this[i].toLowerCase() == inval.toLowerCase())
                {
                    return i; 
                }
              
           }
         return -1;
        };
    }    

   

       
/**************************************************************************************/
/*Commom Page Functions *************************************************************/ 
/* *********************************************************************************** 
-  Commom Page Functions - Function: ChangeLanguage   
-  Purpose:  Change Language Indicator via an AJAX call.
-  Parameters: langType - The numeric representation of the language.  
-  Date: 10/17/2007
-  Mods:
-     
************************************************************************************ */
function ChangeLanguage(langType)
{     
    AjaxProxy.ClientSetLanguage(langType);
}

function CheckAjaxProxy()
{
    
    try
      {
        if(AjaxProxy != null)
        {
            return true;
        } 
      }
     catch(e)
        {
            return false;
        }    

}
/*Image Swap Classes *****************************************************************/
/* *********************************************************************************** 
/* ImgSwap Class                                                                     */
/* *********************************************************************************** 
-  Class: ImgSwap
-  Purpose: Detects and swaps images for navigation.
-  Date:  6/12/2006
-  Mods:
-    
************************************************************************************ */
function ImgSwap(thisForm)
{
// Constructor /////////////////////////////////////////
    this.curForm = thisForm;
////////////////////////////////////////////////////////


// Public Function Declarations ////////////////////////
    this.replaceImage = replaceImage;
////////////////////////////////////////////////////////

// Public Functions ////////////////////////////////////
/* *********************************************************************************** 
-  Public  Functions:  replaceImage()
-  Parameters: String of pagename,target element Id,replacement image. 
-  Purpose: Iterate thru a list of pages and associated images and replace the image
-           if it is found.  The imageList is in the form:
-           pagename,target element Id,replacement image|pagename,target element Id,replacement image......  
-  Date:  6/12/2006
-  Mods:
-     
************************************************************************************ */    
    function replaceImage(imageList)
        {  
            //Find the page, split the page/id/image tag string on the pipe delimiter
            var splitImage = imageList.split("|");
            var imgEle;
            var imgEleArr;
            
            //Go thru the array and pick out all entries for this page, then 
            //substitute the image.
            for(var i = 0; i < splitImage.length; i++)
            {
                //Go locate the page
                if(locatePage(this.curForm,splitImage[i]))
                    {
                        imgEleArr = splitImage[i].split(",");
                        imgEle = document.getElementById(imgEleArr[1]);
                        if(imgEle)
                            {   
                                //This is a bit kludgy we do this because the < and > tags
                                //can't be in the web config entry where the imageList currently
                                //resides.  We can remove this when the data lives in a database.  
                                imgEleArr[2] = imgEleArr[2].replace("@","<");
                                imgEleArr[2] = imgEleArr[2].replace("*",">");
                                imgEle.innerHTML = imgEleArr[2];
                            }
                    
                    }
            
            }
          
        }  
// Private Functions        ////////////////////////        
/* *********************************************************************************** 
-  Private  Functions:  locatePage()
-  Parameters: pageName - Name of the current page
-              imageRow - Current row in imageList in the form pagename,target element Id,replacement image   
-  Purpose: Find a match in the string passed to the current page name. 
-  Date:  6/12/2006
-  Mods:
-     
************************************************************************************ */          
    function locatePage(pageName, imageRow)
        { 
            //Find out if images on this page should be swapped 

            var imgRSplit = imageRow.split(",");   
                        
            if(pageName == imgRSplit[0])
                {
                    return true; 
                
                }
            else
                {
                    return false;
                }    
                
        }        

 
} 
/*End Image Swap Classes *************************************************************/ 
