var tempX = 0;
var tempY = 0;

// parameters for Hint window
var offsetX = -20;
var offsetY = -15;
var n=false;
var ie=false;
//

if (document.layers) {n=1;ie=0;}
if (document.all) {n=0;ie=1;}
if (n==1) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;

function init() {
    if (n) { hintstyle = document.blockDiv; hintblock = document.blockDiv;}
    if (ie) { hintstyle = blockDiv.style; hintblock = blockDiv;}
}

function hideHint(){
    document.getElementById("blockDiv").style.display='none';
}

function showHint(SHtext){
    var scroll_x,scroll_y;
    if (self.pageYOffset) // all except Explorer
    {
        scroll_x = document.body.scrollLeft;
        scroll_y = document.body.scrollTop;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
        scroll_x = document.documentElement.scrollLeft;
        scroll_y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        scroll_x = document.body.scrollLeft;
        scroll_y = document.body.scrollTop;
    }
    document.getElementById("blockDiv").style.left=scroll_x+tempX+offsetX+"px";
    document.getElementById("blockDiv").style.top=scroll_y+tempY+offsetY+"px";
    document.getElementById("blockDiv").style.display='block';
    document.getElementById("blockDiv").innerHTML="<div style=\"position: absolute; width: 250px; border: 2px solid #666; line-height: 1.6em; padding: 5px; background: #eee; right: 0px; bottom: 0px;\">"+SHtext+"</div>";
}

function hideHintDiv(divId){
    document.getElementById(divId).style.display='none';
}

function showHintDiv(divId){
    var scroll_x,scroll_y;
    if (self.pageYOffset) // all except Explorer
    {
        scroll_x = document.body.scrollLeft;
        scroll_y = document.body.scrollTop;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
        scroll_x = document.documentElement.scrollLeft;
        scroll_y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        scroll_x = document.body.scrollLeft;
        scroll_y = document.body.scrollTop;
    }
    document.getElementById(divId).style.left=scroll_x+tempX+offsetX+"px";
    document.getElementById(divId).style.top=scroll_y+tempY+offsetY+"px";
    document.getElementById(divId).style.display='block';
}

function showLargeHintDiv(divId){
    var scroll_x,scroll_y;
    if (self.pageYOffset) // all except Explorer
    {
        scroll_x = document.body.scrollLeft;
        scroll_y = document.body.scrollTop;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
        scroll_x = document.documentElement.scrollLeft;
        scroll_y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        scroll_x = document.body.scrollLeft;
        scroll_y = document.body.scrollTop;
    }
    document.getElementById(divId).style.left="381px";
    document.getElementById(divId).style.top=scroll_y+tempY+offsetY+"px";
    document.getElementById(divId).style.display='block';
}

function getMouseXY(e) {
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)
    {
        tempX = e.pageX;
        tempY = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        if (null != e.clientX && null != document.body) {
            tempX = e.clientX + document.body.scrollLeft;
        }
        if (null != e.clientY && null != document.body) {
            tempY = e.clientY + document.body.scrollTop;
        }
    }
    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}
    showThrobber();
    return true
}
    
function initiateOnClick(id) {
    if (document.createEventObject) {
        document.getElementById(id).fireEvent('onclick');
    } else if (document.createEvent) {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent('click', true, false );
        document.getElementById(id).dispatchEvent(evObj);
    }
    return false;
}

function countCharsAdd() {
    if (null != document.getElementById("variaForm:paperText")) {
        var maxChars = 80;
        var val = $('variaForm:paperText').value;
        if (val.length > maxChars) {
            $('variaForm:paperText').value = val.substring(0, maxChars);
        }
        $('symbols').value = maxChars - $('variaForm:paperText').value.length > -1 ? maxChars - $('variaForm:paperText').value.length : 0;
    }
}

function countCharsWebAdd() {
    var maxChars = 2000;
    var val = $('variaForm:webText').value;
    if (val.length > maxChars) {
        $('variaForm:webText').value = val.substring(0, maxChars);
    }
    $('symbolsWeb').value = maxChars - $('variaForm:webText').value.length > -1 ? maxChars - $('variaForm:webText').value.length : 0;
}

function showPapersAdd() {
    if (null != document.getElementById("variaForm:showPapersCheckBox")) {
        if (null != document.getElementById("papersGroup")) {
            if (null != document.getElementById("variaForm:paperText")) {
                if ($('variaForm:showPapersCheckBox').checked) {
                    $('papersGroup').style.display = '';
                    $('variaForm:paperText').disabled = false;
                }
                else {
                    $('papersGroup').style.display = 'none';
                    $('variaForm:paperText').disabled = true;
                }
            }
        }
    }
}

function countCharsChange() {
    if (null != document.getElementById("changeForm:paperText")) {
        var maxChars = 75;
        var val = $('changeForm:paperText').value;
        if (val.length > maxChars) {
            $('changeForm:paperText').value = val.substring(0, maxChars);
        }
        $('symbols').value = maxChars - $('changeForm:paperText').value.length > -1 ? maxChars - $('changeForm:paperText').value.length : 0;
    }
}

function countCharsWebChange() {
    var maxChars = 2000;
    var val = $('changeForm:webText').value;
    if (val.length > maxChars) {
        $('changeForm:webText').value = val.substring(0, maxChars);
    }
    $('symbolsWeb').value = maxChars - $('changeForm:webText').value.length > -1 ? maxChars - $('changeForm:webText').value.length : 0;
}

function showPapersChange() {
    if (null != document.getElementById("changeForm:showPapersCheckBox")) {
        if (null != document.getElementById("papersGroup")) {
            if (null != document.getElementById("changeForm:paperText")) {
                if ($('changeForm:showPapersCheckBox').checked) {
                    $('papersGroup').style.display = '';
                    $('changeForm:paperText').disabled = false;
                }
                else {
                    $('papersGroup').style.display = 'none';
                    $('changeForm:paperText').disabled = true;
                }
            }
        }
    }
}

function markTwinPapersAdd(chk) {
    if (chk.id == 'variaForm:hasWeb_PP' || chk.id == 'variaForm:hasWeb_PB') {
        if ($('variaForm:hasWeb_PP')) $('variaForm:hasWeb_PP').checked = chk.checked;
        if ($('variaForm:hasWeb_PB')) $('variaForm:hasWeb_PB').checked = chk.checked;
    } else if (chk.id == 'variaForm:hasWeb_YL' || chk.id == 'variaForm:hasWeb_SK') {
        if ($('variaForm:hasWeb_YL')) $('variaForm:hasWeb_YL').checked = chk.checked;
        if ($('variaForm:hasWeb_SK')) $('variaForm:hasWeb_SK').checked = chk.checked;
    }
}

function markTwinPapersChange(chk) {
    if (chk.id == 'variaForm:hasWeb_PP' || chk.id == 'variaForm:hasWeb_PB') {
        if ($('changeForm:hasWeb_PP')) $('changeForm:hasWeb_PP').checked = chk.checked;
        if ($('changeForm:hasWeb_PB')) $('changeForm:hasWeb_PB').checked = chk.checked;
    } else if (chk.id == 'changeForm:hasWeb_YL' || chk.id == 'changeForm:hasWeb_SK') {
        if ($('changeForm:hasWeb_YL')) $('changeForm:hasWeb_YL').checked = chk.checked;
        if ($('changeForm:hasWeb_SK')) $('changeForm:hasWeb_SK').checked = chk.checked;
    }
}

function showThrobber(){
    if (null != document.getElementById("thr")) {
        if (document.getElementById("thr").style.display == '') {
            var scroll_x,scroll_y;
            if (self.pageYOffset) {// all except Explorer
                scroll_x = document.body.scrollLeft;
                scroll_y = document.body.scrollTop;
            }
            else if (document.documentElement && document.documentElement.scrollTop) {
                // Explorer 6 Strict
                scroll_x = document.documentElement.scrollLeft;
                scroll_y = document.documentElement.scrollTop;
            }
            else if (document.body) {// all other Explorers
                scroll_x = document.body.scrollLeft;
                scroll_y = document.body.scrollTop;
            }
            if (null != document.getElementById("thr")) document.getElementById("thr").style.left=scroll_x+tempX+offsetX+28+"px";
            if (null != document.getElementById("thr")) document.getElementById("thr").style.top=scroll_y+tempY+offsetY+9+"px";
        }
    }
}

function resizeMap() {
    var img = document.getElementById('mapImage');
    if (img != null && img != undefined) {
        var maxw = 340;
        var maxh = 235;

        var width = img.width;
        var height = img.height;

        if (img.width >= maxw) {
            if (img.height < maxh) {
                width = maxw;
                height = (img.height * maxw) / img.width;            
            } else if (img.height >= maxh) {
                var heightRatio = img.height / maxh;
                var widthRatio = img.width / maxw;

                if (heightRatio >= widthRatio) {
                    height = maxh;
                    width = img.width / heightRatio;
                } else if (heightRatio < widthRatio) {
                    width = maxw;
                    height = img.height / widthRatio;
                }
            }
        } else if (img.width < maxw) {
            if (img.height >= maxh) {
                height = maxh;
                width = (img.width * maxh) / img.height;
            } else if (img.height < maxh) {
                height = img.height;
                width = img.width;
            }
        }

        if (width != 0 && height != 0) {
            img.width = width;
            img.height = height;
        }
    }
}

function alignDivs() {
    if (document.getElementById('ib1').offsetHeight > document.getElementById('ib2').offsetHeight) {
        document.getElementById('ib2').style.height = (document.getElementById('ib1').offsetHeight - 22) + 'px';
    } else if (document.getElementById('ib1').offsetHeight < document.getElementById('ib2').offsetHeight) {
        document.getElementById('ib1').style.height = (document.getElementById('ib2').offsetHeight - 22) + 'px';
    }
}