function SessionManager()
{
   var myThis = this;
   
   this.productName = '3shop';
   this.LoginFormId = 'LoginForm';
   this.InputLoginId = 'login_test';
   
   this.SessionCookieValue = false;

   this.init = function()
   {
      this.CookieManager = new Cookie();

      this.initSessionCookieValue();
      
      this.initHostLocation();
      
      this.PrepareOnClick();
      
      this.PrepareLoginForm();
   }

   this.initSessionCookieValue = function()
   {
      this.SessionCookieValue = this.CookieManager.getCookie( this.productName );
   }

   this.initHostLocation = function()
   {
      this.HostLocation = window.location.host;

      if( this.HostLocation.indexOf( 'www.' ) == 0 )
         this.HostLocation = this.HostLocation.substring( 3 );
   }

   this.PrepareOnClick = function()
   {
      var LinkCollection = document.getElementsByTagName( 'a' );

      for( var i = 0 ; i != LinkCollection.length ; i++ )
         if( LinkCollection[ i ].href.indexOf( 'http://' ) == 0 || LinkCollection[ i ].href.indexOf( 'https://' ) == 0 )
            if( !LinkCollection[ i ].onclick )
               if( LinkCollection[ i ].hostname.substring( LinkCollection[ i ].hostname.length - this.HostLocation.length ) != this.HostLocation )
                  LinkCollection[ i ].onclick = function(){ return myThis.SessionHref( this ); };
   }

   this.SessionHref = function( LinkObject )
   {
      var HrefHostname = LinkObject.hostname;

      if( HrefHostname.indexOf( 'www.' ) == 0 )
         HrefHostname = HrefHostname.substring( 4 );
      
      this.CookieManager.requestFromCommunicationElement = function(){ window.location = LinkObject.href; };
      
      this.CookieManager.setCookieDomain( this.productName, this.SessionCookieValue, 0, HrefHostname, '/', 0  );
            
      return false;
   }

   this.PrepareLoginForm = function()
   {
      this.LoginFormElement = document.getElementById( this.LoginFormId );

      if( !this.LoginFormElement ) return false;

      this.LoginFormElement.onsubmit = function(){ return myThis.SessionLoginForm( this ); };

      var InputSessionElement = document.createElement( 'input' );
      InputSessionElement.type = 'hidden';
      InputSessionElement.name = this.productName;
      InputSessionElement.value = this.SessionCookieValue;

      this.LoginFormElement.appendChild( InputSessionElement );
   }

   this.SessionLoginForm = function( FormObject )
   {
      var login = null;

      if( document.getElementById( this.InputLoginId ) && document.getElementById( this.InputLoginId ).value )
         login = document.getElementById( this.InputLoginId ).value;

      var VirtualRequest = new RequestSimply( '3library/getDomainNameByLogin.php', 'login=' + ( login ? login : '' ) );

      VirtualRequest.init();
      VirtualRequest.add( '@ok', function()
	      {  
	         if( VirtualRequest.XMLHttp.responseText )
	            FormObject.action = ( FormObject.action.indexOf( 'https://' ) == 0 ? 'https://' : 'http://' ) + VirtualRequest.XMLHttp.responseText + '/';
	
	         FormObject.submit();
	      }
	  );
      
      VirtualRequest.get( null );
      
      return false;
   }

   this.init();
}
