﻿var strInput = "";
var tbInputID = "";
var tbOutputID = "";
var btnID = "";

function InitTimeComparison(theInputID, theOutputID, theButtonID) {
	try {
		tbInputID = theInputID;
		tbOutputID = theOutputID;
		btnID = theButtonID;
		setTimeout(TimeComparison, 500);
	}
	catch (e) {
	}
}

function TimeComparison() {
	//alert(tbInputID);
	try {
		//alert("strinput = " + strInput + " ; tbInput = " + document.getElementById(tbInputID).value);
		if (document.getElementById(tbInputID).value != strInput) {
			strInput = document.getElementById(tbInputID).value;
			document.getElementById(btnID).click();
			//__doPostBack(btnID, '');
		}
		setTimeout(TimeComparison, 300);
	}
	catch (e) {
	}
}

function ScrambleJS(strInput, strCurrentOutput) {
	try{
		var regex = new RegExp("[a-zA-Z]+|[^a-zA-Z]+", "g");
		//regex.compile(regex);
		var regexText = new RegExp("[a-zA-Z]+", "g");
		//regexText.compile(regexText);

		// begin Fill arrayIn
		var arrayIn = new Array();
		var indexIn = 0;
		var result = null;
		do {
			result = regex.exec(strInput);
			if (result != null) {
				arrayIn[indexIn++] = result.toString();
			}
		}
		while (result != null);
		var lengthIn = arrayIn.length;
		// end Fill arrayIn

		// begin Fill arrayOut
		regex.lastIndex = 0;
		var arrayOut = new Array();
		var indexOut = 0;
		result = null;
		do {
			result = regex.exec(strCurrentOutput);
			if (result != null) {
				arrayOut[indexOut++] = result.toString();
			}
		}
		while (result != null);
		var lengthOut = arrayOut.length;
		// end Fill arrayOut

		var strOutput = "";

		for (var i = 0; i < lengthIn; ++i) {
			if (regexText.test(arrayIn[i])) {
				var bScrambledCorrectly = 0;
				
				if (i < lengthOut){
					bScrambledCorrectly = IsScrambledCorrectly(arrayIn[i], arrayOut[i]);
				}

				if (bScrambledCorrectly == 1)
				{
					strOutput = strOutput + arrayOut[i];
				}
				else 
				{
					strOutput = strOutput + ScrambleOneWord(arrayIn[i]);
				}
			}
			else {
				strOutput = strOutput + arrayIn[i];
			}
		}
		return strOutput;
	}
	catch (e) {
		alert(e.description);
	}
}


function IsScrambledCorrectly(str1, str2) {
	var bScrambledCorrectly = 0;
	if (str1 != null && str2 != null && str1 != "" && str2 != "")
	{
		if (str1.length == str2.length) {
			if (str1.length <= 3) {
				if (str1 == str2) {
					bScrambledCorrectly = 1;
				}
			}
			else {
				if (str1 != str2 && str1.charAt(0) == str2.charAt(0) && str1.charAt(str1.length - 1) == str2.charAt(str2.length - 1)) {
					// begin Kijk of het middelste deel van str1 alle letters van het middelste deel van str2 bevat
					var arr1 = new Array();
					var arr2 = new Array();
					for (var i = 0; i < str1.length - 1; ++i) {
						arr1[i] = str1.charAt(i);
					}
					for (var j = 0; j < str2.length - 1; ++j) {
						arr2[j] = str2.charAt(j);
					}
					arr1.sort();
					arr2.sort();
					if (arr1.toString() == arr2.toString()) {
						bScrambledCorrectly = 1;
					}
					// end Kijk of het middelste deel van str1 alle letters van het middelste deel van str2 bevat
				}
			}
		}
	}

	return bScrambledCorrectly;
}

function ScrambleOneWord(strOrigWord) {
	// begin Length 1, 2 or 3
	if (strOrigWord.length < 4) {
		return strOrigWord;
	}
	// end Length 1, 2 or 3

	var strNewWord = "";
	
	// begin Length 4
	if (strOrigWord.length == 4)
	{
		strNewWord = strOrigWord.substr(0, 1) + strOrigWord.substr(2, 1) + strOrigWord.substr(1, 1) + strOrigWord.substr(3, 1);
		return strNewWord;
	}
	// end Length 4
	
	// begin Length 5 or more
	var strOrigMiddle = strOrigWord.substr(1, strOrigWord.length - 2);

	// begin Try five times to get another middle by a random function
	var strNewMiddle = strOrigMiddle;
	var iTry = 0;
	while (iTry++ < 5 && strOrigMiddle == strNewMiddle)
	{
		strNewMiddle = RndScrambleMiddle(strOrigMiddle);
	}
	// end Try five times to get another middle by a random function

	if (strOrigMiddle == strNewMiddle)
	{
		// begin Force to get another middle string without randomization
		// begin Set the last character on the first place
		strNewMiddle = strOrigMiddle.substr(strOrigMiddle.length - 1, 1);
		// end Set the last character on the first place

		// begin Set the 1st char on the 2nd place, the 2nd on the 3rd, and so on
		for (var index = 0; index < strOrigMiddle.length - 1; ++index)
		{
			strNewMiddle = strNewMiddle.concat(strOrigMiddle.charAt(index));
		}
		// end Set the 1st char on the 2nd place, the 2nd on the 3rd, and so on
		// end Force to get another middle string without randomization
	}

	strNewWord = strOrigWord.substr(0, 1);
	strNewWord = strNewWord.concat(strNewMiddle);
	strNewWord = strNewWord.concat(strOrigWord.substr(strOrigWord.length - 1));
	return strNewWord;
	// end Length 5 or more	
}

function RndScrambleMiddle(strWork) {
	var strOut = "";
	var iLength = strWork.length;

	for (var i = 0; i < iLength; ++i) {
		// begin Determine with random function which character must be on place i of the new string
		// return a random integer between [0, iLength- i[
		var iIndex = Math.floor(Math.random() * (iLength - i));
		
		var ch = strWork.charAt(iIndex);
		if (i < iLength - 1){
			if (iIndex == 0) {
				strWork = strWork.substr(1);
			}
			else if (iIndex == strWork.length - 1) {
				strWork = strWork.substr(0, iIndex);
			}
			else {
				strWork = strWork.substr(0, iIndex) + strWork.substr(iIndex + 1, strWork.length - iIndex - 1);
			}
		}
		// end Determine with random function which character must be on place i of the new string

		strOut = strOut.concat(ch); 
	}

	return strOut;
}

