/*******************************************************************************
 * Utility functions to perform various authentication related JS actions
 * @Author perryc
 * 
 * Dependencies:
 * --jQuery (1.3.2)
 *******************************************************************************/
//Create Namespace
cfc.namespace("common.auth");

/*
 * Object to contain methods needed for library
 */
cfc.common.auth = (function() {
	
	/***********************************
	 * PRIVATE METHODS
	 ***********************************/
	var priv = {
			
		/*
		 * 'xmlhttp' request object, do not access directly, use getXmlHttp instead
		 */
		xmlhttp : null,
		
		/*
		 * Retrieves an XMLHttpRequest object for use in AJAX calls
		 */
		getXmlHttp: function() {
		    if (xmlhttp) {
		    	return xmlhttp;
		    }
	
		    if (window.XMLHttpRequest) 
		    {
		    	xmlhttp = new XMLHttpRequest();
		    } else if (window.ActiveXObject) 
		    {
				try {
				    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
				} catch (ex) 
				{
				    try 
				    {
				    	xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
				    } catch (ex) 
				    {}
				}
		    }
		    
		    return xmlhttp;
		}
	};
	
	/***********************************
	 * PUBLIC API
	 ***********************************/
	var pub = {
			
		/*
		 * Logs out of the website's Basic auth mechanism. 
		 * Does NOT clear any SiteMinder session data
		 * 
		 * @param ctx: Current context path
		 * @param redirect: Set to true if the page should reload after logout, false otherwise
		 */	
		logout: function(ctx, redirect) {
		
			// try to clear the authentication cache of the browser for our site
		    if (document.all) {
		        // Internet Explorer: 'ClearAuthenticationCache' is only available in IE
		        document.execCommand('ClearAuthenticationCache');
		
		    } else {
		        if (navigator.userAgent.indexOf("Safari") != -1
		            || navigator.userAgent.indexOf("Chrome") != -1
		            || navigator.userAgent.indexOf("Opera") != -1) {
		
		            // Safari, Chrome and Opera: remove authorization cookie
		            document.cookie = "Authorization=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/";
		        } else {
		        	var xmlhttp = cfc.auth.getXmlHttp();
				    if (!xmlhttp) {
				    	return;
				    }
	
				    if (xmlhttp.readyState < 4) {
						xmlhttp.abort();
					}
				    
		            // Firefox/Mozilla: use anonymous "login" to trigger a "logout"
		            xmlhttp.open("GET", ctx + "?sling:authRequestLogin=1", false, "anonymous", "null");
		            xmlhttp.send('');
		        }
		    }
		    
		    //refresh the page if needed
		    if(redirect)
		    {
		    	document.location.href=document.location.href;
		    }
		    
		    return false;
		},
		
		/*
		 * Checks if a valid SM session cookie is available
		 */	
		hasSmSession : function() {
			if (document.cookie.length>0)
	        {
	            c_start = document.cookie.indexOf("SMSESSION=");
	            if (c_start != -1)
	            {
	                c_start = c_start + 10 //length of cookie name + '=';
	                c_end = document.cookie.indexOf(";", c_start);
	                if (c_end == -1) c_end=document.cookie.length; //fix for last cookie
	                var c_value = unescape(document.cookie.substring(c_start, c_end));
	                if(c_value != null && c_value != "" && c_value != "LOGGEDOFF")
	                {
	                	return true;
	                }
	            }
	        }
			
			return false;
		},
		
		/*
		 * Displays the specified DOM elements if the user has a valid SM session
		 * 
		 * @param selector: CSS selector of DOM element(s) that should be displayed
		 */	
		displayIfLoggedIn : function(selector) {
			if(pub.hasSmSession())
			{
				$(selector).show();
				
				return false;
			}
		},
		
		/*
		 * Displays the specified DOM elements if the user is anonymous
		 * 
		 * @param selector: CSS selector of DOM element(s) that should be displayed
		 */	
		displayIfAnonymous : function(selector) {
			if(!pub.hasSmSession())
			{
				$(selector).show();
				
				return false;
			}
		}
	};
	
	return pub;
});
