/* $File: iw_loadingScript.js $
   $Date: 2009-11-01 $
   $Modify date: 2010-09-09 $
   $Author: AA $
   $Version: 1.0.10 $
*/


//var $jQuery = jQuery.noConflict(); // Avoid jQuery conflicts with another library


/////////////////////////////////////
// Configuration Parameters:
/////////////////////////////////////

//
// userInfoServletPath is the url to UserInfoServlet in IM
//
var userInfoServletPath="https://res.mobistar.be/imfrontend/userinfo";
//
// dwrPath is the url to dwr in IM
//
var dwrPath="https://res.mobistar.be/imfrontend/dwr";
//
// refererId is the id of the current domain
//
var refererId = 'mobistarresidential';
//
// internetEveryWhere defines if we are on a ineteret everywhere or not
//
var internetEveryWhere = false;
//
// The french redirection URL to the client zone for an internet everywhere
var internetClientZoneFR="https://www.mobistar.be/secured/espace-client/internet/activer-service-option-internet-everywhere/index.html";
//
// The dutch redirection URL to the client zone for an internet everywhere
var internetClientZoneNL="https://www.mobistar.be/secured/klantenzone/internet/dienst-optie-internet-everywhere-activeren/index.html";
//
// The identity widget resources folder location
var identitywidgetPath="";
//
// ErrorMessage_fr is the default french message when an error occurs
//
var ErrorMessage_fr="Veuillez réessayer plus tard";
//
// ErrorMessage_nl is the default dutch message when an error occurs
//
var ErrorMessage_nl="Probeer het later opnieuw";
//
// userInfoTimeout is the timeout before displying the login form
//
//
// French error message when user does  not fill in the login or the password fields
//
var EmptyLoginErrorMessage_fr="Veuillez introduire votre login et mot de passe";
//
// Dutch error message when user does  not fill in the login or the password fields
//
var EmptyLoginErrorMessage_nl="Geef uw login en wachtwoord in";
//
// userInfoTimeout is the timeout before displying the login form
//
var userInfoTimeout=10000;
//
// loginTimeout is the timeout when calling backend to connect
//
var loginTimeout=15000;
//
//language parameter should be set in the meta tag DCSext.ctlang,
//and the content of this tag should be fr or fr_BE or nl or nl_BE
//
var language = $('meta').filter(function(){
    return $(this).attr("name") == "DCSext.ctlang";
}).attr('content').substr(0,2);

$(document).ready(function()
{
    timeout = setTimeout('displayLoginForm(language)',userInfoTimeout);
    loadIdentityWidget();
});

/////////////////////////////////////
// function that check if the user is connected and if yes, it will display the welcome message
// and if not it will display the login form
/////////////////////////////////////
/////////////////////////////////////
// function that check if the user is connected and if yes, it will display the welcome message
// and if not it will display the login form
/////////////////////////////////////
function loadIdentityWidget()
{
    $.ajaxSettings.cache = false;
    if((document.cookie.indexOf("SMSESSION")!==-1) && (document.cookie.indexOf("SMSESSION=LOGGEDOFF")===-1))  {
      $.getJSON(
          userInfoServletPath+'?language='+language+'&callback=?',function(data){
              clearTimeout(timeout);
              if  (data.firstname != 'Anonymous')
              {
                  if(data.firstname==null){
                      displayWelcomeMessage('',data.lastname,'','',language);
                  }
                  else {
                      displayWelcomeMessage(data.firstname,data.lastname,'','',language);
                  }
              }
              else
              {
                  displayLoginForm(language);
              }
          });
    } else {
              		clearTimeout(timeout);
                  displayLoginForm(language);
    }
}


/////////////////////////////////////
// function that load the html of login
// form and inserts it in the mbs-portal-identity-widget
/////////////////////////////////////
function displayLoginForm(lg)
{
    $('#mbs-portal-identity-widget').load(identitywidgetPath+'identitywidget/pages/'+lg+'/login.form.' + lg + '.html',function() {
        openNewWindow();
    });
    
}
/////////////////////////////////////
// function that load the html of
// welcome message and inserts it in
// the mbs-portal-identity-widget
/////////////////////////////////////
function displayWelcomeMessage(firstname,lastname,segment,segtype,lg)
{
    $('#mbs-portal-identity-widget').load(
        identitywidgetPath+'identitywidget/pages/'+lg+'/welcome.message.' + lg + '.html',
        function()
        {
            $("#textUserName").html(firstname);
            openNewWindow(lg);
        }
        );
}
/////////////////////////////////////
// function that add a target=blank
// to every link (a)in internet everywhere
// login widget
// 
/////////////////////////////////////
function openNewWindow(lg)
{
    if(internetEveryWhere){
        $('#mbs-portal-identity-widget a[id!=lnkYourProfile]').attr('target','_blank');
        if(lg=='fr') $('#lnkMypage_fr').attr('href',internetClientZoneFR);
        else $('#lnkMypage_nl').attr('href',internetClientZoneNL);
    }
}
//end of iw_loadingScript.js
