/* This script contains commonly used functions for http://www.upenn.edu/oip/
This is nonessential code; the site will still work if a browser does not support 
JavaScript, but some features will no longer be available. 
This file may be cached, so clear browser cache before testing changes.
*/

//Special functions added by Jorey Bump:

function clientSideInclude(id, source, basedir) {
    /*
	Replace specified HTML element on page with code retrieved from source URL.
	
	Arguments:
	
	 id: id attribute of element with content to be replaced
	 
	 source: URL of code to replace. Must be from same host to overcome 
	 JavaScript security restrictions, so URLs relative to document root 
	 are best (/articles/sasquatch.html).
	 
	 basedir (optional): Some sites are actually subdirectories of 
	 larger site, so this can be used to prefix source. Probably a bad idea.
	*/
    var req = false;
    // source MUST be on same host to reliably bypass browser security restrictions
    //   This makes sharing resources between hosts impossible
    //   source will be fetched relative to site root, so specify base directory if site is a subdirectory
    // Set default for basedir to empty string
    var basedir = (basedir == null) ? "" : basedir;
    url = basedir + source
    // For Safari, Firefox, and other non-MS browsers
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch (e) {
            req = false;
        }
    } else if (window.ActiveXObject) {
        // For Internet Explorer on Windows
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                req = false;
            }
        }
    }
    var element = document.getElementById(id);
    if (req) {
        // Synchronous request, wait till we have it all
        req.open('GET', url, false);
        req.send(null);
        element.innerHTML = req.responseText;
    }
}

//for testing
function spew() {
	document.writeln('Host = ' + location.host + '<br>');
	document.writeln('Path = ' + location.pathname + '<br>');
	var path_split = location.pathname.split("/");
	document.writeln('path_split[2] = ' + path_split[2]);
}

function main() {
	/* Load additional sidebar content based on location of page.
	   http://www.upenn.edu/oip/iss/index.html will yield "iss" for path_split[2]
	*/
	var path_split = location.pathname.split("/");
	var section = path_split[2];
	// Adding a random query string will help to overcome browser caching problems.
    var randomnumber=Math.floor(Math.random()*1000);
	if ( section == 'iss' ) {
        clientSideInclude('sidebar1', '/Library/oip/sidebar-iss.lbi?fresh=' + randomnumber, '/oip');
	} else if ( section == 'sa' ) {
        clientSideInclude('sidebar1', '/Library/oip/sidebar-sa.lbi?fresh=' + randomnumber, '/oip');
	}
	// Load main sidebar content that appears on all pages
    clientSideInclude('sidebar2', '/Library/oip/sidebar.lbi?fresh=' + randomnumber, '/oip');
}

function GoUrl(s)
{   var d = s.options[s.selectedIndex].value
    window.top.location.href = d
    s.selectedIndex=0
}
