// General javascript functions for the tutorials:

// Online and offline base url of this web site:
var onlineRegExp = /http:\/\/[\w\.]+\/tutorials/;
var onlineBase = "http://www.customtyping.com/tutorials";  // Default value.
var result = document.URL.match(onlineRegExp);
if (result != null)
{
    onlineBase = result[0];
}
var offlineBase = "file://S:\\Work\\Custom_Solutions_Web\\CustomTyping_Tutorials";

// We need to make sure the current page is in the customtyping domain.  If
// not, redirect to the equivalent page on the CT domain.
if (document.URL.indexOf("customsolutions.us")!=-1 && 
    document.URL.indexOf('http')==0)
{
    var newUrl = document.URL.replace(/customsolutions\.us/,"customtyping.com");
    if (newUrl.indexOf('.htm') == -1)
    {
        // The URL must point to a directory.  Redirect to the index.htm 
        // file in that directory.  This is needed because the web server
        // will give us a customsolutions.us URL for a directory,
        // even though we are specifying a customtyping one.
        if (newUrl.substr(newUrl.length-1,1) != "/")
        {
            newUrl = newUrl + "/";
        }
        newUrl = newUrl + "index.htm";
    }
    window.location.replace(newUrl);
}

// Are we logged in?  Returns true if we are logged in, false otherwise:
function logged_in()
{
    // Uncomment this to simulate being logged in, when testing locally:
    // return true;

    start = document.cookie.indexOf("username=");
    if (start==-1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

// A utility function to generate a file URL, based on the browser in use:
function convert_file_url(url)
{
    if (url.indexOf("file")!=0)
    {   
        return(url);
    }

    if (navigator.appName.indexOf("Internet Explorer")!=-1)
    {
        // Internet Explorer:
        var newUrl = url.replace(/file:\/\/\//,"file://");
        newUrl = newUrl.replace(/\//g,"\\");
        newUrl = newUrl.replace(/file:\\\\/,"file://");
        return(newUrl);
    }
    else
    {
        // Netscape, mozilla, etc.
        var newUrl = url.replace(/\\/g,"/");
        if (newUrl.indexOf("file:///")!=0 && newUrl.indexOf("file://")==0)
        {
            newUrl = newUrl.replace(/file:\/\//,"file:///");
        }
        return(newUrl);
    }
}

// Display the top bar:
function show_top_bar()
{
    offlineBase = convert_file_url(offlineBase);
    if (document.URL.indexOf("file")==0)
    {
        // We're viewing locally, use the local base:
        var baseUrl = offlineBase;
    }
    else
    {
        var baseUrl = onlineBase;
    }

    if (logged_in())
    {
        var flashUrl = convert_file_url(baseUrl+"/top_menu_tutorials.swf");
    }
    else
    {
        var flashUrl = convert_file_url(baseUrl+"/top_menu-free_tutorials.swf");
    }

    // IE does not like %20 (code for a space) in the flash url:
    flashUrl = flashUrl.replace(/\%20/g," ");

    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
    document.write('codebase="http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0"');
    document.write('id="top_menu_tutorials" width="736" height="94">');
    document.write('<param name="movie" value="'+flashUrl+'">');
    document.write('<param name="quality" value="high">');
    document.write('<param name="bgcolor" value="#003399">');
    document.write('<embed name="top_menu_tutorials" src="'+flashUrl+'" quality="high" bgcolor="#003399"');
    document.write('width="736" height="94"');
    document.write('type="application/x-shockwave-flash"');
    document.write('pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">');
    document.write('</embed>');
    document.write('</object>');
}

// Hide the link bar if needed.
function show_hide_link_bar()
{
    if (!logged_in())
    {
        return;
    }

    document.getElementById("link_bar").innerHTML = "";
    document.getElementById("link_bar").style.visibility="hidden";
}

// This function inserts our email address, in a way that prevents automated
// scripts from seeing it.
function insert_email()
{
	var string1 = "typing";
	var string2 = "@";
	var string3 = "customtyping.com";
	var string4 = string1 + string2 + string3;
	document.write("<a href=" + "mai" + "lto:" + string1 + string2 + string3 + ">" + string4 + "</a>");
}
