// File: /include/js/Shared/Http/URL.js
// Desc: VM_URL
// $Revision: 33$
// $Date: 2007-09-05 10:41:35 AM$
// $Author: Donnie Tognazzini$
// $NoKeywords$


var VM_URL =
{
    WWW:  null,
    WS1:  null,
    WS2:  null,
    WX1:  null,
    WX2:  null,
    WSS1: null,
    WSS2: null,
    m_primaryDomainName: "callwave.com",
    m_backupDomainName: "gotpb.com",
    m_loadBalancedThirdLevelName: "wxlb",
    m_wssProtocol: "https:",
    m_protocol: "http:",
    m_wwwThirdLevelName: "www",
    m_wsThirdLevelName: "wxlb",
    m_wxThirdLevelName: null,
    m_webRoot: "",
    m_targetService: "v2w",
    m_wsPath: null,
    m_wxPath: null,

    m_accountNumber: null,
    m_areServersRestricted: false,
    m_isLoadBalancingEnabled: false,

    initialize: function()
    {
        if ( typeof PREF_GetPreferences != 'undefined' )
        {
            this.set_accountNumber(PREF_GetPreferences( ).AuthInfo.get( ).accountNumber);
        }
        else if ( typeof VM_GetPreference != 'undefined' && VM_GetPreference( PREF_KEY_ACCOUNT ) )
        {
            this.set_accountNumber( parseInt( VM_GetPreference( PREF_KEY_ACCOUNT ) ) );
        }
        else
        {
            this.set_accountNumber( 0 );
        }

        if ( typeof( location ) != "undefined" && location[ "protocol" ] && location.protocol.indexOf( "http" ) === 0 )
        {
            this.m_protocol = location.protocol;
        }
    },

    GetInternalWebPageURLBase : function( )
    {
        return this.m_protocol + "//" + this.m_wsThirdLevelName + "." + this.m_primaryDomainName + this.m_webRoot;
    },

    set_targetService: function( svc )
    {
        this.m_targetService = svc;
        this._updatePaths( );
    },

    set_WWW_SERVER: function( WWW_SERVER )
    {
        var re = /^([^\/]+)\/\/([^\.]+)\.[^\/]+(\/.*)?$/,
            matches = re.exec( WWW_SERVER );

        if ( !matches )
        {
            throw "URL::set_WWW_SERVER -- Invalid WWW_SERVER value: " + WWW_SERVER;
        }

        this.m_protocol = matches[ 1 ];
        this.m_wwwThirdLevelName = matches[ 2 ];
        this.m_webRoot = matches[ 3 ] || "";

        this._updateUrls( );
    },

    set_WS_SERVER: function( WS_SERVER )
    {
        var re = /^[^\/]+\/\/([^\.]+)\.[^\/]+(\/.*)?$/,
            matches = re.exec( WS_SERVER );

        if ( !matches )
        {
            throw "URL::set_WS_SERVER -- Invalid WS_SERVER value: " + WS_SERVER;
        }

        this.m_wsThirdLevelName = matches[ 1 ];

        this._updateUrls( );
    },

    set_accountNumber: function( accountNumber )
    {
        if ( !this.m_accountNumber || this.m_accountNumber != accountNumber )
        {
            this.m_accountNumber = accountNumber;
            if ( !this.m_isLoadBalancingEnabled && !this.m_areServersRestricted  )
            {
                this.m_wxThirdLevelName = "wx" + (accountNumber % 32).toString();
            }
            this._updatePaths( );
        }
    },

    set_areServersRestricted: function( bool )
    {
        this.m_areServersRestricted = bool;
        if ( bool )
        {
            this.m_wssProtocol      = this.m_protocol;
            this.m_wxThirdLevelName = this.m_wsThirdLevelName;
            this.m_backupDomainName = this.m_primaryDomainName;

        }
        this._updatePaths( );
    },

    set_isLoadBalancingEnabled: function( bool )
    {
        this.m_isLoadBalancingEnabled = bool;
        if ( bool )
        {
            this.m_wsThirdLevelName = this.m_loadBalancedThirdLevelName;
            this.m_wxThirdLevelName = this.m_loadBalancedThirdLevelName;
        }
        this._updatePaths( );
    },

    _updatePaths: function( )
    {
        this.m_wsPath = "/widgets/" + this.m_targetService + "/service.aspx";

        if ( this.m_isLoadBalancingEnabled )
        {
            this.m_wxPath = "/wx/v2w";
        }
        else if (this.m_areServersRestricted)
        {
            this.m_wxPath = "/widgets/" + this.m_targetService + "/proxy.aspx?url=" +
                            encodeURIComponent( "http://wx" + ( this.m_accountNumber % 32 ).toString() + "." + this.m_primaryDomainName +
                                                "/v2w" );
        }
        else
        {
            this.m_wxPath = "/v2w";
        }

        this._updateUrls();
    },

    _updateUrls: function( )
    {
        this.WWW = this.m_protocol + "//" + this.m_wwwThirdLevelName + "." + this.m_primaryDomainName + this.m_webRoot;
        this.WS1 = this.m_protocol + "//" + this.m_wsThirdLevelName + "." + this.m_primaryDomainName + this.m_webRoot + this.m_wsPath;
        this.WS2 = this.m_protocol + "//" + this.m_wsThirdLevelName + "." + this.m_backupDomainName + this.m_webRoot + this.m_wsPath;
        this.WSS1 = this.m_wssProtocol + "//" + this.m_wsThirdLevelName + "." + this.m_primaryDomainName + this.m_webRoot + this.m_wsPath;
        this.WSS2 = this.m_wssProtocol + "//" + this.m_wsThirdLevelName + "." + this.m_backupDomainName + this.m_webRoot + this.m_wsPath;
        this.WX1 = this.m_protocol + "//" + this.m_wxThirdLevelName + "." + this.m_primaryDomainName + this.m_webRoot + this.m_wxPath;
        this.WX2 = this.m_protocol + "//" + this.m_wxThirdLevelName + "." + this.m_backupDomainName + this.m_webRoot + this.m_wxPath;
    }
};
VM_URL.initialize();