// LoginLogout.js uses in the sites protected by ARS in order to display Login/Logout link and support its work
// Global constants:
// July 22 -c  
// 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/Registration.aspx";
var sLogoutPage = "http://register.thestar.com/Logoutpage.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 = "RC2";


if (document.domain.indexOf('thestar.com') != -1) {
      tlName = '.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');

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

function displayLoginLinksHtml ()
{
            rString = sQueryString;
            if (document.location.href.indexOf("1090491308722") != -1) {
                rString = hQueryString;
            }
            var LoginLinksHtml = "<A HREF="+ sRegistrationPage + rString + " target=_parent>Register</A> | ";
            LoginLinksHtml = LoginLinksHtml + "<a onClick='Logout()' href='"+ sRegistrationPage + 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 + sQueryString +'">'+ sLogout +'</a>');
}

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


//Big Grey Box HTML
function displayBGB ()
{
	     	document.write ('<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td bgcolor="#CCCCCC" height="36"><font size="3" face="Geneva, Arial, Helvetica, san-serif"><b><font color="#FFFFFF">' + sSiteName + ' for members </font></b></font><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><font color="#0000FF"><A HREF='+ sRegistrationPage + sQueryString + '>Register</A> | <a onClick="Logout()" href="'+ sRegistrationPage + sQueryString +'">'+ sLogin +'</a> | <a href="http://www.thestar.com/static/registration/index.html">Help Center</a></font><br><b>Please register to read stories on ' + sSiteUrl + ' -- it\'s FREE!</b></font></td></tr></table>')
}

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.indexOf(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)
{
    if (dName == null) {
        dName = document.domain;
    } 
    document.cookie = sName + "=" + escape(vValue,0) + ";domain=" + dName + ";path=/";
}

function ShowLoginLogoutHtml() {
var HtmlLinks;
if ( testCookie() != "0" )
{
       if (document.domain.indexOf('thestar.com') != -1) {
           if (getCookie(sRegCookie)) {
               HtmlLinks = displayLogoutLinksHtml();
           }
           else {
               setCookieTC(sDataPointListParamName, "");
               HtmlLinks = displayLoginLinksHtml();
           } 
       } else {
           if (request["tacodalogin"])
                setCookieTC("tacodalogin", request["tacodalogin"]);
//           if (!getCookie("tacodalogin"))
//               window.location.href = sCheckLoginPage + sQueryString;
           if (!getCookie("tacodalogin")) {
			   if (window.location.href.indexOf('file:') != -1) {
			     	HtmlLinks = displayLoginLinksHtml();
			   }			   
			   else {
                   window.location.href = sCheckLoginPage + sQueryString;
			   }
           }
           else {
               if (getCookie("tacodalogin")=="yes") {
                   HtmlLinks = displayLogoutLinksHtml();
               } else if (getCookie("tacodalogin")=="no") {
                   setCookieTC(sDataPointListParamName, "" );
                   HtmlLinks = displayLoginLinksHtml(); 
               }
           }          
       }
}
else
{
HtmlLinks = displayLoginLinksHtml();
}
return HtmlLinks;
}

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


function BigGreyBox ()
{

if ( testCookie() != "0" )
{
       if (request["tacodalogin"])
            setCookieTC("tacodalogin", request["tacodalogin"], tlName);
       if (!getCookie("tacodalogin"))
           window.location.href = sCheckLoginPage + sQueryString;
       else
       {
        if (getCookie("tacodalogin")=="no")
            {
	    displayBGB();
            }
        }
}
else
{
displayBGB();
}
}

function Logout()
{
      setCookieTC("tacodalogin", "no", tlName);
      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;
}
}

