var searchnamearray=new Array("searchtext");
var searchactionarray=new Array("enter");
var searchcaptionarray=new Array("any search Criteria"); 

var searchnamearray1=new Array("searchtext1");
var searchactionarray1=new Array("enter");
var searchcaptionarray1=new Array("any search Criteria"); 

var searchnamearray2=new Array("searchtext2");
var searchactionarray2=new Array("enter");
var searchcaptionarray2=new Array("any search Criteria"); 

var Vowel = new Array(123);

var VowelCombination = new Array(6);
var Consonant = new Array(123);
var ConsonantCombination = new Array(25);
var Symbol = new Array(60);
var VIRAM = 1;
var VRU = 2;
var RU = 3;
var language = "Hindi";

Symbol[32] = "\u0020"; // space
Symbol[58] = "\u0903"; // visarg
Symbol[48] = "\u0966"; //0
Symbol[49] = "\u0967"; //1
Symbol[50] = "\u0968"; //2
Symbol[51] = "\u0969"; //3
Symbol[52] = "\u096a"; //4
Symbol[53] = "\u096b"; //5
Symbol[54] = "\u096c"; //6
Symbol[55] = "\u096d"; //7
Symbol[56] = "\u096e"; //8
Symbol[57] = "\u096f"; //9

Vowel[97] = "\u0905"; //a
Vowel[65] = "\u0906"; //A
Vowel[105] = "\u0907"; //i
Vowel[73] = "\u0908"; //I
Vowel[117] = "\u0909"; //u 
Vowel[85] = "\u090a"; //U
Vowel[82] = "\u090b"; // R
Vowel[69] = "\u090d"; // E
Vowel[101] = "\u090f"; //e
Vowel[79] = "\u0911"; //O
Vowel[111] = "\u0913"; // o


VowelCombination[0] = new Array(3);
VowelCombination[0][0]= 97; //a
VowelCombination[0][1]= 97; //a
VowelCombination[0][2]= "\u0906"; //aa

VowelCombination[1] = new Array(3);
VowelCombination[1][0]= 101; //e
VowelCombination[1][1]= 101; //e
VowelCombination[1][2]= "\u0908"; //ee

VowelCombination[2] = new Array(3);
VowelCombination[2][0]= 111; //o
VowelCombination[2][1]= 111; //o
VowelCombination[2][2]= "\u090a"; //oo

VowelCombination[3] = new Array(3);
VowelCombination[3][0]= 82; //R
VowelCombination[3][1]= 85; //U
VowelCombination[3][2]= "\u090b"; 

VowelCombination[4] = new Array(3);
VowelCombination[4][0]= 97; //a
VowelCombination[4][1]= 105; //i
VowelCombination[4][2]= "\u0910"; //ai

VowelCombination[5] = new Array(3);
VowelCombination[5][0]= 97; //a
VowelCombination[5][1]= 117; //u 
VowelCombination[5][2]= "\u0914"; //au

Consonant[94] = "\u0901"; // chandrabindu
Consonant[77] = "\u0902"; // M
Consonant[107] = "\u0915"; //k
Consonant[103] = "\u0917"; //g
Consonant[106] = "\u091c"; //j
Consonant[122] = "\u091d"; //z
Consonant[84] = "\u091f"; //T
Consonant[68] = "\u0921"; //D
Consonant[78] = "\u0923"; //N
Consonant[116] = "\u0924"; //t
Consonant[100] = "\u0926"; //d
Consonant[110] = "\u0928"; //n
Consonant[112] = "\u092a"; //p
Consonant[102] = "\u092b"; //f
Consonant[98] = "\u092c"; //b
Consonant[109] = "\u092e"; //m
Consonant[121] = "\u092f"; //y
Consonant[114] = "\u0930"; //r
Consonant[108] = "\u0932"; //l
Consonant[76] = "\u0933"; //L
Consonant[118] = "\u0935"; //v
Consonant[119] = "\u0935"; //w
Consonant[115] = "\u0938"; //s
Consonant[120] = "\u0915\u094d\u0937"; //kSh
Consonant[104] = "\u0939"; //h
Consonant[97] = ""; // just empty string
Consonant[VIRAM] = "\u094d"; // half letter
Consonant[65] = "\u093e"; //A
Consonant[105] = "\u093f"; //i
Consonant[73] = "\u0940"; //I
Consonant[117] = "\u0941"; //u
Consonant[85] = "\u0942"; //U
Consonant[VRU] = "\u0943"; // VRU
Consonant[69] = "\u0945"; //E
Consonant[101] = "\u0947"; //e
Consonant[79] = "\u0949"; //O
Consonant[111] = "\u094b"; //o
Consonant[75] = "\u0958"; //K
Consonant[71] = "\u095a"; //G
Consonant[90]= "\u095b"; //Z
Consonant[70]= "\u095e"; //F
Consonant[89]= "\u095f"; //Y


ConsonantCombination[0] = new Array(3);
ConsonantCombination[0][0]= 107; //k
ConsonantCombination[0][1]= 104; //h
ConsonantCombination[0][2]= "\u0916"; // kh

ConsonantCombination[1] = new Array(3);
ConsonantCombination[1][0]= 103; //g
ConsonantCombination[1][1]= 104; //h
ConsonantCombination[1][2]= "\u0918"; //gh

ConsonantCombination[2] = new Array(3);
ConsonantCombination[2][0]= 99; //c
ConsonantCombination[2][1]= 104; //h
ConsonantCombination[2][2]= "\u091a"; //ch

ConsonantCombination[3] = new Array(3);
ConsonantCombination[3][0]= 67; //C
ConsonantCombination[3][1]= 104; //h
ConsonantCombination[3][2]= "\u091b"; //Ch

ConsonantCombination[4] = new Array(3);
ConsonantCombination[4][0]= 84; //T
ConsonantCombination[4][1]= 104; //h
ConsonantCombination[4][2]= "\u0920"; //Th

ConsonantCombination[5] = new Array(3);
ConsonantCombination[5][0]= 68; //D
ConsonantCombination[5][1]= 104; //h
ConsonantCombination[5][2]= "\u0922"; //Dh

ConsonantCombination[6] = new Array(3);
ConsonantCombination[6][0]= 116; //t
ConsonantCombination[6][1]= 104; //h
ConsonantCombination[6][2]= "\u0925"; //th

ConsonantCombination[7] = new Array(3);
ConsonantCombination[7][0]= 100; //d
ConsonantCombination[7][1]= 104; //dh
ConsonantCombination[7][2]= "\u0927"; //dh

ConsonantCombination[8] = new Array(3);
ConsonantCombination[8][0]= 112; //p
ConsonantCombination[8][1]= 104; //ph
ConsonantCombination[8][2]= "\u092b"; //ph

ConsonantCombination[9] = new Array(3);
ConsonantCombination[9][0]= 98; //b
ConsonantCombination[9][1]= 104; //h
ConsonantCombination[9][2]= "\u092d"; //bh

ConsonantCombination[10] = new Array(3);
ConsonantCombination[10][0]= 115; //s
ConsonantCombination[10][1]= 104; //h
ConsonantCombination[10][2]= "\u0936"; //sh

ConsonantCombination[11] = new Array(3);
ConsonantCombination[11][0]= 83; //S
ConsonantCombination[11][1]= 104; //h
ConsonantCombination[11][2]= "\u0937"; //Sh

ConsonantCombination[12] = new Array(3);
ConsonantCombination[12][0]= 74; //J
ConsonantCombination[12][1]= 104; //h
ConsonantCombination[12][2]= "\u091c\u094d\u091e"; // Jh

ConsonantCombination[13] = new Array(3);
ConsonantCombination[13][0]= 97; //a
ConsonantCombination[13][1]= 97; //a
ConsonantCombination[13][2]= "\u093e"; //aa

ConsonantCombination[14] = new Array(3);
ConsonantCombination[14][0]= 101; //e
ConsonantCombination[14][1]= 101; //e
ConsonantCombination[14][2]= "\u0940"; //ee

ConsonantCombination[15] = new Array(3);
ConsonantCombination[15][0]= 111; //o
ConsonantCombination[15][1]= 111; //o
ConsonantCombination[15][2]= "\u0942"; //oo

ConsonantCombination[16] = new Array(3);
ConsonantCombination[16][0]= 97; //a
ConsonantCombination[16][1]= 105; //i
ConsonantCombination[16][2]= "\u0948"; //ai

ConsonantCombination[17] = new Array(3);
ConsonantCombination[17][0]= 97; //a
ConsonantCombination[17][1]= 117; //u
ConsonantCombination[17][2]= "\u094c"; //au

ConsonantCombination[18] = new Array(3);
ConsonantCombination[18][0] = 78; // N
ConsonantCombination[18][1] = 71; // G
ConsonantCombination[18][2] = "\u0919"; //NG

ConsonantCombination[19] = new Array(3);
ConsonantCombination[19][0] = 78; // N
ConsonantCombination[19][1] = 89; // Y
ConsonantCombination[19][2] = "\u091e"; //NY

ConsonantCombination[20] = new Array(3);
ConsonantCombination[20][0] = 75; // K
ConsonantCombination[20][1] = 104; // h
ConsonantCombination[20][2] = "\u0959"; //Kh

ConsonantCombination[21] = new Array(3);
ConsonantCombination[21][0] = 68; // D
ConsonantCombination[21][1] = 68; // D
ConsonantCombination[21][2] = "\u095c"; //DD

ConsonantCombination[22] = new Array(3);
ConsonantCombination[22][0] = 68; // D
ConsonantCombination[22][1] = 72; // H
ConsonantCombination[22][2] = "\u095d"; //DH

ConsonantCombination[23] = new Array(3);
ConsonantCombination[23][0] = 78; // N
ConsonantCombination[23][1] = 78; // N
ConsonantCombination[23][2] = "\u0929"; //NN

ConsonantCombination[24] = new Array(3);
ConsonantCombination[24][0] = 82; // R
ConsonantCombination[24][1] = 82; // R
ConsonantCombination[24][2] = "\u0931"; //RRa

ConsonantCombination[25] = new Array(3);
ConsonantCombination[25][0] = 76; // L
ConsonantCombination[25][1] = 76; // L
ConsonantCombination[25][2] = "\u0934"; //LL


var prevkey = 32;
var hidden=false;
var posChanged = true;
var isEng = false;

var previousConsonant = false;
var previouspreviousConsonant = false;

function changeCursor(txtarea)	{
	if (txtarea.createTextRange)	{
	txtarea.cursorPos = document.selection.createRange().duplicate();
	}
}
function GetVowelCombination(key0, key1)
{
	var i=0;
	for(i=0;i<VowelCombination.length;i++)
	{
		if((VowelCombination[i][0] == key0) && (VowelCombination[i][1]==key1))
			return VowelCombination[i][2];	
	}
}
function GetConsonantCombination(key0, key1)
{
	var i=0;
	for(i=0;i<ConsonantCombination.length;i++)
	{
		if((ConsonantCombination[i][0] == key0) && (ConsonantCombination[i][1]==key1))
			return ConsonantCombination[i][2];	
	}
}

function positionChange(evt,status)
{
var born = new Date();
currentkey=born.getSeconds();
lastkey=document.getElementById('lastkeytime').value;
var keyCode = 
document.layers ? evt.which :
document.all ? event.keyCode :
document.getElementById ? evt.keyCode : 0;
if(status=='Y')
{
 	if(keyCode==38)rotateUp(document.getElementById('resultwords'),document.getElementById('searchtext1'));
	else if(keyCode==40)rotateDown(document.getElementById('resultwords'),document.getElementById('searchtext1'));
	else if(keyCode==13)submitform();
	else
	{
		if(keyCode==8)
			document.getElementById('searchtext1').value=document.getElementById('searchtext1').value+'';
		if(currentkey!=lastkey)
		{
			showrosterhindi(document.getElementById('searchtext1').value);
			document.getElementById('lastkeytime').value=currentkey;
		}
	}
}
  if (keyCode >= 37 && keyCode <=40)
  {
	posChanged = true;
  }	
}

function change(txtarea, evt)
{            

    var displace = 0;
	var text='';
    var previousValue = "";
    //var e = evt; //(evt)? e: ((window.event)? event : null);
    var e = window.event? event : evt;
    if( e.altKey || e.ctrlKey)
    {
		return true;
	}
	var key=0;
	var keychar='';
	var shift=false;
	var pos= 0; // doGetCaretPosition (txtarea);
	var prevpos = 0;
	if ( document.all )
	{
		key = e.keyCode;
	}	
	else
	{
		key = e.which;
	}

	if ((key <32) || (key >=33 && key <=47) ||(key>=59 && key <=64)||(key>=91 && key<=96 && !key==94)||(key>=123 && key<=127) )
		return true;
	

	keychar = String.fromCharCode(key);
	if(posChanged)
	{
		prevkey = 32;
		hidden=false;
		prevpos = 0;
		previousConsonant = false;
		previouspreviousConsonant = false;
		
	}
		switch(keychar)
		{
			case "a":
			case "e":
			case "i":
			case "o":
			case "u":
			case "A":
			case "I":
			case "U":
			case "O":
			case "E":
						var cComb = GetConsonantCombination(prevkey,key);
						var vComb = GetVowelCombination(prevkey,key);

						if(previousConsonant)
						{
							displace --;
							text=Consonant[key]; // nothing to append. just remove viram

						}
						else if (typeof(vComb) != 'undefined')
						{
							if(!previouspreviousConsonant)
							{
								
								displace --; // aa, ai, au.......
								text = vComb;
							}
							else
							{
								displace = 0;
								text = cComb;
							}
						}
						else
						{
							displace = 0;
							text = Vowel[key]; 
						}
						previouspreviousConsonant = previousConsonant;
						previousConsonant = false;
						hidden=false;
						break;
			case 'R':
						if(previousConsonant)
						{
							displace--; // replace the viram
							text = Consonant[VRU];
							// here no need for another U in VRU. by detecting VR it is assumed the whole word
						}
						else
						{
							displace=0; // nothing to displace
							text = Vowel[key];
						}
						previouspreviousConsonant = previousConsonant;
						previousConsonant = false;
						hidden = false;
						prevkey = key;
						break;
			case '^':
			case 'M':
						displace = 0;
						text = Consonant[key];
						previouspreviousConsonant = previousConsonant;
						previousConsonant = false;
						hidden = false;
						prevchar = keychar;
						break;
			case ' ':
						if(previousConsonant)
						{
							displace --;
						}
						else
						{
							displace = 0;
						}
						text = Symbol[key];
						previouspreviousConsonant = previousConsonant;
						previousConsonant = false;
						hidden = false;
						prevkey = key;
						break;
			default:
						var cComb = GetConsonantCombination(prevkey,key);
						if (hidden)
						{
							if(typeof(cComb) !='undefined')
							{
								displace = 0; // because previous word was not shown in textarea.
								text = cComb+ Consonant[VIRAM];
								previouspreviousConsonant = previousConsonant;
								previousConsonant = true;
							}
							else if (typeof(Consonant[key]) !='undefined')
							{
								displace = 0;
								text = Consonant[key] + Consonant[VIRAM];
								previouspreviousConsonant = previousConsonant;
								previousConsonant = true;
								hidden = false;
							}
							else
							{
								displace =0;
								previouspreviousConsonant = previousConsonant;
								previousConsonant = false;
							}
						}
						else
						{
							if(typeof(cComb) !='undefined')
							{
								displace -=2; // for d, dh, D, Dh etc.
								text = cComb+ Consonant[VIRAM];
								previouspreviousConsonant = previousConsonant;
								previousConsonant = true;
							}
							else if(typeof(Consonant[key])!='undefined')
							{
								displace = 0;
								text = Consonant[key] + Consonant[VIRAM]; // letter + viram
								previouspreviousConsonant = previousConsonant;
								previousConsonant = true;
							}
							else if (typeof(Symbol[key]) != 'undefined')
							{
								displace =0;
								text = Symbol[key];
								previouspreviousConsonant = previousConsonant;
								previousConsonant = false;
							}
							else
							{
								previouspreviousConsonant = previousConsonant;
								previousConsonant = true;
								hidden = true;
							}
							
						}
		}

		prevkey = key;
	if(txtarea.setSelectionRange)
	{
		
		// Mozilla specific
		pos = txtarea.selectionStart;
		var s1 = txtarea.value.substring(0,pos + displace);
		var s2 = txtarea.value.substring(pos, txtarea.value.length);
		var scrollPos = txtarea.scrollTop;
		if(typeof(text) != 'undefined')
			prevpos = pos + displace + text.length;
		else
			prevpos = pos + displace;
		txtarea.value = s1 + text + s2;
		txtarea.scrollTop = scrollPos;
		txtarea.focus();
		txtarea.setSelectionRange(prevpos,prevpos);
	}
	else if (txtarea.createTextRange && txtarea.cursorPos)
	{
		// IE specific
		var range = txtarea.cursorPos;
		range.moveStart('character', displace);
	    range.text =	(range.text.charAt(range.text.length - 1)	== ' ' ? text +	' '	: text);
		range.collapse(false);
	    range.scrollIntoView(true);
	}
	else
	{
		// Other browser
		if (displace==0)
		{
			txtarea.value += text;
		}
		else
		{
			txtarea.value =	txtarea.value.substring(0, txtarea.value.length-1) + text;
		}
	}
	txtarea.focus();
	posChanged=false;
	return false;
}

function PrePage()
{
  
	document.getElementById('pageNumber').value = eval(document.getElementById('pageNumber').value) - 1  ;
	document.frm1.submit();
}
function NextPage()
{	
   document.getElementById('pageNumber').value = eval(document.getElementById('pageNumber').value) + 1;
   //alert(document.getElementById('pageNumber').value);
   document.frm1.submit();
}
function setvalue(val)
{
	document.getElementById('pageNumber').value= val;
	document.frm1.submit();
}
//===================function for mot leaving mandatory fields empty===========================
//..................used for mandatory fields validation.................................

 var PLSCAP = "Please ";
 var FULLSTOP = ".";
 var ENTER = "Enter ";

//***************************************************************************************
  
function IsNotEmpty(FormName, FieldNameArr, FieldCaptionArr, FieldActionArr)
{
	var Message = '';
	var FocusField = '';
	var Error = 0;
	var TotalFields = eval(FieldNameArr.length);
	//..........check for all mandatory fields....................
	for(var Counter = 0; Counter < TotalFields; Counter++)
	{
		if(document.getElementById(FieldNameArr[Counter]).value == '' || document.getElementById(FieldNameArr[Counter]).value == 0 || document.getElementById(FieldNameArr[Counter]).value =='null')
		{  
		
			Message+= PLSCAP + FieldActionArr[Counter] + " "+ FieldCaptionArr[Counter] + FULLSTOP +"\n";
			Error = 1;
			if(FocusField == '') 
			  {  
			   FocusField = document.getElementById(FieldNameArr[Counter]); 
			  }
	    
	   }
  }
	if(Error == 1)
	{  
	    alert(Message);
		FocusField.focus();
		return false;
	}
	 else
	 {  
	   return true;
	  
	 }
}
function SubmitRecordPage()
{
		document.frm1.submit();
}
function showroster(e)
{
	if(document.getElementById('edition').value=='law')
		$table='lawdictionary';
	else if(document.getElementById('edition').value=='computer')	
		$table='computerdictionary';
	else
		$table='dictionary_data';
	var born = new Date();
	currentkey=born.getSeconds();
	lastkey=document.getElementById('lastkeytime').value;
	$chosefont=document.getElementById('chosefont').value;
	$searchtext=document.getElementById('searchtext');
	$resultwords=document.getElementById('resultwords');
	if($chosefont=='English')
	{
			$var=document.getElementById('searchtext').value;
			$searchtext=document.getElementById('searchtext');
	}
	else
	{
			$var=document.getElementById('searchtext2').value;
			$searchtext=document.getElementById('searchtext2');
	}
	if(window.event) // IE
		keynum = e.keyCode
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which
	if(keynum==38)rotateUp(document.getElementById('resultwords'),$searchtext);
	else if(keynum==40)rotateDown(document.getElementById('resultwords'),$searchtext);
	else if(keynum==13)submitform();
	else
	{
		keychar = String.fromCharCode(keynum);
		if(keynum==8)
		{
			$var=$var+'';
		}	
		else
		{
			$var=$var+keychar;
		}	
		if(currentkey!=lastkey)
		{
			xajax_showresult($var,$chosefont,$table);
			document.getElementById('lastkeytime').value=currentkey;
		}
	}
}
function showrosterhindi(vars)
{
		if(document.getElementById('edition').value=='law')
		$table='lawdictionary';
		else if(document.getElementById('edition').value=='computer')	
			$table='computerdictionary';
		else
			$table='dictionary_data';
	    $chosefont=document.getElementById('chosefont').value;
		xajax_showresult(vars,$chosefont,$table);
}	
function rotateDown(result,text)
{
	resultlength=result.length;
	var index=result.selectedIndex;
	if(index < 0 || index >= resultlength-1)index=0;
	else index++;
	result.selectedIndex=index;
	text.value=result.value;
}
function rotateUp(result,text)
{
	resultlength=result.length;
	var index=result.selectedIndex;
	if(index <= 0 )index=resultlength-1;
	else index--;
	result.selectedIndex=index;
	text.value=result.value;
}
function changetextbox()
{
	if(document.getElementById('chosefont').value=='English')
	{
		document.getElementById('searchtext').value='';
		document.getElementById('englishdiv').style.display = 'inline';
		document.getElementById('hindidiv').style.display = 'none';
		document.getElementById('romandiv').style.display = 'none';
	}
	else if(document.getElementById('chosefont').value=='Roman')
	{
		document.getElementById('searchtext2').value='';
		document.getElementById('romandiv').style.display = 'inline';
		document.getElementById('hindidiv').style.display = 'none';
		document.getElementById('englishdiv').style.display = 'none';
	}
	else
	{
	    document.getElementById('searchtext1').value='';
		document.getElementById('hindidiv').style.display = 'inline';
		document.getElementById('englishdiv').style.display = 'none';
		document.getElementById('romandiv').style.display = 'none';
	}	
}
function settextsubmitform()
{	
	    $var=document.getElementById('chosefont').value;
		if($var=='English')
        document.getElementById('searchtext').value=document.getElementById('resultwords').value;
		else if($var=='Hindi')
        document.getElementById('searchtext1').value=document.getElementById('resultwords').value;
	    else
        document.getElementById('searchtext2').value=document.getElementById('resultwords').value;
	    checkUrl();
}	
function showdiv()
{
	if(document.getElementById('consentence').style.display == 'inline')
	{
		document.getElementById('consentence').style.display = 'none';
	}
	else
	{
		document.getElementById('consentence').style.display = 'inline';
	}
}
function checkNotEmpty()
{
	    var msg='';
		var field1=document.getElementById('engsentence').value;
		var field2=document.getElementById('hindisentence').value;
		if((field1==0 || field1=='' || field1== 'null') && (field2==0 || field2=='' || field2== 'null'))
		{
				msg+="please enter at least one sentence.";
				alert(msg);
				return false;
		}
		else
		{
				return true;
		}
}
function submitform()
{
	document.forms[0].submit();
}
function checkUrl()
{
	$chosefont=document.getElementById('chosefont').value;
	$edition=document.getElementById('edition').value;
	if($edition=='')
	{
			document.getElementById('edition').value='general';
	}
	if($chosefont=='English')
	{
		word=document.getElementById('searchtext').value;
		lang='en';
	}
	else if($chosefont=='Hindi')
	{
		word=document.getElementById('searchtext1').value;
		lang='hindi';
	}
	else if($chosefont=='Roman')
	{
		word=document.getElementById('searchtext2').value;
		lang='roman';
	}
	if($edition=='law')
		var url="http://www.pelagiandictionary.com/"+lang+"/lawdictionary/" +word;
	else if($edition=='computer')
		var url="http://www.pelagiandictionary.com/"+lang+"/computerdictionary/" +word;
	else	
		var url="http://www.pelagiandictionary.com/"+lang+"/" +word;
	setTimeout('window.location="'+url+'"',500);
}
function checkedition(val)
{
document.getElementById('edition').value=val;
document.getElementById('searchtext').value='';
document.getElementById('searchtext1').value='';
document.getElementById('searchtext2').value='';
obj=document.getElementById('chosefont');
if(val=='law' || val=='general')
{
	if(obj.options.length==3)
        obj.options[2] = null;
}
if(val=='general' || val=='')
{		
	if(obj.options.length==2)
   	 	 obj.options[2] = new Option('Romanized', 'roman');
}
}





	


