// Global variables (WARNING):
var source = ""; // Text will be check
var dictionary = []; // Set of the correct words

/**
 * Read the file selected and it invoke the callback passed the content of file.
 * @param idFile {String}     Id input file
 * @param callback {Function} Function to be call when the reader have been read the file
 */
function selectFile(idFile,callback) {
    var file = document.getElementById(idFile).files[0];
    var reader = new FileReader();

    reader.onload = function (event) {
	// The file's text will be solved
	callback(event.target.result);
    };

    reader.readAsText(file);
}

/**
 * Set global variable dictionary with the content of the file selected.
 */
function selectDictionary() {
    selectFile("ckdictionary",function (text) {
	dictionary = JSON.parse(text).dictionary;
    });
}

/**
 * Set global variable source with the content of file selected.
 */
function selectSource() {
    selectFile("cksource",function (text) {
	source = text;
    });
}

/**
 * Return the select html element. The inside options will be set with the array values.
 * @param array {Array} Array of options
 * @return {HTMLSelect} A select element with the options of the array
 */
function getSelect(array) {
    var select = document.createElement("select");
    var option = undefined;	

    array.forEach( function (value) {
	option = document.createElement("option");
	option.value = value;
	option.text = value;
	
	select.appendChild(option);
    });

    return select;
}

function solve(text) {
    var regWord = /\w+/g;

    var controls = [];

    var words = source.match(regWord);
    var before = "";

    var start = 0;

    words.forEach( function (word) {
	var pullTarget = [];

	dictionary.forEach( function (target) {
	    var distance = getEditDistance(word.toLowerCase(),target);

	    if (distance > 0 && distance <= 2) {
		pullTarget.push(word,target);
	    }
	});

	if (pullTarget.length > 0) {
	    var chunk = {
		end: source.indexOf(word,start),
		mistake: pullTarget			
	    };

	    start = chunk.end + word.length;
	    
	    controls.push(chunk);
	}
    });

    start = 0;
    var result = document.getElementById("check");

    controls.forEach( function ( chunk ) {
	if ( chunk.end - start > 0 ) {
	    result.innerHTML = 
		result.innerHTML + source.substring(start,chunk.end);
	}	

	var choice = getSelect(chunk.mistake);
	result.appendChild(choice);

	start = chunk.end + chunk.mistake[0].length; 
    });

    if ( start > 0 ) {
	result.innerHTML = result.innerHTML + source.substring(start);
    }
}





@@ -1131,16 +1131,61 @@
 options%0A
+ * @param tag %7Bnumber%7D  End position of word%0A
  * @retu
@@ -1275,16 +1275,20 @@
 ct(array
+,tag
 ) %7B%0A    
@@ -1334,16 +1334,52 @@
 lect%22);%0A
+    select.setAttribute(%22tag%22,tag);%0A
     var 
@@ -1482,16 +1482,38 @@
 tion%22);%0A
+%09option.text = value;%0A
 %09option.
@@ -1531,32 +1531,8 @@
 ue;%0A
-%09option.text = value;%0A%09%0A
 %09sel
@@ -1587,16 +1587,593 @@
 ect;%0A%7D%0A%0A
+/**%0A * Return the string with the correction selected.%0A * @return %7Bstring%7D Correct text%0A */%0Afunction correct() %7B%0A    var list = document.querySelectorAll(%22div#check %3E select%22);%0A    var buffer = %22%22;%0A    var start = 0;%0A%0A    for ( var select of list ) %7B%0A%09var tag = select.getAttribute(%22tag%22);%0A%09var mistake = select.options%5B0%5D.value;%0A%09var replacement = select.options%5Bselect.selectedIndex%5D.value;%0A%09%0A%09// Substiture the word mistake with the work replacement.%0A%09buffer += source.substring(start,tag).split(mistake).join(replacement);%0A%09start = tag;%0A    %7D%0A%0A    console.log(buffer);%0A%7D%0A%0A%0A
 function
@@ -2352,24 +2352,129 @@
 on (word) %7B%0A
+%09// Append the uncorrect word in the array assure that it is the first %0A%09// option in the select element%0A
 %09var pullTar
@@ -2476,24 +2476,28 @@
 llTarget = %5B
+word
 %5D;%0A%0A%09diction
@@ -2594,16 +2594,49 @@
 rget);%0A%0A
+%09    // Append any likely matchs%0A
 %09    if 
@@ -2687,21 +2687,16 @@
 et.push(
-word,
 target);
@@ -2734,17 +2734,17 @@
 ength %3E 
-0
+1
 ) %7B%0A%09   
@@ -2752,24 +2752,13 @@
 var 
-chunk = %7B%0A%09%09end:
+pos =
  sou
@@ -2780,16 +2780,74 @@
 d,start)
+;%0A%0A%09    var chunk = %7B%0A%09%09end: pos,%0A%09%09tag: pos + word.length
 ,%0A%09%09mist
@@ -2912,21 +2912,16 @@
 length;%0A
-%09    
 %0A%09    co
@@ -3217,16 +3217,26 @@
 .mistake
+,chunk.tag
 );%0A%09resu

Add method to substiture the incorret words with the words matched in the dictionary