/*
 * XHTML Utils
 * Copyright 2006 J. Donald Tillman, till.com
 *
 */


/**
 *  XHTML1.0 Strict does not allow target=_blank on anchors.  I think
 * that's stupid, so here's a way around it.  If you give an anchor
 * a class of "extlink" this will dynamically add the target=_blank 
 * attribute.  It also works for any containing class, so you can 
 * externalize a bunch multple anchors with a single div.
 */
function setupExternalLinks() { 
    if (!document.getElementsByTagName) return; 
    var anchors = document.getElementsByTagName('a'); 
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i]
        if (anchor.getAttribute('href') && findClassName(anchor, 'extlink')) {
            anchor.setAttribute('target', '_blank');
        }
    }
 }

/**
 * Helper function for the above.
 * Does this element, or any of its ancestors, have this class name.
 */
function findClassName(element, className) {
    while (element.tagName) {
        var classNameAtr = element.className;
	if (classNameAtr) {
            // handle multiple classes
	    var classNames = classNameAtr.split(' ');
	    for (var i = 0; i < classNames.length; i++) {
		var eachClassName = classNames[i];
	        if (className == eachClassName) return true;
            }
        }
        element = element.parentNode;
    }
    return false;
} 

