// VietChar.js - Version 1.0
// Created by Jake Tran
// Email: jaketran@hotmail.com
// Copyright 2003 VietPark.com.  All Rights Reserved. 
// Contact the author if you want to modify or use this script.

// Global variables
var charIndex = null;

var allChar, one, two, three, four, five, six, seven, eight, nine;
// a a( â e ê i o ô o* u u* y A A( Â E Ê I O Ô O* U U* Y d D
allChar = new Array(97, 259, 226, 101, 234, 105, 111, 244, 417, 117, 432, 121, 
					65, 258, 194, 69, 202, 73, 79, 212, 416, 85, 431, 89, 100, 68);

// Accent 1: '
one = new Array(225, 7855, 7845, 233, 7871, 237, 243, 7889, 7899, 250, 7913, 253, 
				193, 7854, 7844, 201, 7870, 205, 211, 7888, 7898, 218, 7912, 221);
// Accent 2: `
two = new Array(224, 7857, 7847, 232, 7873, 236, 242, 7891, 7901, 249, 7915, 7923,
				192, 7856, 7846, 200, 7872, 204, 210, 7890, 7900, 217, 7914, 7922);
// Accent 3: ?
three = new Array(7843, 7859, 7849, 7867, 7875, 7881, 7887, 7893, 7903, 7911, 7917, 7927, 
				  7842, 7858, 7848, 7866, 7874, 7880, 7886, 7892, 7902, 7910, 7916, 7926);
// Accent 4: ~
four = new Array(227, 7861, 7851, 7869, 7877, 297, 245, 7895, 7905, 361, 7919, 7929, 
				 195, 7860, 7850, 7868, 7876,  296, 213, 7894, 7904, 360, 7918, 7928);
// Accent 5: .
five = new Array(7841, 7863, 7853, 7865, 7879, 7883, 7885, 7897, 7907, 7909, 7921, 7925, 
				 7840, 7862, 7852, 7864, 7878, 7882, 7884, 7896, 7906, 7908, 7920, 7924);
// Accent 6: ^
six = new Array(226, 234, 244, 194, 202, 212); // a^, e^, o^, A^, E^, O^ 
// Accent 7: o*, u*, O*, U*
seven = new Array(417, 432, 416, 431); 
// Accent 8: a(, A(
eight = new Array(259, 258); 
// Accent 9: d-, Ð 
nine = new Array(273, 272); 

function emailPage_onload()
{
	document.VietParkForm.nameTextBox.onkeydown = KeyDown;
	document.VietParkForm.subjectTextBox.onkeydown = KeyDown;
	document.VietParkForm.messageTextBox.onkeydown = KeyDown;
}

function searchPage_onload()
{
	document.VietParkForm.searchTextBox.onkeydown= KeyDown;
}

function clearText()
{
	document.VietParkForm.searchTextBox.value ="";
}		

function addURLPage_onload()
{
	document.VietParkForm.titleTextBox.onkeydown= KeyDown;
	document.VietParkForm.descriptionTextBox.onkeydown= KeyDown;
}

// Get the index number (of the char entered) from the allChar array
function getPreCharIndex()
{
	var index, found = false;
	
	for(i=0; i< 26 ; i++ )
	{
		if(String.fromCharCode(allChar[i]) == preChar())
		{	
			index = i;
			found = true;
			break;
		}
	}

	if(found)
		return index;
	else
		return -1;

}

// Output new char according to the char index
function setNewChar(Char)
{
	// Convert the code to a viet char 
	var newChar = String.fromCharCode(Char);
	// Select a range
	var range = document.selection.createRange();

	range.moveEnd("character", -1); 
	range.expand("character");
	this.curchar = range.duplicate();
	this.curchar.text = newChar;	
	event.returnValue = false; // Discard the new character
}

// Get the previous char from the cursor of the current textbox
function preChar()
{
	var range;
    range = document.selection.createRange();
    range.moveStart("character", -1);
	with(document.VietParkForm)
	{	
		this.curChar = range.duplicate();
		return this.curChar.text;
	}
}

// Handle keydown event
function KeyDown()
{
	var keyCode = event.keyCode - 48;
	var charIndex = getPreCharIndex();

	if(keyCode >= 1 && keyCode <= 9 && charIndex >= 0)
	{	
		switch(keyCode)
		{
			case 1: 
				setNewChar(one[charIndex]);
				break;
			case 2:
				setNewChar(two[charIndex]);
				break;
			case 3: 
				setNewChar(three[charIndex]);
				break;
			case 4: 
				setNewChar(four[charIndex]);
				break;
			case 5: 
				setNewChar(five[charIndex]);
				break; 
			case 6: 
				markSixthAccent(charIndex);					
				break;
			case 7: 
				markSeventhAccent(charIndex);
				break;
			case 8: 
				if(charIndex == 0) // a
					setNewChar(eight[0]);
				else if(charIndex == 12)
					setNewChar(eight[1]);
				break; 
			default: // 9
				if(charIndex == 24) // d
					setNewChar(nine[0]);
				else if(charIndex == 25) // D
					setNewChar(nine[1]);
				break; 
			
		}
	}
}

// Mark the 6th accent ^ to the char
function markSixthAccent(charIndex)
{
	switch(charIndex)
	{
		case 0:  // a
			setNewChar(six[0]);
			break;
		case 3:  // e
			setNewChar(six[1]);
			break;
		case 6:  // o
			setNewChar(six[2]);
			break;
		case 12: // A
			setNewChar(six[3]);
			break;
		case 15: // E
			setNewChar(six[4]);
			break;
		case 18: // O
			setNewChar(six[5]);
			break;
	}
}

// Mark the 7th accent to the char
function markSeventhAccent(charIndex)
{
	switch(charIndex)
	{
		case 6:  // o
			setNewChar(seven[0]);
			break;
		case 9:  // u
			setNewChar(seven[1]);
			break;
		case 18: // O
			setNewChar(seven[2]);
			break;
		case 21: // U
			setNewChar(seven[3]);
			break;
	}
} 
