/*
 * This javascript file is used to track IP address of the visitor.
 *
 * To use, include the following in the body of a html file:
 * <script src="http://www2.hawaii.edu/~chenx/ip.js"></script>
 *
 * Note: 
 * 1) This needs the user's browser to support javascript.
 * 2) This can be included into the header or footer file, and then
 *    it works for every page that includes the header or footer file.
 *
 * @Author: Xin Chen
 * @Created on: 11/17/2007
 * @Last updated: 11/17/2007
 */

var base_url = "http://www2.hawaii.edu/~chenx/";
var base_url_len = base_url.length;

ip_logger();
/* ip_logger_test(); */

/*
 * If don't need to record referrer, just remove it below.
 */
function ip_logger() {
  document.open();
  document.write ("<IFRAME src='http://www.cssauh.com/cgi-bin/ip.pl?src=" 
                  + url_encode(window.location) 
                  + "&ref=" + document.referrer + "'");
  document.write (" style='display:none;width:0px; height:0px; border: 0px'>");
  document.write ("<\/IFRAME>");
  document.close();
}


/*
 * This shows the iframe, and does not really write into log file.
 * For testing use only.
 */
function ip_logger_test() {
  document.open();
  document.write ("<IFRAME src='http://www.cssauh.com/cgi-bin/pltest.pl?src=" 
                  + url_encode(window.location) + "'");
  document.write (" style='display:block;width:500px; height:200px; border: 0px'>");
  document.write ("<\/IFRAME>");
  document.close();
}


/*
 * Remove the base url and file suffix from the url.
 * If the url is the same as base_url, replace it with "/".
 */
function url_encode(url) {
  var pos;
  url = "" + url; // cast into string type. 
  if (url.indexOf(base_url) == 0) {
    url = url.substring(base_url_len);
    // alert(url);
  }
  pos = url.lastIndexOf(".");
  if (pos > 0) {
    url = url.substring(0, pos);
    // alert(url); 
  }
  if (url.length == 0) {
    url = "/"; 
  }
  return url;
}

