﻿var allCookies = document.cookie.split(';');
var cookieName = 'FedAuth';
var queryStringStart = '&';
var isAuth = false;
var isAuthUrl = (window.location.href.indexOf('au=1') > 0);

for (i = 0; i < allCookies.length; i++) {
    currentCookie = allCookies[i].split('=');
    currentCookie[0] = currentCookie[0].replace(/^\s+|\s+$/g, '');
    if (currentCookie[0] == cookieName) {
        isAuth = true;
    }
}

if (window.location.href.indexOf('?') < 0) queryStringStart = '?';
// If the user is authenticated but the url does not contain 'au=1' I redirect to the authenticated url
if (isAuth && !isAuthUrl) {
    window.location = window.location + queryStringStart + 'au=1';
}
else if (!isAuth && isAuthUrl) { // If the user is NOT authenticated but the url does contain 'au=1' I redirect to the non authenticated url
    window.location = window.location.href.replace('?au=1&', '?').replace('&au=1', '').replace('?au=1', '');
}
