
/*
 * this code converts decimal NCRs to html character strings
 * script injection loads jQuery if it's not defined
 * ignore all elements with a class name of: noFixDNRs
 *
 * ajoslin - 09/01/2011
 */

// convert one DNR
// from: http://www.rishida.net/tools/conversion/
function dnr_dec2char ( n ) {
	var result = '';
	if (n <= 0xFFFF) { result += String.fromCharCode(n); } 
	else if (n <= 0x10FFFF) {
		n -= 0x10000
		result += String.fromCharCode(0xD800 | (n >> 10)) + String.fromCharCode(0xDC00 | (n & 0x3FF));
	} 
	// we should not print any error messages, it'll only confuse the issue
	// else { result += 'dnr_dec2char error: Code point out of range: '+dec2hex(n); }
	return result;
}

// convert a run of DNRs
// from: http://www.rishida.net/tools/conversion/
function dnr_convertDecNCR2Char ( str ) { 
	// convert up to 6 digit escapes to characters
	str = str.replace(/&#([0-9]{4,7});/g, function(matchstr, parens) { return dnr_dec2char(parens); });
	return str;
}

// inject a script tag to load some javascript, use the callback when it's loaded
// (copied from jquery core and adapted by paul irish)
function InjectScript ( url, success ) {
	try {
		var script = document.createElement('script');
		script.src = url;
		var head = document.getElementsByTagName('head')[0],
		done = false;
		// Attach handlers for all browsers
		script.onload = script.onreadystatechange = function(){
			if ( !done && (!this.readyState
				|| this.readyState == 'loaded'
				|| this.readyState == 'complete') ) {
				done = true;
				success();
				script.onload = script.onreadystatechange = null;
				head.removeChild(script);
			}
		};
		head.appendChild(script);
	} catch (i0) {}
}

// search for elements containing DNRs
function dnr_scheduleCleanup () {
	try {
		$(document).ready(function(){
			
			/********************* elements having value() **************************/
			
			try { 
				// input elements
				$('input:[value*="&#"]:not(".noFixDNRs")').each(function(x,e){
					try { $(e).val(dnr_convertDecNCR2Char($(e).val())); } catch (i){}
				});
			 } catch (i2){}
		
			try { 
				// textareas
				$('textarea:contains("&#"):not(".noFixDNRs")').each(function(x,e){
					try { $(e).val(dnr_convertDecNCR2Char($(e).val())); } catch (i){}
				});
			 } catch (i2){}
		
			/********************* elements having text() **************************/
		
			try { 
				// font runs
				$('font:contains("&#"):not(".noFixDNRs")').each(function(x,e){
					try { $(e).text(dnr_convertDecNCR2Char($(e).text())); } catch (i){}
				});
			 } catch (i2){}
		
			try { 
				// hyperlinks
				$('a:contains("&#"):not(".noFixDNRs")').each(function(x,e){
					try { $(e).text(dnr_convertDecNCR2Char($(e).text())); } catch (i){}
				});
			 } catch (i2){}
		
			/********************* elements having value() & text() **************************/
			
			try { 
				// select options 
				$('option:contains("&#"):not(".noFixDNRs")').each(function(x,e){
					try { $(e).val(dnr_convertDecNCR2Char($(e).val())); } catch (i){}
					try { $(e).text(dnr_convertDecNCR2Char($(e).text())); } catch (i){}
				});
			 } catch (i2){}
		
			/********************* miscellaneous elements **************************/
		
			// fix page titles
			try { $(document).attr('title', dnr_convertDecNCR2Char($(document).attr('title'))); } catch (i){}
			
		});
	} catch (i3) {}
}

// set to false to disable
if (true) {
	
	// check for jQuery
	if(typeof jQuery!='undefined') {
		// jQuery is loaded, do cleanup
		dnr_scheduleCleanup();
	} else {
		// load jQuery, and then request the cleanup
		InjectScript("/js/jquery/jquery.min.js",function(){
			dnr_scheduleCleanup();
		});
	}
}

/* done */

