<% /*
    $Revision$
    $Id$

    File:           Shared/locale.inc
    Description:    Kiest en initialiseert de juiste taal voor de applicatie.
                    Bij het inloggen is deze bepaald en in Session("user_lang") gezet.
                    Deze is dan altijd gedefinieerd (evt. met defaultwaarde) en uppercase.

*/

/* global */ cache_lcls = [];

if (typeof THIS_FILE != "undefined")
    lcl_path = THIS_FILE
else
{
    var lcl_path = String(Request.ServerVariables("SCRIPT_NAME"));
    // canonical maken
    lcl_path = lcl_path.substring(rooturl.length + 1); // die er af
}
lcl_path = lcl_path.toLowerCase().replace(/\\/g,"/"); // Trigger maakt er ook lowercase van in database

// Shortcut
function L(p_lcl, params)
{
    return lcl.L(p_lcl, params);
}

// Let op: komt ook nog terug voor clientside in date.js
calendar_names = {  // Compatible met datepicker
    				days: ["zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag", "zondag"],
    				daysShort: ["Zon", "Maa", "Din", "Woe", "Don", "Vri", "Zat", "Zon"],
    				daysMin: ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za", "Zo"],
    				months: ["januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"],
    				monthsShort: ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"],
    				weekMin: "wk"
    			  }
if ( Session("user_lang") == "EN") {
    calendar_names = { // Compatible met datepicker
    					days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
    					daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
    					daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
    					months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
    					monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
    					weekMin: "wk"
     				}
}
else if ( Session("user_lang") == "DE") {
calendar_names =  { // Compatible met datepicker
    					days: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
    					daysShort: ["Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam", "Son"],
    					daysMin: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"],
    					months: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
    					monthsShort: ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
    					weekMin: "wo"
    				}
}
else if ( Session("user_lang") == "FR") {
calendar_names =  { // Compatible met datepicker
    					days: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"],
    					daysShort: ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim"],
    					daysMin: ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa", "Di"],
    					months: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
    					monthsShort: ["Jan", "Fév", "Mar", "Avr", "Mai", "Jun", "Jul", "Aoû", "Sep", "Oct", "Nov", "Déc"],
    					weekMin: "se"
    				}
}
else if ( Session("user_lang") == "NO") {
calendar_names =  { // Compatible met datepicker
    					days: ["søndag", "mandag", "tirsdag", "onsdag", "torsdag", "fredag", "lørdag", "søndag"],
    					daysShort: ["søn", "man", "tir", "ons", "tor", "fre", "lør", "søn"],
    					daysMin: ["sø", "ma", "ti", "on", "to", "fe", "lø", "sø"],
    					months: ["januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "desember"],
    					monthsShort: ["jan", "feb", "mar", "apr", "mai", "jun", "jul", "aug", "sep", "okt", "nov", "dec"],
    					weekMin: "uk"
    				}
}
else if ( Session("user_lang") == "SV") {
calendar_names =  { // Compatible met datepicker
    					days: ["söndag", "måndag", "tisdag", "onsdag", "torsdag", "fredag", "lördag", "söndag"],
    					daysShort: ["sön", "mån", "tis", "ons", "tor", "fre", "lör", "sön"],
    					daysMin: ["sö", "må", "ti", "on", "to", "fe", "lö", "sö"],
    					months: ["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"],
    					monthsShort: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"],
    					weekMin: "vk"
    				}
}
else if ( Session("user_lang") == "DA") {
calendar_names =  { // Compatible met datepicker
            			days: ["søndag", "mandag", "tirsdag", "onsdag", "torsdag", "fredag", "lørdag", "søndag"],
            			daysShort: ["søn", "man", "tir", "ons", "tor", "fre", "lør", "søn"],
            			daysMin: ["sø", "ma", "ti", "on", "to", "fe", "lø", "sø"],
            			months: ["januar", "februar", "marts", "april", "maj", "juni", "juli", "august", "september", "oktober", "november", "desember"],
            			monthsShort: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"],
            			weekMin: "ug"
    				}
}
else if ( Session("user_lang") == "FI") {
calendar_names =  { // Compatible met datepicker
            			days: ["sunnuntai", "maanantai", "tiistai", "keskiviikko", "torstai", "perjantai", "lauantai", "sunnuntai"],
            			daysShort: ["sun", "maa", "tii", "kes", "tor", "per", "lau", "sun"],
            			daysMin: ["su", "ma", "ti", "ke", "to", "pe", "la", "su"],
            			months: ["tammikuu", "helmikuu", "maaliskuu", "huhtikuu", "toukokuu", "kesäkuu", "heinäkuu", "elokuu", "syyskuu", "lokakuu", "marraskuu", "joulukuu"],
            			monthsShort: ["tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mar", "jou"],
            			weekMin: "vi"
    				}
}

lcl = {
        languages : { "NL": "Nederlands",
                      "EN": "English",
                      "DA": "Danish",
                      "DE": "Deutsch",
                      "FI": "Finnish",
                      "FR": "Français",
                      "NO": "Norwegian",
                      "SV": "Swedish"
                    },

        hascached: false,

		// params.autolcl zet je als je niet zeker weet of het een lcl is (srtnotificatie en zo)
        L: function _L(p_lcl, params)
        {
            params = params || {};
            if (Session("logging")&4)
                return "^" + p_lcl;

            if (params.autolcl && p_lcl.substr(0,4) != "lcl_")
                return p_lcl;

            if (!this.hascached)
            {
                if (typeof LCL_Disable != "undefined")
                {
                    __Log("LCL_Disable stond aan maar toch LCL Loading triggered by: " + p_lcl + "<br>Suboptimaal maar niet problematisch", "#0ff");
                    //INTERNAL_ERROR_LCL_Disable;
                }
                __Log("LCL Loading triggered by: " + p_lcl);
                this.loadLCL();
            }

            if (!(p_lcl in cache_lcls) || cache_lcls[p_lcl].overrulefallback)
            {   // Laatste keer individueel uit de database ophalen
                var sql = "SELECT fac_locale_xsl_key,"
                        + "       COALESCE(fac_locale_xsl_cust, fac_locale_xsl_tekst) fac_locale_xsl_tekst"
                        + "  FROM fac_locale_xsl"
                        + " WHERE fac_locale_xsl_lang = " + safe.quoted_sql(user_lang)
                        + "   AND fac_locale_xsl_label = " + safe.quoted_sql(p_lcl)
                        + "   AND fac_locale_xsl_module = 'ASP'";
                var oRs = Oracle.Execute(sql);
                var locale_xsl_key = -1;
                if (!oRs.Eof)
                {
                    locale_xsl_key = oRs("fac_locale_xsl_key").Value;
                    cache_lcls[p_lcl] = { txt: oRs("fac_locale_xsl_tekst").Value };
                }
                oRs.Close();
                if (locale_xsl_key == -1)
                {
                    if (!cache_lcls[p_lcl])
                    {
                        cache_lcls[p_lcl] = { txt: "??" + safe.html(p_lcl) + "??" }; // zelfs geen overrulefallback
                        __DoLog("{0} LCL '{1}' is undefined!".format(user_lang, p_lcl), "#F00");
                    }
                    else
                    {
                        __DoLog("{0} LCL '{1}' is undefined but overruled to '{2}'".format(user_lang, p_lcl, cache_lcls[p_lcl].txt), "#FF0");
                    }
                    return cache_lcls[p_lcl].txt||"";
                }
                else
                {
                    // Hij was er nog niet. Tijd om te gaan cachen!
                    // Merk op dat we dat doen per taal, wat licht inefficient is. Als een label in NL gebruikt wordt in een
                    // bestand dan gebeurt dat voor andere talen natuurlijk ook.
                    sql = "INSERT INTO fac_locale_xref(fac_locale_xref_filepath, fac_locale_xsl_key)"
                        + "     VALUES (" + safe.quoted_sql(lcl_path) + ", "
                        +                 locale_xsl_key + ")";
                    Oracle.Execute(sql);
                }

            }
            return cache_lcls[p_lcl].txt||"";
        },

        loadLCL: function(path)
        {
            path = path||lcl_path;
            var sql = "SELECT fac_locale_xsl_label, "
                    + "       COALESCE(fac_locale_xsl_cust, fac_locale_xsl_tekst) fac_locale_xsl_tekst,"
                    + "       fac_locale_xref_client"
                    + "  FROM fac_locale_xsl xsl, fac_locale_xref xref"
                    + " WHERE xsl.fac_locale_xsl_key = xref.fac_locale_xsl_key"
                    + "   AND xref.fac_locale_xref_filepath = " + safe.quoted_sql(path)
                    + "   AND fac_locale_xsl_lang = " + safe.quoted_sql(user_lang)
                    + "   AND fac_locale_xsl_module = 'ASP'";

            var nn = 0;
            var oRs = Oracle.Execute(sql);
            while (!oRs.Eof)
            {
                cache_lcls[oRs("fac_locale_xsl_label").Value] = { txt: oRs("fac_locale_xsl_tekst").Value||"", client: oRs("fac_locale_xref_client").Value == 1 };
                oRs.MoveNext();
                nn++;
            }
            oRs.Close();
            this.hascached = true;

            // Optionele overrules in cust/custenv.wsc en cust/XXXX/custenv.inc
            // Waren al ingelezen in common.inc/tryOverrule(). Nu toepassen.
            var naam;
            for (naam in overrule_lcl)
            {
                if (cache_lcls[naam])
                {
                    cache_lcls[naam].orgtxt = cache_lcls[naam].txt;
                    cache_lcls[naam].txt = overrule_lcl[naam];
                }
                else
                    cache_lcls[naam] = { txt: overrule_lcl[naam], overrulefallback: true };
            }

            __Log("LCL's cached: " + nn);
        },

        buildClientLCL: function ()
        {
            if (typeof LCL_Disable != "undefined")
                return;
            if (!this.hascached)
            {
                __Log("LCL Loading triggered by buildClientLCL()");
                this.loadLCL();
            }
            var lcl;
            var nn=0;
            for (lcl in cache_lcls)
            {
                nn++;
                if (cache_lcls[lcl].client)
                    Response.Write("\nLCL[\"" + safe.jsstring(lcl) + "\"]=\"" + safe.jsstring(cache_lcls[lcl].txt||"") + "\";");
            }
            if (Session("logging")&64)
            {   // Achteraan elke pagina een linkje naar de lcl-edit
                %>
                if (window.$)
                    window.$(function () {
                        url = 'appl/fac/fac_locale_list_std.asp?sFile=<%=safe.htmlattr(lcl_path)%>';
                        var html = "<span id='lcllink' onclick='FcltMgr.openDetail(url, \"<%=safe.jsstring(lcl_path)%>\")'>lcl <%=safe.html(lcl_path)%> (<%=nn%>)</span>";
                        $("body").append(html);
                     });
                <%
            }
        },

        xsql2:  // standaard zonder kolom-alias
            function (pkolomnaam, lclcode, pkey, pwithalias, noautolcl) {
                var llocaleclause;
                 var parts = pkolomnaam.split(".");
                if (parts.length ==1)
                    var alias = pkolomnaam;
                else
                    var alias = parts[1];
                if (!lclcode) // moet een FAC_LOCALE.FAC_LOCALE_KOLOMNAAM waarde zijn
                    lclcode = alias;
                // Als de user-taal de db-taal (nu nog hard NL) is gewoon pkolomnaam opleveren
                if (S("multi_language_option") == 0 || S("language_toggles").length <= 1 || user.lang() == S("db_lang"))
                    llocaleclause = pkolomnaam+" "+(pwithalias ? alias+" " : "");
                else
                    llocaleclause = " lcl.x('"+lclcode+"', "+pkey+", "+pkolomnaam+", " + (noautolcl?0:1) + ")"+(pwithalias ? alias+" " : "");
                if (Session("logging")&4)
                    llocaleclause = "'$'||"+ llocaleclause;
                return llocaleclause;
            },
        xsql:  // standaard zonder kolom-alias
            function (pkolomnaam, pkey, pwithalias, noautolcl) {
                return lcl.xsql2(pkolomnaam, null, pkey, pwithalias, noautolcl);
            },
        xsqla2:   // met kolom-alias
            function (pkolomnaam, lclcode, pkey) {
                return lcl.xsql2(pkolomnaam, lclcode, pkey, true);
            },
        xsqla:   // met kolom-alias
            function (pkolomnaam, pkey) {
                return lcl.xsql(pkolomnaam, pkey, true);
            },
        set_dialect:
            function _set_dialect (selector_key, selector_id)
            {
                if (!this.hascached)
                {
                    __Log("LCL Loading triggered by set_dialect()");
                    this.loadLCL();
                }
                // cache_lcls is nu nog gevuld met database taal. Dat gaan we overrulen
                // Fallback naar databasetaal?
                var plus = Session("logging")&4?"(+)":""; // zodat we alles met @ kunnen markeren wat *vertaalbaar* is
                                                          // (anders komt alleen naar voren wat vertaald *is*)
                var sql = "SELECT fac_localeitems_lcl, fac_locale_tekst"
                        + "  FROM fac_locale fl, fac_localeitems fli"
                        + " WHERE fl.fac_localeitems_key"+plus+" = fli.fac_localeitems_key"
                        + "   AND fl.fac_locale_dialect_key"+plus+" = " + selector_key
                        + "   AND fli.fac_localeitems_dialect_id = " + safe.quoted_sql(selector_id)
                        + "   AND fl.fac_locale_lang"+plus+" = " + safe.quoted_sql(user.lang());
                var oRs = Oracle.Execute(sql);
                while (!oRs.Eof)
                {
                    var tekst = oRs("fac_locale_tekst").Value;
                    if (Session("logging")&4) // @@=vertaalbaar, @=vertaald
                    {
                        if (tekst)
                            tekst = "@"+tekst; // markeren dattie is vertaald
                        else
                        {
                            if (oRs("fac_localeitems_lcl").Value in cache_lcls) // dan maar markering op origineel
                            {
                                tekst = "@@" + cache_lcls[oRs("fac_localeitems_lcl").Value].txt;
                            }
                            else
                            {
                                // Waarschijnlijk vervallen in FAC_LCLASP.inc maar nog niet gedropt uit FAC_LOCALEITEMS
                                // Of: (bijvoorbeeld mld_melding.asp) wel een set_dialect maar veel lcl's worden
                                //     in dit bestand helemaal niet gebruikt
                                __SafeLog("<span style='color:#f00;'>Dialect label not used in this file: " + oRs("fac_localeitems_lcl").Value + "</span>");
                            }
                        }
                    }
                    if (oRs("fac_localeitems_lcl").Value in cache_lcls)
                        cache_lcls[oRs("fac_localeitems_lcl").Value].txt = tekst; // de echte overrule
                    oRs.MoveNext();
                }
                oRs.Close();
            }
}

%>
