function isUpper(a){return(65<=a&&a<=90);}
function isLower(a){return(97<=a&&a<=122);}
function isAlpha(a){return(isUpper(a)||isLower(a));}
function isNum(a){return(48<=a&&a<=57);}
function isAlphaNum(a){return(isAlpha(a)||isNum(a));}
function isSpace(a){return(a<33||126<a);}

function isAllAlpha(s)
{
	i = 0;
	while (i < s.length && isAlpha(s.charCodeAt(i)))
		i++;
	return (i == s.length);
}

function indexOfSpace(s,i)
{
	while (i < s.length)
	{
		if (isSpace(s.charCodeAt(i)))
			return i;
		i++;
	}
	return -1;
}

function indexOfAlpha(s,i)
{
	while (i < s.length)
	{
		if (isAlpha(s.charCodeAt(i)))
			return i;
		i++;
	}
	return -1;
}

function indexOfNonAlpha(s,i)
{
	while (i < s.length)
	{
		if (!isAlpha(s.charCodeAt(i)))
			return i;
		i++;
	}
	return -1;
}

function indexOfNonAlphaNum(s,i)
{
	while (i < s.length)
	{
		if (!isAlphaNum(s.charCodeAt(i)))
			return i;
		i++;
	}
	return -1;
}

String.prototype.setCharAt = function(i, c)
{
	if (0 <= i && i < this.length)
		return this.substr(0, i) + c + this.substr(i + 1);
	return this;
}

String.prototype.reverse = function()
{
	splitext = this.split("");
	revertext = splitext.reverse();
	reversed = revertext.join("");
	return reversed;
}

function randInt(min, max)
{
	max -= min;
	var i = Math.floor(Math.random() * max + 1);
	i += min;
	return i;
}

function typoglycemia_scrambleWord(w)
{
	if (w.length - (this.skipLeft + this.skipRight) < 2)
		return w;

//	if (this.method == 0 || this.method == 1)
//	{
		firstLetter = this.skipLeft;
		lastLetter = w.length - (this.skipRight + 1);
//	}
//	else
//	{
//		midLetter = w.length >> 1;
//		firstLetter = midLetter - (this.changeMiddle >> 1);
//		lastLetter = midLetter + (this.changeMiddle >> 1);
//	}

	if (this.method == 0) // randomly scramble interior chars
	{
		for (i = firstLetter; i < lastLetter; i++)
		{
			ri = randInt(firstLetter, lastLetter);
			if (ri != i)
			{
				c = w.charAt(i);
				w = w.setCharAt(i, w.charAt(ri));
				w = w.setCharAt(ri, c);
			}
		}
	}
	else if (this.method == 1) // reverse interior chars
	{
		lastLetter++;
		var mid = w.substring(firstLetter, lastLetter);
		mid = mid.reverse();
		w = w.substr(0, firstLetter) + mid + w.substr(lastLetter);
	}

	return w;
}

function typoglycemia_scramble(ti)
{
	var to = "";
	i1 = 0;
	while (i1 < ti.length)
	{
		isWord = isAlpha(ti.charCodeAt(i1));
		if (isWord)
			i2 = indexOfNonAlphaNum(ti,i1);
		else
			i2 = indexOfAlpha(ti,i1);
		if (i2 == -1)
			i2 = ti.length;
		s = ti.substr(i1,i2 - i1);
		if (isWord && isAllAlpha(s))
			s = this.scrambleWord(s);
		to += s;
		i1 = i2;
	}
	return to;
}

function Typoglycemia()
{
	this.method = 0;
	this.skipLeft = 1;
	this.skipRight = 1;
	this.changeMiddle = 2;
	this.scramble = typoglycemia_scramble;
	this.scrambleWord = typoglycemia_scrambleWord;
}

function TypoglycemiaUI_setScramble(m)
{
	if (this.typo.method != m)
	{
		this.typo.method = m;
		this.scramble();
	}
}

function TypoglycemiaUI_setSkipLeft()
{
	this.typo.skipLeft = parseInt($('#sl').val());
	this.scramble();
}

function TypoglycemiaUI_setSkipRight()
{
	this.typo.skipRight = parseInt($('#sr').val());
	this.scramble();
}

function TypoglycemiaUI_scramble()
{
	$('#to').val(this.typo.scramble($('#ti').val()));
}

function TypoglycemiaUI()
{
	this.typo = new Typoglycemia();
	this.setSkipLeft = TypoglycemiaUI_setSkipLeft;
	this.setSkipRight = TypoglycemiaUI_setSkipRight;
	this.setScramble = TypoglycemiaUI_setScramble;
	this.scramble = TypoglycemiaUI_scramble;
}

var ui;
$(document).ready(function() {
	ui = new TypoglycemiaUI();

	$('#appClose').click(function(){
		window.location = '../';
		});

	});
