/*
 * @(#) xtd.js  1.0 02/10/04
 *
 * Copyright 2002 Orgdot AS. All Rights Reserved.
 * http://www.orgdot.com/javaopensource
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/**
 * Javascript utilities that should work on both old and new browsers
 *
 * @author      Olaf Havnes
 * @version     1.0, 10/04/02
 * @since       SWFIT1.0
 */

/**
 * Preload an image
 */
function preloadImage(url)
{
    var im = new Image();
    im.src = url;
    return im;
}

/**
 * Preload an array of images
 */
function preloadImages (urls)
{
    var ims = new Array();
    if (document.images != null) for (var i = 0; i < urls.length; i++)
    {
        ims [i] = new Array();
        for (var j = 0; j < urls[i].length; j++) ims[i][j] = preloadImage (urls[i][j]);
    }
    return ims;
}

/**
 * Swap images using the preloaded array
 */
function flipIm(i, j)
{
    if
    (
        document.images &&
        ims.length >= i &&
        ims[i].length >= j &&
        eval ("document.images.im_" + i) != null
    )
    eval ("document.images.im_" + i + ".src=ims[" + i + "][" + j + "].src");
    return true;
}


/**
 * Pop a variety of windows
 */
function winPop ( url, width, height, xpos, ypos, toolbar, scrollbar, resizable, name )
{
    var wspec = "width="  + width  + ",";
    var hspec = "height=" + height + ",";
    var xspec = "screenX=" + xpos + ",left=" + xpos + ",";
    var yspec = "screenY=" + ypos + ",top="  + ypos + ",";

    var toolspec   = (toolbar)   ? "toolbar=yes,"    : "toolbar=no,";
    var scrollspec = (scrollbar) ? "scrollbars=yes," : "scrollbars=no,";
    var resizspec  = (resizable) ? "resizable=yes,"  : "resizable=no,";

    var specs  = wspec + hspec + xspec + yspec;
    specs += toolspec + scrollspec + resizspec;
    specs += "status=no,titlebar=no,directories=no";

    var popper = window.open(url,name,specs);
    if (window.focus) popper.focus();
}


function winPopThin ( url, width, height )
{
    winPop ( url, width, height, 100, 100, false, false, false, "miniwin" );
}

function winPopFat ( url, width, height )
{
    winPop ( url, width, height, 100, 100, true, true, true, "workwin" );
}
/**
 * Flash 4 code
 */
function flash4Code (mov_src, mov_wdt, mov_hgt, mov_col)
{
    var code = '<object \n';
    code += '  classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" \n';
    code += '  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"\n';
    code += '  width="' + mov_wdt + '" height="' + mov_hgt + '">\n';
    code += '  <param name="movie" value="' + mov_src + '">\n';
    code += '  <param name="quality" value="best">\n';
    code += '  <param name="menu" value="false">\n';
    code += '  <param name="align" value="lt">\n';
    code += '  <param name="scale" value="exactfit" \n';
    code += '  <param name="bgcolor" value="' + mov_col + '">\n';
    code += '<embed \n';
    code += '  src="' + mov_src + '" \n';
    code += '  quality="best" scale="exactfit" salign="lt" menu="false" \n';
    code += '  bgcolor=' + mov_col + ' \n';
    code += '  width="' + mov_wdt + '" \n';
    code += '  height="' + mov_hgt + '" \n';
    code += '  type="application/x-shockwave-flash" \n';
    code += '  pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash">\n';
    code += '</embed></object>';
    return code;
}


/**
 * URL-encode
 */
function urlEncVar (str)
{
    return str + "=" + escape (eval (str)) + "&";
}


/**
 * Parse simple positive numbers - ignore non-number chars.
 */
var num_chars = '0123456789';
function parseSimpleInt (str)
{
    var parsed = 0;
    for (var i = 0; i < str.length; i++)
    for (var j = 0; j < 10; j++) if (str.charAt (i) == num_chars.charAt(j))
    {
        parsed = 10 * parsed + j;
        break;
    }
    return parsed;
}

/**
 * Get a single-character String from a given character code from 0 to 255.
 */
var hex_chars = '0123456789ABCDEF';
function strFromCharCode (n)
{
    return unescape ('%' + hex_chars.charAt (n >> 4) + '' + hex_chars.charAt (n % 16));
}

/**
 * Get a character code from a single-character String.
 */
function allChars ()
{
    var ac = new Array (256);
    for (var i = 0; i < 256; i++) ac[i] = strFromCharCode(i);
    return ac;
}
var all_chars = allChars();

function charCodeFromStr (str)
{
    for (var i = 0; i < all_chars.length; i++)
    if (all_chars[i] == str) return i;
    return 0;
}

/**
 * XOR scramble a given String. NB! This has nothing WHATSOEVER to do with encryption,
 * the point of this function is merely to hide mail addresses on HTML pages from
 * bots that harvest mail addresses for spammers.
 */
function scrambleString (clean)
{
    var scrambled = '';
    for (var i = 0; i < clean.length; i++)
        scrambled += ( 255 ^ charCodeFromStr (clean.charAt (i)) );

    return scrambled;
}

/**
 * XOR scramble a given String. NB! This has nothing WHATSOEVER to do with encryption,
 * the point of this function is merely to hide mail addresses on HTML pages from
 * bots that harvest mail addresses for spammers.
 */
function scrambleMail ()
{
    var mail = prompt ('please input the mail address:', '');
    prompt
    (
        'please copy this code and paste it into the file basic.js:',
        'scramble_mail=\'' + scrambleString (mail) + '\';'
    );
}


/**
 * XOR unscramble a given String.
 */
function unScrambleString (scrambled)
{
    var clean = '';
    for (var i = 0; i < scrambled.length; i+=3)
        clean += strFromCharCode (255 ^ parseSimpleInt(scrambled.substring(i,i+3)));

    return clean;
}