/*
 * This files contains javascript code to be used on Enterprise Outfitters'
 * web site.
 */

/*
 * This function makes all anchor ("a") tags with attribute rel="external"
 * open in a new browser window. This script was adapted from an article
 * by Kevin Yank, dated March 4, 2003, and published at:
 * http://www.sitepoint.com/article/standards-compliant-world
 */
function externalLinks() {
    // If the function is not available, it is an old browser; skip it.
    if (!document.getElementsByTagName) {
        return;
    }
    // Get the collection of anchor elments in the document
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
                anchor.getAttribute("rel") == "external") {
            anchor.target = "_blank";
        }
    }
    // Do the same thing for form tags with class="external"
    var forms = document.getElementsByTagName("form");
    for (var i = 0; i < forms.length; i++) {
        var form = forms[i];
        if (form.getAttribute("class") == "external") {
            form.target = "_blank";
        }
    }
}

/*
 * This function finds the first <meta> tag with name="content-revised" and
 * uses the value from that tag to generate a formatted version of the date
 * in that tag.
 */
function contentModifiedDate(stringType) {
    // Qualify the browser
    if (! document.implementation) {
        return;
    }
    if (! document.implementation.hasFeature) {
        return;
    }
    if (! document.implementation.hasFeature("html", "1.0")) {
        return;
    }
    // Find the tag
    var metaTags = document.getElementsByTagName("meta");
    var contentModified = null;
    for (var i = 0; i < metaTags.length; i++) {
        var tag = metaTags[i];
        if (tag.getAttribute("name") != null
                && tag.getAttribute("content") != null
                && tag.getAttribute("name") == "content-revised") {
            contentModified = tag.getAttribute("content");
            break;
        }
    }
    if (contentModified == null) {
        return;
    }
    /*
     * Strip off leading and trailing text placed by keyword substitution.
     * We must break it up into pieces to avoid keyword substitution here.
     */
    contentModified = contentModified.replace(/^\$Date: /, "");
    contentModified = contentModified.replace(/ \$$/, "");
    if (stringType == "CVS") {
        return parseCVSDate(contentModified);
    }
    if (stringType == "Subversion") {
        return parseSVNDate(contentModified);
    }
}

/*
 * Converts date string expanded by CVS to a javascript Date object.
 */
function parseCVSDate(contentModified) {
    /* CVS returns dates in UTC time.
     * Example CVS date (after standard mods above):
     *     2005/10/06 17:40:26 */

    /* Set up a regular expression to parse the remainder of the date. */
    var expression = /^(\d*)\/(\d*)\/(\d*) (\d*):(\d*):(\d*)/
    // Parse the date string
    var result = contentModified.match(expression);
    // Create a Date object from the parsed values
    var date = new Date(Date.UTC(result[1], result[2] - 1, result[3],
        result[4], result[5], result[6]));
    return date;
}

/*
 * Converts date string expanded by Subversion to a javascript Date object.
 */
function parseSVNDate(contentModified) {
    /* SVN returns dates in local time.
     * Example SVN date (after mods above):
     *     2005-10-05 13:56:37 -0600 (Wed, 05 Oct 2005)
     */

    /* Remove the paranthetical date representation at the end. */
    contentModified = contentModified.replace(/ \(.*\)/, "");

    /* Example SVN date (after mods above): 2005-10-05 13:56:37 -0600 */

    /* Set up a regular expression to parse the remainder of the date. */
    var expression = /^(\d*)-(\d*)-(\d*) (\d*):(\d*):(\d*) ([+-]?\d*)/
    // Parse the date string
    var result = contentModified.match(expression);
    // Store the local time in a Date object as if it were a UTC time
    var date = new Date(Date.UTC(result[1], result[2] - 1, result[3],
        result[4], result[5], result[6]));
    /*
     * Compute the number of milliseconds by which the local time should be
     * adjusted to make it a UTC time. The first two digits are the hours
     * and the second two are the minutes.
     */
    var timeZoneOffsetMS
        = ((result[7] / 100 * 60 * 60000)   // Convert the hours to MS
        + (result[7] % 100) * 60000);       // Convert the minutes to MS
    /*
     * Adjust the value in date by the time zone offset amount.
     */
    date.setTime(date.getTime() - timeZoneOffsetMS);
    // date now contains the UTC time of the last commit.
    return date;
}

/*
 * This function writes a <br/> tag into the document. Some XHTML validators
 * don't like to see the <> markers, even in a script.
 */
function writeLineBreak() {
    document.write("<br/>");
}

window.onload = externalLinks;

/* Last line of file. */
