var UserCookie = new Array(30);
var CookieUsername = 1, CookieUser_Id = 2, CookieGroup_Id = 3, CookiePerson_Id = 4, CookieName = 5, CookieEmail = 6, CookiePrefs = 7, CookiePrivs = 8, CookieValid = false;
		
function getCookie( name ) {
  var arg = name + '=';
  if ( document.cookie && document.cookie.length != 0 )
  {
  	var cstart = document.cookie.indexOf( arg );
    if ( cstart == -1 )
      return null;
    else
    {
      var tmpStr = document.cookie.substring( cstart, ( document.cookie.length + 1 ) );
    	var cend = tmpStr.indexOf( ';' );
      if ( cend == -1 ) cend = tmpStr.length + 1;
	  	tmpStr = unescape( tmpStr.substring( arg.length, cend ) );
      return tmpStr;
    }
  }
  else
    return null;
}

function ReadCookie() {
	CookieValid = false;
  var j=1, i=0;
  var str = getCookie( 'Marketbright_Cookie' );
	if ( str == null || str == "" ) return false;
  
	if ( str.indexOf( "/" ) != -1 )
  {
		while ( str.substring( i, str.length ).indexOf( "/" ) != -1 && j < 50 )
    {
      cStr = "";
      for ( var i; str.substring( i, i + 1 ) != "/"; i++ ) cStr += str.charAt(i);
      i++;
      UserCookie[j] = cStr;
      j++
		}
	}
 	UserCookie[j] = str.substring( i, str.length );
 	UserCookie[0] = j-1;
	CookieValid = true;

  return true;
}

function ReadRootLevelCookie()
{
  CookieValid = false;
  var j=1, i=0;
  var str = getCookie( 'SerenaCookie' );
  if ( str ) {
    var values = str.split("&");
    UserCookie[0] = 3;
    var temp_val = Array();
    for(var z=0; z < values.length; z++)
    {
    	temp_val[z] = Array();
     	temp_val[z] = values[z].split("=");
    }
    UserCookie[CookieEmail] = temp_val[0][1];
    UserCookie[CookieUsername] = temp_val[0][1];
    UserCookie[CookieName] = temp_val[1][1];
    UserCookie[CookieUser_Id] = '-1';
    CookieValid = true;
  }
  return true;
}


function getWelcome( Text, altText )
{
  if ( !CookieValid ) ReadRootLevelCookie();
  if ( !Text ) Text = 'Welcome [Name]';
  if ( !altText ) altText = 'My Profile';
  var Str = '';
  var thisCookieName = UserCookie[CookieName];
  if ( thisCookieName && thisCookieName.indexOf( ' ' ) > -1 ) 
    thisCookieName = thisCookieName.substring( 0, thisCookieName.indexOf( ' ' ) );
  
  if ( CookieValid && UserCookie[CookieUser_Id] != '1' && thisCookieName != 'Guest' )    
    Str = Text.replace( '[Name]', thisCookieName );
  else
    Str = altText;
  return Str;
}

function signOut(anchor) {
	//Cookie.dispose('SerenaCookie');
	var d = new Date();
  document.cookie = "v0=1;expires=" + d.toGMTString() + ";" + ";";
	//return false;
}

function getSignOut( Text, Action )
{
  if ( !Text ) Text = 'If you are not [Name] <a target="_top" href="[Logout]" onclick="signOut(this)">Sign Out</a>.';
  if ( !Action ) Action = 'Refresh';
  
  if ( document.location.href.indexOf( 'www.serena.com' ) > -1 || document.location.href.indexOf( 'www-stage.serena.com' ) > -1 ) {
     Text = Text.replace( '[Logout]', '/api/Update_Users.Logout?Action=' + escape(Action) );
  } else {
     Text = Text.replace( '[Logout]', 'http://www.serena.com/api/Update_Users.Logout?Action=' + escape(Action) );
  }
	
  Text = Text.replace( '[Name]', UserCookie[CookieName] );
  Text = Text.replace( '[Email]', UserCookie[CookieEmail] );
  return Text;
}


function getParameter( Parameter, URL )
{
	var Value = '';
	if ( !URL ) URL = document.location.search.substring(1);
	
	var Parameters = URL.split( '&' );

	for ( var i = 0; i < Parameters.length; i++ )
	{
		if ( Parameters[i].indexOf( Parameter + '=' ) > -1 )
		{
			Value = Parameters[i].substring( Parameters[i].indexOf( '=' ) + 1 );
			if ( Value == 'undefined' ) Value = '';
		}
	}
	if ( Value.substring( Value.length-1 ) == '#' ) Value.substring( 0, Value.length-2 );
	return Value;
}

ReadRootLevelCookie();
