/*
 * @(#)logclient.js $Revision: 1.1 $ $Date: 2007/05/04 05:29:37 $
 *
 * Copyright (c) 2003-2005 Carnegie Mellon University.
 */


/**
 * <p>Logging service client.</p>
 *
 * @author     John A Rinderle
 * <a href="mailto:jar2@andrew.cmu.edu">(jar2@andrew.cmu.edu)</a>
 */
var log_service_url = "http://collective.chem.cmu.edu/log"

// Browser detection code
var agt				= navigator.userAgent.toLowerCase();
var is_major		= parseInt(navigator.appVersion);
var is_minor		= parseFloat(navigator.appVersion);

var is_mac			= (agt.indexOf("mac") != -1);

var is_ie			= ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3			= (is_ie && (is_major < 4));
var is_ie4			= (is_ie && (is_major == 4) && (agt.indexOf("msie 4") != -1));
var is_ie5			= (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0") != -1));
var is_ie5_22   	= (is_ie && (is_major == 4) && (agt.indexOf("msie 5.22") != -1));
var is_ie5_5		= (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") != -1));
var is_ie5_5up		= (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_22);

var is_nav			= ((agt.indexOf("mozilla") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("safari") == -1));
var is_gecko		= (agt.indexOf('gecko') != -1);
var is_nav6up		= (is_nav && is_gecko && (is_major >= 5));

var is_saf			= ((agt.indexOf('safari') != -1) && (agt.indexOf('mac') != -1));
var is_saf1_3up		= (is_saf && (parseSafariMajorBuild(agt) >= 312));

var is_supported_browser = ((is_ie5_5up && !is_mac) || is_nav6up || is_saf1_3up);

function parseSafariMajorBuild(agent) {
	var start = agent.indexOf('safari/') + 7;
	var stop = agent.lastIndexOf('.');
	stop = ((stop > start) ? stop : agent.length);
	return parseInt(agent.substring(start, stop));
}


/**
 * <p>Logs a user action. This method returns the XML message sent
 * to the logging server or <tt>null</tt> if the web browser does
 * not support logging.</p>
 *
 * @param     action_id     action identifier
 * @param     info_type     optional, type of data in the <tt>info</tt> field
 * @param     info          optional, information to be logged
 * @return    the XML message sent to the logging server, represented as a string
 */
function logUserActionFullXML(action_id, info_type, info) {
	if (!is_supported_browser) {
		return null;
	}
	
	var timezone = "US/Eastern";
	var timestamp = _formatTimestamp(new Date());

	var xml = escape(info);

	_getXMLHttpConnection(log_service_url + "/server").send(xml);

	return xml;
}


/**
 * <p>Private method, used by <tt>logUserAction</tt> to generate a
 * timestamp in the proper format for logging. The resulting string
 * contains the specified date, converted to UTC.</p>
 *
 * @param     timestamp     date object, the timestamp to format as a string
 * @return    timestamp string
 */
function _formatTimestamp(timestamp) {
	var s = "";

	var year = timestamp.getUTCFullYear();
	s += year + "/";

	var month = timestamp.getUTCMonth() + 1;
	s += ((month < 10) ? ("0" + month) : month) + "/";

	var date = timestamp.getUTCDate();
	s += ((date < 10) ? ("0" + date) : date) + " ";

	var hours = timestamp.getUTCHours();
	s += ((hours < 10) ? ("0" + hours) : hours) + ":";

	var mins = timestamp.getUTCMinutes();
	s += ((mins < 10) ? ("0" + mins) : mins) + ":";

	var secs = timestamp.getUTCSeconds();
	s += ((secs < 10) ? ("0" + secs) : secs);

	return s;
}

/* DHD: From John Rinderle to add support for older versions of IE. *./
   /* Returns XMLHTTPREQUEST object.  See next function for connection to the logging server */
function get_http_request_object() {
  var xmlHttp;

  if (window.XMLHttpRequest) {
    try {
      httpReq = new XMLHttpRequest();
    } catch (e) {
      return false;
    }
  } else if (window.ActiveXObject) {
    try {
      httpReq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
	httpReq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (f) {
	return false;
      }
    }
  }
  return(httpReq);
}


/**
 * <p>Private method, used by <tt>logUserAction</tt> to establish a
 * connection to the logging server.</p>
 *
 * @param      log_service_url     location of the logging server
 * @return     connection to logging server
 */
function _getXMLHttpConnection(log_service_url) {
  var xmlHttp;

  /*@cc_on @*/

  /*@if (@_jscript_version >= 5)
                try {
                        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (f) {
                                xmlHttp = false;
                        }
                }

        @else
                xmlHttp = false;
		@end @*/

  /* Previously:

                try {
                        xmlHttp = new XMLHttpRequest();
                } catch (e) {
                        xmlHttp = false;
                }
  */

  if (!xmlHttp) {
    xmlHttp = get_http_request_object();
  }

  xmlHttp.open("POST", log_service_url, true);
  xmlHttp.setRequestHeader("Content-Type", "text/xml");

  return xmlHttp;
}
