// LoginLogout.js uses in the sites protected by ARS in order to display Login/Logout link and support its work
// Global constants:
// In 4 lines below replace <ars_server> with the name of the server where ARS is installed
var sCheckLoginPage = "http://register.thestar.com/CheckLogin.aspx";
var sRegistrationPage = "http://register.thestar.com/RegistrationPage.aspx";
var sLogoutPage = "http://register.thestar.com/Logoutpage.aspx";
var sLoginPage = "http://register.thestar.com/Loginpage.aspx";
var sEditProfile = "http://register.thestar.com/VisitorProfileEditPage.aspx";

// the lable for the Login link 
var sLogin = "Login";

// the label for the Logout link 
var sLogout = "Logout";

// the label for the edit profile link
var sEditLabel = "Edit Profile";

// Site ID Param Name
var sSiteIDParamName = "SID";

// Content URL Param Name
var sContentURLParamName = "CU";

// Session Cookie Name 
var sDataPointListParamName = "SC";

// Registration Cookie Name 
var sRegCookie 	  = "TCSAC";
var sRegCookieOld = "RC226";


if ( location.search.indexOf('tacodalogin') == -1 ) {
        if ( document.cookie.indexOf('tacodalogin') == -1 ) {
        document.write ("<SCR"+"IPT SRC=http://register.thestar.com/tacodalogin.js></SCR"+"IPT>");
        }
}


if (document.domain.indexOf('http://www.lisashore.com/Media/Star/TorStar060817_files/thestar.com') != -1) {
      tlName = 'http://www.lisashore.com/Media/Star/TorStar060817_files/.thestar.com';
} else {
      tlName = document.domain; 
}

var request = createRequestObject();
var sQueryString = "?" + sSiteIDParamName + "=" + sSiteID + "&" + sContentURLParamName + "=" + escape(window.location.href);
var hQueryString = "?" + sSiteIDParamName + "=" + sSiteID + "&" + sContentURLParamName + "=" + escape("http://www.thestar.com/NASApp/cs/ContentServer?pagename=thestar/Render&c=Page&cid=968332188492");
var sQueryStringForLogout = "?" + sSiteIDParamName + "=" + sSiteID;

// Login/Register links HTML
function displayLoginLinks ()
{
	    document.write ("<A HREF="+ sRegistrationPage + sQueryString + " target=_parent>Sign up</A> | ");
            document.write ("<a onClick='Logout()' href='"+ sLoginPage + sQueryString +"'>"+ sLogin +"</a>");
}

function displayLoginLinksHtml ()
{
            rString = sQueryString;
            if (document.location.href.indexOf("1090491308722") != -1) {
                rString = hQueryString;
            }
            var LoginLinksHtml = "<A HREF="+ sRegistrationPage + rString + " target=_parent>Sign up</A> | ";
            LoginLinksHtml = LoginLinksHtml + "<a onClick='Logout()' href='"+ sLoginPage + rString +"'>"+ sLogin +"</a>";
return LoginLinksHtml;
}


// Logout/Edit Profile links HTML
function displayLogoutLinks ()
{
            document.write ('<A HREF='+ sEditProfile + sQueryString + '>' + sEditLabel +'</A> | ');
            document.write ('<a onClick="Logout()" href="'+ sLogoutPage + sQueryStringForLogout +'">'+ sLogout +'</a>');
}

function displayLogoutLinksHtml ()
{
            var LogoutLinksHtml =  '<A HREF='+ sEditProfile + sQueryString + '>' + sEditLabel +'</A> | ';
            LogoutLinksHtml = LogoutLinksHtml + '<a onClick="Logout()" href="'+ sLogoutPage + sQueryStringForLogout +'">'+ sLogout +'</a>';
return LogoutLinksHtml;
}


function createRequestObject()
{
    var request   = new Object();
    var nameVal   = "";
    var inString  = location.search;
    var separator = ",";

    if (inString.charAt(0) == "?")
    {
        inString = inString.substring(1, inString.length);
        keypairs = inString.split("&");

        for (var i=0; i < keypairs.length; i++)
        {
            nameVal = keypairs[i].split("=");

            if (request[nameVal[0]])
            {
                  request[nameVal[0]] += separator + nameVal[1];
            }
            else
            {
                  request[nameVal[0]] = nameVal[1];
            }
        }
    }
    return request;
}


function getCookie (sCookieName)
{
    var sName=sCookieName+"=", ichSt, ichEnd;
    var sCookie=document.cookie;

    if ( sCookie.length && ( -1 != (ichSt = sCookie.lastIndexOf(sName)) ) )
    {
        if (-1 == ( ichEnd = sCookie.indexOf(";",ichSt+sName.length) ) )
            ichEnd = sCookie.length;
        return unescape(sCookie.substring(ichSt+sName.length,ichEnd));
    }
    return null;
}


function setCookieTC (sName, vValue, dName, eTime)
{
    if (dName == null) {
        //dName = document.domain;
    	}
    if ( eTime != null ) {
	var Expires = "; expires="+ new Date ((new Date()).getTime() - 60480000);
	}
    else
	{ var Expires = "";
	}
    //document.cookie = sName + "=" + escape(vValue,0) + Expires + ";domain=" + dName + ";path=/";
    document.cookie = sName + "=" + escape(vValue,0) + Expires + ";path=/";
}




function ShowLoginLogoutHtml() {
var HtmlLinks;
if ( testCookie() != "0" ) {
	if (request["tacodalogin"]) {
		setCookieTC("tacodalogin", request["tacodalogin"]);
		//setCookieTC("tacodalogin", request["tacodalogin"] );
        	}
	if (getCookie("tacodalogin")=="yes") {
		HtmlLinks = displayLogoutLinksHtml();
		} 
	else if (getCookie("tacodalogin")=="no") {
		setCookieTC(sDataPointListParamName, "" );
		HtmlLinks = displayLoginLinksHtml(); 
		}
	else 	{
		HtmlLinks = displayLoginLinksHtml();
		}
	}
else {
	HtmlLinks = displayLoginLinksHtml();
	}
return HtmlLinks;
}



function ShowLoginLogout()
{
var LoginLogoutHtml = ShowLoginLogoutHtml();
document.write(LoginLogoutHtml);
}


function Logout()
{
        setCookieTC("tacodalogin", "no" );
	setCookieTC(sDataPointListParamName, "");
}

function testCookie(popUp)
{
if (!getCookie("tacodalogin")){
        document.cookie=("CookieTest=Yes;");
        if ( !getCookie("CookieTest")) {
                if ( popUp == "1" ) {
                        alert  ("It appears as though cookies are disabled in your browser.\n\n" +
                                "Cookies will need to be turned on to access articles on thestar.com.\n" +
                                "For more information on cookies please visit the Help Centre page\n" +
                                "located under Membership Centre.\n\n" +
                                "thestar.com team.");
                        }
                return 0;
                }
}
else
{
document.cookie=("CookieTest=;");
return 1;
}
}



