onerror = reportError;

function reportError( errorMessage, errorURL, errorLine ) {
	var xmlHttp;
	
	var urlEncode = function( str ) {
		RegExp.escape = function(text) {
			if (!arguments.callee.sRE) {
				var specials = [
								'/', '.', '*', '+', '?', '|',
								'(', ')', '[', ']', '{', '}', '\\'
								];
				arguments.callee.sRE = new RegExp(
												  '(\\' + specials.join('|\\') + ')', 'g'
												  );
			}
			return text.replace(arguments.callee.sRE, '\\$1');
		};		
		
		str = escape( str );
		str = str.replace(RegExp( RegExp.escape( "+" ), 'g' ), '%2B');
		str = str.replace(RegExp( RegExp.escape( "%20" ), "g" ), '+');
		str = str.replace(RegExp( RegExp.escape( "*" ), "g" ), '%2A');
		str = str.replace(RegExp( RegExp.escape( "/" ), "g" ), '%2F');
		str = str.replace(RegExp( RegExp.escape( "@" ), "g" ), '%40');
		str = str.replace(RegExp( RegExp.escape( "." ), "g" ), '%2E');
		return str;
	};
	
	if( window.XMLHttpRequest ){
		xmlHttp = new XMLHttpRequest();
	}
	else if( window.ActiveXObject ) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		return false;
	}
	
	xmlHttp.open( "POST", "js_error_reporter.php5", true );
	xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlEncoded' );
	
	errorMessage += '\n\nnavigator.appName = ' + navigator.appName + '\nnavigator.appVersion = ' + navigator.appVersion + '\nnavigator.appCodeName = ' + navigator.appCodeName + '\nnavigator.platform = ' + navigator.platform;
	
	xmlHttp.send( 'errorURL=' + urlEncode( errorURL ) + '&errorLine=' + urlEncode( errorLine ) + '&errorMessage=' + urlEncode( errorMessage ) );
	
	alert( 'Darn! Statecraft encountered a JavaScript error. An automatic error report has been submitted, and I will get to work right away to iron out this bug. If I will email you if I need more information to address the problem. Thanks for playing Statecraft!' );
	
	return false;
};

/*var urlDecode = function( str ) {
	str = str.replace(RegExp( RegExp.escape( "%2E" ), "g" ), '.');
	str = str.replace(RegExp( RegExp.escape( "%40" ), "g" ), '@');
	str = str.replace(RegExp( RegExp.escape( "%2F" ), "g" ), '/');
	str = str.replace(RegExp( RegExp.escape( "%2A" ), "g" ), '*');
	str = str.replace(RegExp( RegExp.escape( "+" ), "g" ), '%20');
	str = str.replace(RegExp( RegExp.escape( "%2B" ), "g" ), '+');
	str = unescape( str );
	return str;
};*/

var collectFormData = function( pageElement ) {
	var result = "";
	
	if( pageElement.tagName == "INPUT" ) {
		if( pageElement.type == "checkbox" ) {
			if( pageElement.checked ) {
				result += HANS.utf8.encode( pageElement.name ) + "=" + HANS.utf8.encode( pageElement.value ) + "&";
			} else {
				result += HANS.utf8.encode( pageElement.name ) + "=&";
			}
		}
		else if( pageElement.type == "radio" ) {
			if( pageElement.checked ) {
				result += HANS.utf8.encode( pageElement.name ) + "=" + HANS.utf8.encode( pageElement.value ) + "&";
			}
		}
		else {
			result += HANS.utf8.encode( pageElement.name ) + "=" + HANS.utf8.encode( pageElement.value ) + "&";
		}
	}   
	else if( pageElement.tagName == "SELECT" ) {
		result += HANS.utf8.encode( pageElement.name ) + "=" + HANS.utf8.encode( pageElement.options[ pageElement.selectedIndex ].value ) + "&";
	}
	else {
		for( var i = 0; i < pageElement.childNodes.length; i++ ) {
			result += collectFormData( pageElement.childNodes[i] );
		}
	}
	return result;
};

var AJAXconduit = function( ) {
	var xmlHttp;
	
	if( window.XMLHttpRequest ){
		// Firefox, Safari, Opera...
		xmlHttp = new XMLHttpRequest();
	}
	else if( window.ActiveXObject ) {
		// Internet Explorer 5+
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		// There is an error creating the object,
		// just as an old browser is being used.
		alert('Statecraft cannot function properly because there was a problem creating the XMLHttpRequest object. You might need to update your web browser.');
		return;
	}
	
	xmlHttp.onreadystatechange = function() {
		if( ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) && ( xmlHttp.status == 200 ) ) {
			try {
				eval( xmlHttp.responseText );
			}
			catch( evalError ) {
				reportError( 'JavaScript Eval:\n' + xmlHttp.responseText + '\n\nError during eval: ' + evalError.message, evalError.location, evalError.line );
			}
		}
	};
	
	return {
		
		postFormElement : function( pageElement ) {
			xmlHttp.open( "POST", pageElement.action, true );
			xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlEncoded' );
			
			xmlHttp.send( collectFormData( pageElement ) );
			
			return false;
		},
		
		postElementToURL : function( pageElement, destinationURL ) {
			xmlHttp.open( "POST", destinationURL, true );
			xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlEncoded' );
			
			xmlHttp.send( collectFormData( pageElement ) );
			
			return false;
		},
		
		postDataToURL : function( postData, destinationURL ) {
			xmlHttp.open( "POST", destinationURL, true );
			xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlEncoded' );
			
			xmlHttp.send( postData );
			
			return false;
		}
	}
}( );