

var SSOServiceUrl = "http://www.rocwb.nl/webservices/ssoservice.svc";
var SSOClientServiceUrl = "http://www.markiezaat.nl/webservices/ssoclientservice.svc";
var logoffLandingUrl = "http://www.markiezaat.nl";

var isClientAuthenticated = false;
var sso = null;

function SyncAuthenticationStatus() {
  if (window.location.hostname.indexOf('www.', 0) == -1) {
    SSOClientServiceUrl = SSOClientServiceUrl.replace("www.", "")  
    logoffLandingUrl = logoffLandingUrl.replace("www.", "")
  }

  // check if we are authenticated in the login domain
  $.get(SSOServiceUrl + '/token/RequestToken?callback=?', {}, RequestTokenCallBack, 'jsonp');
}

function RequestTokenCallBack(ssodata) {
    sso = ssodata;
    
    // we are authenticated in the login domain
    // check if we are also authenticated in the client domain.
    $.ajax({
        type: "POST",
        url: SSOClientServiceUrl + "/IsAuthenticated",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: true,
        success: IsAuthenticatedSuccess,
        error: IsAuthenticatedError
    });

}

function LoginSuccessX(data, textStatus, XMLHttpRequest) {
    window.location.reload();
}

function LoginErrorX(XMLHttpRequest, textStatus, errorThrown) {
    //alert('Er ging iets fout bij het lokaal aanmelden');
}

function IsAuthenticatedSuccess(data, textStatus, XMLHttpRequest) {
    isClientAuthenticated = data.IsAuthenticatedResult;

    if (isClientAuthenticated == false && sso.Status == 'SUCCESS') {
        // We are authenticated in the login domain but not the client domain
        // log me in and verify the token is genuine
        $.ajax({
            type: "POST",
            url: SSOClientServiceUrl + "/ClientAuthenticate",
            data: '{ "loginDomainTicket": "' + sso.Token + '", "username": "' + sso.Username + '", "expire": "' + sso.Expire + '", "persitent": "' + sso.Persistent + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: LoginSuccessX,
            error: LoginErrorX
        });
    }

    if (isClientAuthenticated == true && sso.Status == 'DENIED') {
        $.ajax({
            type: "POST",
            url: SSOClientServiceUrl + "/ClientLogoff",
            data: '{ }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: ClientLogoffSuccess,
            error: ClientLogoffError
        });
    }
    
}

function IsAuthenticatedError(XMLHttpRequest, textStatus, errorThrown) {
    //alert('Kon niet bepalen of je in het rocwb domein bent aangemeld');
}

function ClientLogoffSuccess(data, textStatus, XMLHttpRequest) {
    isClientAuthenticated = data.ClientLogoffSuccessResult; ;
    // reload the document
    //document.location = logoffLandingUrl;
    window.location.reload();
}

function ClientLogoffError(XMLHttpRequest, textStatus, errorThrown) {
    //alert('Kan niet afmelden');
}

function Logoff() {
    // log off in the login domain
    $.get(SSOServiceUrl + '/token/ClientLogoff?callback=?', {}, LocalLogOff, 'jsonp');
}

function LocalLogOff() {
    // log off in the client domain
    $.ajax({
        type: "POST",
        url: SSOClientServiceUrl + "/ClientLogoff",
        data: '{ }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: ClientLogoffSuccess,
        error: ClientLogoffError
    });
}

