/*==============================================================================


Filename: jbugspray.js
Created: 2008-01-29
Initial Developer: Fabio Serra (faser@omniwhere.com)

Usage:

var postUrl = "/bugspray/collector.cfm";
jbugspray.run(postUrl);

$LastChangedDate:: 2010-06-02#$
$LastChangedRevision: 1831 $
$LastChangedBy: faser $
==============================================================================*/

var jbugspray = {

	collectorUrl: "",
	counter: 0,
	limitPostTo: 5,


	run:function(postUrl){
		this.collectorUrl = postUrl;
		window.onerror = this.catchError;
	},

	stop:function(){
		window.onerror = null;
	},

	catchError: function(msg, url, line) {
		// prevent too many errors in the same page
		if(jbugspray.counter < jbugspray.limitPostTo) {
			errorMsg = "Javascript Error: " + msg;
			errorUrl = url;
			errorLine = line;
			errorBrowser = ""; 
			if(navigator){errorBrowser = navigator.userAgent}
			jbugspray.postError();
		}
	},

	postError:function() {
		if(window.XMLHttpRequest){
			try {
				var req = new XMLHttpRequest();
				req.open("POST", this.collectorUrl, true);
				req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				var body="message="+errorMsg+"&url="+errorUrl+"&line="+errorLine+"&browser="+errorBrowser;
				req.setRequestHeader("Content-length", body.length);
				req.setRequestHeader("Connection", "close");
				req.send(body);
				jbugspray.counter++;
			//fail silently
			}catch(ex){};
		}
	}
}