function replaceLinks(url){
  var rootPath = url.replace(/^(http:\/\/[^\/]*).*$/, "$1");

  // Step 1: remove everything after ;?
  var baseUrl = url.replace(/^([^\?\;]*).*/,"$1");

  // Step 2: extract everything after the last slash and:
  // if it has a . in it then take everything before it
  // else stick / at the end unless it already has one 
  var everythingAfterTheLastSlash = baseUrl.replace(/^.*?([^\/]*)$/,  "$1");
  var everythingBeforeTheLastSlash = baseUrl.replace(/^(.*?)[^\/]*$/, "$1");

  if(everythingAfterTheLastSlash.search(/\./) > -1 && everythingBeforeTheLastSlash.search(/:\/\/$/) < 0) {
    baseUrl = everythingBeforeTheLastSlash;
  } else if(baseUrl.search(/\/$/) < 0) {
    baseUrl = baseUrl + "/";
  }

  baseUrl = baseUrl.replace(/\/+$/, "");

  $$('img').each( function(element) {
    // Converting http://abc.com/a/b/c to http://abc.com
    var path = normalizePath(rootPath, baseUrl, element.readAttribute('src'));

    // FIXME: This is a hack for FSM because their site keeps handing back
    // pixelated images. We found that the pixelated images only happen when
    // being called through proxy. And they only happen when the proxy hands
    // back images that are image/gif instead of image/jpeg. Normally it will
    // hand back image/jpeg but when we call through the Java proxy we only
    // get image/gif. So for now we will just search and replace image/gif
    // with image/jpeg
    if(path && path.search(/blank.asp/) && path.search(/image\/gif/)) {
      path = path.replace(/image\/gif/, "image/jpeg");
    }

    element.src = path;
  });  

  $$('a').each( function(element) { 
    var path = element.readAttribute('href');
    if(path == undefined || path.search(/^#/) > -1 ) return;
    element.writeAttribute({href: window.location.href.replace(/\?.*/, "") + '?link=' + escape(normalizePath(rootPath, baseUrl, path)) });
  });
  
  $$('link').each( function(element) {
	var path = element.readAttribute('href');
	if(path == undefined || path.search(/mobileCss.css/) > -1) return;
    element.href = normalizePath(rootPath, baseUrl, path);
  });

} //End of function ReplaceLinks

function normalizePath(rootPath, baseUrl, path) {
  if(path == undefined) return path;
  if(!hasHostAndProtocol(path)) {
     // If we have an absolute path then just stick the root path in front:
     // http://foo.com
     if(pathIsAbsolute(path)) {
       path = rootPath + path; 

     // If we have a relative path then we figure out the relative url and
     // stick that in front. For example:
     // Let's say we have 
     //  imageSource: foo/bar/abc.jpg and 
     //  url: http://a.com/1/2.html
     // Then we need to go to: http://a.com/1/foo/bar/abc.jpg
     //
     // Let's say we have 
     //  imageSource: foo/bar/abc.jpg and 
     //  url: http://a.com/1/2/
     // Then we need to go to: http://a.com/1/2/foo/bar/abc.jpg
     } else {
       path = baseUrl + "/" + path; 
     }
   }

   return path;
}

function hasHostAndProtocol(path) {
  return path.search(/^[A-Za-z]+:/) > -1
}

function pathIsAbsolute(path) {
  return path.search(/^\//) > -1
}

