﻿// Reference Below Provides Intellisense in Visual Studio 2008
/// <reference path="RealAge.Utils.js"/>

// Declare Base Namespace
RealAge.Utils.declareNamespace("RealAge.Ajax");

// Create Class
RealAge.Ajax.SignInStatus = function(webServiceUrl, signInElementId, signOutElementId)
{
	/// <summary>Queries from Client Browser to Service to Get Users ASP.NET Sign-In Status</summary>
	
	var XmlHttp;

	this.WebServiceUrl = webServiceUrl;
	this.signInElementId = signInElementId;
	this.signOutElementId = signOutElementId;
	
	// Called on Initialization
	this.init = function() 
	{
		XmlHttp = RealAge.Utils.CreateXmlHttp();
		this.queryLoginStatus();
	}
	
    this.queryLoginStatus = function()
    {
        /// <summary>Calls service to retrieve ASP.NET sign-in status.</summary>
		try
		{
			if (XmlHttp) 
			{	
				// Abort Existing XmlHttp Request
				if((XmlHttp.readyState > 0) && (XmlHttp.readyState < 4)) XmlHttp.abort();
			      
				XmlHttp.open('POST', this.WebServiceUrl , true);
				XmlHttp.onreadystatechange = RealAge.Utils.createDelegate(this, this.AjaxResultSuccessful);			
				
				// Request JSON Response Type
				XmlHttp.setRequestHeader('Content-Type', 'application/json');
				
				// Send Parameters in JSON Format
				XmlHttp.send('{}'); 
	        }
        }
	    catch(ex) 
	    { 
			/* Error Occured */ 
			this.XmlHttpError();
	    }
    }

    this.AjaxResultSuccessful = function(strResult)
    {
        try 
        {
        	if (XmlHttp.readyState == 4) 
        	{
	            if (XmlHttp.status == 200)
	            {
	               	if(XmlHttp.responseText == "1")
	               	{
						document.getElementById(this.signInElementId).style.display = "none";
						document.getElementById(this.signOutElementId).style.display = "";                	
	               	}
	               	else if(XmlHttp.responseText == "2" || XmlHttp.responseText == "3")
	               	{
	               	        // Login state changed, force a redirect to update the page
	               	        window.location = document.location;
	               	}
	               	else
	               	{
						document.getElementById(this.signInElementId).style.display = "";
						document.getElementById(this.signOutElementId).style.display = "none";                	
	               	}
	            }
            }
		}
		catch(ex)
		{
		    // Error Occured in Response Eval
		}
    }

    this.XmlHttpError = function()
    { 
    	// An Error Occured Trying to use XMLHttp Object 
   	}
    
    RealAge.Utils.registerClass(this, arguments);
}
