app = new Object() ;
app.textSource = null ;
app.textTarget = null ;


latin_1 = 	[

 			['degree', '\u00b0'],
			['!!', '\u00a1'],		['plusminus', '\u00b1'],
			['cent', '\u00a2'],		['^2', '\u00b2'],
			['pound', '\u00a3'],		['^3', '\u00b3'],
			//['', '\u00a4'],			
							['acute', '\u00b4'],
			['yen', '\u00a5'],		['micro', '\u00b5'],
			['pipe', '\u00a6'],		['paragraph', '\u00b6'],
			['section', '\u00a7'],		['dot', '\u00b7'],
			['diaresis', '\u00a8'],		['cedilla', '\u00b8'],
			['copyright', '\u00a9'],	['^1', '\u00b9'],
			['^a', '\u00aa'],		['^o', '\u00ba'],
			['<<', '\u00ab'],		['>>', '\u00bb'],
			//['', '\u00ac'],			
							['1/4', '\u00bc'],
							['1/2', '\u00bd'],
			['reg', '\u00ae'],		['3/4', '\u00be'],
			['dash', '\u00af'],		['??', '\u00bf'],
			['A`', '\u00c0'],		['Dh', '\u00d0'], 
			['a`', '\u00e0'],		['dh', '\u00f0'], 
			["A'", '\u00c1'],		['N~', '\u00d1'], 
			["a'", '\u00e1'],		['n~', '\u00f1'], 
			['A^', '\u00c2'],		['O`', '\u00d2'], 
			['a^', '\u00e2'],		['o`', '\u00f2'], 
			['A~', '\u00c3'],		["O'", '\u00d3'], 
			['a~', '\u00e3'],		["o'", '\u00f3'], 
			['A"', '\u00c4'],		['O^', '\u00d4'], 
			['a"', '\u00e4'],		['o^', '\u00f4'], 
			['Ao', '\u00c5'],		['O~', '\u00d5'], 
			['ao', '\u00e5'],		['o~', '\u00f5'], 
			['Ae', '\u00c6'],		['O"', '\u00d6'], 
			['ae', '\u00e6'],		['o"', '\u00f6'], 
			['C,', '\u00c7'],		['times', '\u00d7'], 
			['c,', '\u00e7'],		['divide', '\u00f7'], 
			['E`', '\u00c8'],		['O/', '\u00d8'], 
			['e`', '\u00e8'],		['o/', '\u00f8'], 
			["E'", '\u00c9'],		['U`', '\u00d9'], 
			["e'", '\u00e9'],		['u`', '\u00f9'], 
			['E^', '\u00ca'],		["U'", '\u00da'], 
			['e^', '\u00ea'],		["u'", '\u00fa'], 
			['E"', '\u00cb'],		['U^', '\u00db'], 
			['e"', '\u00eb'],		['u^', '\u00fb'], 
			['I`', '\u00cc'],		['U"', '\u00dc'], 
			['i`', '\u00ec'],		['u"', '\u00fc'], 
			["I'", '\u00cd'],		["Y'", '\u00dd'], 
			["i'", '\u00ed'],		["y'", '\u00fd'], 
			['I^', '\u00ce'],		['Th', '\u00de'], 
			['i^', '\u00ee'],		['th', '\u00fe'], 
			['I"', '\u00cf'],		['sz', '\u00df'], 
							['ss', '\u00df'], 
			['i"', '\u00ef'],		['y"', '\u00ff'], 

// Esperanto bindings.
// Should be in Latin A Extended, but putting here for now. 

			['Jx', '\u0134'],		['jx', '\u0135'],
			['Cx', '\u0108'],		['cx', '\u0109'],
			['Gx', '\u011c'],		['gx', '\u011d'],
			['Hx', '\u0124'],		['hx', '\u0125'],
			['Sx', '\u015c'],		['sx', '\u015d'],
			['Ux', '\u016c'],		['ux', '\u016d'],

// Belongs elsewhere, but putting here for now.

			['euro', '\u20ac']
		] ;

//




function comparePairs( a, b )
{
	if ( a[0] > b[0] )
		return 1 ;
	else if ( a[0] < b[0] )
		return -1 ;
	else
		return 0 ;
}

latin_1.sort( comparePairs ) ;


for ( var j = 0 ; j < latin_1.length ; j++ )
{
	latin_1[j][0] = latin_1[j][0] + "&" ;
}

function myAlert( pairs )
{
	var s = "" ;
	for ( var i = 0 ; i < pairs.length ; i++ )
	{
		s = s + pairs[i][0] + ':  ' + pairs[i][1] + '\n' ;
	}
	alert( s ) ;
}


function convert( input, pairs )
{
	var index = 0 ;
	var pairIndex ;
	var pair ;
	//var output = '' ;
	var output = new Array( input.length ) ;
	var outputIndex = 0 ;

	while ( index < input.length )
	{
		pairIndex = findLongest( input, index, pairs ) ;
		if ( pairIndex < 0 )
		{
			//output = output + input.charAt( index ) ;
			output[outputIndex] = input.charAt( index ) ;
			outputIndex++ ;
			index++ ;
		}
		else
		{
			pair = pairs[pairIndex] ;
			//output = output + pair[1] ;
			output[outputIndex] = pair[1] ;
			outputIndex++ ;
			index = index + pair[0].length ;
		}
	}

	output = output.join( "" ) ;
	return output ;
}


function find( string, index, substitutions )
{
	var lowI = 0 ;
	var highI = substitutions.length - 1 ;

	if ( compare( substitutions[lowI][0], string, index ) == 0 )
		return lowI ;
	else if ( compare( substitutions[highI][0], string, index ) == 0 )
		return highI ;

	var thisI = -1 ;
	var lastI ;

	var compareRes ;

	var sanity = 0 ;

	while ( true )
	{
		thisI = Math.floor( ( lowI + highI ) / 2 ) ;

		if (( thisI == lowI ) || (thisI == highI))
			return -1 ;

		compareRes = compare( substitutions[thisI][0], string, index ) ;
		//var msg =  [['lowI',lowI], ['highI',highI],
		//	['compareRes', compareRes], ['thisI',thisI],
		//	['substitutions[thisI][0]',substitutions[thisI][0]],
		//	['string',string]] ;
		//myAlert( msg ) ;
		if ( compareRes < 0 )
		{
			lowI = thisI ;
		}
		else if ( compareRes > 0 )
		{
			highI = thisI ;
		}
		else
			return thisI ;
	}
}

function findLongest( string, index, substitutions )
{
	var match = find( string, index, substitutions ) ;
	if ( match < 0 )
		return match ;

	while ( true )
	{
		if ( compare( substitutions[match][0], string, index+1 ) == 0 )
			match++ ;
		else
			break ;
	}

	return match ;
}

function compare( substring, string, index )
{
	var substringB = string.substring( index, index + substring.length ) ;
	//myAlert( [ ['substring',substring],
	//	['substringB',substringB] ] ) ;
	if ( substring < substringB )
		return -1 ;
	else if ( substring > substringB )
		return 1 ;
	else return 0 ;
}

function convertOnPage()
{
	var text = app.textSource.value ;
	app.textTarget.value = "(Bonvolu atendi momenton.)" ;
	var output = convert( text, latin_1 ) ;
	app.textTarget.value = output ;
}









