/*
 * indic_support.js
 *
 * This page loads the script that handles the key events on the fly. 
 * This page also sets and gets cookie values.
 *
 * Source: http://mail.sarai.net:8080/indic/index.html
 *
 * Copyright (C) 2006 Khader Abbeb N <khader@users.sourceforge.net>
 * Copyright (C) 2006 Srinivas Raghavan <raghavan@servelots.com>
 * Copyright (C) 2006 Surekha Sastry <surekha@servelots.com>
 * Copyright (C) 2006 Vigneswaran R <vignesh@atc.tcs.co.in>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA. 
 *
 */

//variable iexplore will be set if the browser is Internet Explorer
var iexplore = !window.opera && (window.event || document.all); 

var filePath = "";

function setPath()
{
    if(typeof scriptFilePath != 'undefined') {
        filePath = scriptFilePath;
        if (filePath && filePath.substr(-1) != "/")
            filePath += "/";
    }
    else
        filePath = "";
}

/*
This function is called in response to keypress event.
*/
function eventCaptured(evt)
{
//alert("eventCaptured");

    var target,event;

    if(iexplore) {  //if the browser is IE
        event = window.event;
        target = window.event.srcElement;
    }
    else { //for other browsers
        event = evt;
        target = evt.target;
    }

    //hidden field, tells whether convertion of english characters to
    //indic should take place or not
    if(document.frmComments.convertFlag.value != "true")
        return;

    eventHandler(target,event);
    //event handler function of specific file corresponding
    //to specific keyboard layout
}

function togHintMenu() {
//alert("togHintMenu");

    var selectedIndex,keyboardLabel,offset;
    var showHint,convertFlag,selectCombo;

    selectCombo     = document.frmComments.selectLanguage;
    
    selectedIndex   = selectCombo.selectedIndex;
    keyboardLabel   = selectCombo.options[selectedIndex].label;
    offset          = document.frmComments.offset.value;
    convertFlag     = document.frmComments.convertFlag.value;  
    showHint        = document.frmComments.chk_show_hint.checked;
    
    writeCookie(keyboardLabel,selectedIndex,offset,convertFlag,showHint);
}

function selectLang(selectCombo) {
  
    var valueAtIndex,result,selectedIndex,keyboardLabel;
    var showHint,offset,convertFlag,div_keymap_hint,div_lang_info;
    
    div_keymap_hint = document.getElementById("div_keymap_hint");
   
    div_keymap_hint.style.display = 'none';
  
    selectedIndex = selectCombo.selectedIndex;
    keyboardLabel = new String(selectCombo.options[selectedIndex].label);
    showHint      = document.frmComments.chk_show_hint.checked;
    valueAtIndex  = "";
    if (selectedIndex !=0)
    {
    
	div_lang_info= document.getElementById("div_lang_info");
	//alert('div_lang_info'+div_lang_info);
	div_lang_info.style.display = '';	
	
    }
    else
    {
	div_lang_info= document.getElementById("div_lang_info"); 
        div_lang_info.style.display = 'none';
    }
    if(keyboardLabel.indexOf("multi") != 0) {
        valueAtIndex = selectCombo.options[selectedIndex].value;
    }
  
    result        = setOffset(valueAtIndex);

    offset        = result.offset;
    convertFlag   = result.convertFlag;  
  
    document.frmComments.offset.value = offset;
    document.frmComments.convertFlag.value = convertFlag;
  
    if(result.languageSelected) {
        writeCookie(keyboardLabel,selectedIndex,offset,convertFlag,showHint);
    }

    setPath();
 
    if(keyboardLabel == "inscript") {
        loadContent(filePath + "indic_ins.js","keyboardScript");
    }
    else if(keyboardLabel == "wx"){
        loadContent(filePath + "indic_wx.js","keyboardScript");
    }
    else if(keyboardLabel.indexOf("multi") == 0) {
        loadContent(filePath + "indic_multi.js","keyboardScript");
    }
}

function setOffset(value) {

    var languageSelected = true;
    var offset = "",convertFlag = true;

    switch(value) {
      
        case '':
            break;
        case '1': // for english
            offset=0;
            convertFlag = false;
            break;
        case '2' : // for hindi
            offset=0;
            break; 

        case '3' : // for tamil
            offset=(128*5);
            break; 
        case '4' : // for telugu
            offset=(128*6);
            break; 
        case '5' : // for kannada
            offset=(128*7);
            break;
        case '6' : // for malayalam
            offset=(128*8);
            break;
        default: 
            convertFlag = false;
            languageSelected = false;
    }
    return {offset:offset,convertFlag:convertFlag,languageSelected:languageSelected};  
}
	
function writeCookie(keyboardLabel,selectedIndex,offset,convertFlag,showHint) {
//alert("writeCookie");

    var today   = new Date();
    var expires = new Date(today.getTime() + (56 * 86400000));
    var indexes = new String();
    indexes     = keyboardLabel +"-"+ selectedIndex +"-"+ offset +"-"+ convertFlag +"-"+ showHint;
    
    setCookie("indicLangCookie",indexes,expires);
}

function initializeIndicCookie() {
//alert("initializeIndicCookie");
        
    var languageIndex, cookieValue, keyboardLabel, indicCookie;
    var offset, convertFlag, showHint,div_keymap_hint;
    
    div_keymap_hint = document.getElementById("div_keymap_hint");
    div_keymap_hint.style.display = 'none';

    keyboardLabel = new String();  
    indicCookie   = getCookie("indicLangCookie");
  
    if(indicCookie) {
  
        cookieValue     = new String(indicCookie);
        keyboardLabel   = cookieValue.substring(0,cookieValue.indexOf("-"));
        cookieValue     = cookieValue.substring(cookieValue.indexOf("-")+1,cookieValue.length);
        languageIndex   = cookieValue.substring(0,cookieValue.indexOf("-"));
        cookieValue     = cookieValue.substring(cookieValue.indexOf("-")+1,cookieValue.length);
        offset          = cookieValue.substring(0,cookieValue.indexOf("-"));
        cookieValue     = cookieValue.substring(cookieValue.indexOf("-")+1,cookieValue.length);    
        convertFlag     = cookieValue.substring(0,cookieValue.indexOf("-"));
        showHint        = cookieValue.substring(cookieValue.indexOf("-")+1,cookieValue.length);

        //document.frmComments.selectLanguage.options[languageIndex].selected = true;
        document.frmComments.offset.value = offset;
        document.frmComments.convertFlag.value = convertFlag;
        
        showHint = (showHint == "true")? true : false;
        document.frmComments.chk_show_hint.checked = showHint;
    }

    setPath();
 
    if(keyboardLabel == "inscript") {
        loadContent(filePath + "indic_ins.js","keyboardScript");
    }
    else if(keyboardLabel == "wx") {
        loadContent(filePath + "indic_wx.js","keyboardScript");
    }
    else if(keyboardLabel.indexOf("multi") == 0) {
        loadContent(filePath + "indic_multi.js","keyboardScript");
    }
}


/* Functions to Set and Get Cookies */

function getCookie(name) {
  var start = document.cookie.indexOf(name+"=");
  var len = start+name.length+1;
  if ((!start) && (name != document.cookie.substring(0,name.length))) 
    return null;
  if (start == -1) 
    return null;
  var end = document.cookie.indexOf(";",len);
  if (end == -1) 
    end = document.cookie.length;
  return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

/* This method changes the javascript depending on the keyboard layout selected */
function loadContent(file_name,element_name) {
//alert("loadContent "+file_name+" "+element_name);
    var head,script;

    head = document.getElementsByTagName('head').item(0);
    script = document.getElementsByName(element_name).item(0);
    if(script)
        head.removeChild(script);
    script = document.createElement('script');
    script.src = file_name;
    script.type = 'text/javascript';
    script.name = element_name;
    head.appendChild(script);
}
