var g_isIE = navigator.userAgent.indexOf("MSIE") >= 0;
var g_oDragInfo = new Object();

function dragStart(event, id, idTop, idLeft)
{
    if (id)
    {
        g_oDragInfo.elNode = document.getElementById(id);
        g_oDragInfo.elTop = document.getElementById(idTop);
        g_oDragInfo.elLeft = document.getElementById(idLeft); 
    }
    else
    {
        return false;
    }

    var x = 0;
    var y = 0;

    if (g_isIE)
    {
        g_oDragInfo.cursorStartX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        g_oDragInfo.cursorStartY = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
    }
    else
    {
        g_oDragInfo.cursorStartX = event.clientX + window.scrollX;
        g_oDragInfo.cursorStartY = event.clientY + window.scrollY;
    }
    
    g_oDragInfo.elStartLeft = parseInt(g_oDragInfo.elNode.style.left, 10);
    g_oDragInfo.elStartTop = parseInt(g_oDragInfo.elNode.style.top, 10);

    if (isNaN(g_oDragInfo.elStartLeft)) g_oDragInfo.elStartLeft = 0;
    if (isNaN(g_oDragInfo.elStartTop)) g_oDragInfo.elStartTop = 0;

    //g_oDragInfo.elNode.style.zIndex = ++g_oDragInfo.zIndex;

    if (g_isIE)
    {
        document.attachEvent("onmousemove", dragGo);
        document.attachEvent("onmouseup", dragStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    else
    {
        document.addEventListener("mousemove", dragGo, true);
        document.addEventListener("mouseup", dragStop, true);
        event.preventDefault();
    }
}

function dragGo(event)
{

    var x = 0;
    var y = 0;

    if (g_isIE)
    {
        x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
    }
    else
    {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }

    // Move drag element by the same amount the cursor has moved.
    nleft = (g_oDragInfo.elStartLeft + x - g_oDragInfo.cursorStartX);
    ntop  = (g_oDragInfo.elStartTop + y - g_oDragInfo.cursorStartY);
    
    nleft = (nleft < 0) ? 0 : nleft;
    ntop  = (ntop  < 0) ? 0 : ntop;

    if (g_oDragInfo.elLeft && g_oDragInfo.elTop)
    {
        g_oDragInfo.elLeft.value = nleft;
        g_oDragInfo.elTop.value = ntop;
    }
    
    g_oDragInfo.elNode.style.left = nleft + "px";
    g_oDragInfo.elNode.style.top = ntop + "px";
    
    if (g_isIE)
    {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    else
    {
        event.preventDefault();
    }
}

function dragStop(event)
{
    // Stop capturing mousemove and mouseup events.

    if (g_isIE)
    {
        document.detachEvent("onmousemove", dragGo);
        document.detachEvent("onmouseup", dragStop);
    }
    else
    {
        document.removeEventListener("mousemove", dragGo, true);
        document.removeEventListener("mouseup", dragStop, true);
    }
}