function ShowHide(el, sw, coo)
{
    var s = document.getElementById(el).style;
    var b = (s.display == "none");
    s.display = (b ? "block" : "none");
    s = document.getElementById(sw);
    s.innerHTML = b ? "[-]" : "[+]";
    if(typeof(coo) == "string" && coo.length)
        SetCookie(coo, b ? "1" : "0");
}

function Log(s)
{
    document.getElementById("log").innerHTML += s+"<br/>";
}

function trim(s)
{
    var c;
    while(s.length && ((c = s.substr(0, 1)) == ' ' || c == '\t' || c == '\n' || c == '\r'))
        s = s.substr(1);
    var n;
    while((n = s.length) && ((c = s.substr(n-1, 1)) == ' ' || c == '\t' || c == '\n' || c == '\r'))
        s = s.substr(0, n-1);
    return s;
}

function SetCookie(n, v)
{
    var a = document.cookie.split(";");
    var i;
    for(i = 0; i < a.length; i++)
    {
        var nv = a[i].split("=");
        if(trim(nv[0]) == n)
        {
            a[i] = nv[0] + "=" + v;
            var now = new Date();
            var d = new Date(now.getFullYear() + 5, now.getMonth(), now.getDay());
            
            document.cookie = nv[0] + "=" + v + ";expires=" + d.toGMTString();
            break;
        }
    }
}

function OnStop(e, ui)
{
    SetCookie(ui.options.coo, ui.size.height.toString());
}

function OnResize(e, ui)
{
    ui.options.d.style.height = (ui.size.height - 5).toString() + "px";//(ui.size.height - dy).toString() + "px";
    if(MoreAnchor)
        PlaceUnder(document.getElementById("MorePopup"), MoreAnchor);
}

function h2b(s)
{
    return s;
}

function OnLoad()
{
    $("#srcre").resizable({handles:"s", stop:OnStop, coo : (Res ? "ja" : "jb"), d:document.forms[0].src, resize:OnResize});
    if(Res)
    {
        $("#resultre").resizable({handles:"s", stop:OnStop, coo : "r", d:$("#result")[0], resize:OnResize});
        $("#analysisre").resizable({handles:"s", stop:OnStop, coo : "a", d:$("#analysis")[0], resize:OnResize});
    }
}

function SelectRText()
{
    var d = document.getElementById("result");
    if(document.selection)
    {
        var r = document.body.createTextRange();
        r.moveToElementText(d);
        r.setEndPoint("EndToEnd", r);
        r.select();
    }
    else
    {
        s = window.getSelection();
        var r = document.createRange();
        r.setStartBefore(d);
        r.setEndAfter(d);
        s.addRange(r);
    }
  
    //getSelection().selectAllChildren(document.getElementByTagName('result'));

/*
    var cr = document.body.createRange();
    var t = document.getElementById("result").firstChild;
   
    cr.addElement(t);
    cr.select();
*/    
}

function PlaceUnder(el, anchor)
{
    var y = anchor.offsetTop + anchor.offsetHeight;
    var x = anchor.offsetLeft;
    while(anchor.tagName.toLowerCase() != "html" && anchor.tagName.toLowerCase() != "body")
    {
        y += anchor.offsetParent.offsetTop;
        x += anchor.offsetParent.offsetLeft;
        anchor = anchor.offsetParent; 
    }
    var a = document.getElementById("analysis");
    x -= (document.body.scrollLeft + a.scrollLeft);
    y -= (document.body.scrollTop + a.scrollTop);
    
    el.style.top = y.toString() + "px"; 
    el.style.left = x.toString() + "px";
}

var MoreAnchor = false;

function More(i, j)
{
    MoreAnchor = document.getElementById("more"+i.toString() +"_"+j.toString()),
       p = document.getElementById("MorePopup");
    PlaceUnder(p, MoreAnchor);
    p.style.display = "block";
    p.innerHTML = document.getElementById("mtext"+i.toString() +"_"+j.toString()).innerHTML;
}

function mpoff()
{
    document.getElementById("MorePopup").style.display = "none";
    MoreAnchor = false;
}

function OnAnaScroll()
{
    if(MoreAnchor)
        PlaceUnder(document.getElementById("MorePopup"), MoreAnchor);
}

function ShowAllAna()
{
    if(typeof(AnaCount) == "number")
    {
        var i;
        for(i=0; i<AnaCount; i++)
        {
            var id = "sent" + i.toString();
            if(document.getElementById(id).style.display == "none")
                ShowHide(id, "sentplus"+i.toString());
        }
    }
}
