﻿// JScript File

// Function to handle key image click event for keyboard image.
// All the coding for this function is from http://advancedcentrepunjabi.org/pedic/Default.aspx, with some modifications.
function pad(myValue)
{ 
    // Determines which text control to use for output i.e. checks which div is showing.
    // For grammar checking.
    if(document.getElementById("GrammarChecking").style.display != "none")
    {
        control = document.getElementById("txtMain");    
    }
    // For morph.
    else if(document.getElementById("Morph").style.display != "none")
    {
        control = document.getElementById("txtMorphInput");    
    }
    // For part of speech tagging.
    else if(document.getElementById("Tagging").style.display != "none")
    {
        control = document.getElementById("txtTaggingInput");    
    }
    // For phrase chunking.
    else if(document.getElementById("Chunking").style.display != "none")
    {
        control = document.getElementById("txtChunkingInput");    
    }
    // Returns back, if no input control is available to show output.
    else
    {
        return;
    }
	
	control.focus();

	if (document.selection)
	{
		var selection = document.selection.createRange();
		selection.text = myValue;
	
	}
	else 
	{
	    control.value = control.value + myValue;
	}
	
}

// Function to handle key press events for various text input controls.
function KeyPressed(control, keyEvent)
{
    // Array to hold ascii codes.
    // This mapping is as per AnmolLipi font. This mapping is being used on Learn Punjabi websites of Punjabi University as of April 2008.
    var asciiCodes = new Array("38","60","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","124","126");
    
    // Array to hold unicodes equivalent to ascii codes in above array.
    var uniCodes = new Array("2654","2676","2565","2605","2587","2599","2579","2594","2584","2637,2617","2624","2589","2582","2611","2672","2562","2636","2603","2597","2637,2608","2614","2592","2626","2652","2622,2562","2607","2632","2650","2404","2590","2404,2404","2649","2637,2613","2673","2569","2604","2586","2598","2567","2593","2583","2617","2623","2588","2581","2610","2606","2600","2635","2602","2596","2608","2616","2591","2625","2613","2622","2595","2631","2651","2585","2673");
    
    // Gets the code of the key pressed.
    var keyCode = keyEvent.keyCode;
    
    // For Mozilla/FireFox/Netscape etc.
    if(keyCode == 0)
    {
        keyCode = keyEvent.which;
    }

    // Variable to hold the value to be inserted for this 'keyCode'.
    var keyOutput ="";
    
    // Finds 'keyCode' in 'asciiCodes' and gets its in 'keyOutput'.
    for(i = 0; i < asciiCodes.length; i++)
    {
        if(asciiCodes[i] == keyCode)
        {
            keyOutput = String.fromCharCode(uniCodes[i]);
        }
    }

    // Checks if 'keyOutput' is empty then lets the 'control' insert the default key output for the current keycode.
    if(keyOutput.length == 0)
    {
        //control.focus();
        return true;
    }
    
    // Calls the function to insert 'keyOutput' at current cursor position in 'control'.
    insertValue(keyOutput, control);
    
    return false;
}

// Function to insert 'value' at current cursor position in 'control'.
function insertValue(value, control)
{
    // Code to insert 'value' at current cursor position in 'control'.
    // For IE.
    if(document.selection)
    {
        control.focus();

        // Replaces the current selection in 'control' by 'value'.
        var sel = document.selection.createRange();
        sel.text = value;
    }
    // For Mozilla/Firefox/Netscape 7+.
    else if(control.selectionStart || control.selectionStart == '0')
    {
        // Gets the starting and ending positions of selection.
        var startPos = control.selectionStart;
        var endPos = control.selectionEnd;

        // Inserts 'value' at the cursor position in 'control'.
        control.value = control.value.substring(0,startPos) + value + control.value.substring(endPos, control.value.length);
    }
    // Default behavior, if above conditions not satisfied.
    else
    {
        // Appends 'value' to the end of 'control'.
        control.value += value;
    }

    // Brings focus on 'control'.
    control.focus();    
}