/*
  Copyright (C) 2001-2009 CODERESEARCH, All rights reserved.

  Redistribution and modification of this code is strictly prohibited.

  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED.  IN NO EVENT SHALL CODERESEARCH OR ITS CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * since    2000/09/22
 * version     $Id: xdhlib.js 2312 2009-05-07 09:30:17Z ylept $
 * author     Yves Lepthien
 */

// Global variables.
var DOM = (document.getElementById) ? true : false;
var OP = (window.opera) ? true : false;
var OP5h = (OP && DOM) ? true : false;
var NS = (window.outerWidth && !OP) ? true : false;
var NS6h = (NS && DOM) ? true : false;
var NS4 = (NS && !DOM) ? true : false;
var IE = (document.all && !OP)? true : false;
var IE5h = (IE && DOM) ? true : false;
var IE4 = (IE && !DOM) ? true : false;   

/**
 * Window dimensioning.
 */
function getInnerWidth(pWindow) {
    if (IE)
        return pWindow.document.body.clientWidth;
    else
        return pWindow.innerWidth;
}

function getInnerHeight(pWindow) {
    if (IE)
        return pWindow.document.body.clientHeight;
    else
        return pWindow.innerHeight;
}


/**
 * Object grabbing.
 */

function getObject(pName) {
    if (DOM) {
        return document.getElementById(pName);
    }

    if (IE) {
        return eval('document.all.' + pName);
    }

    if (NS4) {
        return findObject(pName, document);
    }

    return null;
}

function findObject(pName, pDocument) {
  var i, tObject;

    for (i = 0; i < pDocument.layers.length; i++) {
    tObject = pDocument.layers[i];
    if (tObject.name == pName)
        return tObject;
    if (tObject.document.layers.length > 0)
        if ((tObject = findLayer(pName, tObject.document)) != null)
        return tObject;
    }
    return null;
}

/**
 * Visibility.
 */
function showObject(pObject) {
    var tVisible = (NS4) ? "show" : "visible";

    if (NS4) {
        if (pObject)
            pObject.visibility = tVisible;
    } else {
        if (pObject)
            pObject.style.visibility = tVisible;
    }
}

function hideObject(pObject) {
    var tVisible = (NS4) ? "hide" : "hidden";
    if (NS4) {
        if (pObject)
            pObject.visibility = tVisible;
    } else {
        if (pObject)
            pObject.style.visibility = tVisible;
    }
}

function setVisible(pObject, pValue) {
    if (pValue) {
        showObject(pObject);
    } else {
        hideObject(pObject);
    }
}

function setVisibleInherit(pObject) {
    if (NS4)
        pObject.visibility = "inherit";
    else
        pObject.style.visibility = "inherit";
}

function isVisible(pObject) {
    if (!pObject) return false;

    if (NS4) {
        if (pObject.visibility == "show")
            return true;
        if (pObject.visibility == "hide")
            return false;
        if (pObject.visibility == "visible")
            return true;
        if (pObject.visibility == "hidden")
            return false;
    } else {
        if (pObject.style.visibility == "visible")
            return true;
        if (pObject.style.visibility == "hidden")
            return false;
    }
    return false;
}

/**
 * Positioning.
 */

function translateTo(pObject, pX, pY) {
    if (NS4)
        pObject.moveTo(pX, pY);
    else {
        pObject.style.left = pX + 'px';
        pObject.style.top  = pY + 'px';
    }
}

function translateBy(pObject, pX, pY) {
    if (NS4)
        pObject.moveBy(pX, pY);
    else {
        pObject.style.left = (parseInt(pObject.style.left) + pX) + 'px';
        pObject.style.top  = (parseInt(pObject.style.top ) + pY) + 'px';
    }
}

function resizeTo(pObject, pWidth, pHeight) {
    if (NS4)
        pObject.resizeTo(pWidth, pHeight);
    else {
        pObject.style.width = pWidth + 'px';
        pObject.style.height = pHeight + 'px';
    }
}

function resizeBy(pObject, pWidth, pHeight) {
    if (NS4)
        pObject.resizeBy(pWidth, pHeight);
    else {
        pObject.style.width = (parseInt(pObject.style.width) + pWidth) + 'px';
        pObject.style.height  = (parseInt(pObject.style.height) + pHeight) + 'px';
    }
}

function getLeft(pObject) {
    if (NS4)
        return pObject.left;
    else
        return pObject.style.pixelLeft;
}

function getTop(pObject) {
    if (NS4)
        return pObject.top;
    else
        return pObject.style.pixelTop;
}

function getRight(pObject) {
    if (NS4)
        return pObject.left + getWidth(pObject);
    else
        return pObject.style.pixelLeft + getWidth(pObject);
}

function getBottom(pObject) {
    if (NS4)
        return pObject.top + getHeight(pObject);
    else
        return pObject.style.pixelTop + getHeight(pObject);
}

function getPageXOffset(pObject) {
    var x;

    if (NS4)
        return pObject.pageX;
    else {
        x = 0;
        while (pObject.offsetParent != null) {
            x += pObject.offsetLeft;
            pObject = pObject.offsetParent;
        }
        x += pObject.offsetLeft;
        return x;
    }
}

function getPageYOffset(pObject) {
    var y;

    if (NS4)
        return pObject.pageY;
    else {
        y = 0;
        while (pObject.offsetParent != null) {
            y += pObject.offsetTop;
            pObject = pObject.offsetParent;
        }
        y += pObject.offsetTop;
        return y;
    }
}

function getWidth(pObject) {
    if (NS4) {
        if (pObject.document.width)
            return pObject.document.width;
        else
            return pObject.clip.right - pObject.clip.left;
    } else {
        if (pObject.style.pixelWidth)
            return pObject.style.pixelWidth;
        else if (pObject.clientWidth)
            return pObject.clientWidth;
        else
            return pObject.offsetWidth;
    }
}

function getHeight(pObject) {
    if (NS4) {
        if (pObject.document.height)
            return pObject.document.height;
        else
            return pObject.clip.bottom - pObject.clip.top;
    } else {
        if (pObject.style.pixelHeight)
            return pObject.style.pixelHeight;
        else if (pObject.clientHeight)
            return pObject.clientHeight;
        else
            return pObject.offsetHeight;
    }
}

function getZIndex(pObject) {
    if (NS4)
        return pObject.zIndex;
    else
        return pObject.style.zIndex;
}

function setZIndex(pObject, pZIndex) {
    if (NS4)
        pObject.zIndex = pZIndex;
    else
        pObject.style.zIndex = pZIndex;
}

/**
 * Clipping
 */

function clipLayer(pLayer, pClipleft, pCliptop, pClipright, pClipbottom) {

    if (NS4) {
        pLayer.clip.left   = pClipleft;
        pLayer.clip.top    = pCliptop;
        pLayer.clip.right  = pClipright;
        pLayer.clip.bottom = pClipbottom;
    } else
        pLayer.style.clip = 'rect(' + pCliptop + 'px ' +  pClipright + 'px ' + pClipbottom + 'px ' + pClipleft +'px)';
}

function getClipLeft(pLayer) {

    if (NS4)
        return pLayer.clip.left;

    else {
        var tStr = pLayer.style.clip;
        if (!tStr)
            return 0;

        var tClip = parseClipValues(pLayer.style.clip);
        return(tClip[3]);
    }
}

function getClipTop(pLayer) {

    if (NS4)
        return pLayer.clip.top;

    else {
        var tStr = pLayer.style.clip;
        if (!tStr)
            return 0;

        var tClip = parseClipValues(pLayer.style.clip);
        return tClip[0];
    }
}

function getClipRight(pLayer) {

    if (NS4)
        return pLayer.clip.right;

    else {
        var tStr = pLayer.style.clip;
        if (!tStr)
            return pLayer.style.pixelWidth;

        var tClip = parseClipValues(pLayer.style.clip);
        return tClip[1];
    }
}

function getClipBottom(pLayer) {

    if (NS4)
        return pLayer.clip.bottom;

    else {
        var tStr = pLayer.style.clip;
        if (!tStr)
            return pLayer.style.pixelHeight;

        var tClip = parseClipValues(pLayer.style.clip);
        return tClip[2];
    }
}

function getClipWidth(pLayer) {

    if (NS4)
        return pLayer.clip.width;

    else {
        var tStr = pLayer.style.clip;
        if (!tStr)
            return pLayer.style.pixelWidth;

        var tClip = parseClipValues(pLayer.style.clip);
        return tClip[1] - tClip[3];
    }
}

function getClipHeight(pLayer) {

    if (NS4)
        return pLayer.clip.height;

    else {
        var tStr = pLayer.style.clip;
        if (!tStr)
            return pLayer.style.pixelHeight;

        var tClip = parseClipValues(pLayer.style.clip);
        return tClip[2] - tClip[0];
    }
}

function parseClipValues(pStr) {

    var tClip = new Array();
    var i;

    i = pStr.indexOf("(");
    tClip[0] = parseInt(pStr.substring(i + 1, pStr.length), 10);

    i = pStr.indexOf(" ", i + 1);
    tClip[1] = parseInt(pStr.substring(i + 1, pStr.length), 10);

    i = pStr.indexOf(" ", i + 1);
    tClip[2] = parseInt(pStr.substring(i + 1, pStr.length), 10);

    i = pStr.indexOf(" ", i + 1);
    tClip[3] = parseInt(pStr.substring(i + 1, pStr.length), 10);

    return tClip;
}

/**
 * Scrolling
 */

function scrollLayerTo(pLayer, pX, pY, pBound) {
    var dx = getClipLeft(pLayer) - pX;
    var dy = getClipTop(pLayer) - pY;

    scrollLayerBy(pLayer, -dx, -dy, pBound);
}

function scrollLayerBy(pLayer, pX, pY, pBound) {
    var cl = getClipLeft(pLayer);
    var ct = getClipTop(pLayer);
    var cr = getClipRight(pLayer);
    var cb = getClipBottom(pLayer);

    if (pBound) {
        if (cl + pX < 0)
            pX = -cl;
        else if (cr + pX > getWidth(pLayer))
            pX = getWidth(pLayer) - cr;

        if (ct + pY < 0)
            pY = -ct;
        else if (cb + pY > getHeight(pLayer))
            pY = getHeight(pLayer) - cb;
    }
    clipLayer(pLayer, cl + pX, ct + pY, cr + pX, cb + pY);
    translateBy(pLayer, -pX, -pY);
}

/**
 * Image.
 */

function getImage(pName) {
//    if (NS4) {
        return findImage(pName, document);
//    }
//    if (IE)
//        return eval('document.all.' + pName);

//    return null;
}

function findImage(pName, pDocument) {
    var i, tImage;

    for (i = 0; i < pDocument.images.length; i++)
        if (pDocument.images[i].name == pName)
            return pDocument.images[i];

    for (i = 0; i < pDocument.layers.length; i++)
        if ((tImage = findImage(pName, pDocument.layers[i].document)) != null) {
            tImage.container = pDocument.layers[i];
            return tImage;
        }

    return null;
}

function getImagePageLeft(pImage) {

    var x, tObj;

    if (NS4) {
        if (pImage.container != null)
            return pImage.container.pageX + pImage.x;
        else
            return pImage.x;
    } else {
        x = 0;
        tObj = pImage;

        while (tObj.offsetParent != null) {
            x += tObj.offsetLeft;
            tObj = tObj.offsetParent;
        }
        x += tObj.offsetLeft;
        return x;
    }
}

function getImagePageTop(pImage) {

    var y, tObj;

    if (NS4) {
        if (pImage.container != null)
            return pImage.container.pageY + pImage.y;
        else
            return pImage.y;
    } else {
        y = 0;
        tObj = pImage;
        while (tObj.offsetParent != null) {
            y += tObj.offsetTop;
            tObj = tObj.offsetParent;
        }
        y += tObj.offsetTop;
        return y;
    }
}

function focusNext(pSize, pCurrent, pNext, pPrev) {
    if (pCurrent.value.length == pSize) {
        pNext.focus();
        //pNext.select();
    }
}
