//
//  MODULE:  L:/RCS/lagancentre/code/com/lagan/lagancentre/core/eform/jsp/GeneralFunctions.js
//  VERSION: 1.7
//
//  DESCRIPTION: Javascript functions that are called by the form
//

//==========================================================================================
//==========================================================================================
//
//  FUNCTION:   isAllowedChars()
//
//  DESCRIPTION
/**
 *  This function will restrict the allowed characters in a text field to the given argument in
 *  the first parameter.
 */
//==========================================================================================
//==========================================================================================
function isAllowedChars(all, letters, numbers, allowedChars, enterallowed, keyEvent)
{
    var LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
    var DIGITS = "0123456789";
    var allowed = "";

    if (letters == "true")
    {
        allowed += LETTERS;
    }

    if (numbers == "true")
    {
        allowed += DIGITS;
    }

    if (allowedChars != "") allowed += allowedChars;

    var key;
    var keychar;

    if (window.event)
    {
        key = window.event.keyCode;
    }
    else if (keyEvent)
    {
        key = keyEvent.which;
    }
    else
    {
        return true;
    }

    keychar = String.fromCharCode(key);
    //keychar = keychar.toLowerCase();

    // Check if any of the control keys have been pressed
    if ((key==null)//Null
        || (key==0)//Null
        || (key==8)//Backspace
        || (key==9)//Tab
        || (key==27)//Escape
        )
    {
        return true;
    }
    else if (key==13)
    {
        if (enterallowed=="true")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else if (all == "true")
    {
        return true;
    }

    // Check if any of the allowed characters have been pressed
    else if (allowed.indexOf(keychar) > -1)
    {
        return true;
    }
    else
    {
        return false;
    }

}// End of isAllowedChars()

//==========================================================================================
//==========================================================================================
//
//  FUNCTION:   isValidTime()
//
//  DESCRIPTION
/**
 *  This function is used to format a correct time.
 *
 */
//==========================================================================================
//==========================================================================================
function isValidTime(timeField)
{
    var objRegExp = /^\d{0,2}(\:){0,1}\d{0,2}$/
    var dString = timeField.value;

    //  no point in checking an empty string
    if ((dString == null) || (dString == ""))
    {
        return;
    }

    if (objRegExp.test(dString))
    {
        //  split up string around the colon (may not be one)
        var split_time=dString.split(":");

        //  might be nothing on lhs of colon
        if (split_time[0].length == 0)
        {
            split_time[0] = '0';
        }

        //  might be nothing on rhs of colon (or no colon)
        if ((split_time.length == 1) || (split_time[1].length == 0))
        {
            var timestr = split_time[0];
            if (timestr.length >= 3)
            {
                //  lhs of colon is too long, break in half -
                //  3 digits goes 1 for hour and 2 for minute
                //  4 digits goes 2 and 2
                split_time[0] = timestr.substring(0, timestr.length-2);
                split_time[1] = timestr.substring(timestr.length-2, timestr.length);
            }
            else
            {
                //  lhs of colon is 1 or 2, use that as the hour and 0 for the minute
                split_time[1] = '0';
            }
        }

        //  fill in the time field
        timeField.value = padOutDate(split_time[0]) + ':' + padOutDate(split_time[1]);

        //  check the hour and minute are valid values
        if (split_time[0] < 0 || split_time[0] > 23)
        {
            alert("Hour must be between 0 and 23.");
            timePart.focus();
            return;
        }
        if (split_time[1] < 0 || split_time[1] > 59)
        {
            alert("Minute must be between 0 and 59.");
            timeField.focus();
            return;
        }
    }
    else
    {
        alert('Not a valid time');
        timeField.focus();
        return;
    }
}


//==========================================================================================
//==    fill up the left and right chooser boxes from a comma-seperated list
//==========================================================================================

function fillComboFromField(Realvalue, Leftbox, Rightbox, choiceLabels, choiceValues)
{
    //  remove all rows from the left and right box
    RemoveAll(Leftbox);
    RemoveAll(Rightbox);

    //  split up the input string into an array
    var chosenOnes=Realvalue.split(",");

    var leftrow = 0;
    var rightrow = 0;
    var rowselected = 0;

    //  go through all the possible choices
    for (choiceid = 0; choiceid < choiceValues.length; choiceid++)
    {
        //  go through all the chosen values
        rowselected = 0;
        for (chosenid = 0; chosenid < chosenOnes.length; chosenid++)
        {
            //  found a match, add it to the rhs
            if (chosenOnes[chosenid] == choiceValues[choiceid])
            {
                Rightbox.options[rightrow] =
                    new Option(choiceLabels[choiceid], chosenOnes[chosenid]);
                rightrow++;
                rowselected = 1;
                break;
            }
        }

        //  no match on chosen values, add it to the lhs
        if (rowselected == 0)
        {
            Leftbox.options[leftrow] =
                new Option(choiceLabels[choiceid], choiceValues[choiceid]);
            leftrow++;
        }
    }
}

//  clear a combo box
function RemoveAll(obj)
{
    for (row = obj.length; row >= 0;   row--)
    {
        obj.remove(0);
    }
}

function moveAllValues(Lobj, Robj)
{
    for (optionid=0; optionid < Lobj.length; optionid+1)
    {
        Robj.options[Robj.options.length] =
            new Option(Lobj.options[optionid].text, Lobj.options[optionid].value);

        Lobj.remove(0);
    }
}

function moveAllValues1(Lobj, Robj, Realvalue)
{
    for (optionid=0; optionid < Lobj.length; optionid+1)
    {
        Robj.options[Robj.options.length] =
            new Option(Lobj.options[optionid].text, Lobj.options[optionid].value);

        Lobj.remove(0);
    }

    Realvalue.value = fillRealobjValues(Robj);
}

function moveValuesRight(Lobj, Robj, value, Realvalue)
{
    if (value != null && value != -1)
    {
        Robj.options[Robj.options.length] =
            new Option(Lobj.options[value].text, Lobj.options[value].value);

        Lobj.options[value] = null;

        Realvalue.value = fillRealobjValues(Robj);

    }
}

function moveValuesLeft(Lobj, Robj, value, Realvalue)
{
    if (value != null && value != -1)
    {
        Lobj.options[Lobj.options.length] =
            new Option(Robj.options[value].text, Robj.options[value].value);

        Robj.options[value] = null;

        Realvalue.value = fillRealobjValues(Robj);

    }
}

function fillRealobjValues(Robj)
{
    var temp = "";

    if (Robj.options.length == 0)
    {
        return temp;
    }
    else if (Robj.options.length == 1)
    {
        temp = temp + Robj.options[0].value;
    }
    else if (Robj.options.length > 1)
    {
        for (optionid=0; optionid < Robj.options.length-1; optionid++)
        {
            temp = temp + Robj.options[optionid].value + ",";
        }

        temp = temp + Robj.options[Robj.options.length-1].value;
    }

    return temp;
}

function printit()
{
    if ((navigator.appName == "Netscape"))
    {
        window.print() ;
    }
    else
    {
        var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
        document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
        WebBrowser1.ExecWB(6, -1);
        WebBrowser1.outerHTML = "";
    }
}

function fillComboWithValues(obj, list)
{
    for (groupid=0; groupid < list.length; groupid++)
    {
        obj.options[groupid] = new Option(list[groupid], list[groupid]);
    }
}
