/* This code is for handling PHONETIC layout */

/*  This javascript code captures events on a textArea or a textField.
    The key character is captured and converted to Unicode for display. */

			/**
			 * According to WX Notation, if any of "A,e,eV,EY,i,I,o,oV,O,OY,u,U,q,Q,z,M,H" 
			 * is preceeded by 'a', then the resultant character is a vowel.
			 * If any of "A,e,eV,EY,i,I,o,oV,O,OY,u,U,q,Q,z,M,H" is preceeded by any character 
			 * other than 'a' then the resultant character is  "Matra"(vowel sign).
			 */	
			var vowelFlag = false;

			/**
			 * This flag is used when Netscape or Mozilla browser is used.
			 * A vowel is produced by two or three key combinations.
			 */
			var vowel_nn = false;

			/**
			 * In Unicode standard, the Indian Language code pages are placed with an offset
			 * of 128. So, depending on the language selected from the drop down list, the  
			 * offset is calculated.This offset when added to the code point of Devanagari script 
			 * gives the appropriate language character.
			 */
			var offset;

			/**
			 * This flag determines, whether to convert the key to an Indian Language Character 
			 * or not. This flag becomes true when user selects any of the Indian Languages from the
			 * combo.
			 */
			var convertFlag = false;

			/**
			 * The ISCII to Unicode Mapping for Devanagari script.
			 * In WX Notation, the two keys 'V' and 'Y' function as navigators. Typing 'V' 
			 * after some character will give the previous character of the current character 
			 * in the ISCII code chart and 'Y' will give the next 
			 * character of the current character in the ISCII code chart.
			 */
			var isciiToUnicodeArray = new Array();

			/**
			 * This variable will be true, if the page is viewed either in Mozilla or Netscape.
			 */
			var isNetscape = false;

			/**
			 * 'windowObject' is a variable reference to the window in which the mappings is displayed.
			 * 'languageIndex' is the index of the language selected from the combo box.
			 * From the languageIndex we compute the offset.
			 */
			var windowObject,languageIndex;
			
			/**
			 * Netscape and Mozilla has very poor rendering capability for Indian Languages.
			 * The small 'i' matra (vowel sign) when typed after a consonant should be prefixed 
			 * to the character, instead appears after the consonant which is wrong. 
			 * This flag is used to swap 'i' matra with the previous character to take it 
			 * to the correct position.(Only for Devanagari script)
			 */
			var imatraFlag = false;

			/**
			 * These are the variables used to determine the range from which the characters 
			 * have to be replaced.
			 * These variables are used only for Netscape and Mozilla.
			 */
			var oldSelectionStart,oldSelectionEnd;

			/**
			 * Initializes the ISCII to Unicode Array.
			 */
			initializeArray();

			/**
			 * This piece of code is used to capture the KEYPRESS event on all the form 
			 * elements in the document.
			 */
			if(isNetscape) {
			    //Only for Netscape.
				document.captureEvents(Event.KEYPRESS | Event.FOCUS);
			}	
		  document.onkeypress = eventCaptured;// For IE 
			
			/**
			 * This function captures the Keypress event and handles the event accordingly.
			 */
			function eventCaptured(evt) {
				if(!isNetscape) {
					// If the from element which generated this event is either 
					// TextArea or TextField, then handle event otherwise ignore.
					if(window.event.srcElement.type=="textarea" ||
						window.event.srcElement.type=="text") {
							handleCharacter(window.event.srcElement,window.event,replaceKeyChar);
					}
				}
				else {
					if(evt.target.type=="textarea" || evt.target.type=="text") {
				  		handleCharacter(evt.target,evt,replaceKeyChar);
					}
				}	
			}

		/**
		 * Initializes the ISCII to Unicode Array.
		 */
		function initializeArray() {
			isciiToUnicodeArray.push(2305); // ISCII 161
			isciiToUnicodeArray.push(2306); // ISCII 162
			isciiToUnicodeArray.push(2307); // ISCII 163
			isciiToUnicodeArray.push(2309); // ISCII 164
			isciiToUnicodeArray.push(2310); // ISCII 165
			isciiToUnicodeArray.push(2311); // ISCII 166
			isciiToUnicodeArray.push(2312); // ISCII 167
			isciiToUnicodeArray.push(2313); // ISCII 168
			isciiToUnicodeArray.push(2314); // ISCII 169
			isciiToUnicodeArray.push(2315); // ISCII 170
			isciiToUnicodeArray.push(2318); // ISCII 171
			isciiToUnicodeArray.push(2319); // ISCII 172
			isciiToUnicodeArray.push(2320); // ISCII 173
			isciiToUnicodeArray.push(2317); // ISCII 174
			isciiToUnicodeArray.push(2322); // ISCII 175
			isciiToUnicodeArray.push(2323); // ISCII 176
			isciiToUnicodeArray.push(2324); // ISCII 177
			isciiToUnicodeArray.push(2321); // ISCII 178
			var uniHinCons = 2325;
			for(var i=180;i<=223;i++) {
				if(i == 217) {
					isciiToUnicodeArray.push(0);
					uniHinCons = 2366;
				}
				isciiToUnicodeArray.push(uniHinCons);
				uniHinCons++;
			}
			isciiToUnicodeArray.push(2374); // ISCII 224
			isciiToUnicodeArray.push(2375); // ISCII 225
			isciiToUnicodeArray.push(2376); // ISCII 226
			isciiToUnicodeArray.push(2373); // ISCII 227
			isciiToUnicodeArray.push(2378); // ISCII 228
			isciiToUnicodeArray.push(2379); // ISCII 229
			isciiToUnicodeArray.push(2380); // ISCII 230
			isciiToUnicodeArray.push(2377); // ISCII 231
			isciiToUnicodeArray.push(2378); // ISCII 232
			isciiToUnicodeArray.push(2364); // ISCII 233
			isciiToUnicodeArray.push(2404); // ISCII 234
			for(var i=235;i<=240;i++) {
				isciiToUnicodeArray.push(0);
			}
			var hinUnicodeDigits = 2406;  
			for(var i=241;i<=250;i++) {
				isciiToUnicodeArray.push(hinUnicodeDigits);
				hinUnicodeDigits++;
			}
			if(parseInt(navigator.appVersion)>=4) {
				if(navigator.appName=="Netscape") {
					isNetscape = true;
				}
			}
			else 
				isNetscape = false;
		}		
			
		/**
		 * This function handles the character which is keyed in and replaces it with the
		 * appropriate Indian Language character.
		 */
		function handleCharacter(textControl, event, keyChecker) {
			if(convertFlag) {
				var keyCode = event.keyCode ? event.keyCode :
							event.charCode ? event.charCode :
							event.which ? event.which : void 0;
                                 
				var key = String.fromCharCode(keyCode);
				if(! ( keyCode == 8 || keyCode ==13 || keyCode == 47 ||
				keyCode == 96 || (keyCode >=32 && keyCode <=45) || 
				(keyCode >=58 && keyCode <=64) || (keyCode >=91 &&
				keyCode <=94) || (keyCode >122 && keyCode <=126) || 
				(keyCode == 86) || (keyCode == 89)
				) ) //if
				{	 
					var keyCheck = keyChecker(keyCode);
					//If IE
					if (keyCode && window.event && !window.opera) {
						if (keyCheck.replaceKey) { 
							window.event.keyCode = keyCheck.newKey.charCodeAt(0);
							if(keyCheck.newKey.length == 2) {
								textControl.value = textControl.value +
								keyCheck.newKey.charAt(1);
							}
							if (window.event.preventDefault) { 
								window.event.preventDefault();
							}
							return true;
						}
					}//if
					//If netscape
					else if (typeof textControl.setSelectionRange != 'undefined') {
						if (keyCheck.replaceKey) {
				    	// cancel the key event and insert the newKey for the current selection
       	 			if (event.preventDefault) {
								// Don't print the character of the key typed after printing the new
								// converted Indian Language character
								event.preventDefault();
							}
							oldSelectionStart = textControl.selectionStart;
							oldSelectionEnd = textControl.selectionEnd;
							var selectedText = textControl.value.substring(
							oldSelectionStart,oldSelectionEnd);
						  var newText = typeof keyCheck.newKey != 'undefined' ?
						  keyCheck.newKey.charAt(0) : String.fromCharCode(
							keyCheck.newKey.charCodeAt(0));
							if(keyCheck.newKey.length == 2) {
								textControl.value = textControl.value.substring(0, 
								oldSelectionStart -1) + keyCheck.newKey.charAt(1) + 
								newText + textControl.value.substring(oldSelectionEnd);
							}
							// If the current character converted is a Vowel.
						  else if(vowel_nn) { 
							  textControl.value = textControl.value.substring(0,
								(oldSelectionStart-1)) +  newText + textControl.value.substring(
								oldSelectionEnd);
							  vowel_nn = false;
						  }
						  else {
							  textControl.value = textControl.value.substring(0, 
								oldSelectionStart) +  newText + textControl.value.substring(
								oldSelectionEnd);
							}
							textControl.setSelectionRange(oldSelectionStart + newText.length,
						  oldSelectionStart + newText.length);
							// If small 'i' maatra is the current character and the 
							// Indian Language selected
							// is Hindi, then swap this character with the previous character 
							// in the textControl.
							if(imatraFlag) {
								var lastChar = textControl.value.substring(
													textControl.value.length-1,
													textControl.value.length);
								var previousToLastChar = textControl.value.substring(
													textControl.value.length-2,
													textControl.value.length-1);
								textControl.value = textControl.value.substring(
														0,textControl.value.length-2)+
													lastChar + previousToLastChar;
								imatraFlag = false;
							}
						  return false;
						}
						else {
							return true;
						}    
					}
				}
				// if not special symbols
				// If the key pressed is either 'V' or 'Y', these are navigator keys 
				// in WX Notation as mentioned at the top.
				else if( keyCode == 86 || keyCode == 89) {
					var key = String.fromCharCode(keyCode);
					var textAreaContent = textControl.value;
					var lastCharCode = textAreaContent.charCodeAt(
									textAreaContent.length-1,textAreaContent.length);
					lastCharCode = lastCharCode - offset;
					isciiCode = getIsciiCode(lastCharCode);
					switch(key) {
						case 'V': {
							--isciiCode;
							unicode = getUnicodeFromIsciiCode(isciiCode);
							break;
						}
						case 'Y':{
							isciiCode++;
							unicode = getUnicodeFromIsciiCode(isciiCode);
							break;
						}
					}
					textControl.value = textControl.value.substring(
							0,textControl.value.length-1) +
							String.fromCharCode(unicode+offset);
					if(!isNetscape) {
						window.event.keyCode = 0;
					}
					else {
						event.preventDefault();
					}
				}
				else {
					if(vowelFlag) {
						var ch = String.fromCharCode((2309+offset));
						if(!isNetscape) {
							textControl.value = textControl.value + ch;
						}
						else{
							textControl.value = textControl.value.substring(0,
								oldSelectionStart) +  ch ;
						}
						vowelFlag = false;
					}
				}
			}
		}

		/**
		 * Returns the Unicode value of the isciicode passed.
		 */
		function getUnicodeFromIsciiCode(isciiCode) {
			isciiCode = isciiCode - 161;
			return isciiToUnicodeArray[isciiCode];
		}
		
		/**
		 * Returns the iscii code of the unicode value passed.
		 */	
		function getIsciiCode(unicode) {
			var isciiCode=0;
			for(var ai=0;ai<isciiToUnicodeArray.length;ai++) {
				if(isciiToUnicodeArray[ai] == unicode) {
					isciiCode = ai + 161;	
			    break;
				}
			}
			return isciiCode;
		}

		/**
		 * This function replaces the key typed in with the new Indian Language Character.
		 */
		function replaceKeyChar(keyCode) {
				var unicodeCharacters = getUnicode(keyCode);
				if(unicodeCharacters.newKeyCode1==0) {
					newChar = String.fromCharCode(unicodeCharacters.newKeyCode2);
				}
				else {
					newChar = String.fromCharCode(unicodeCharacters.newKeyCode1) + 
					String.fromCharCode(unicodeCharacters.newKeyCode2);
				}
				return { replaceKey:true, newKey:newChar };  
		}

		/**
		 * Returns the unicode corresponding to key typed in.
		 */
		function getUnicode(keyCode) {
				var key = String.fromCharCode(keyCode);
				if(vowelFlag) {
					vowel_nn = navigator.appName=="Netscape" ?true : false ;
					vowelFlag = false;
					switch(key) {
						case 'A': { 
								return {newKeyCode1:0, newKeyCode2:(2310+offset)}; // Vowel "AA"
								break;
						}
						case 'i': {
								return { newKeyCode1:0, newKeyCode2:(2311+offset) }; // Vowel "I"
								break;
						}
						case 'I': { 
								return { newKeyCode1:0, newKeyCode2:(2312+offset) }; // Vowel "II"
								break;
						}
						case 'u': { 
								return { newKeyCode1:0, newKeyCode2:(2313+offset) }; // Vowel "U"
								break;
						}
						case 'U': { 
								return { newKeyCode1:0, newKeyCode2:(2314+offset) };// Vowel "UU"
								break;
						}
						case 'q': { 
								if(offset != 640) {
									return { newKeyCode1:0, newKeyCode2:(2315+offset) }; // Vowel "VOCALIC R"
								}
								else {
									if(isNetscape) {
										return { newKeyCode1:0,newKeyCode2:-1};
									}
									else {
										return { newKeyCode1:0,newKeyCode2:0};
									}
								}  
								break;
						}
						case 'L': { 
								if(offset != 640) {
									return { newKeyCode1:0, newKeyCode2:(2316+offset) }; // Vowel "VOCALIC L"
								}
								else{
									if(isNetscape) {
										return { newKeyCode1:0, newKeyCode2:-1 }; 
									}
									else {   
										return { newKeyCode1:0,newKeyCode2:0};
									}
								}
								break;
						}
						case 'e': { 
								return { newKeyCode1:0, newKeyCode2:(2319+offset) }; // Vowel "E"
								break;
						}
						case 'E': { 
								return { newKeyCode1:0, newKeyCode2:(2320+offset) }; // Vowel "EE"
								break;
						}
						case 'o': { 
								return { newKeyCode1:0, newKeyCode2:(2323+offset) }; // Vowel "O"
								break;
						}
						case 'O': { 
								return { newKeyCode1:0, newKeyCode2:(2324+offset) }; // Vowel "OO"
								break;
						}
						case 'Q': {	
								if(offset != 640) { // if input language is not Tamil
									return { newKeyCode1:0, newKeyCode2:(2400+offset) }; // Vowel "VOCALIC RR"
								}
								break;
						}
						default : { 
							return { 
								newKeyCode1:getConsonant(keyCode), newKeyCode2:(2309+offset)
							};
							break;
						}
					}
				}
				else {
					if(isNetscape) vowel_nn=false;
					var newKeyCode = getConsonant(keyCode);
					return { newKeyCode1:0, newKeyCode2:newKeyCode };
				}
		}
	
		/**
		 * Returns the Consonant of the selected Indian Language that the keycode maps to.
		 */
		function getConsonant(keyCode) {
			var key = String.fromCharCode(keyCode);
			switch(key) {	
				case 'a': { 
					vowelFlag = true; 
		      if(isNetscape) {
						return ; 
					} 
					else if(navigator.appName == "IE") {
						return 0;
					}   
					break; 
				} 
				case 'Z': { 
						// If the input Language is either Tamil or Kannada
					if( (offset != 640) && (offset != 896) ) {
						return (2305+offset);
					}
					else {
						if(isNetscape) {
							return -1;
						}
						else {  
							return 0;
						}
					}
					break; 
				} // "chandabindu"
				case 'M': { 
					return (2306+offset); 
					break; 
				} // DEVANAGARI CHARACTER "anuswar"
				case 'H': { 
					return(2307+offset); 
					break; 
				} // DEVANAGARI CHARACTER "visarg"
				case 'k': { 
					return(2325+offset); 
					break; 
				} // DEVANAGARI CHARACTER "ka"
				case 'K': {
					if(offset == 640) {	
						return(2325+offset);
					}
					else {
						return(2326+offset); // "kha"
					}
				break; 
				} 
				case 'g': { 
					if(offset == 640) {
						return(2325+offset);
					}
					else {
						return(2327+offset); //  "ga"
					}
				break; 
				} 
				case 'G': { 
					if(offset == 640) {
						return(2325+offset);
					}
					else {
						return(2328+offset); //  "gha"
					}
				break; 
				} 
				case 'f': { 
					return(2329+offset); //"nga"
					break; 
				} 
				case 'c': { 
					return(2330+offset); // "ca"
					break; 
				} 
				case 'C': { 
					if(offset == 640 ) { // if the input language is Tamil.
						return(2330+offset); // ca and cha are same in Tamil.
					}
					else {
						return(2331+offset);   // "cha"
					}
				break; 
				} 
				case 'j': { 
					return(2332+offset); // "ja"
					break; 
				} 
				case 'J': { 
					if(offset == 640) {
						return(2332+offset); // in tamil ja and jha are same.
					}
					else {
						return(2333+offset); //  "jha"
					}
					break; 
				} 
			case 'F': { 
					return(2334+offset); // "nya"
					break; 
				} 
				case 't': { 
					return(2335+offset); // "tta"
					break; 
				} 
				case 'T': { 
					if(offset == 640) {
						return(2335+offset); // in tamil tta and ttha are same.
					}
					else {
						return(2336+offset); //  "jha"
					}
					break; 
				} 
				case 'd': { 
					if(offset == 640) {
						return(2335+offset); // in tamil tta,ttha,dda are same.
					}
					else {
						return(2337+offset); //  "jha"
					}
					break; 
				} 
				case 'D': {
					if(offset == 640) {
						return(2335+offset); // in tamil tta,ttha,dda,ddha are same.
					}
					else {
						return(2338+offset); //  "ddha"
					}
					break; 
				}
				case 'N': { 
					return(2339+offset); // "nna"
					break; 
				} 
				case 'w': { 
					return(2340+offset); // DEVANAGARI CHARACTER "ta" 
					break; 
				} 
				case 'W': { 
					if(offset == 640) {
						return(2340+offset); // in tamil ta and tha are same.
					}
					else {
						return(2341+offset); //  "tha"
					}
					break; 
				}
				case 'x': {
					if(offset == 640) {
						return(2340+offset); // in tamil ta,tha,da are same.
					}
					else {
						return(2342+offset); //  "da"
					}
					break; 
				}			
				case 'X': { 
					if(offset == 640) {
						return(2340+offset); // in tamil ta,tha,da and dha are same.
					}
					else {
						return(2343+offset); //  "dha"
					}
					break; 
				}
				case 'n': { 
					return(2344+offset); // "na"
					break; 
				} 
				case 'p': { 
					return(2346+offset); // "pa"
					break; 
				} 
				case 'P': {
					if(offset == 640) {
						return(2346+offset); // in tamil pa,pha are same.
					}
					else {
						return(2347+offset); //  "dha"
					}
					break; 
				}
				case 'b': {
					if(offset == 640) {
						return(2346+offset); // in tamil pa,pha,ba are same.
					}
					else {
						return(2348+offset); //  "ba"
					}
					break; 
				}			
				case 'B': { 
					if(offset == 640) {
						return(2346+offset); // in tamil pa,pha,ba & bha are same.
					}
					else {
						return(2349+offset); //  "bha"
					}
					break; 
				}			
				case 'm': { 
					return(2350+offset); // "ma"
					break; 
				} 
				case 'y': { 
					return(2351+offset); // "ya"
					break; 
				} 
				case 'r': { 
					return(2352+offset); // "ra"
					break; 
				} 
				case 'l': { 
					return(2354+offset); // "la"
					break; 
				} 
				case 'v': { 
					return(2357+offset); // "va"
					break; 
				} 
				case 'S': { 
					if(offset != 640) { //if the input language is not tamil.
						return(2358+offset); // "Sha"
					}
					break; 
				} 
				case 'R': { 
					return(2359+offset); // "SSa"
					break; 
				} 
				case 's': { 
					return(2360+offset); // "Sa"
					break; 
				} 
				case 'h': { 
					return(2361+offset); // "ha"
					break; 
				} 
				case 'z': { 
					if(offset < 640) { // 'Nukta' will not be in south indian scripts.
						return(2364+offset);
					}
					break; 
				} //  "Nukta" (only in North indian scripts).
				case 'A': { 
					return(2366+offset); // "aA"
					break; 
				} 
				case 'i': { 
					if(isNetscape && offset==0) {
						imatraFlag = true;
					}
					return(2367+offset); // Vowel Sign "ii"
					break; 
				} 
				case 'I': { 
					return(2368+offset); // Vowel Sign "iI"
					break; 
				} 
				case 'u': { 
					return(2369+offset); // Vowel Sign "u"
					break; 
				} 
				case 'U': { 
					return(2370+offset); // Vowel Sign "uU"
					break; 
				} 
				case 'q': { 
					return(2371+offset); // Vowel Sign Vocalic "R"
					break; 
				} 
				case 'e': { 
					if(offset == 896) {
						return (3270);
					}
					else if(offset == 768)  {
						return (3142);
					}
					else {
						return(2375+offset); // Vowel Sign "E"
					}
					break; 
				} 
				case 'E': { 
					return(2376+offset); // Vowel Sign "AI"
					break; 
				} 
				case 'o': { 
					return(2379+offset); // Vowel Sign "O"
					break; 
				} 
				case 'O': { 
					return(2380+offset); // Vowel Sign "AU"
					break; 
				} 
				case '_': { 
					return(2381+offset); // HALANT
					break; 
				} 
				case '.': { 
					if(offset < 640) { //if the input language is not a South indian lang.
						return(2404+offset); // VIRAM
					}
					else {
						return(46);
					}	 
					break;
				} 
				case '0': { 
					if(offset != 640) { 
						return(2406+offset); // DEVANAGARI DIGIT ZERO
					} 
					else { 
							if(isNetscape) {
								return -1;
							}
							else { 
								return 0;
							}
					}
					break; 
				} 
				case '1': { 
					return(2407+offset); // DIGIT ONE
					break; 
				} 
				case '2': { 
					return(2408+offset); // DIGIT TWO
					break; 
				} 
				case '3': { 
					return(2409+offset); // DIGIT THREE
					break; 
				} 
				case '4': { 
					return(2410+offset); // DIGIT FOUR
					break; 
				} 
				case '5': { 
					return(2411+offset); // DIGIT FIVE
					break; 
				} 
				case '6': { 
					return(2412+offset); // DIGIT SIX
					break; 
				} 
				case '7': { 
					return(2413+offset); // DIGIT SEVEN
					break; 
				} 
				case '8': { 
					return(2414+offset); // DIGIT EIGHT
					break; 
				} 
				case '9': { 
					return(2415+offset); // DIGIT NINE
					break; 
				}
			}
		}

		/**
		 * This function changes to the Indian Language selected and accordingly
		 * the value of offset is calculated.
		 */
		function changeLanguage(selectControl,obj) {
			if(obj=="")
			{
				var languageIndex = selectControl.options[selectControl.options.selectedIndex].value;
				var languageIndex = selectControl.value;
			}else
			{
				var languageIndex = obj;
			}
			
			convertFlag = true;
			switch(languageIndex) {
					case 'Devanagari' :
					case 'devanagari' :
					case 'DEVANAGARI' :	
					 { 
						offset=0;
						break; 
					}
					case 'BENGALI' :
					case 'bengali' :
					case 'Bengali' :
					 { 
						offset=(128*1);
						break; 
					}
					case 'PUNJABI' :
					case 'Punjabi' :
					case 'punjabi' :		
					 { 
						offset=(128*2);
						break; 
					}
					case 'GUJARATI' :
					case 'Gujarati' :
					case 'gujarati' :
					 { 
						offset=(128*3);
						break; 
					}
					case 'Oriya' :
					case 'oriya' :					
					case 'ORIYA' : { 
						offset=(128*4);
						break; 
					}
					case 'Tamil' :
					case 'tamil' :
					case 'TAMIL' : { 
						offset=(128*5);
						break; 
					}
					case 'Telugu' :
					case 'telugu' :
					case 'TELUGU' : { 
						offset=(128*6);
						break; 
					}
					case 'Kannada' :
					case 'kannada' :
					case 'KANNADA' : { 
						offset=(128*7);
						break; 
					}
					case 'Malayalam' :
					case 'malayalam' :
					case 'MALAYALAM' : { 
						offset=(128*8);
						break; 
					}
					default: { convertFlag=false;}
			}
		} 

//end of IME			

