//
// Filename : misc.js
// Author   : Wayne Howarth <wayne@pyesmeadow.fsnet.co.uk>
// Created  : 15th June 2002
//
// Miscellaneous routines.
//
// Copyright (c) 2002 Wayne Howarth, All Rights Reserved
//
// Routines in this file may be freely distributed providing the above
// copyright message is preserved.
//

function rnd( lower, upper )
{	//
    // rnd()
    // Generates a random number between lower and upper
    // inclusive.
    //

    return lower + Math.floor( Math.random() * (upper - lower + 1) )
}

function extractFilename( strUrl )
{   //
    // extractFilename()
    // Extracts the filename from the given url.
    //
    // Assumes 'default.htm' if a path is specified.
    //

    var i;

    if( strUrl.charAt(strUrl.length - 1) == '/' )
    	return "default.htm"

    if( (i = strUrl.lastIndexOf( '/' )) == -1 )
        return strUrl;

    return strUrl.substr( i + 1 );
}

function replaceAll( src, replaceStr, withStr )
{
    var strLength = src.length;
    var txtLength = replaceStr.length;

    if( strLength == 0 || txtLength == 0 )
        return src;

    var i = src.indexOf( replaceStr );
    if( (!i) && (replaceStr != src.substring(0,txtLength)) )
        return src;

    if( i == -1 )
        return src;

    var newstr = src.substring(0, i) + withStr;

    if( i + txtLength < strLength )
        newstr += replaceAll( src.substring(i+txtLength,strLength), replaceStr, withStr );

    return newstr;
}

function toHtml( str )
{
    var temp = replaceAll( str, ' ', '&nbsp;' );
    return temp;
}

function monthStr( m )
{   //
    // monthStr()
    //

    var arrMonth = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
    return arrMonth[m-1];
}

function longDateStr( dte )
{   //
    // longDateStr()
    // Returns a string containing the specified date in long date
    // format.
    //

    var year = dte.getYear();
    if( year < 1900 )
        year += 1900;

    return dte.getDate() + ' ' + monthStr(dte.getMonth() + 1) + ' ' + year;
}