var g_curIme = 2;

function ChangeLang(ob, lang)
{
	if (typeof(ob.OldLang) == "undefined")
	    ob.OldLang = "CHS";
    
	ob.className = ob.className.replace(lang, ob.OldLang);
	ob.OldLang = lang;
	
	if ((ob.tagName == "INPUT" && ob.type == "text") || (ob.tagName == "TEXTAREA" ))
	{
		ob.value = "";
		ob.blur();
		ob.focus();
	}
}

//firefox时，调用此函数
function InsertCode(ob, e, code)
{
	if (!ob.setSelectionRange)
		return;
	
	var start = ob.selectionStart;   
	var end = ob.selectionEnd;
	ob.value = ob.value.substring(0, start) + String.fromCharCode(code) + ob.value.substr(end); 
	ob.setSelectionRange(start + 1, start + 1); 
	e.preventDefault();
}

function GetEvent()
{
	if (navigator.product != "Gecko")
		return window.event;//如果是ie
		
	func = GetEvent.caller;
	while (func != null)
	{
		var arg0 = func.arguments[0];
		if(arg0)
		{
			if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof(arg0) == "object" && arg0.preventDefault && arg0.stopPropagation))
			{
				return arg0;
			}
		}
		func=func.caller;
	}
	return null;
}

function SetupIme()
{
	var allTag = document.getElementsByTagName("*");
	for(var i=0; i < allTag.length; i++)
	{
		var tag = allTag.item(i);
		if((tag.tagName == "INPUT" && tag.type == "text") || tag.tagName == "TEXTAREA")
		{
			tag.onkeydown = OnEdit;
			tag.onkeypress = OnEdit;
		}
	}
}

function OnEdit()
{
    var lang = "CHS";
    var e;
    var ob;
    if (arguments.length == 1)//HtmlEditor
    {
        if (arguments[0].event != null)
            e = arguments[0].event;
        else
            e = arguments[0];
            
        ob = e.srcElement || e.target;
        if (g_curIme == 2)
            lang = "UYG";
        else if (g_curIme == 3)
            lang = "KZA";
	}
	else
	{
	    var e = GetEvent();
	    ob = e.srcElement || e.target;
    	
	    if (ob.className.indexOf("UYG") != -1)
	        lang = "UYG";
	    else if (ob.className.indexOf("KZA") != -1)
	        lang = "KZA";
	}
	
	var code = e.keyCode | e.which;
	if (e.type == "keydown")
	{
		if (code == 113)
		{
			if (e.ctrlKey)
			{//CTRL+F2
			    ChangeLang(ob, lang);
			    ob.English = false;
			}
			else
			{//F2
			    ob.English = !ob.English;
			}
		}
	}
	else if (lang != "CHS" && !ob.English)
	{
		var browser = "IE";
		if (navigator.userAgent.indexOf("Gecko") != -1)//FireFox
			browser = "FF";
		
		if (arguments.length == 0 && browser != "IE" && e.charCode == 0)
			return true;

		if (lang == "UYG")
			code = StandardUyghurIme(code);
		else if (lang == "KZA")
			code = KazakIme(code);
		
		if (code < 0x0600)
			return true;
			
		if (/*arguments.length == 1 || */browser == "IE")
			e.keyCode = code;			
		else
			InsertCode(ob, e, code);
	}
	return true;
}

function UyghurIme(code)
{
	switch (code)
	{
		case 97: //a
			return 0X0647;
		case 65: //A
			return 0;
		case 98: //b
			return 0X0628;
		case 66: //B
			return 0;
		case 99: //c
			return 0X063A;
		case 67: //C
			return 0;
		case 100: //d
			return 0X062F;
		case 68: //D
			return 0X0622;
		case 101: //e
			return 0X0626;
		case 69: //E
			return 0;
		case 102: //f
			return 0X0627;
		case 70: //F
			return 0X0641;
		case 103: //g
			return 0X0629;
		case 71: //G
			return 0X0636;
		case 104: //h
			return 0X0639;
		case 72: //H
			return 0X062E;
		case 105: //i
			return 0X062B;
		case 73: //I
			return 0;
		case 106: //j
			return 0X0642;
		case 74: //J
			return 0X062C;
		case 107: //k
			return 0X0643;
		case 75: //K
			return 0X0623;
		case 108: //l
			return 0X0644;
		case 76: //L
			return 0;
		case 109: //m
			return 0X0645;
		case 77: //M
			return 0;
		case 110: //n
			return 0X0646;
		case 78: //N
			return 0;
		case 111: //o
			return 0X0648;
		case 79: //O
			return 0;
		case 112: //p
			//return 0X067E;;
			return 0X0635;
		case 80: //P
			return 0;
		case 113: //q
			return 0X062D;
		case 81: //Q
			return 0;
		case 114: //r
			return 0X0631;
		case 82: //R
			return 0;
		case 115: //s
			return 0X0633;
		case 83: //S
			return 0;
		case 116: //t
			return 0X062A;
		case 84: //T
			return 0;
		case 117: //u
			return 0X0630;
		case 85: //U
			return 0;
		case 118: //v
			return 0X0649;
		case 86: //V
			return 0;
		case 119: //w
			return 0X0624;
		case 87: //W
			return 0;
		case 120: //x
			return 0X0634;
		case 88: //X
			return 0;
		case 121: //y
			return 0X064A;
		case 89: //Y
			return 0;
		case 122: //z
			return 0X0632;
		case 90: //Z
			return 0;
		case 44: //,
			return 0X060C;
		case 62: //>
			return 0X003C;
		case 41: //)
			return 0X0028;
		case 60: //>
			return 0X003E;
		case 40: //(
			return 0X0029;
		case 47: ///
			return 0X0638;
		case 63: //?
			return 0X061F;
		case 59: //;
			return 0X061B;
		case 39: // '
			return 0X002C;
		case 34: //"
			return 0X0022;
		case 91: //[
			return 0X005D;
		case 123: //{
			return 0X007D;
		case 93: //]
			return 0X005B;
		case 125: //}
			return 0X007B;
	}
	return code;
}

function StandardUyghurIme(code)
{
	switch(code)
	{
		case 97: //a
			return 0X06BE;//v
		case 65: //A
			return 0;
		case 98: //b
			return 0X0628;//b
		case 66: //B
			return 0;
		case 99: //c
			return 0X063A;//gh
		case 67: //C
			return 0;
		case 100: //d
			return 0X062F;//d
		case 68: //D
			return 0X0698;//zj
		case 101: //e
			return 0X06D0;//e
		case 69: //E
			return 0;
		case 102: //f
			return 0X0627;//a
		case 70: //F
			return 0X0641;//f
		case 103: //g
			return 0X06D5;//ae
		case 71: //G
			return 0X06AF;//g
		case 104: //h
			return 0X0649;//i
		case 72: //H
			return 0X062E;//h
		case 105: //i
			return 0X06AD;//ng
		case 73: //I
			return 0;
		case 106: //j
			return 0X0642;//kh
		case 74: //J
			return 0X062C;//j
		case 107: //k
			return 0X0643;//k
		case 75: //K
			return 0X06C6;//oe
		case 108: //l
			return 0X0644;//l
		case 76: //L
			return 0;
		case 109: //m
			return 0X0645;//m
		case 77: //M
			return 0;
		case 110: //n
			return 0X0646;//n
		case 78: //N
			return 0;
		case 111: //o
			return 0X0648;//o
		case 79: //O
			return 0;
		case 112: //p
			return 0X067E;//p
		case 80: //P
			return 0;
		case 113: //q
			return 0X0686;//q
		case 81: //Q
			return 0;
		case 114: //r
			return 0X0631;//r
		case 82: //R
			return 0;
		case 115: //s
			return 0X0633;//s
		case 83: //S
			return 0;
		case 116: //t
			return 0X062A;//t
		case 84: //T
			return 0;
		case 117: //u
			return 0X06C7;//u
		case 85: //U
			return 0;
		case 118: //v
			return 0X06C8;//ue
		case 86: //V
			return 0;
		case 119: //w
			return 0X06CB;//w
		case 87: //W
			return 0;
		case 120: //x
			return 0X0634;//x
		case 88: //X
			return 0;

		case 121: //y
			return 0X064A;//y
		case 89: //Y
			return 0;
		case 122: //z
			return 0X0632;//z
		case 90: //Z
			return 0;
		case 44: //,
			return 0X060C;//逗号
		case 62: //>
			return 0X003C;
		case 41: //)
			return 0X0028;
		case 60: //>
			return 0X003E;
		case 40: //(
			return 0X0029;
		case 47: ///
			return 0X0626;//Hamza
		case 63: //?
			return 0X061F;//问号
		case 59: //;
			return 0X061B;//分号
		case 39: // '
			return 0X002C;
		case 34: //"
			return 0X0022;
		case 91: //[
			return 0X005D;
		case 123: //{
			return 0X007D;
		case 93: //]
			return 0X005B;
		case 125: //}
			return 0X007B;
	}
	return code;
}

function KazakIme(code)
{
	switch(code)
	{
		case 97: //a
			return 0X06BE;//v
		case 65: //A
			return 0;
		case 98: //b
			return 0X0628;//b
		case 66: //B
			return 0;
		case 99: //c
			return 0X0639;//gh
		case 67: //C
			return 0;
		case 100: //d
			return 0X062F;//d
		case 68: //D
			return 0X0698;//zj
		case 101: //e
			return 0X06D0;//e
		case 69: //E
			return 0;
		case 102: //f
			return 0X0627;//a
		case 70: //F
			return 0X0641;//f
		case 103: //g
			return 0X06D5;//ae
		case 71: //G
			return 0X06AF;//g
		case 104: //h
			return 0X0649;//i
		case 72: //H
			return 0X062D;//h
		case 105: //i
			return 0X06AD;//ng
		case 73: //I
			return 0;
		case 106: //j
			return 0X0642;//kh
		case 74: //J
			return 0X062C;//j
		case 107: //k
			return 0X0643;//k
		case 75: //K
			return 0X06C5;//oe
		case 108: //l
			return 0X0644;//l
		case 76: //L
			return 0;
		case 109: //m
			return 0X0645;//m
		case 77: //M
			return 0;
		case 110: //n
			return 0X0646;//n
		case 78: //N
			return 0;
		case 111: //o
			return 0X0648;//o
		case 79: //O
			return 0;
		case 112: //p
			return 0X067E;//p
		case 80: //P
			return 0;
		case 113: //q
			return 0X0686;//q
		case 81: //Q
			return 0;
		case 114: //r
			return 0X0631;//r
		case 82: //R
			return 0;
		case 115: //s
			return 0X0633;//s
		case 83: //S
			return 0;
		case 116: //t
			return 0X062A;//t
		case 84: //T
			return 0;
		case 117: //u
			return 0X06C7;//u
		case 85: //U
			return 0;
		case 118: //v
			return 0X06C8;//ue
		case 86: //V
			return 0;
		case 119: //w
			return 0X06CB;//w
		case 87: //W
			return 0;
		case 120: //x
			return 0X0634;//x
		case 88: //X
			return 0;
		case 121: //y
			return 0X064A;//y
		case 89: //Y
			return 0;
		case 122: //z
			return 0X0632;//z
		case 90: //Z
			return 0;
		case 44: //,
			return 0X060C;//逗号
		case 62: //>
			return 0X003C;
		case 41: //)
			return 0X0028;
		case 60: //>
			return 0X003E;
		case 40: //(
			return 0X0029;
		case 47: ///
			return 0X0621;//Hamza
		case 63: //?
			return 0X061F;//问号
		case 59: //;
			return 0X061B;//分号
		case 39: // '
			return 0X002C;
		case 34: //"
			return 0X0022;
		case 91: //[
			return 0X005D;
		case 123: //{
			return 0X007D;
		case 93: //]
			return 0X005B;
		case 125: //}
			return 0X007B;
	}
	return code;
}
