var langTrans;

function get_req()
{
    var http_request = false;
    if (window.XMLHttpRequest)
    {
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    return http_request;
}

function changeLang(lang)
{
    var reqObj = get_req();
    if (reqObj)
    {
        reqObj.open('GET', "langs/"+lang+".js", false);
        reqObj.send(null);
        if(reqObj.status == 200)
        {
            var js = reqObj.responseText;
            window.eval(js);
            transUI();
            if (this != top.frames["mainContent"])
            {
                top.frames["mainContent"].changeLang(lang);
            }
            var expires = new Date();
            expires.setTime (expires.getTime() + (10 * 365 * 24 * 60 * 60 * 1000));
            setCookie("lang", lang, expires);
            //alert("ok");
        }
        else
        {
            var expires = new Date();
            expires.setTime (expires.getTime() + (10 * 365 * 24 * 60 * 60 * 1000));
            setCookie("lang", "en", expires);
        }
    }
}

function transUI()
{
    var elem = document.getElementsByTagName("span");
    var pattern = new RegExp("(^| )trans( |$)");
    if (elem && elem.length>0)
    {
        for (var i=0; i<elem.length; i++)
        {
            if (pattern.test(elem[i].className) && elem[i].title)
            {
                var t = langTrans[ elem[i].title ];
                if (t != undefined)
                {
                    elem[i].innerHTML = t;
                }
            }
        }
    }
    //alert("OK!");
}

function setCookie(name, value, expires)
{
    cookieStr = name + "=" + escape(value);
    if (expires)
    {
        cookieStr += "; expires=" + expires.toGMTString();
    }
    document.cookie = cookieStr;
}

function getCookie(Name) 
{
    var search = Name + "="
    if (document.cookie.length > 0) { 
        // if there are any cookies
        offset = document.cookie.indexOf(search)
        if (offset != -1)
        { 
            // if cookie exists
            offset += search.length

            // set index of beginning of value
            end = document.cookie.indexOf(";", offset)

            // set index of end of cookie value
            if (end == -1)
            end = document.cookie.length

            return unescape(document.cookie.substring(offset, end))
        }
    }
    return 0;
}

