﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="SimpleControls.js" />

if (typeof(Sb) === "undefined")
{
	Type.registerNamespace("Sb");
}


Sb.persistAuthentication = function()
{
	var applicationPathFieldId = "applicationPathField";
	var persistentFlagKey = "persistentAuthentication";
	var tokenKey = "DncWebAuthenticationToken";
	var timeoutSecsKey = "loginTimeOut";

	var getCookieValue = function(name)
	{
		if ((!name) || (name.length == 0))
		{
			return null;
		}

		var start = document.cookie.indexOf(name);
		if (start < 0)
		{
			return null;
		}
		
		start += (name.length + 1);
		
		var end = document.cookie.indexOf(";", start);
		end = (end < 0) ? document.cookie.length : end;
		return document.cookie.substring(start, end);
	};
	
	var applicationPath = $get(applicationPathFieldId);
	if ((!applicationPath) || (!applicationPath.value) || (applicationPath.value.lenght < 0))
	{
		return;
	}
	
	var setCookie = function(name, value, expiry)
	{
		var cookieValue = name + "=" + value;
		if (expiry)
		{
			cookieValue += (";expires=" + expiry.toUTCString());
		}
		
		document.cookie = (cookieValue + ";path=/");
	};
	
	var persistentAuthentication = (getCookieValue(persistentFlagKey) == "1");
	var tokenValue = getCookieValue(tokenKey);
	var timeoutSecs = parseInt(getCookieValue(timeoutSecsKey));
	
	var hasToken = (tokenValue && (tokenValue.length > 0));

	var persistCookie = (persistentAuthentication && hasToken &&
		timeoutSecs && (!isNaN(timeoutSecs)));

	if (persistCookie)
	{
		var expirationDays = Math.round(timeoutSecs / 86400);
		persistCookie = (expirationDays > 0);
	}

	if (persistCookie)
	{
		var rememberDate = new Date();
		rememberDate.setDate(rememberDate.getDate() + 365);
		setCookie(persistentFlagKey, "1", rememberDate);

		var expiration = new Date();
		expiration.setDate(expiration.getDate() + expirationDays);
		setCookie(tokenKey, tokenValue, expiration);
	}
	else
	{
		var rememberDate = new Date();
		rememberDate.setDate(rememberDate.getDate() + 365);
		setCookie(persistentFlagKey, "0", rememberDate);
		
		if (hasToken)
		{
			setCookie(tokenKey, tokenValue);
		}
	}

}

// A class for common functionality and multibrowser support
Sb.Common = function()
{
	Sb.Common.initializeBase(this);	
}
Sb.Common.getWindowSize = function()
{
    return;    
}
Sb.Common.registerClass("Sb.Common");

Sb.BrowserInfo = function()
{
    Sb.BrowserInfo.initializeBase(this);	
    
    // Defining what kind of browser we have
        // convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();

    // *** BROWSER VERSION ***
    // Note: On IE5, these return 4, so use is_ie5up to detect IE5.
    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    var is_nav2 = (is_nav && (is_major == 2));
    var is_nav3 = (is_nav && (is_major == 3));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav4up = (is_nav && (is_major >= 4));
    var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    var is_nav6 = (is_nav && (is_major == 5));
    var is_nav6up = (is_nav && (is_major >= 5));
    var is_gecko = (agt.indexOf('gecko') != -1);


    var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var is_ie3    = (is_ie && (is_major < 4));
    var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
    var is_ie4up  = (is_ie && (is_major >= 4));
    var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
    var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
    var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
    var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
    var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

    // KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
    // or if this is the first browser window opened.  Thus the
    // variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.
    var is_aol   = (agt.indexOf("aol") != -1);
    var is_aol3  = (is_aol && is_ie3);
    var is_aol4  = (is_aol && is_ie4);
    var is_aol5  = (agt.indexOf("aol 5") != -1);
    var is_aol6  = (agt.indexOf("aol 6") != -1);

    var is_opera = (agt.indexOf("opera") != -1);
    var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
    var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
    var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
    var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
    var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);
    
    // Exposing what we need
    this.isNav  = is_nav;
    this.isGecko = is_gecko;

    this.isIE     = is_ie;
    this.isIE5_5up = is_ie5_5up;
    this.isIE6    = is_ie6;
    this.isIE6up  = is_ie6up;
    
    this.isOpera = is_opera;    
}
Sb.BrowserInfo.registerClass("Sb.BrowserInfo");

if (typeof(Sys.Application) !== "undefined")
{
	Sys.Application.add_init(function()
	{
		window.logger = new Sb.LogClient();
		window.errorManager = new Sb.ErrorManager();
		window.browserInfo = new Sb.BrowserInfo();
		
		Sb.persistAuthentication();
	});
}

if (typeof(Sys.Application) !== "undefined") Sys.Application.notifyScriptLoaded();
