/**
 * @author clemens
 * @date 2006-08-07
 * these functions will provide enhanced javascript error
 * handling facilities which will log all javascript errors
 * to database
 */

// catch errors for IE
window.onerror=ErrorFunction;
// catch Mozilla errors
window.onError=ErrorFunction;

/**
 * this error function will call the db script for saving error messages.
 * if we are not able to correctly save the error to database no further
 * measures will be taken.
 * @param	msg	the error message
 * @param	url	location where the error occured
 * @param	line	line on which the error occured
 * @return true on success, false on error   
 */ 
function ErrorFunction(msg, url, line)
{
  // if we encountered an error save it to DB
	if (window.location.search.indexOf('JsError=return')==-1) {
		var saveURL = JSLOG_STAG;
		saveURL += "&JSLOG_USER_ID=" + escape(JSLOG_USER_ID);
		saveURL += "&JSLOG_QS=" + escape(window.location.search);
		saveURL += "&JSLOG_DO=" + escape(JSLOG_DO);
		saveURL += "&JSLOG_ERRLINE=" + escape(line);
		saveURL += "&JSLOG_ERRMSG=" + escape(msg);
		saveURL += "&JSLOG_ERRAGENT=" + escape(navigator.userAgent);
		saveURL += "&do=21";
    makeRequest(JSLOG_STAG + saveURL);
  }
  return true;
}

/**
 * will request a specific page
 * @param	url	page to be requested
 * @return	true on success, false on error 
 */  
function makeRequest(url) {
  var http_request = false;
	// Firefox
  if (window.XMLHttpRequest) {
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
          http_request.overrideMimeType('text/xml');
      }
  // IE
	} else if (window.ActiveXObject) {
      try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try {
              http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
      }
  }
	
	// if we were not able to create a http request object just fail
  if (!http_request) {
      return false;
  }
  // this enables us to interact with and retrieve info on our request
  // disabled till further extension
	// http_request.onreadystatechange = function() { httpState(http_request); };
  http_request.open('GET', url, true);
  http_request.send(null);
  return true;
}

/*
// alert response text
function httpState(http_request) {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
          alert(http_request.responseText);
      } else {
          alert('There was a problem with the request.');
      }
  }
}*/