/*
 * list.js
 * This file is part of Entrans
 *
 * Copyright (C) 2005 Khader Abbeb N
 * Copyright (C) 2005 R.Vigneswaran 
 *
 * 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 classAttrib = (iexplore)? "className":"class";
var scriptFilePath = "scripts/";

document.onkeypress = eventCaptured;// For IE & Netscape
document.onkeydown = ieEvent;  //for IE

function ieEvent(evt)      //keypress event can't recognize control keys
{
    var key;
    if(iexplore) {
        key = window.event.keyCode;
        if(window.event.altKey && key != 18 || key == 8 || key == 46) {
        //key value 18 is for 'alt' key itself , 8 for backspace, 46 for delete
            eventCaptured(evt);
        }
    }
}

/*
This function is called in response to keypress event. This inturn calls the eventHandler function by sending
the target element and the event, if convertion is needed from english to indic language.
*/
function eventCaptured(evt)
{
//alert("eventCaptured");
    var target,event;
    var keyCode,button_name;

    if(iexplore) {  //if the browser is IE
        event = window.event;
        target = window.event.srcElement;
        keyCode = event.keyCode;    //get the key code of the key which triggers the event
    }
    else { //for other browsers
        event = evt;
        target = evt.target;
        keyCode = event.which;
    }

//alert(keyCode);
    if(event.altKey) {
        switch(keyCode) {
            case 107:              //Keyboard Layout
            case 75:
                document.indicForm.selectLanguage.focus();
                prevent(event);
                break;
            case 104:              //Hint Menu - Toggling
            case 72:
                if(document.getElementById('div_keymap_hint').style.display == 'none') {
                    break;
                }
                if(document.indicForm.chk_show_hint.checked) {
                    document.indicForm.chk_show_hint.checked = false;
                }
                else {
                    document.indicForm.chk_show_hint.checked = true;
                }
                togHintMenu();      //function has been defined inside 'IndicSupport.js'
                prevent(event);
                break;
            case 115:              //Suggestion
            case 83:
                document.edit_form.user_sug.focus();
                prevent(event);
                break;
        }
        if(keyCode == 13 && target.type == "textarea" && target.getAttribute(classAttrib) == "indic_ime") {
            submitAndMove();
        }

        return;
    }
    if(target.type == "textarea" && target.getAttribute(classAttrib) == "indic_ime") {
        if(keyCode == 13) {
            prevent(event);              //disable ENTER key, no newline characters allowed
            return;
        }
        else if(document.edit_all && !isCtrlKey(event,iexplore)) {
            txtarea_name = target.getAttribute('name');
            str_id       = txtarea_name.substr(txtarea_name.lastIndexOf("_")+1);
            chk_box     = document.getElementsByName('select_'+str_id)[0];

            chk_box.checked = true;
        }
    }
    if(document.indicForm.convertFlag.value != "true")  //hidden field, tells whether convertion should take place or not
        return;
    eventHandler(target,event);  //event handler of specific file that handles specific keyboard layout
}

function prevent(event) {
    if(event.preventDefault){
        event.preventDefault();
    }
    else {
        event.returnValue = false;
    }
    if(iexplore) {
        //if the key combination is already defined for some shortcuts
        //the following line is necessary to prevent that functionality 
        window.event.keyCode=0;
    }
}

function getScrollPos()
{
    var scrollTop;
    
    if (self.pageYOffset)   // all except Explorer
        scrollTop = self.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop) {
        // Explorer 6 Strict
        scrollTop = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
        scrollTop = document.body.scrollTop;

    return scrollTop;
}

function setScrollPos(scrollTop)
{
    window.scrollTo(0,scrollTop);
}

function reDirect(f_edit,r_edit,f_skip,r_skip,f_scrollPos)
{
    var href = window.location.pathname + window.location.search;
    var new_href;
    
	href = href.replace("&edit=" + f_edit,"");
    if(r_edit != "")
        href += "&edit=" + r_edit;
        
	href = href.replace("&skip=" + f_skip,"");
    if(r_skip != "")
        href += "&skip=" + r_skip;
        
	href = href.replace("&scrollPos=" + f_scrollPos,"");
    href += "&scrollPos=" + getScrollPos();
	
    window.location	= href;

    return false;               //to stop loading the anchor's href
}

function warnLang()
{
    window.alert("Please select the language of your translation");
    return false;               //to stop loading the anchor's href
}

var groupClass;

function myGetElementByClass(classname)	// Get all Html elements belonging to particular 'class'
{
    groupClass	= new Array();
    var ind=0;
    var allTags = (document.all)?document.all:document.getElementsByTagName("*");
    for (i=0;i<allTags.length;i++)
    {
	if (allTags[i].className == classname)
	{
	   groupClass[ind++] = allTags[i];
	}
    }
}
	
function togClass(classname)	// Hide or Display html elements of a specific 'Class'
{				// Used to Toggle width of Valid Translation/Most Popular suggestion <DIV>
    myGetElementByClass(classname);
    for ( j=0; j<groupClass.length;j++)
    {
    	groupClass[j].style.display =(groupClass[j].style.display=='none')?'block':'none';
    }
    var a_view = document.getElementById('a_view');
    if(a_view.firstChild.nodeValue == 'View Translation')
	a_view.firstChild.nodeValue = 'Hide Translation';
    else
	a_view.firstChild.nodeValue = 'View Translation';

    togWidth('tdtrans');
		
}

function togWidth(classname)	// Change the width of <TD> elements by class
{				// Used to Toggle width of Valid Translation/Most Popular suggestion <TD>
    myGetElementByClass(classname);
    width = groupClass[0].getAttribute("width");
    if(width != 1)
    {
        for ( i = 0; i<groupClass.length; i++)
    	groupClass[i].setAttribute("width","1");
    }
    else
    {
        for ( i = 0; i<groupClass.length; i++)
    	groupClass[i].setAttribute("width","40%");
    }
		
}

function togDiv(id)		// Hide or Display html element of a specific id
{				// Not Used.
    el = document.getElementById(id);
    if (el.style.display == 'none')
    {
	el.style.display = 'block';
    }
    else
    {
	el.style.display = 'none';
    }
}

    function submitAndMove()
    {
        var lang_sug    = document.edit_form.lang_sug.value;
        if (!lang_sug)
        {
            window.alert("Please select the language of your translation");
            return;
        }

        var loc = document.getElementsByName('edit_form')[0].action;
        loc += "&scrollPos=" + getScrollPos();

        document.getElementsByName('edit_form')[0].action = loc;
        document.edit_form.submit();
    }

    function submitAllAndMove()
    {
        var lang_sug    = document.edit_all.lang_sug.value;
        if (!lang_sug)
        {
            window.alert("Please select the language of your translation");
            return;
        }
	document.edit_all.submit();
    }

    function checkAll()
    {
	var i;
	myGetElementByClass("checkstring");
	for(i=0; i<groupClass.length;i++)
	{
	    groupClass[i].checked	= true; 
	}
	
    }

    function clearAll()
    {
	var i;
	myGetElementByClass("checkstring");
	for(i=0; i<groupClass.length;i++)
	{
	    groupClass[i].checked 	= false; 
	}
    }

    function placeFocus() 
    {
        var field="";
        if (document.edit_form) 
        {
            field = document.edit_form;
        }
        else if (document.edit_all)
        {
            field = document.edit_all;
        }
        if(field != "")
        {
            for (i = 0; i < field.length; i++) 
            {
                if ((field.elements[i].type == "textarea")) 
                {
                    field.elements[i].focus();
                    break;
                }
            }
        }
    }

    function setLang(old_lang, edit)
    {
        var lang_sel  	= document.lang_form.lang_sel;
        var index     	= lang_sel.selectedIndex;
        var lang 	= lang_sel.options[index].value;
        var href	= window.location.href;
        var new_href  	= href.replace('&lang='+old_lang, "&lang="+lang);
	if (href == new_href)
	    new_href	= href + '&lang=' + lang;

        new_href  	= new_href.replace('&edit='+edit, "");
        new_href  	= new_href.replace('&edit_all=yes', "");

        window.location = new_href;
    }

