var reload = false;
var cancel = false;

// transform list of combo boxes into semicolon separated list
// of active dictionaries - i.e. "S_hmi;Normen;MS_rulez"
function GetVisibleDictionaries( doc )
{
    var res = "";
    var frm = doc.forms["VisibleDictionaries"];
    var lng = frm.elements.length;
    for (i=0; i < lng; i++ )
    {
        var tag = frm.elements[i];
        if ( tag.checked )
        {
            res += tag.value + ";";
        }
    }
    // remove last ;
    res = res.substring(0, res.length - 1 );
    doc.forms["mainForm"].id___.value = res;
    return true; // continue submitting the form
}

function DataSourceChange()
{
    var frm = document.forms[0];
    var i = frm.DataSrc_.selectedIndex;
    var filters = frm.Filters.value.split("#")[i];
    var rights = frm.Rights.value.split("#")[i];
    frm.Filter_.options.length = 1;
    frm.Right_.options.length = 1;
    var f = filters.split(";");
    var r = rights.split(";");
    for ( i = 0; i < f.length; i++ )
    {
        if ( f[i].length == 0 )
            continue;
        if ( document.all && document.createElement) // IE
        {
            var oOption = document.createElement("OPTION");
            oOption.text= f[i];
            oOption.value= f[i];
            frm.Filter_.options[frm.Filter_.options.length] = oOption;
        }
        else // NS
        {
            var oOption = new Option( f[i], f[i] );
            frm.Filter_.options[frm.Filter_.options.length] = oOption;
        }
    }
    for ( i = 0; i < r.length; i++ )
    {
        if ( r[i].length == 0 )
            continue;
        if ( document.all && document.createElement ) // IE
        {
            var oOption = document.createElement("OPTION");
            oOption.text= r[i];
            oOption.value= r[i];
            frm.Right_.options[frm.Right_.options.length] = oOption;
        }
        else // NS
        {
            var oOption = new Option( r[i], r[i] );
            frm.Right_.options[frm.Right_.options.length] = oOption;
        }
    }
    frm.Filter_.selectedIndex = 0;
    frm.Right_.selectedIndex = 0;
}

function Cancel()
{
    parent.body.cancel = true; // signal which button caused unload of the document
    parent.window.close();
}

function Update(mustbenumber, cantbeempty, asciiletter, numorempty)
{
    if( parent.body.document.forms[0].beforeSubmit )
        // execute the beforeSubmit script in context of "body" frame
        eval("parent.body."+parent.body.document.forms[0].beforeSubmit.value);
    if ( parent.body.document.forms[0].validate )
    {
        if ( !Validate(mustbenumber, cantbeempty, asciiletter, numorempty) )
            return;
    }
    parent.body.reload = true; // create variable signalizing we should reload parent list
    parent.header.savedUrl = parent.body.location.pathname + parent.body.location.search;
    parent.body.document.forms[0].submit();
}

function onUnload()
{
    if ( reload )
    {
        //parent.opener.location.reload();
        parent.opener.location.href = parent.opener.location.pathname + "?call=getConfig";
    }
    if ( reload || cancel )
        parent.window.close();
}

function Validate(mustbenumber, cantbeempty, asciiletter, numorempty)
{
    var Frm = parent.body.document.forms[0];
    var valStr = Frm.validate.value;
    var elmArr = valStr.split("|");
    var errMsg = "";
    for ( i = 0; i < elmArr.length; i++ )
    {
        if ( elmArr[i].length == 0 )
            continue;
        var prmArr = elmArr[i].split("~");
        var val = eval("Frm." + prmArr[0]).value;
        var type = prmArr[1];
        if ( type == "num" ) // number
        {
            if ( trim(val) == "" || isNaN(val) )
                errMsg += "-- " + prmArr[2] + " " + mustbenumber + "\n";
        }
        else if ( type == "nonempty" ) // nonempty field
        {
            if ( trim(val) == "" )
                errMsg += "-- " + prmArr[2] + " " + cantbeempty + "\n";
        }
        else if ( type == "ident" ) // XML-syntax name
        {
            val = trim(val);
            var re = new RegExp("^([a-z]|[0-9])([a-z]|[0-9]|_)*$","i");
            ok = re.test(val);
            if ( !ok )
                errMsg += "-- " + prmArr[2] + " " + asciiletter + "\n";

        }
        else if ( type == "numorempty" ) // number or empty field
        {
            if ( (!trim(val)=="") && isNaN(val) )
                errMsg += "-- " + prmArr[2] + " " + numorempty + "\n";
        }
    }
    if ( errMsg.length > 0 )
    {
        alert( errMsg );
        return false;
    }
    return true;
}

function trim( str ){
    if( (str.charAt(0) != ' ') && (str.charAt(str.length-1) != ' ') ) {
        return str;
    }
    while( str.charAt(0)  == ' ' ) {
        str = '' + str.substring(1,str.length);
    }
    while( str.charAt(str.length-1)  == ' ' ) {
        str = '' + str.substring(0,str.length-1);
    }
    return str;
}


