/*********************************************************
 *   Off Campus Link Rewriting Script
 *   James Muir <muir.29@osu.edu> Systems Developer/Engineer, Ohio State University Libraries
 *   Last Updated 5/6/2008
 *   Automatically adds prepends proxy url to the link if the user is off campus
 *********************************************************/  

/* Constants - these values will be specific to your setup  */

 // Links with this class name will be affected. 
 // Usage: <a href="http://example.com/protectedresource" class="offcampus_check">Resource</a>
var className    =   'offcampus_check';
 // Proxy string prefix 
var proxyString  =   'http://proxy.lib.ohio-state.edu/login?url=';

/***********************************************************/
 var done = 0; // Initializes 'done' to be false  

/* Goes through the document and checks for links with the classname specified above and prepends
 * the proxy string to them. */ 
function rewritelinks(){
  if(done == 1) return; // if script has already run, return
    var numLinks = document.getElementsByTagName('a').length; //get # links on page
    for (j=0; j< numLinks; j++){
      if(document.getElementsByTagName('a')[j].className == className){
        resourceLink = document.getElementsByTagName('a')[j].href; //get the href value of the existing link
        document.getElementsByTagName('a')[j].href = proxyString+resourceLink; // prepend proxy string to the link
       } //end if
    } // end for
  done = 1; // set done = 1 so the script will not run again!
}

/* This is where the actual check is done */ 
if(oncampus == false) {
  rewritelinks();
}