/**
  NOTE: This library requires the MD5 javascript library to be in include in the page in order to work properly
*/

var memberLabel = "";
var LoginLabel = "";
var ErrorMessageLabel = "";
var loggedSection;
var notLoggedSection;
var btnJoin;
var MemberInfo = "";

/**
  IsAuthenticated() : boolean
  Defines if the current user is authenticated
*/
function IsAuthenticated() {
  //check if cirque-club cookie exists
  return connectorGetCookie("cds_clubmember_id") != '';
}

/**
  IsAuthenticated_OnLoad() : void
  Event triggered after document ready event to adjust connector display depending user current loggin state
*/
function IsAuthenticated_OnLoad() {
  if(IsAuthenticated()) {
    btnJoin.style.visibility='hidden';
    btnJoin.style.display='none';
    document.getElementById("ConnectorErrorMessage").style.display='none';
    document.getElementById("ConnectorErrorMessage").style.visibility='hidden';
    notLoggedSection.style.visibility='hidden';
    notLoggedSection.style.display='none';
    loggedSection.style.visibility='visible';
    loggedSection.style.display='block';
    document.getElementById("userLoginLabel").firstChild.nodeValue = memberLabel ;
  } else {
    loggedSection.style.visibility='hidden';
    loggedSection.style.display='none';
  }
}

/**
  GetUserName() : void
  Handles the connector CirqueClub Button click event
*/
function GetUserName() {
  if(MemberInfo == "") {
    var params = new SOAPClientParameters();
    var member = SOAPClient.invoke(cirqueClubSoapServicesUrl, "IsAuthenticated", params, false);
    if(member.IsAuthenticated) {
      MemberInfo = member.FirstName + " " + member.LastName;
    }
  }
  return MemberInfo;
}

/**
  ClubPopup_OnClick() : void
  Handles the connector CirqueClub Button click event
*/
function ClubPopup_OnClick() {
  if(IsAuthenticated()) {
    document.getElementById("userName").firstChild.nodeValue = GetUserName();
  }
}

/**
  MemberLogout_CallBack() : void
  Adjusts the CirqueClub popup display after member's logout
*/
function MemberLogout_CallBack() {
  jQuery('.cirqueClubPopUpBtn').fadeOut("fast");
  jQuery('.cirqueClubPopUp').fadeOut("fast");
  loggedSection.style.visibility='hidden';
  loggedSection.style.display='none';
  notLoggedSection.style.visibility='visible';
  notLoggedSection.style.display='block';
  document.getElementById("userLoginLabel").firstChild.nodeValue = LoginLabel ;
  window.location.reload();
}

/**
  SetMemberDisplay() : void
  Adjusts the CirqueClub popup display after member's logout
*/
function SetMemberDisplay(memberObj) {
  btnJoin.style.visibility='hidden';
  btnJoin.style.display='none';
  document.getElementById("ConnectorErrorMessage").style.display='none';
  document.getElementById("ConnectorErrorMessage").style.visibility='hidden';
  notLoggedSection.style.visibility='hidden';
  notLoggedSection.style.display='none';
  loggedSection.style.visibility='visible';
  loggedSection.style.display='block';
  document.getElementById("userName").firstChild.nodeValue = memberObj.FirstName +" "+ memberObj.LastName;
  document.getElementById("userLoginLabel").firstChild.nodeValue = memberLabel ;
}

/**
  SetLoginErrorDisplay() : void
  Adjusts the CirqueClub popup display on loggin error
*/
function SetLoginErrorDisplay() {
  var errorMsg = document.getElementById("ConnectorErrorMessage");
  btnJoin.style.visibility='visible';
  btnJoin.style.display='block';
  notLoggedSection.style.visibility='visible';
  notLoggedSection.style.display='block';
  loggedSection.style.visibility='hidden';
  loggedSection.style.display='none';
  errorMsg.firstChild.nodeValue = ErrorMessageLabel;
  errorMsg.style.display='block';
  errorMsg.style.visibility='visible';
}

/**
  Logout() : void
  Logs out the current user and adjust connector display
*/
function Logout() {
   var params = new SOAPClientParameters();
   SOAPClient.invoke(cirqueClubSoapServicesUrl, "Logout", params, true, MemberLogout_CallBack);
   btnJoin.style.visibility='visible';
   btnJoin.style.display='block';
}

/**
  Login() : void
  Logs in the current user and adjust connector display
*/
function Login() {
  var params = new SOAPClientParameters();
  document.getElementById('ConnectorEmail').value;
  params.add('email',document.getElementById('ConnectorEmail').value); 
  params.add('encryptedPwd',MD5(document.getElementById('ConnectorPassword').value)); 
  var member = SOAPClient.invoke(cirqueClubSoapServicesUrl, "Login", params, false);

  if(member != null) {
    if(!member.IsAuthenticated && member.RedirectUrl != '' && member.RedirectUrl != null) {
      document.location.href = member.RedirectUrl;
    } else if(member.IsAuthenticated) {
      SetMemberDisplay(member);
      window.location.reload();
    } else {
      SetLoginErrorDisplay();
    }
  } else {
    SetLoginErrorDisplay();
  }
}

/***************************************************************************
  COOKIE UTIL SECTION
***************************************************************************/

/**
  connectorGetCookie() : string
  Params: 
	- name : string
  Returns a cookie string if found, string.Empty otherwise
*/
function connectorGetCookie(name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(name + "=");
        if (c_start != -1) {
            c_start = c_start + name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end=document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}