var errorLogged = false;

/**
 * Catch all IE errors and send them to the server
 */
function errorHandler(description,pagename,lineno) {
	if (errorLogged) {
		return;
	}
	
	// Only send one js error per day
	if (document.cookie.indexOf('errorsent') != -1) {
		return;
	}
	
	if (typeof Ext != "object") {
		return true;
	}
	
	if (description == "Error loading script" && lineno == 1) {
		return true;
	}
	if (description == "Script error." && lineno == 0) {
		return true;
	}
	if (pagename.indexOf('127.0.0.1') > -1) {
		return true;
	}
	if (description.indexOf('PropertiesImpl') > -1) {
		return true;
	}
	if (description.indexOf('The system cannot locate the resource specified.') > -1 && lineno == 36) {
		return true;
	}
	if (description.indexOf("'graph' is undefined") > -1 && lineno == 1) {
		return true;
	}
	if (description == "El._flyweights is undefined" && lineno == 15) {
		return true;
	}
	if (description.indexOf("Syntax") > -1 && lineno == 2) {
		return true;
	}
	if (Ext.isIE && description == "Permission denied") {
		return true;
	}
	if (Ext.isIE6 && (description == "Invalid character" || description == "Expected ')'")) {
		return true;
	}
	if (Ext.isIE7 && (description == "Syntax error" || description == "Expected ';'")) {
		return true;
	}
	if (Ext.isGecko2) {
		return true;
	}
	
	
	
	var liveLessonData = {};
	if (typeof lesson == "object") {
		for(var key in lesson) {
			if (typeof lesson[key] == "function") {
				continue;
			}
			if (typeof lesson[key] == "object" && key != 'exercises ') {
				continue;
			}
			liveLessonData[key] = lesson[key];
		}
	}
	var startLessonData = {};
	if (typeof lessonData == "object") {
		for(var key in lessonData) {
			if (typeof lessonData[key] == "function") {
				continue;
			}
			if (typeof lessonData[key] == "object" && key != 'exercises ') {
				continue;
			}
			startLessonData[key] = lessonData[key];
		}
	}
	
	if (pagename != location.href) {
		pagename += " ("+location.href+")";
	}
	
	if (!pagename) {
		return;
	}
	
	if (pagename.indexOf('www.google-analytics.com') > -1) {
		return;
	}
	
	Ext.Ajax.request({
		url: '/index/jserror/',
		success: function() {errorLogged = true;},
		params: {
			description: description,
			pagename: pagename,
			lineno: lineno,
			liveLessonData: Ext.encode(liveLessonData),
			startLessonData: Ext.encode(startLessonData)
		},
		failure: Ext.emptyFn
	});
	return true;
}
window.onerror = errorHandler;