/**
 * Javascript function or library
 *
 * This library is released under the Creative Commons ShareAlike License v2.5
 * License reference: http://creativecommons.org/licenses/by-sa/2.5/
 *
 * Copyright (c) 2007, Tonica Inc.
 * All rights reserved.
 *
 * Author: Todd Warner <t a w { a t } p o b o x . c o m>
 *
 * $Id: general-lib.js 1158 2007-01-24 16:20:49Z taw $
 */


//onerror = handleError

function handleError(msg, url, line)
{
    txt  = "Please be patient. Todd is probably experimenting with Javascript.\n\n"
    txt += "There was an error on this page.\n\n"
    txt += "Error: " + msg + "\n"
    txt += "URL: " + url + "\n"
    txt += "Line: " + line + "\n\n"
    txt += "Click OK to continue.\n\n"
    alert(txt)
    return true
}

/**
 * Send mail mechanism that helps avoid adding your email address
 * to spam lists. This was originally written by Joseph Pelrine.
 *
 * Strings together name/company/domain to what one would think
 * and opens the mail client
 *
 * Usage note: if you use the email address in the anchor text, then you
 * may want to encode it with ascii ord numbers:
 * http://www.metaprog.com/samples/encoder.htm
 * It would look something like this in your XHTML code:
 * <a href="javascript:sendMailTo('taw','lkjfas', 'pobox','com')">&#116;&#97;&#119;&#64;&#112;&#111;&#98;&#111;&#120;&#46;&#99;&#111;&#109;</a>
 */
function send_mail_to(name, random, company, domain) {
    locationstring = 'mai' + 'lto:' + name + '@' + company + '.' + domain;
    window.location.replace(locationstring);
}

/* pad single-digit numbers with a zero: 1 -> 01 */
function pad_two(n) {
    if ( n < 10 ) {
        n = "0" + n;
    }
    return (n);
}

/* "morning", "afternoon", "evening" */
function get_salutation() {
    d = new Date();
    h = d.getHours();
    the_salutation = "evening";
    if (h < 18) {
            the_salutation = "afternoon";
    }
    if (h < 12) {
            the_salutation = "morning";
    }
    return the_salutation;
}

/* get date string: YY-MM-DD HH:MM:SS -0H00 */
function get_date_string() {
    d = new Date();
    month = pad_two(d.getMonth()+1);

    // timezone can be -0H00 or +0H00
    tz = d.getTimezoneOffset()/60;
    plus_minus = "-";
    if (tz < 0) {
        plus_minus = "+";
        tz = tz * (-1);
    }
    tz = plus_minus + "0" + tz + "00";

    the_date = d.getFullYear() + "-" + month + "-" + pad_two(d.getUTCDate()) + " " + pad_two(d.getHours()) + ":" + pad_two(d.getMinutes()) + ":" + pad_two(d.getSeconds()) + " " + tz;
    return (the_date);
}

/* get UTC date string: YY-MM-DD HH:MM:SS GMT (or UTC) */
function get_utc_date_string() {
    d = new Date();
    month = pad_two(d.getUTCMonth()+1);
    the_date = d.getUTCFullYear() + "-" + month + "-" + pad_two(d.getUTCDate()) + " " + pad_two(d.getUTCHours()) + ":" + pad_two(d.getUTCMinutes()) + ":" + pad_two(d.getUTCSeconds()) + " GMT";
    return (the_date);
}

/* doesn't work, not sure why. Doesn't like date_function() or the usage of
 * id in the setTimeout()
 */
function run_clock__(id, date_function) {
    if (!id) {
        id = "date_string";
    }
    if (!date_function) {
        date_function = get_date_string;
    }
    document.getElementById(id).innerHTML=date_function();
    t = setTimeout('run_clock(id, date_function)', 500);
}

function run_clock() {
    id = "date_string";
    date = get_date_string();
    document.getElementById(id).innerHTML=date;
    t = setTimeout('run_clock()', 500);
}

function run_utc_clock() {
    id = "utc_date_string";
    date = get_utc_date_string();
    document.getElementById(id).innerHTML=date;
    // run every half second 
    t = setTimeout('run_utc_clock()', 500);
}

function run_salutation() {
    id = "salutation";
    sal = get_salutation();
    document.getElementById(id).innerHTML=sal;
    // run every half hour
    t = setTimeout('run_salutation()', 180000);
}
