﻿/* --------------------------------------------------------------------------------
 Filename: Utility.js                                                   

 Description:

 This is a utility class.
 The idea is to be able to create a utility object and then use it to have access
 to some functions that come in handy and are used in more than one place. 
 
 History:                                                                        
   Ver         Inits   Date        Comments                                        
   1.00.00     jwl     02/25/08    Created

-------------------------------------------------------------------------------- */


// Utility:
//
// This is the constructor for the Utility class.
//
function Utility()
{
    // DATA:    
    // May not have any public or private data in the Utility class.
               
    // PRIVATE METHODS:               
    // No private methods yet in the Utility class.
                   
               
    // PUBLIC METHODS:    
               
                       
    // Utility::ShowElement:  
    //
    // Utility function to safely hide or show an element.
    // Pass in the id of the element and wrap it in a span tag using 
    // "span" + id of the element you want to get as the id name.
    //  
    function ShowElement(szID, szTagName, bShow)
    {        
        if(!szID || szID.length < 1)
            return null;
                        
        var objElement = GetElementUsingSpanId(szID, szTagName);                        
        if(objElement)
        {
            if(bShow)
                objElement.style.display = 'block'; 
            else 
                objElement.style.display = 'none';
            return objElement; 
        } 
        else
            return null;
                    
    }                    
    this.ShowElement = ShowElement;                 
    
                       
    // Utility::GetElementUsingSpanId
    //
    // If you wrap a span around an element using "span" + the element's id,
    // then you can use this method to return a reference to the element.
    //
    function GetElementUsingSpanId(szID, szTagName)
    {
        if(!szID || szID.length < 1)
            return null;
                                                
        var objSpanElement = document.getElementById("span" + szID);
        if(objSpanElement)
        {
            var objElements = objSpanElement.getElementsByTagName(szTagName); 
            if(objElements && objElements.length > 0)
                return objElements[0]; 
            else
                return null;
        } 
        else
            return null;               
    }   
    this.GetElementUsingSpanId = GetElementUsingSpanId;            
      
      
    // Utility::GetElementsUsingSpanId
    //
    // If you wrap a span around an element using "span" + the element's id,
    // then you can use this method to return a reference to the elements within
    // that span that have the passed in tag name.
    //
    function GetElementsUsingSpanId(szID, szTagName)
    {
        if(!szID || szID.length < 1)
            return null;
              
        var objSpanElement = document.getElementById("span" + szID);
        if(objSpanElement)
        {
            var objElements = objSpanElement.getElementsByTagName(szTagName); 
            if(objElements && objElements.length > 0)
                return objElements; 
            else
                return null;
        } 
        else
            return null;               
    }   
    this.GetElementsUsingSpanId = GetElementsUsingSpanId;            
    
                
    // Utility::GetElementUsingId
    //
    // Safely return an element with the passed in id, or null if it's not found.
    //
    function GetElementUsingId(szID)
    {
        if(!szID || szID.length < 1)
            return null;
                                                
        var pElement = document.getElementById(szID);
        if(pElement)
            return pElement;
        else
            return null;               
    }   
    this.GetElementUsingId = GetElementUsingId;                            
                
                
    //	Utility::GetQueryVariable:
    //
    //	Pass in the name of the query string variable you want and this function
    //	will return the value of that variable.
    //
    function GetQueryVariable(varName) 
    {
	    var query = window.location.search.substring(1);
	    var vars = query.split("&");
	    for (var i=0;i<vars.length;i++) 
	    {
		    var pair = vars[i].split("=");
		    if(pair[0] == varName)
			    return pair[1];
	    } 
	    return ""; 	
      
    } 
    this.GetQueryVariable = GetQueryVariable;    
                 
                 
    // Utility:SetCookie:
    //
    //		name - name of the cookie
    //		value - value of the cookie
    //		[expires] - expiration date of the cookie
    //		(defaults to end of current session)
    //		[path] - path for which the cookie is valid
    //		(defaults to path of calling document)
    //		[domain] - domain for which the cookie is valid
    //		(defaults to domain of calling document)
    //		[secure] - Boolean value indicating if the cookie transmission requires
    //		a secure transmission
    //		* an argument defaults when it is assigned null as a placeholder
    //		* a null placeholder is not required for trailing omitted arguments
    //
    //	Example: setCookie("counter", visits, now);
    //
    function SetCookie(name, value, expires, path, domain, secure) 
    {
      var curCookie = name + "=" + escape(value) +
          ((expires) ? "; expires=" + expires.toGMTString() : "") +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          ((secure) ? "; secure" : "");
          
      document.cookie = curCookie;
      
    } // SetCookie
    this.SetCookie = SetCookie;     // Make this a public method.    


    //	Utility:GetCookie:
    //
    //		name	- name of the desired cookie
    //		returns	- string containing value of specified cookie or 
    //				  null if cookie does not exist
    //
    //	Example: var visits = getCookie("counter");
    //
    function GetCookie(name)
    {
	    var dc = document.cookie;
	    var prefix = name + "=";
	    var begin = dc.indexOf("; " + prefix);
    	
	    if (begin == -1) 
	    {
    	    begin = dc.indexOf(prefix);
    	    if (begin != 0) 
    		    return "";
  	    } 
  	    else
    	    begin += 2;
        	
	    var end = document.cookie.indexOf(";", begin);
	    if (end == -1)
    	    end = dc.length;
        	
	    return unescape(dc.substring(begin + prefix.length, end));
      
    } // Utility:GetCookie
    this.GetCookie = GetCookie;     // Make this a public method.    


    //	Utility:DeleteCookie:
    //
    //		name 		- name of the cookie
    //		[path]		- path of the cookie (must be same as path used to create cookie)
    //		[domain]	- domain of the cookie (must be same as domain used to create cookie)
    //
    //		Path and domain default if assigned null or omitted if no explicit argument proceeds.
    //
    //	Example: 
    //
    function DeleteCookie(name, path, domain) 
    {
	    if(getCookie(name))
	    {
		    document.cookie = name + "=" +
			    ((path) ? "; path=" + path : "") +
			    ((domain) ? "; domain=" + domain : "") +
			    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	    }
    	
    } // Utility:DeleteCookie
    this.DeleteCookie = DeleteCookie;     // Make this a public method.                     
                 
                 
    // Utility::AreYouSureDelete:
    //
    // Presents a dialog and asks if they are sure they want to delete whatever thing they passed in.
    // Returns true if they are sure and false if they are not.
    // This can be hooked up to a ASP button to cancel the execution when they select false.
    //
    // Example: deleteButton.Attributes.Add("onclick", "return g_utility.AreYouSureDelete('class', false);")
    //
    function AreYouSureDelete(szThing, bPlural) 
    {      
        var szMessage = "Are you sure you want to delete this " + szThing + "?";
        if(bPlural)
            szMessage = "Are you sure you want to delete these " + szThing + "?";

        if (confirm(szMessage) == false) {
            //alert(confirm(szMessage));

            //window.event.returnValue = false; This line removed for FireFox fix. Need tested in older versions
            return false;
        }
        else
            return true;
                    
    } // AreYouSureDelete          
    this.AreYouSureDelete = AreYouSureDelete;   
                                   
} // Utility:       




