//========================== server connection ====================================//
//===============================================================================//

function GetXmlHttp()
{
	var x = null;
    
	try {
		x = new ActiveXObject("Msxml2.XMLHTTP");
	} 
    catch(e) {
    
		try {
			x = new ActiveXObject("Microsoft.XMLHTTP");
		}
        catch(oc) {
			x = null;
		}
    }
    
	if (!x && (typeof(XMLHttpRequest) != "undefined")) {
		x = new XMLHttpRequest();
	}    
	return x;
}

function IntGetXmlHttp()
{
   	// Load XmlHttp Component
   	s_XmlHttp = GetXmlHttp();
}

//========================== server connection ====================================//
//===============================================================================//




//========================== replace spelling =====================================//
//===============================================================================//

function set_input(val,rep)
{
	//logSpelling(rep,val);
	tmp = document.FormInvoer.invoer.value;
	fnd = new RegExp (rep, 'gi') ;
	tmp = tmp.replace(fnd,""+val);
	document.FormInvoer.invoer.focus();
	document.FormInvoer.invoer.value = tmp;
	window.Correction.style.display = "none";
	checkInput('FormInvoer');
	sugQ();
}

function set_question(zoekstring)
{
	//logSpelling(rep,val);
	document.FormInvoer.invoer.value = zoekstring;
	window.suggestQ.style.display = "none";
	document.FormInvoer.submit();
}

function changeWord()
{
	var temp = document.FormInvoer.selectWord.value;
	var tempArray=temp.split(",");
	
	rep = tempArray[0];
	val = tempArray[1];
	
	tmp = document.FormInvoer.invoer.value;
	fnd = new RegExp (rep, 'gi') ;
	tmp = tmp.replace(fnd,""+val);
	document.FormInvoer.invoer.value = tmp;
	window.Correction.style.display = "none";
	checkInput('FormInvoer');
	sugQ();
}

//========================== replace spelling =====================================//
//===============================================================================//




//========================== check input string ====================================//
//================================================================================//

function submitCheck()
{
	var zoekstring = document.FormInvoer.invoer.value;
	
	if(zoekstring == "")
	{
		alert("U heeft geen vraag ingevuld!");
		return false;
	}
	//alert(zoekstring);
	
	//var correct = checkVraag(zoekstring);
	var correct="1";
	//alert(correct);
	
	if(correct == "2")
	{
		//alert("scherm 1 a");
		document.FormInvoer.checkSpell.value=1;
		document.FormInvoer.submit();
	}
	else if(correct == "1")
	{
		document.FormInvoer.submit();
	}
	else
	{
		alert("Error: fout in controleren vraaginvoer.");
		return false;
	}
}

function checkVraag(zoekstring) 
{ 
    var response = null;
	var s = "checkvraag.php?q=" + zoekstring;
    var connection = new ActiveXObject("Microsoft.XMLHTTP"); 
    try 
    { 
        connection.open("GET", s, false); 
        connection.send(); 
        if(connection.readyState == 4) response = connection.responseText; 
    } 
    catch(e) 
    { 
        return false; 
    }
    return response;
} 

function addword(sugword)
{
	document.FormInvoer.stype.value = sugword;
	document.FormInvoer.submit()
}

function replaceword(sugword)
{
	document.FormInvoer.invoer.value = sugword;
	document.FormInvoer.submit()
}

//========================== check input string ====================================//
//================================================================================//




//========================== fuzzy analyser =======================================//
//===============================================================================//

function checkInput(formname)
{
	var zoekstring = document.FormInvoer.invoer.value;
	var old = document.FormInvoer.oldvalue.value;
	var suggest = document.FormInvoer.newvalue.value;
	var possibleText = 0;
	var stringlength = zoekstring.length;
	var noMark = 0;
	
	// check for leestekens
	for (var i = 0; i < zoekstring.length; i++) 
	{
		if (zoekstring.charAt(i) == '.' || zoekstring.charAt(i) == ',' || zoekstring.charAt(i) == '!' || zoekstring.charAt(i) == '?' || zoekstring.charAt(i) == '\;' || zoekstring.charAt(i) == ':') 
		{
			possibleText++;
		}
	}
	
	// it >= 2 leestekens or length of zoekvraag > 75, ask for shorter question
	if(stringlength > 100)
	{
		var remark2 = "<br><font color=\"red\">"+msg_telang+"</font><br>&nbsp;";
		window.document.getElementById("Correction").innerHTML = remark2;
		window.Correction.style.display = "";
	}
	else if(possibleText >= 1 && stringlength > 75)
	{
		var remark2 = "<br><font color=\"red\">"+msg_telang+"</font><br>&nbsp;";
		window.document.getElementById("Correction").innerHTML = remark2;
		window.Correction.style.display = "";
	}
	else if(possibleText >= 2 && stringlength > 50)
	{
		var remark2 = "<br><font color=\"red\">"+msg_telang+"</font><br>&nbsp;";
		window.document.getElementById("Correction").innerHTML = remark2;
		window.Correction.style.display = "";
	}
	else if(possibleText >= 3 && stringlength == possibleText)
	{
		var remark2 = "<br><font color=\"red\">"+msg_leestekens+"</font><br>&nbsp;";
		window.document.getElementById("Correction").innerHTML = remark2;
		window.Correction.style.display = "";
	}
	else if(possibleText >= 5)
	{
		var remark2 = "<br><font color=\"red\">"+msg_teveelleestekens+"</font><br>&nbsp;";
		window.document.getElementById("Correction").innerHTML = remark2;
		window.Correction.style.display = "";
	}
	else
	{
		 noMark = 1;
	}
	/*
	var wordcheck;
	var stringlength = zoekstring.length;
	var lastcharacter = zoekstring.substr(stringlength-1,1);
	
	// clean string
	zoekstring = zoekstring.replace("{","");
	zoekstring = zoekstring.replace("/","");
	zoekstring = zoekstring.replace("\\","");
	zoekstring = zoekstring.replace(":","");
	zoekstring = zoekstring.replace(";","");
	zoekstring = zoekstring.replace("#","");
	zoekstring = zoekstring.replace("*","");
	
	if(lastcharacter == " " || lastcharacter == "." || lastcharacter == "?" || lastcharacter == "!" || lastcharacter == ",")
	{
		// check complete string, only loop untill a word has a improvement
		var loopTrue = true;
		var zoekstringtemp = zoekstring;
		var templength = stringlength;
		
		while(loopTrue)
		{
			var lastwordcount = GetLastWord(zoekstringtemp);
			//alert(lastwordcount);
			var lastword = zoekstringtemp.substr(templength-lastwordcount-1,lastwordcount);
			//alert(lastword);
			
			var templength = templength-lastwordcount-1;
			//alert(startpoint);
			
			zoekstringtemp = zoekstring.substr(0,templength);
			//alert(zoekstringtemp);
			if(zoekstringtemp == "" || zoekstringtemp == " ")
			{
				loopTrue = false;
			}
			
			// clean string
			lastword = lastword.replace("{","");
			lastword = lastword.replace("/","");
			lastword = lastword.replace("\\","");
			lastword = lastword.replace(":","");
			lastword = lastword.replace(";","");
			lastword = lastword.replace("#","");
			lastword = lastword.replace("*","");
			
			// get length
			var lastlength = lastword.length;
			
			// must be more than 3 character and not a number
			if(lastlength > 3 && isNaN(lastword))
			{
				// check if word is known
				var newword = checkWord(lastword);
				var old = document.FormInvoer.oldvalue.value=lastword;
				var neww = document.FormInvoer.newvalue.value=newword;
				
				if(newword == "" || newword == " ")
				{
					// unknown word, exit loop
					loopTrue = false;
					var remark;
					remark = "<br><font color=\"red\">&nbsp;Het woord <em><strong>"+lastword+"</strong></em> is niet bekend.</font><br>&nbsp;";
					
					window.document.getElementById("Correction").innerHTML = remark;
					window.Correction.style.display = "";
					//document.formname.oldvalue.value=lastword;
					//document.formname.newvalue.value=newword;
					noMark = 0;
				}
				else if(newword != lastword)
				{
					// new word is given
					loopTrue = false;
					var remark;
					
					var chgCount =  newword.substr(0,1);
					var newlength = newword.length;
					//alert(newword);exit;
					newword = newword.substr(1,newlength);
					if(chgCount == 1)
					{
						// one new spellword
						remark = "<a href=\"javascript:set_input('"+newword+"','"+lastword+"');\"><font color=\"red\">&nbsp;Klik hier indien u <em>"+ newword + "</em> bedoelt, in plaats van <em><strong>" + lastword + "</strong></em></font></a>";
					}
					else
					{
						remark = "<font color=\"red\">&nbsp;Bedoelt u misschien <select name=\"selectWord\" onChange=\"changeWord();\"><option selected value=\"\">--selecteer verbetering--";
						
						var tempArray=newword.split(",");
						var tempWord;
						for (var ii = 0; ii < chgCount; ii++) 
						{
							tempWord = tempArray[ii];
							remark = remark+"<option value=\""+lastword+","+tempWord+"\">"+tempWord;
						}
						remark = remark+"</select>, in plaats van <em><strong>" + lastword + "</strong></em></font>";
					}
					window.document.getElementById("Correction").innerHTML = remark;
					window.Correction.style.display = "";
					//document.formname.oldvalue.value=lastword;
					//document.formname.newvalue.value=newword;
					noMark = 0;
				}
				else if(old != lastword && suggest != "")
				{
					window.Correction.style.display = "none";
					//document.formname.oldvalue.value= "";
					//document.formname.newvalue.value="";
					noMark = 0;
				}
			}
			if(noMark == 1)
			{
				window.Correction.style.display = "none";
				window.suggestQ.style.display = "none";
				noMark = 0;
			}
		}
	}
	else if(zoekstring.length < 2)
	{
		window.Correction.style.display = "none";
		window.suggestQ.style.display = "none";
		var old2 = document.FormInvoer.oldvalue.value="";
		var neww2 = document.FormInvoer.newvalue.value="";
		//document.formname.oldvalue.value= "";
		//document.formname.newvalue.value="";
		noMark = 0;
	}*/
}

function Reverse(strToReverse)
{
 var strRev = new String;
 var i = strToReverse.length;

 while (i--)
  strRev += strToReverse.charAt(i);

 return strRev;
}

function GetLastWord(szLine)
{
	// Return the last word of a list on space separated words
    /*var i, j = 0, szWord = "";
    
    while ((i = szLine.indexOf(" ")) != -1) {
    	szWord = szLine.substr(0, i);
        szLine = szLine.substr(i + 1);
    }*/
	var i;
	var szWord = "";
	var rev = Reverse(szLine);
	var rev2 = rev.substr(1,rev.length-1);

	// loop through it to find next space
	for(i=0;i<rev2.length;i++)
 	{
		testChar = rev2.substr(i,1)
		if(testChar == " " || testChar == "." || testChar == "?" || testChar == "!" || testChar == ",")
		{
			 var szWord = Reverse(szWord);
			 return i;
		}
		else
		{
			szWord = szWord+testChar;
		}
    }
	var szWord = Reverse(szWord);
  	return i;
}

function checkWord(word) 
{ 
    var response = null;
	var s = "wordexists.php?w=" + word;
    var connection = new ActiveXObject("Microsoft.XMLHTTP"); 
    try 
    { 
        connection.open("GET", s, false); 
        connection.send(); 
        if(connection.readyState == 4) response = connection.responseText; 
    } 
    catch(e) 
    { 
        return false; 
    }
    return response;
} 

//========================== fuzzy analyser =======================================//
//===============================================================================//




//========================== suggest question =====================================//
//===============================================================================//

function sugQ()
{
	var zoekstring = document.FormInvoer.invoer.value;
	
	// clean string
	zoekstring = zoekstring.replace("{"," ");
	zoekstring = zoekstring.replace("/"," ");
	zoekstring = zoekstring.replace("\\"," ");
	zoekstring = zoekstring.replace(".","");
	zoekstring = zoekstring.replace(",","");
	zoekstring = zoekstring.replace(":"," ");
	zoekstring = zoekstring.replace(";"," ");
	zoekstring = zoekstring.replace("#"," ");
	zoekstring = zoekstring.replace("!","");
	zoekstring = zoekstring.replace("*"," ");
	zoekstring = zoekstring.replace("?","");
	zoekstring = zoekstring.replace(" ","");
	zoekstring = zoekstring.replace("  ","");
	zoekstring = zoekstring.replace("   ","");
	zoekstring = zoekstring.replace("    ","");
	zoekstring = zoekstring.replace("     ","");
	zoekstring = zoekstring.replace("      ","");
	var stringlength = zoekstring.length;
	var lastChar = zoekstring.charAt(stringlength-1);
	
	if(stringlength > 2 && lastChar == " ")
	{
		var newVraag = suggestQuestion(zoekstring);
		var checkString = zoekstring+"?";
		
		if(newVraag != "" && newVraag != checkString)
		{
			
			var vraaglength = newVraag.length;
			if(vraaglength > 27)
			{			
				// show suggestQuestion
				remark = "<br><font color=\"#000000\">&nbsp;Bedoelt u misschien de vraag: </font><br>&nbsp;"+newVraag;
				window.suggestQ.style.display = "";
				window.document.getElementById("suggestQ").innerHTML = remark;
			}
			else
			{
				// show suggestQuestion
				remark = "<br><font color=\"#000000\">&nbsp;Bedoelt u misschien de vraag: </font> "+newVraag;
				window.suggestQ.style.display = "";
				window.document.getElementById("suggestQ").innerHTML = remark;
			}
		}
		else
		{
			window.document.getElementById("suggestQ").innerHTML = "";
			window.suggestQ.style.display = "none";
		}
	}
}

function suggestQuestion(zoekstring) 
{ 
    var response = null;
	var s = "suggestquestion.php?v=" + zoekstring;
    var connection = new ActiveXObject("Microsoft.XMLHTTP"); 
    try 
    { 
        connection.open("GET", s, false); 
        connection.send(); 
        if(connection.readyState == 4) response = connection.responseText; 
    } 
    catch(e) 
    { 
        return false; 
    }
    return response;
}

//========================== suggest question =====================================//
//===============================================================================//




//========================== log use of spelling ===================================//
//===============================================================================//

function logSpelling(woord,verbetering) 
{ 
    var logstring = woord+" "+verbetering;
	var s = "logspelling.php?ls=" + logstring;
    var connection = new ActiveXObject("Microsoft.XMLHTTP"); 
    try 
    { 
        connection.open("GET", s, false); 
        connection.send(); 
        if(connection.readyState == 4) response = connection.responseText; 
    } 
    catch(e) 
    { 
        return false; 
    }
    return true;
}  

//========================== log use of spelling ===================================//
//===============================================================================//



