function readGreetingCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) 
    	return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function isAuthenticated()
{
	// First we check to see if there is a cookie stored.
	// Otherwise the length of document.cookie would be zero.	
	if (document.cookie.length > 0)
	{
		// Second we check to see if the cookie's name is stored in the
		// "document.cookie" object for the page.

		// Since more than one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie" object
		// is not just an empty text.
		// If our cookie name is not present the value -1 is stored
		// in the variable called "begin".

		begin = document.cookie.indexOf('IPCZQX'); 
		if (begin != -1) 
		{
			return true;
		}

	}
	return false;
}

function testAuthentication()
{
	var authenticated = isAuthenticated();
	return authenticated;
}

function openChat() { 
	var refererName = "";
	refererName = encodeURIComponent(refererName);
	var refererurl = encodeURIComponent(document.location.href);
	var hashIndex = refererurl.lastIndexOf('#');
	if(hashIndex != -1){
		refererurl = refererurl.substring(0,hashIndex);
	}
	var w = 630, h = 440;
	var t = 0, l = 0;
	if (window.screen) {
		if (window.screen.availWidth == 800)
			w = window.screen.availWidth * 41 / 100;
		else {
			w = window.screen.availWidth * 31 / 100;
		}
		h = window.screen.availHeight * 95 / 100;
		l = (window.screen.availWidth - w)*98/100;
	}
	if(testAuthentication())
	{
		if (refererurl.indexOf('%2Ffac%2F') > 0) {	
			var params =  "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ",resizable=no,scrollbars=yes,toolbar=no";
			window.open('https://service.thrivent.com/apps/SecureInbox/RedirectToChat?eglvcmd=CustEntry&entryPointName=Logged In FAC Chat&eglvepid=1001&departmentid=999&eglvpartid=1&fieldname_1=&fieldname_2=&fieldname_3=&fieldname_4=&fieldname_5=&referer='+refererurl+'&eglvrefname='+refererName, 'chatwindow', params);
		}
		else {
			var params =  "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ",resizable=no,scrollbars=yes,toolbar=no";
			window.open('https://service.thrivent.com/apps/SecureInbox/RedirectToChat?eglvcmd=CustEntry&entryPointName=Logged In FAC Chat&eglvepid=1003&departmentid=999&eglvpartid=1&fieldname_1=&fieldname_2=&fieldname_3=&fieldname_4=&fieldname_5=&referer='+refererurl+'&eglvrefname='+refererName, 'chatwindow', params);
		}
	}
	else
	{
		if (refererurl.indexOf('%2Ffac%2F') > 0) {
			var params =  "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ",resizable=no,scrollbars=yes,toolbar=no";
			window.open( 'https://service.thrivent.com/apps/SecureInbox/RedirectToChat?eglvcmd=CustEntry&entryPointName=Not Logged In FAC Chat&eglvepid=1002&departmentid=999&eglvpartid=1&referer='+refererurl+'&eglvrefname='+refererName,'chatwindow',params);
		}
		else {
			var params =  "width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + ",resizable=no,scrollbars=yes,toolbar=no";
			window.open( 'https://service.thrivent.com/apps/SecureInbox/RedirectToChat?eglvcmd=CustEntry&entryPointName=Not Logged In FAC Chat&eglvepid=1004&departmentid=999&eglvpartid=1&referer='+refererurl+'&eglvrefname='+refererName,'chatwindow',params);
		}
	}
}



function getLoggedInInfo()
{

//START: Outage message text
var HtmlMessage = "<div id=\"outage\" class=\"outage_container\"><div class=\"outage_top\">&nbsp;</div><div class=\"out_message\"><a href=\"javascript:HideContent('outage')\"><img src=\"/images/help_close.jpg\" align=\"right\" border=\"0\"/></a><span style=\"color: #FF0000; font-size: 18px; font-weight: bold;\">Please Note: </span>On Wednesday, July 30 from midnight to 1 a.m., Central Time, some Insurance & Annuity Account Values may not be available due to system maintenance.<p align=\"center\"><a href=\"javascript:HideContent('outage')\">Close</a></p></div><div class=\"outage_bottom\">&nbsp;</div></div>";


var displayed = false;
var upperHTML = null;
//END: Outage message text

	// Check to see if the person has authenticated
	var showLogin = new Boolean(true);

	if (isAuthenticated()) 
	{ 

		showLogin = false;
			
		// The cookie signifying someone has logged in was set 
		// Display the appropriate greeting

	        document.writeln('<div id="loginbox_inner">');
		// The cookie signifying someone has logged in was set 
		// see if the greetingCookie has been set
		greetingCookie = document.cookie.indexOf('TFLGreetingString');
		if (greetingCookie != -1)
		{
			// the greeting cookie exists, just display that value
			var message = readGreetingCookie('TFLGreetingString');
			document.writeln('<div id="greetingString" class="mythriventheader">');
			document.writeln(message);
			document.writeln('</div>');
		}
		else
		{
			// use ajax to get the greeting message
       			document.writeln('<div id="nameDisplay" class="mythriventheader">&nbsp;</div>');
			var url = 'https://service.thrivent.com/apps/login/LoggedInInfoServlet';
			var myAjaxDiv = new Ajax.Updater('nameDisplay', url, {method: 'get', parameters: ''});
		}

	        document.writeln('<div id="loginbox_lgbtn"><a href="http://www.lutheransonline.com/servlet/lo_ProcServ/dbpage=rur&ruraction=logout&REMOTE_DEST=https://service.thrivent.com/apps/logout/logout.do"><img src="https://www.thrivent.com/images/btn_logout.gif" alt="Log Out" width="72" height="24" border="0"></a></div>');		  
            document.writeln('<div id=\"loginbox_maint\">');					
			
//START: Outage Message
upperHTML = "<a href=\"javascript:ShowContent('outage')\" class=\"sitemaintenance\" >Site Maintenance</a>&nbsp;&nbsp;&nbsp;<span class=\"forgottext\">|</span>&nbsp;&nbsp;&nbsp;";

if((displayed == true))
		 document.writeln(upperHTML + HtmlMessage);
//END: Outage message

		document.writeln('<a href=\"https://www.thrivent.com/aboutus/privacy/\" class=\"forgottext\">Secure</a>&nbsp;<a href=\"https://www.thrivent.com/aboutus/privacy/\"><img src=\"https://www.thrivent.com/images/lock.gif\" width=\"11\" height=\"14\" alt=\"Security\" border=\"0\"/></a></div>');		
		document.writeln('</div>');

		return false;	
	}
	if (showLogin)
	{

        document.writeln('<div id="loginbox_inner">');
          document.writeln('<form name="MemberSignon" method="post" action="https://service.thrivent.com/apps/login/LoginServlet">');
			document.writeln('<span class="logintext"><div id="loginbox_usrbx">User ID: <input type="text" id="username" name="username" size="9"/></div><div id="loginbox_pwdbx">Password: <input type="password" name="password" size="9"/></div></span><input type="hidden" name="destination" value="http://myservice.thrivent.com/portal/mythrivent" />');
	      document.writeln('<div id="loginbox_lgbtn"><input type="image" name="Find2" value="Log In" src="https://www.thrivent.com/images/btn_login.gif" alt="Login" width="70" height="24" border="0" /></div>');
		  document.writeln('<div id="loginbox_frgt"><span class="forgottext">Forgot:</span>&nbsp;&nbsp;<a href="https://service.thrivent.com/security/Security/MbrForgotUserID?from=https://myservice.thrivent.com/portal/mythrivent&wssrc=homepage&wssubject=forgotuserid" class="forgottext">ID</a>&nbsp;&nbsp;<span class="forgottext">|</span>&nbsp;&nbsp;<a href="https://service.thrivent.com/security/Security/MbrForgotPassword?from=https://myservice.thrivent.com/portal/mythrivent&wssrc=homepage&wssubject=forgotpassword" class="forgottext">Password</a>&nbsp;&nbsp;<span class="forgottext">|</span>&nbsp;&nbsp;<a href="https://service.thrivent.com/apps/MbrEnrollment/registerAccessInfoInit.do?from=https://service.thrivent.com/apps/MbrMaintenance/MaintInit.do&wssrc=homepage&wssubject=registernow" class="forgottext">Register</a>');

document.getElementById('username').focus();

			  document.writeln('</div>');	
		      document.writeln('</form>');				  
              document.writeln('<div id=\"loginbox_maint\">');				  

//START: Outage Message
upperHTML = "<a href=\"javascript:ShowContent('outage')\" class=\"sitemaintenance\" >Site Maintenance</a>&nbsp;&nbsp;&nbsp;<span class=\"forgottext\">|</span>&nbsp;&nbsp;&nbsp;";

if((displayed == true))
		 document.writeln(upperHTML + HtmlMessage);
//END: Outage message

		document.writeln('<a href=\"https://www.thrivent.com/aboutus/privacy/\" class=\"forgottext\">Secure</a>&nbsp;<a href=\"https://www.thrivent.com/aboutus/privacy/\"><img src=\"https://www.thrivent.com/images/lock.gif\" width=\"11\" height=\"14\" alt=\"Security\" border=\"0\"/></a></div>');		
		document.writeln('</div>');
	}

	return null; 

	// Our cookie was not set. 
	// The value "null" is returned from the function.
}

function getNameOnly()
{


	// Check to see if the person has authenticated
	var showLogin = new Boolean(true);

	if (isAuthenticated()) 
	{ 

		showLogin = false;
			
		// The cookie signifying someone has logged in was set 
		// Display the appropriate greeting

	        document.writeln('<div id="loginbox_inner_nameonly">');
		// The cookie signifying someone has logged in was set 
		// see if the greetingCookie has been set
		greetingCookie = document.cookie.indexOf('TFLGreetingString');
		if (greetingCookie != -1)
		{
			// the greeting cookie exists, just display that value
			var message = readGreetingCookie('TFLGreetingString');
			document.writeln('<div id="greetingString" class="mythriventheader">');
			document.writeln(message);
			document.writeln('</div>');
		}
		else
		{
			// use ajax to get the greeting message
       			document.writeln('<div id="nameDisplay" class="mythriventheader"></div>');
			var url = 'https://service.thrivent.com/apps/login/LoggedInInfoServlet';
			var myAjaxDiv = new Ajax.Updater('nameDisplay', url, {method: 'post', parameters: ''});
		}
		document.writeln('</div>');
	        

		return false;	
	}

}
