//PBt_cookie.js
//
// (1) 3rd Party HTML should include file specific to client, e.g. <clientname>_cookie.js
//
// (2) 3rd Party HTML includes file, PBt_cookie.js (this file)
//
//	  e.g.
//
//	  	<script language="JavaScript" src='/pb3/js/Newco_cookie.js'></script>
//		<script language="JavaScript" src='/pb3/js/PBt_cookie.js'></script>
//
// (3) <clientname>_cookie.js is created & maintained by PBt.
//     For example, newco_cookie.js
//
//     var PBtCookieName = "NEWCOCORPORATE_CANDIDATE";
//     var PBtLoginLink = "../pbank/owa/NewcoLogin";     <== login procedure
//     var PBtLogoffLink = "../pbank/owa/NewcoLogout";   <== logoff procedure
//     var PBtGuestHome  = ".. /corporate/...[home URL];
//     var PBtCandidateHome = "../pbank/owa/pbk07w10.main?p_sCandId="+PBtCookieName";
//
//
// (4) 3rd Party HTML calls
//
//     PBtShowLoginLogoffHTML( <login_HTML_fragment> , <logout_HTML_fragment> )
//     PBtShowLoginLogoffText( <login_text> , <logout_text> )
//         - to display correct image or text link
//
//     PBtDoLoginLogoff()
//         - OnClick for image/link to action Login/Logoff correctly
//
//     PBtSessionUserName()
//         - to return string containing name of user (logged-in) or "Guest"
//
//     PBtIsSession()
//         - if required to determine if "Candidate" or "Guest"
//
//--     PBtHomeURL()
//--         - Home URL depend of session type
//
//     PBtDoHome()
//         - OnClick for image/link to action link correctly

// ========================================================
// 						UTILITY SECTION
// ========================================================
//
// "Constants" to be used throughout
//

var PBtIsGuest 		= "Guest" ;
var PBtIsCandidate	= "Candidate" ;

// ========================================================
function PBtReadCookies ( CookieName )
// ========================================================
{
	if ( (null == CookieName) || ('' == CookieName) ) {
			alert('?? Cannot find PBtCookieName ??') ;
			return "" ;
	}
	else {
	  var dc = document.cookie;
	  var prefix = CookieName + "=";
     if ( "=" == prefix ) return "";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
		 begin = dc.indexOf(prefix);
		 if (begin != 0) return "";
	  } else
		 begin += 2;
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
		 end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}
}

// ========================================================
function PBtReportError( sErr )
// ========================================================
{
	document.write( sErr ) ;		// report validation error to page
}

//
// ========================================================
//                PUBLISHED HTML INTERFACE
// ========================================================
//

// ========================================================
function PBtIsSession()
// ========================================================
{
	var sSessionType = PBtIsGuest ;			// most likely?
	if ( PBtIsGuest != PBtSessionUserName() ) {
		sSessionType = PBtIsCandidate ;
   }
	return( sSessionType );
}

// ========================================================
function PBtSessionUserName ()
// ========================================================
{
	var sName = PBtReadCookies( PBtCookieName );
	
	if ( (null == sName) || ('' == sName) || ( sName.indexOf(".")>= 8 ) ) {
		sName = PBtIsGuest ;
   }
	return( sName ) ;
}

// ========================================================
function PBtShowLoginLogoffHTML ( login_HTML_fragment, logoff_HTML_fragment )
// ========================================================
{
	if ( ( (null == login_HTML_fragment) || (null == logoff_HTML_fragment) ) ||
		  ( ('' == login_HTML_fragment)   || ('' == logoff_HTML_fragment) ) )
	{
		PBtShowLoginLogoffText('','') ;			// show a simple text link instead
	}
	else {
	   var sIam = PBtIsSession() ;
		if ( (null == sIam) || ('' == sIam) ) {
				PBtReportError( '??BadSessionType??' ) ;
		}
		else {
			if ( sIam == PBtIsGuest ) {
				document.write( login_HTML_fragment ) ;
			}
			else {
				if ( sIam == PBtIsCandidate ) {
					document.write( logoff_HTML_fragment ) ;
				}
				else {
					PBtReportError( '??BadSessionType??' ) ;
				}
			}
		}
	}
}

// ========================================================
function PBtShowLoginLogoffText ( logon_text, logoff_text ) {
// ========================================================
	var sLogon 	= "Log In" ;
	var sLogoff = "Log Off" ;
	if ( (null != login_text) && ('' == login_text) ) {
		sLogon = logon_text ;
	}
	if ( (null != logoff_text) && ('' != logoff_text) )
	{
		sLogoff = logoff_text ;
	}
   var sIam = PBtIsSession() ;
	if ( (null == sIam) || ('' == sIam) ) {
			PBtReportError( '??BadSessionType??' ) ;
	}
	else {
		if ( sIam == PBtIsGuest ) {
			document.write( '<a href="javascript:PBtDoLoginLogoff();">' || sLogon || '</a>' ) ;
		}
		else {
			if ( sIam == PBtIsCandidate ) {
				document.write( '<a href="javascript:PBtDoLoginLogoff();">' || sLogoff || '</a>' ) ;
			}
			else {
				PBtReportError( '??BadSessionType??' ) ;
			}
		}
	}
}

// ========================================================
function PBtDoLoginLogoff ()
// ========================================================
{
	var sIam = PBtIsSession() ;
	var url = '/PBtBadLoginLogoffURL.htm' ;   // force a 404 on a bad circumstance, this page should NEVER exist!
	if ( sIam == PBtIsGuest ) {
		url = PBtLoginLink ;					 // from client-specific include js file
	}
	else {
		if ( sIam == PBtIsCandidate ) {
			url = PBtLogoffLink + PBtReadCookies( PBtCookieSession );			 // from client-specific include js file
		}
	}

	window.location=url ;
}

// ========================================================
function PBtDoHome ()
// ========================================================

{
	var sIam = PBtIsSession() ;
	var url = '/PBtBadLoginLogoffURL.htm' ;   // force a 404 on a bad circumstance, this page should NEVER exist!
	if ( sIam == PBtIsGuest ) {
		url = PBtGuestHome ;					 // from client-specific include js file
	}
	else {
		if ( sIam == PBtIsCandidate ) {
			url = PBtCandidateHome + PBtReadCookies( PBtCookieSession );			 // from client-specific include js file
		}
	}

	window.location=url ;
}
// ========================================================
function PBtShowCandHome () {
// ========================================================
	var sIam = PBtIsSession() ;
	var url = '/PBtBadLoginLogoffURL.htm' ;   // force a 404 on a bad circumstance, this page should NEVER exist!
  if ( sIam == PBtIsCandidate  ) {
      document.write( '<a href="javascript:PBtDoHome()" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'n311_r1_c1\',\'\',\'/pb3/corporate/Tesco/debut/album/shared/images/secnav/3.11/3.11_r1_c1_f2.gif\',1);"><img name="n311_r1_c1" src="/pb3/corporate/Tesco/debut/album/shared/images/secnav/3.11/3.11_r1_c1.gif" width="143" height="11" border="0" alt="Home" /></a>');
    }
  }

// ========================================================

