API Server

The APIServer function generates a hostname for the API server by adding 'api-' to the current domain name after removing the prefix 'www.', if present. If SSL is being used, the returned port is 8443; other 8080.

var
APIServer = Resolve;
APIServer.IsSubdomain = true;

function Resolve( host )
{
    if ( host === undefined ) host = "api";

    var base_domain = "." + location.hostname;

    if ( Resolve.UseExtractBaseDomain )
    {
        base_domain = Resolve.ExtractBaseDomain( location.hostname );
    }

	var dom         = "";
    var http_port   = Resolve.httpPort  ? Resolve.httpPort  : "80";
    var https_port  = Resolve.httpsPort ? Resolve.httpsPort : "443";

	switch ( location.protocol )
	{
	case "http:":
		dom = location.protocol + "//" + host + base_domain + ":" + http_port;
		break;

	case "https:":
        dom = location.protocol + "//" + host + base_domain + ":" + https_port;
		break;
	}

	return dom;
}

Resolve.ExtractBaseDomain
=
function( domain )
{
	var base_domain = "";
	var bits = domain.split( "-" );
	
	if ( 1 == bits.length )
	{
		base_domain = bits[0];
	}
	else
	{
		base_domain = bits[1];
	}

    //  If base_domain == 'example.com'
    if ( Resolve.IsRegisteredDomain( base_domain ) )
    {
        //  Then return '.example.com'
        base_domain = "." + base_domain;
    }
    else
    if ( APIServer.IsSubdomain )
    {
        //  Then return '.myapp.example.com'
        base_domain = "." + base_domain;
    }
    else    // If base_domain == 'myapp.example.com'
    {
        //  Then return '-myapp.example.com'
        base_domain = "-" + base_domain;
    }

	return base_domain;
}

Resolve.IsLocalAPIServer
=
function()
{
    return (-1 !== location.hostname.indexOf( ".local" )) || StringEndsWith( location.hostname, ".test" );
}

Resolve.IsRegisteredDomain
=
function( domain )
{
    //  First remove '.local' if it is on domain.
    domain = domain.replace( ".local", "" );

    //  Could be:
    //
    //  1)           example            returns false
    //  2)           example.com        returns true
    //  3)           example.com.au     returns true
    //  4)  mydomain.example.com        returns false
    //  5)  mydomain.example.com.au     returns false

    var bits = domain.split( "." );

    switch ( bits.length )
    {
    case 1:
        //  Is an invalid domain so return false.
        return false;

    case 2:
        //  Is a registered domain if last bit is a TLD.
        return Resolve.IsTopLevelDomain( bits[1] );

    case 3:
        //  Is a registered domain if last bit is a TLD.
        return Resolve.IsSecondLevelDomainOf( bits[1], bits[2] );

    default:
        //  Any with more will not be registered domain.
        return false;
    }
}

Resolve.IsTopLevelDomain
=
function( tld )
{
    switch( tld )
    {
    case "com":
    case "net":
    case "online":
    case "org":
    case "xyz":
        return true;

    default:
        return false;
    }
}

Resolve.IsSecondLevelDomainOf
=
function( sld, tld )
{
    switch( tld )
    {
    case "au":
        switch ( sld )
        {
        case "com":
        case "net":
        case "org":
        case "info":
            return true;

        default:
            return false;
        }
        break;

    default:
        return false;
    }
}