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)
	{
		// 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;
		}
// CODE REMOVED WHILE WE DETERMINE A BETTER WAY TO SEE IF SOMEONE HAS AUTHENTICATED
//		begin = document.cookie.indexOf('TFLGreetingString');
//		if (begin != -1) 
//		{
//			// we found the cookie - now read it and check the length
//			message = readGreetingCookie('TFLGreetingString');
//			if (message.length > 5)
//			{
//				return true;
//			}
//			else
//			{
//				// the cookie just said 'hello' so not logged in
//				return false;
//			}
//
//		}
//		else
//		{
//			// the cookie wasn't found so not logged in
//			return false;
//		}
	}
	else
	{
		// no cookies were found so not logged in
		return false;
	}
	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) {	
			ntptEventTag('ev=SecureMsg_LoggedInFACChat');		
			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 {
			ntptEventTag('ev=SecureMsg_LoggedInCCCChat');			
			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) {
			ntptEventTag('ev=SecureMsg_NotLoggedInFACChat');			
			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 {
			ntptEventTag('ev=SecureMsg_NotLoggedInCCCChat');				
			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() {
var showLogin = new Boolean(true);
if (isAuthenticated()) { 
		showLogin = false;
	        document.write('<div id="loginbox_inner">');
	        
	        // The cookie signifying someone has logged in was set
	        // see if the greeting cookie 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
		{
			// Just write out Hello since we know they have the IPCZQX cookie
			document.writeln('<div id="greetingString" class="mythriventheader">');
			document.writeln('Hello');
			document.writeln('</div>');			
		}


// determine which logout URL to show - in the test environments go straight to the app - production go to LO first
var member_host = window.location.host;
var logout_url = "http://www.lutheransonline.com/servlet/lo_ProcServ/dbpage=rur&ruraction=logout&REMOTE_DEST=https://service.thrivent.com/apps/logout/logout.do";

if (member_host.search(/dev/i) > 0)
{
	logout_url = "https://service.dev.thrivent.com/apps/logout/logout.do";
}
else if (member_host.search(/systst/i) > 0)
{
	logout_url = "https://service.systst.thrivent.com/apps/logout/logout.do";
}
else if (member_host.search(/inttst/i) > 0)
{
	logout_url = "https://service.inttst.thrivent.com/apps/logout/logout.do";
}
else
{
	logout_url = "http://www.lutheransonline.com/servlet/lo_ProcServ/dbpage=rur&ruraction=logout&REMOTE_DEST=https://service.thrivent.com/apps/logout/logout.do";
}
            document.writeln('<div id="logoutbox_lgbtn"><a href="' + logout_url + '"><img src="https://www.thrivent.com/images/btn_logout.gif" alt="Log Out" width="72" height="24" border="0"></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.write('<div id="loginbox_inner">');
document.writeln('<form name="MemberSignon" enctype="application/x-www-form-urlencoded" method="post" action="https://idp.thrivent.com/nidp/app/login?id=membernmpw&target=https://myservice.thrivent.com/portal/mythrivent" AUTOCOMPLETE="off">');

document.writeln('<div style="width: 129px; height: 100px; float: right;">');
document.writeln('<div style="width: 2px; height: 98px; float: left; background-color: #FFFFFF; margin: 2px 12px 0px 0px"> </div>');
document.writeln('<div style="width: 110px; height: 100px; float: left; padding: 8px 0px 0px 0px"><div class="loginbox_registerbox"><span class="logintext">Need to Register?</span></div><div style="padding: 3px 0px 0px 0px"><a href="https://service.thrivent.com/apps/MbrEnrollment/registerAccessInfoInit.do?from=https://service.thrivent.com/apps/MbrMaintenance/MaintInit.do&wssrc=homepage&wssubject=registernow"><img src="https://www.thrivent.com/images/btn_register_nonow.gif" border="0" width="90" height="24" /></a><br /><div id="loginbox_whyregister"><a href="#" onclick="MM_openBrWindow(\'https://service.thrivent.com/content/includes/member/siteRegistration/whyregister.html\',\'WhyRegister\',\'width=1000,height=670,resizable=yes,scrollbars=yes\'); ntptEventTag(\'ev=sitereg.whyreg\'); return false;" class="forgottext">Why register?</a></div></div></div>');
document.writeln('</div>');

document.writeln('<span class="logintext"><div id="loginbox_usrtxt">User ID:</div><div id="loginbox_usrbx"><input type="text" id="Ecom_User_ID" name="Ecom_User_ID" size="8" style="width: 70px; height: 15px;" /></div><br /><div id="loginbox_pwdtxt">Password:</div><div id="loginbox_pwdbx"><input type="password" name="Ecom_Password" id="Ecom_Password" size="8" style="width: 70px; height: 15px;" /></div></span><br />');
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" />&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 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>');
document.getElementById('Ecom_User_ID').focus();
document.writeln('</div>');	
document.writeln('</form>');			  
             
		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;	
	}

}

