function displayUserInfoIframe()
{
  beginIframeString = '<iframe src="http';
  endIframeString = '://www.mobistar.be/www/be/mobistar/publicportal/web/jsp/userInfo.jsp" style="display:none"></iframe>';
  protocolSubString = window.location.protocol.substr(4,1);
  if (protocolSubString == 's') $('mbs-portal-user-info-iframe').update(beginIframeString + 's' + endIframeString);
  else $('mbs-portal-user-info-iframe').update(beginIframeString + endIframeString);
}

function setUserInfoBean(firstName)
{
  var htmlWelcomeString = "Bienvenue";
  var htmlDeconnexionString = '<a target="_parent" href="https://www.mobistar.be/fr/e-services/page/logout" class="go b">D&eacute;connectez-vous</a>';
  $('portaluserinfo').update('<strong>' + htmlWelcomeString + '&nbsp;' + firstName + '</strong>' + htmlDeconnexionString);
}

function setDefaultMessage()
{
  var htmlConnexionString = '<a target="_parent" href="https://www.mobistar.be/fr/e-services/page/applicationAuthentication" id="XPortalLogin" class="go b" style="margin-left:50px;">Identifiez-vous</a>';
  $('portaluserinfo').update(htmlConnexionString);
}

function readCookie(cookieName) {
  var cookieNameStart,valueStart,valueEnd,cookieValue,returnValue;
    cookieNameStart = document.cookie.indexOf(cookieName+'=');
    if (cookieNameStart < 0)
    {
      returnValue = null;
    }
    else
    {
      valueStart = document.cookie.indexOf(cookieName+'=') + cookieName.length + 1;
      valueEnd = document.cookie.indexOf(";",valueStart);
      if (valueEnd == -1)
      {
        valueEnd = document.cookie.length;
      }
      cookieValue = document.cookie.substring(valueStart,valueEnd );
      returnValue = (cookieValue == '') ? null : unescape(cookieValue);
    }
    return returnValue;	
}

var portalInfoFailure = 0;
var portalInfomaxFailure = 10;

function pollUserinfo()
{
  new PeriodicalExecuter(
    function(pe)
      { 
        var userFirstName = readCookie('portalUserInfo');
        if ((userFirstName != null) && (userFirstName != '' ) && (userFirstName != 'Anonymous'))
        {
          setUserInfoBean(userFirstName);
          pe.stop();
            document.stopObserving("dom:loaded", pollUserinfo);
          }
          else
          {   
              portalInfoFailure++;
              if(portalInfoFailure == portalInfomaxFailure)
              {
                setDefaultMessage();
              }
        }
      }
      , 0.1);
}  

if (window.location.hostname != 'www.mobistar.be')
{
	// Display of the Iframe which call the Java Server Page to set the User Info cookie
	displayUserInfoIframe();
	// Creation of the PeriodicalExecuter when the html DOM is loaded 
	document.observe("dom:loaded", pollUserinfo);
}
