function showDiv(divId)
{
    var elem = document.getElementById(divId);
    if (divId) {
        elem.style.display = 'block';
    } else {
        alert('Javascript error: cannot show element: ' + divId);
    }
}

function hideDiv(divId)
{
    var elem = document.getElementById(divId);
    if (divId) {
        elem.style.display = 'none';
    } else {
        alert('Javascript error: cannot hide element: ' + divId);
    }
}

function toggleDiv(divId)
{
    var elem = document.getElementById(divId);
    if (divId) {
        if (elem.style.display=='block') {
            elem.style.display = 'none';
        } else {
            elem.style.display = 'block';
        }
    } else {
        alert('Javascript error: cannot hide element: ' + divId);
    }
}

function copyDiv(sourceDivId, targetDivId)
{
    var elem1 = document.getElementById(sourceDivId);
    var elem2 = document.getElementById(targetDivId);
    if (elem1 && elem2) {
        elem2.innerHTML = elem1.innerHTML;
    } else {
        alert('Javascript error: cannot copy layer');
    }
}

function clearDiv(divId)
{
    var elem = document.getElementById(divId);
    if (elem) elem.innerHTML = '';
}

function getElementPosition(elemID)
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}


function rand(intMin, intMax)
{
    return intMin + Math.floor(Math.random()*(intMax+1));
}

function popupWindow(url,width,height,options)
{
    var randomName = Math.round(Math.random()*1000);
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var default_options = 'height='+height+',width='+width+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
    //toolbar=no,location=no,directories=no,resizable=no,status=no,
    //alert(default_options + ',' + options);

    var new_options = default_options;
    if (options!='') { new_options = (new_options + ',' + options); }
    var winHandle = window.open(url, 'popup'+randomName, new_options);
    winHandle.focus();
}

function centerPopupWindow(url, windowName, width, height, options) {
    var windowHandle;
    var left = (screen.width-width)/2;
    var top = (screen.height-height)/3;
    if (options!='') {
        param = "," + options;
    } else {
        param = "";
    }
    windowHandle = window.open(url, windowName, "width="+width+",height="+height+",left="+left+",top="+top+param);
    windowHandle.focus();
}
