
	//Do you want to remember what keycode means what? Me neither.
	var TAB = 9;
	var ESC = 27;
	var KEYUP = 38;
	var KEYDN = 40;
	// aded by a.e
	var ENTER = 13;
	var BACKSPACE = 8;
	var SPACE = 32;
	// A pointer to the index of the highlighted item. -1 means nothing
	// highlighted.
	var highlighted = 1;

	// used to hold auto suggestions
	var autoSuggestDiv=null;
	
	// used in useSuggestions
	var suggestions = null;
	
	// used in traslateration/transformation
	var oldValue='';
	
	// used for getting cursor position
	var globalCursorPos;
	var globalStart;// put in positions array
	var globalEnd;// put in positions array
	// var option='0';//option 0==enumeration, 1==suggestions
	var mode ='translateration';
	
	var latinArray = new Array(7);
	var arabicArray = new Array(7);
	var p = 0;
	var selectionMade = 0;// used if space entered without selection being
							// made
	// var count = 0;//counts number of chars of current word. Is rest when user
	// hits space. Used when inserting arabic instead of latin -line 456
	var startToCursor = '';// comeback to understand. Used in getCursorPosition
							// and to position translateration menu
	// var correction ='0';//used when correction is made for translateration

	/***************************************************************************
	 * Helper function to determine the keycode pressed in a browser-independent
	 * manner.
	 **************************************************************************/
	function getKeyCode(ev)
	{
		if(ev)			// Moz
		{
			return ev.keyCode;
		}
		if(window.event)	// IE
		{
			return window.event.keyCode;
		}
	}

/*************************************
Process Input
*************************************/
function process1(choice,hintDiv,autoSuggestVar,suggestionULVar){
var value = document.getElementById(choice).value;// choice is a textfield
if(logha=='Ar') {
   getPossibilities(choice,autoSuggestVar,suggestionULVar);
} //end ar part

else {
      getSuggestions(choice,autoSuggestVar,suggestionULVar,'1');
	 }
}
//Review this function again.  Comeback.  maybe buggy
// processOnClick starts. Used in re-editing case


function processOnClick(choice,autoSuggestVar,suggestionULVar){
var value = document.getElementById(choice).value;// choice is a textfield
var currentWord = getCurrentWord(choice);
if(!((currentWord == '') || (currentWord == null))){
if(logha=='Ar') {
   //see if currentWord in arabicArray
   var matchFound = false;
   var index = -1;
   for(var l=0;((matchFound == false)&&(l<arabicArray.length));l++){
     if(arabicArray[l] == currentWord){
	    matchFound = true;
		// correction ='1';//important to signify correction will be made
		}
   }
   if(matchFound == true) {
   index=l-1;
   currentWord = latinArray[index];
    getPossibilities2(currentWord,choice,autoSuggestVar,suggestionULVar);
   } 
   else{
        getPossibilities(/*null*/choice,autoSuggestVar,suggestionULVar);// comeback
   }
}//if
}//if what was entered is nothing 
}//function


// end processOnClick ends

function getSuggestions(choice,autoSuggestVar,suggestionULVar,option)
{
	var query = document.getElementById(choice).value;
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
 	 {
 	 // code for IE6, IE5
 	 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 	 }
	else
	  {
	  alert("Your browser does not support XMLHTTP!");
 	 }
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
 	 {
 	 var suggestionsStr = xmlhttp.responseText;
 	 suggestions = suggestionsStr.split("^");
 	 // populate list
	 if(suggestionsStr.length > 0){ //when no results returned, suggestionsStr.length == 0 BUT suggestions.length == 1.
		highlighted = -1;
		autoSuggest(document.getElementById(choice),autoSuggestVar,suggestionULVar,option);
		}
	 else{
	     highlighted = -1;
		 suggestions = null;
		}
 	 }
	}
	var encodedQuery = '';
	if(logha == "Ar"){
	   encodedQuery = encodeURIComponent(query);
	   encodedQuery=encodedQuery.replace(/%/g,"_");// work around. come back
													// and evaluate
	   }
	else{
	   encodedQuery = query;
	   encodedQuery=encodedQuery.replace(/ /g,"*");// work around. come back
													// and evaluate
	}
	var url = "/yabhath/suggestion?sentence="+encodedQuery+"&"+"ms=" + new Date().getTime();
	xmlhttp.open("GET",url,true);// look into true/false in
									// ajax-async-synchronus
	xmlhttp.send(null);
}

////
// Following functiosn used to get cursor position
function setCursorPos(choice) {
 globalCursorPos = getCursorPos(document.getElementById(choice));
 if(globalCursorPos > 0)  globalCursorPos= globalCursorPos-1;
}


function getCursorPos(textElement) {
	if (document.selection) {
		var sOldText = textElement.value;
		var objRange = document.selection.createRange();
		var sOldRange = objRange.text;
		var sWeirdString = '#%~';
		objRange.text = sOldRange + sWeirdString; 
		objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));
		var sNewText = textElement.value;
		objRange.text = sOldRange;
		for (i=0; i <= sNewText.length; i++) {
			var sTemp = sNewText.substring(i, i + sWeirdString.length);
			if (sTemp == sWeirdString) {
				var cursorPos = (i - sOldRange.length);
				startToCursor = textElement.value.substring(0,cursorPos); // comeback
																			// to
																			// understand.
																			// Used
																			// to
																			// position
																			// translateration
																			// menu
				return cursorPos;// returns position of element after cursor
									// if -1 not present
			}
		}
	}else if (textElement.selectionStart || textElement.selectionStart == '0'){
		var cursorPos = textElement.selectionStart;
		startToCursor = textElement.value.substring(0,cursorPos);
		return cursorPos;
	}
}

//Comeback to understand function below.  Used in positioning small autocomplete list -translateration
function getWordForLeft(){
    var strArr = startToCursor.split(" ");
    var strWidthString = "";
    if(strArr.length > 2){
        for(var i=0;i<strArr.length - 1;i++){
            strWidthString = strWidthString + strArr[i] + " ";
        }
        return getWidth(strWidthString)+10;
    }else if(strArr.length==2){
        strWidthString = strWidthString + strArr[0] + " ";
        return getWidth(strWidthString)+10;
    }else{
        return 0;
    }
}

function getWidth(textElement)
{
    var spanElement = document.createElement('span');
    // spanElement.className = "textbox";
    /*spanElement.style.whiteSpace = "nowrap";
    spanElement.style.font = "bolder 12px Arial, Helvetica, sans-serif;";*/
    spanElement.innerHTML = textElement;
    document.getElementById("textSearchArea").appendChild(spanElement);
    var width = spanElement.offsetWidth;
    document.getElementById("textSearchArea").removeChild(spanElement);
    return width;
}


//
function getCharArray(strValue){
    var start=0;
    var end =0;
	
	start = globalCursorPos;
	end = globalCursorPos;
	var found = false;
	if(strValue.charAt(globalCursorPos) == ' ') {
	   currentWord =' ';
	   // start = globalCursorPos -1;
	   // end = globalCursorPos -1;
	   }
	  else {
    for(var i=start;((!found)&&(i>=0));i--){
        if((strValue.charAt(i) == ' ') || (strValue.charAt(i) == '')){
			found = true;
			
        }
    }
	if(found) start=i+2; else start=0;
	found = false;
    for(var j=end;((!found)&&(j<strValue.length));j++){
        if((strValue.charAt(j) == ' ') || (strValue.charAt(j) == '')){
			found = true;
        }
    }
	if(found) end = j-2; else end=strValue.length-1;
	currentWord = strValue.substring(start,end+1);
	globalStart = start;
	globalEnd = end;
	}//big else if not space
	positions = new Array(start,end,currentWord);// start and end not correct
													// in case of space.
													// Comeback
	return(positions);
}
function getCurrentWord(choice){
  var positions = getCharArray(document.getElementById(choice).value);
  var currentWord = positions[2];
  return(currentWord);
}
function getPossibilities(choice,autoSuggestVar,suggestionULVar)
{
    	var currentWord=null;
    if(choice == null) {clearSuggestionUL(suggestionULVar);}//This resolves bug: if onclick on autocompleted word, that word is sent back for possibilities
	else {
	//check if choice is string or control.  Used in re-edit case
	currentWord = encodeURIComponent(getCurrentWord(choice));
	currentWord=currentWord.replace(/%/g,"_");// work around. come back and
												// evaluate
	// see if currentWord is space
		        // alert('currentWord='+currentWord);
	if(currentWord ==' ') {
	       // alert('space');
			getSuggestions(choice,autoSuggestVar,suggestionULVar,'0');	
	}
	else{
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
 	 {
 	 // code for IE6, IE5
 	 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 	 }
	else
	  {
	  alert("Your browser does not support XMLHTTP!");
 	 }
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
 	 {
 	 var possibilitiesStr = xmlhttp.responseText;
	 suggestions = possibilitiesStr.split("^");
 	 if(suggestions.length > 1){//1 because latin is always there, suggestions.length is 1 at minimum
	   highlighted = 1;
	   }
	  else{
	   highlighted = 0;		
	  }
 	  autoSuggest(document.getElementById(choice),autoSuggestVar,suggestionULVar);
 	 }
	}
	var url = "/yabhath/translateration?sentence="+currentWord+"&"+"ms=" + new Date().getTime();
    xmlhttp.open("GET",url,true);// look into true/false in
									// ajax-async-synchronus
    xmlhttp.send(null);
   // xmlhttp.close();//
 }//else  not space
}//!=null
}
//Overload getPossibilities
function getPossibilities2(currentWord,choice,autoSuggestVar,suggestionULVar)
{
	   var word = encodeURIComponent(currentWord);
	   word = word.replace(/%/g,"_");// work around. come back and evaluate
	// see if currentWord is space
	if(word ==' ') {
			getSuggestions(choice,autoSuggestVar,suggestionULVar,option);	
	}
	else{
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
 	 {
 	 // code for IE6, IE5
 	 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 	 }
	else
	  {
	  alert("Your browser does not support XMLHTTP!");
 	 }
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
 	 {
 	 var possibilitiesStr = xmlhttp.responseText;
 	 if(possibilitiesStr.length > 1){
	    highlighted = 1;
	 }
	 else{
	    highlighted = 0;	 
	 
	 }
	     suggestions = possibilitiesStr.split("^");
 	 // populate list
 	 autoSuggest(document.getElementById(choice),autoSuggestVar,suggestionULVar,'0');
 	 }
	}
	var url = "/yabhath/translateration?sentence="+word+"&"+"ms=" + new Date().getTime();
    xmlhttp.open("GET",url,true);// look into true/false in
									// ajax-async-synchronus
    xmlhttp.send(null);
   }//else  not space
}
function getEnumeration(currentWord){
var enumeration =currentWord +"^"+currentWord+"1"+"^"+currentWord+"3"+"^"+currentWord+"4"+"^"+currentWord+"5";
return(enumeration);
}
function autoSuggest(choice,autoSuggestVar,suggestionULVar,option){
	//choice is textfield id
	clearSuggestionUL(suggestionULVar);
	createDiv(autoSuggestVar,suggestionULVar);
	positionDiv(choice,option);
	showDiv();
}

function clearSuggestionUL(suggestionULVar){
	var suggestionUL = document.getElementById(suggestionULVar);
	suggestionUL.innerHTML="";// come back and check other approach comeback
}
function createDiv(autoSuggestVar,suggestionULVar){
	autoSuggestDiv = document.getElementById(autoSuggestVar);
	var suggestionUL = document.getElementById(suggestionULVar);
	var countx = 0;
	if(logha != 'En'){suggestionUL.dir="rtl";}
	else{suggestionUL.dir="ltr";}
	if((mode == 'translateration') &&(logha != 'En')){
		var showDiv = document.createElement("div");
	    showDiv.id = "divShow";
	    suggestionUL.appendChild(showDiv);
	    var hiddenDiv = document.createElement("div");
	    hiddenDiv.id = "divHide";
	    suggestionUL.appendChild(hiddenDiv);
	    hiddenDiv.style.display = 'none';
	}
	//Create an array of LI-s for the words.
	for (i in suggestions)
	{
		var word = suggestions[i];

		var li = document.createElement('li');
		var a = document.createElement('a');
		if(logha=='Ar'){
			li.dir="rtl";
			li.style.textAlign="right";
			a.dir="rtl";
		}else{
			li.dir="ltr";
			li.style.textAlign="left";
			a.dir="ltr";
		}
		a.id = "suggestValue"+i;
		a.href="javascript:false";
		a.style.fontSize = "16px";
		a.innerHTML = word;
		li.appendChild(a);
		if (highlighted == i)
		{
			li.className = "selected";
		}
		if((mode == 'translateration') && (logha != 'En')){
		    if(countx<4)
			    showDiv.appendChild(li);
		    else
			    hiddenDiv.appendChild(li);
		    if(countx==3){
			    var li1 = document.createElement("li");
			    li1.id="showM";
			    var a1 = document.createElement('a');
			    a1.id = "suggestValueM";
		        a1.href="javascript:false";
		        a1.style.fontSize = "16px";
		        a1.style.color="brown";
		        a1.innerHTML = "+ &#1582;&#1610;&#1575;&#1585;&#1575;&#1578; &#1573;&#1590;&#1575;&#1601;&#1610;&#1577;";
		        if(logha=='Ar'){
					li1.dir="rtl";
					li1.style.textAlign="right";
					a1.dir="rtl";
				}else{
					li1.dir="ltr";
					li1.style.textAlign="left";
					a1.dir="ltr";
				}
		        li1.appendChild(a1);
			    showDiv.appendChild(li1);
		    }
		    countx++;
		}
		else suggestionUL.appendChild(li);
	}//for

		// /autoSuggestDiv.replaceChild(suggestionUL,autoSuggestDiv.childNodes[0]);///
		autoSuggestDiv.className="suggestion_list";
		autoSuggestDiv.style.position = 'absolute';// not sure. come back
		//
	}

	function positionDiv(elem,option){ //Come back to understand
		var el = elem;
		var x = 0;
		var y = el.offsetHeight;

		// Walk up the DOM and add up all of the offset positions.
		while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
		{
			x += el.offsetLeft;
			y += el.offsetTop;
			el = el.offsetParent;
		}

		x += el.offsetLeft;
		y += el.offsetTop;
		
		if((option == '1')||(logha == 'En')){
			autoSuggestDiv.style.left = x +'px';
			autoSuggestDiv.style.width = elem.offsetWidth - 10 +'px';// why
																		// need
																		// -10
																		// come
																		// back
			autoSuggestDiv.style.top = y +'px';
		}else{ //comeback to fix
		    var widthVal = 143;// variable to hold the width of the div
		    autoSuggestDiv.style.width='auto';// Setting the width property to
												// auto
		    var leftVal;// variable to hold the left position of the div
		    var subtractVal=autoSuggestDiv.offsetWidth;// varibale that has
														// current width of the
														// div
		    if(subtractVal < widthVal){//If current width is less than 143 px, make it always 143 pixel width
		        autoSuggestDiv.style.width = widthVal + 'px';
		        leftVal = x+elem.offsetWidth-widthVal-10-getWordForLeft();
		    }else if(subtractVal > (elem.offsetWidth - 50)){
		        leftVal = x - 350 - getWordForLeft();
		    }else{//else increase the size towards left
		        leftVal = x + elem.offsetWidth - subtractVal - getWordForLeft();
		    }
		    if(leftVal<=x-153){
		        leftVal = x-153;
		    }
			autoSuggestDiv.style.left = leftVal + 'px';// Setting the left
														// property of the div
			autoSuggestDiv.style.top = y +'px';	// Setting the top property of
												// the div
		}
	}

	 function showDiv(){
		autoSuggestDiv.style.display = 'block';
	}

	/********************************************************
	Insert the highlighted suggestion into the input box, and
	remove the suggestion dropdown.
	********************************************************/
	function useSuggestion(choice,extra) // come back understanding
	{
	 if(logha=='En'){
				document.getElementById(choice).value = suggestions[highlighted]+extra;
	 }
	 else{
		 if (highlighted != 'showM')
		{
			 if(mode == 'suggestion'){
			    if(highlighted != -1)
					document.getElementById(choice).value = suggestions[highlighted];
					globalCursorPos = (suggestions[highlighted]+extra).length;
			}
			else if((typeof suggestions == "object") && (suggestions!=null)){ 
				//var currentWordLength  = suggestions[0].length;
				var suggestion ='';
				suggestion = suggestions[highlighted]+extra;// extra typically
															// space. this is
															// easy method used
															// when space bar
															// used for
															// selections..
			   var currentValue = document.getElementById(choice).value;
			   var currentWord = getCurrentWord(choice);
			   if (currentWord == ' ') // this small exception where space bar
										// is used for selection. We need to
										// ignore space,afterward, and get
										// currentword. This is done by
										// decrementing globalCursorPos which is
										// used in getCharArray
			      {
				   globalCursorPos--;
				   currentWord = getCurrentWord(choice);
				  }
			    var s1 = currentValue.substring(0,globalStart);
				var s2 = suggestions[highlighted]+extra;
				var s3 = currentValue.substring((s1+currentWord).length);
				globalCursorPos = (s1+s2).length;// used later to set cursor
													// at end of
													// currentWord-important
				document.getElementById(choice).value = s1+s2+s3;
				if((highlighted > 0) &&(highlighted != 'showM')){
				arabicArray[p] = suggestion.trim();
				latinArray[p] = suggestions[0];
				p = p+1;
				}
		    }
		}
		if(mode == 'suggestion') mode='translateration';
		else if(mode == 'translateration') mode='suggestion';		
	  }//logha =='Ar'
		// highlighted = -1;//is it important that this is reset to -1. come
		// back
		suggestions=null;
		// setCursorAtEnd(choice);
		// calculculate where cursor should be placed (after selected word)
	  var genFunct = new AjandGenric();
	  genFunct.setAjandCursorWord(choice);
	}


	/********************************************************
	Modify the HTML in the dropdown to move the highlight.
	********************************************************/
	function changeHighlight(choice) // come back undrstanding
	{
		var lis = autoSuggestDiv.getElementsByTagName('LI');
		for(i in lis)
		{
			var li = lis[i];

			if (highlighted == i)
			{
				li.className = "selected";
			    // if(mode == 'suggestion')
				// document.getElementById(choice).value =
				// suggestions[highlighted];
			}
			else
			{
				li.className = "";
			}
		}
	}
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


