// 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://reg.mgnetwork.com/CheckLogin.aspx";
var sRegistrationPage = "http://reg.mgnetwork.com/RegistrationPage.aspx";
var sLoginPage = "http://reg.mgnetwork.com/LoginPage.aspx";
var sLogoutPage = "http://reg.mgnetwork.com/Logoutpage.aspx";
var sEditProfile = "http://reg.mgnetwork.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 = "MGSC";

// 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://reg.mgnetwork.com/tacodalogin.js></SCR"+"IPT>");
        }
}


if (document.domain.indexOf('mgnetwork.com') != -1) {
      tlName = '.mgnetwork.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.mgnetwork.com/NASApp/cs/ContentServer?pagename=mgnetwork/Render&c=Page&cid=968332188492");
var sQueryStringForLogout = "?" + sSiteIDParamName + "=" + sSiteID;

// Display Press Release
function showPress ()
{
	if ((typeof sPressRelease == "undefined") || (sPressRelease == "")) return "";
	return " | <a href='"+sPressRelease+"'>Press Release</a>";
}

// Display Promotion
function showPromo ()
{
	if ((typeof sPromoBox == "undefined") || (sPromoBox == "")) return "";
	return sPromoBox;	
}

// Login/Register links HTML
function ShowLoginLogout ()
{
	    document.write ("<A HREF="+ sRegistrationPage + sQueryString + " target=_parent>Register</A> | ");
            document.write ("<a onClick='Logout()' href='"+ sLoginPage + sQueryString +"'>"+ sLogin +"</a><br>");
              document.write ('<A HREF='+ sEditProfile + sQueryString + '>' + sEditLabel +'</A> | ');
			document.write ('<a onClick="Logout()" href="'+ sLogoutPage + sQueryStringForLogout +'">'+ sLogout +'</a>');
           // document.write ('<A HREF='+ sEditProfile + sQueryString + '>' + sEditLabel +'</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='"+ sLoginPage + rString +"'>"+ sLogin +"</a>";
return LoginLinksHtml;
}
*/

// Logout/Edit Profile links HTML
//function displayLogoutLinks ()
//function ShowLoginLogout ()
//{
//            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;
//}
*/
//Big Grey Box HTML
function BigGreyBox ()
{
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="'+ sLoginPage + sQueryString +'">'+ sLogin +'</a> | <a onClick="Logout()" href="'+ sLogoutPage + sQueryStringForLogout +'">'+ sLogout +'</a> | <a href="http://reg.mgnetwork.com/sitehtml/'+sSiteAbbrev+'/helpcenter.html">Help Center</a>'+showPress()+'</font><br><b>Please register to read stories on ' + sSiteUrl + ' -- it\'s FREE!</b></font></td>'+showPromo ()+'</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.lastIndexOf(sName)) ) )
    {
        if (-1 == ( ichEnd = sCookie.indexOf(";",ichSt+sName.length) ) )
            ichEnd = sCookie.length;
        return unescape(sCookie.substring(ichSt+sName.length,ichEnd));
    }
    return null;
}

function setCookie (sName, vValue)
{
    document.cookie = sName + "=" + escape(vValue,0) + ";";
}




//{
//	if ( testCookie() != "0" )
//	{
//       	if (request["tacodalogin"])
//		{
//	            setCookie("tacodalogin", request["tacodalogin"]);
//		} 
//	      if (!getCookie("tacodalogin"))
//		{
//	           window.location.href = sCheckLoginPage + sQueryString;
//		}
//	      else
//      	{
//            	if (getCookie("tacodalogin")=="yes")
//	            {
//		            displayLogoutLinks();
//			}
//	            else if (getCookie("tacodalogin")=="no")
//      	      {
//		            displayLoginLinks();
//			}
//      	}
//	}
//	else
//	{
//		displayLoginLinks();
//	}


//}

function Logout()
{
        setCookie("tacodalogin", "no" );
	setCookie(sDataPointListParamName, "");
	setCookie(sRegCookie, "")
}



