/*******************************************************************************
 * Vietnamese Keyboard Online JavaScript (used with vietuni.js)
 * by NGUYEN, Ba Thanh <btnguyen2k@yahoo.com>
 * No parts of this script can be use outside DDTHSoft's software
 * without prior consent.
 * Language: VIETNAMESE
 ******************************************************************************/
/*Change logs:
* Version 2.0 (12/07/2003):
- use F8 to switch between typing methods (for Mozilla/Netscape)
  use F9 to switch between typing methods (for IE)
- correctly typing Vietnamese anywhere with Mozilla (1.3+ recommended)
- IE 5.0+ & Mozilla 1.3+: type Vietnamese anywhere
  Netspace 7: type Vietnamese when the cursor is at the end of the text
  (there're still some errors! Need futher improvement!!!)

* Version 1.0 (about end of 2002):
- intergrated with vietuni.js
- use cookie to remember last chosen typing method
- display staus on browst's status bar
- use F9 to switch between typing methods
*/

_vCOOKIE_PREFIX = 'ddth_';
var _VK_ON = true;

function _VK_createCaret(textEl) {
	if (textEl.createTextRange) {
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

function _VK_printForm() {
	//document.write("<form name='_frm_VK'>");
	document.write("<table border=0 cellpadding=0 cellspacing=0>");
	document.write("<tr>");
	document.write("<td align='center'><small>B&#7897; g&#245; ti&#7871;ng Vi&#7879;t tr&#7921;c tuy&#7871;n (H&#7895; tr&#7907; b&#7903;i <a href='http://www.avys.de/js/src/vudownload.html' target='_blank'>VietUni</a>) [<a href=\"javascript:;\" onclick=\"url_popup(baseURL+'js/vietuni_help.html', '', 'location=0,menubar=0,width=640,height=480,status=1,resizable=1');\">H&#432;&#7899;ng d&#7851;n</a>]");
	document.write("<br>Nh&#7845;n F8 ho&#7863;c F9 &#273;&#7875; &#273;&#7893;i ki&#7875;u g&#245;: ");
	document.write("<input class='radio' type='radio' class='cRadio' name='_vietkey_mode' value=0 onFocus='setTypingMode(0);'>T&#7855;t | ");
	document.write("<input type='radio' class='cRadio' name='_vietkey_mode' value=1 onFocus='setTypingMode(1);'>Telex | ");
	document.write("<input type='radio' class='cRadio' name='_vietkey_mode' value=2 onFocus='setTypingMode(2);'>VNI | ");
	document.write("<input type='radio' class='cRadio' name='_vietkey_mode' value=3 onFocus='setTypingMode(3);'>VIQR");
	document.write("</small></td>");
	document.write("</tr>");
	document.write("</table>");
	//document.write("</form>");
} // end _VK_printForm
function _VK_printFormHidden() {
	document.write('<input type="hidden" name="_vietkey_mode">');
	document.write('<input type="hidden" name="_vietkey_mode">');
	document.write('<input type="hidden" name="_vietkey_mode">');
	document.write('<input type="hidden" name="_vietkey_mode">');
} // end _VK_printFormHidden

function _VK_hasForm() {
	for (var i=0; i<document.forms.length; i++)
		if (document.forms[i]._vietkey_mode) {
			_VK_ON = true;
			return true;
		}
	_VK_ON = false;
	return false;
} //end _VK_hasForm

function _VK_updateVietkey() {
	var vk = _GetCookie(_vCOOKIE_PREFIX+'VK');
	switch (vk) {
		case '1':
			//if (document._frm_VK)
			//	document._frm_VK._vietkey_mode[1].checked = true;
			for (var i=0; i<document.forms.length; i++)
				if (document.forms[i]._vietkey_mode)
					document.forms[i]._vietkey_mode[1].checked = true;
			break;
		case '2':
			//if (document._frm_VK)
			//	document._frm_VK._vietkey_mode[2].checked = true;
			for (var i=0; i<document.forms.length; i++)
				if (document.forms[i]._vietkey_mode)
					document.forms[i]._vietkey_mode[2].checked = true;
			break;
		case '3':
			//if (document._frm_VK)
			//	document._frm_VK._vietkey_mode[3].checked = true;
			for (var i=0; i<document.forms.length; i++)
				if (document.forms[i]._vietkey_mode)
					document.forms[i]._vietkey_mode[3].checked = true;
			break;
		default:
			//if (document._frm_VK)
			//	document._frm_VK._vietkey_mode[0].checked = true;
			for (var i=0; i<document.forms.length; i++)
				if (document.forms[i]._vietkey_mode)
					document.forms[i]._vietkey_mode[0].checked = true;
			break;
	} //end switch
} //end _VK_updateVietkey
function _VK_key_press(e) {
	if ( !_VK_ON ) return false;
	if (document.all)
		return _VK_key_press_IE();
	else
		return _VK_key_press_Other(e);
} //end _VK_key_press
function _VK_key_press_IE() {
	var el = event.srcElement;
	if (event.keyCode == 120 || event.keyCode == 119) {
		/* F9/F8: change typing mode */
		_switchKeymode();
		_VK_updateVietkey();
		return false;
	}
	if(el.type=='text'||el.type=='textarea') {
		telexingVietUC(el, event.keyCode);
		_VK_createCaret(el);
		return true;
	} else {
		return false;
	}
} //end _VK_key_press_IE
function _VK_key_press_Other(e) {
	var el = e.target;
	if (e.keyCode == 120 || e.keyCode == 119) {
		/* F9/F8: change typing mode */
		_switchKeymode();
		_VK_updateVietkey();
		return false;
	}
	if(el.type=='text'||el.type=='textarea') {
		telexingVietUC(el, e.keyCode);
		_VK_createCaret(el);
		return true;
	} else {
		return false;
	}
} //end function _VK_key_press_Other

function _VK_page_load() {
	var vk = _GetCookie(_vCOOKIE_PREFIX+'VK');
	switch (vk) {
		case '1': setTypingMode(1); break;
		case '2': setTypingMode(2); break;
		case '3': setTypingMode(3); break;
		default: setTypingMode(0); break;
	}
	_VK_updateVietkey();
	_HookKeydown(_VK_key_press);
	return false;
}
_HookPageload(_VK_page_load);
