/**
 *  function to create a seperate window for the URL specified.
 */
function popUp(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=480,height=360');");
}

/**
 *  function to close the current window.
 */
function popClose() {
  self.close();
}

/**
 *  function to print the current page.
 */
function printPage() {
  self.print();
}


//
//  Place the cursor in the first field of the first form that is not
//  the 'searchForm' or 'loginForm'.
//
function placeFocus() {
    for (var i = 0; i < document.forms.length; i++) {
        switch (document.forms[i].name) {
        case 'searchForm':
        case 'loginForm':
            // excluded forms
            break;
        default:
            var elems = document.forms[i].elements;
            for (var j = 0; j < elems.length; j++) {
                switch (elems[j].type.toLowerCase()) {
                case 'text':
                case 'password':
                case 'textarea':
                case 'select-one':
                    elems[j].focus();
                    return true;
                }
            }
        }
    }
}


//
//  This handy function from Simon Willison allows you to stack up
//  'window.onload' events without them stepping on each other's toes. It's
//  explained here - http://www.sitepoint.com/blog-post-view.php?id=171578
//
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}


//
//  This will create a popup window for the specified url.
//
function popupWindow(strURL,strWidth,strHeight) {
    var strOptions="resizable,scrollbars=1,height="+strHeight+",width="+strWidth;
    window.open(strURL, '', strOptions);
}


//
//  Make adjustments to links based on coding added to the rel attribute.
//
function windowLinks() {
    if(!document.getElementsByTagName) {
         return;
    }
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        var relIndex = anchor.rel;
        if (relIndex) {
            var relSplit = relIndex.split("|");
            if (relSplit[0] == "external") {
                anchor.target = "_blank";
                anchor.className = "external";
                anchor.title = "Link loads in new window";
            }
            else if (relSplit[0] == "popup") {
                anchor.className = "popup";
                anchor.title = "Link loads in Popup Window";
                anchor.popupWidth = relSplit[1];
                anchor.popupHeight = relSplit[2];
                anchor.onclick = function() {
                    popupWindow(this.href,this.popupWidth,this.popupHeight);
                    return false;
                };
            }
        }
    }
} 

//
//  Initialize on loading.
//
addLoadEvent(windowLinks);  
addLoadEvent(placeFocus);


