/*
    Collection of misc javascript routines used by product selector components.

    PORTIONS TAKEN FROM:
    DHTMLAPI.js
    "Javascript & DHTML Cookbook"
    by Danny Goodman
    Published by O'Reilly & Associates  ISBN 0-596-00467-2
    http://www.oreilly.com
    Copyright 2003 Danny Goodman.  All Rights Reserved.
*/

/**
 * Check for cascading style sheet support.
 */
function isCSS()
    {
    return (document.body && document.body.style) ? true : false;
    }

/**
 * Check for W3C standard implementation.
 */
function isW3C()
    {
    return (isCSS() && document.getElementById) ? true : false;
    }

/**
 * Check for Internet Explorer 4.
 */
function isIE4()
    {
    return (isCSS() && document.all) ? true : false;
    }

/**
 * Check for Netscape Navigator 4.
 */
function isNN4()
    {
    return (document.layers) ? true : false;
    }

/**
 * Check for Internet Explore 6 CSS implementation.
 */
function isIE6CSS()
    {
    return (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    }

/**
 * Find the specified element in the current inclosing document.
 */
function getElement ( elementId )
    {
    return getElementInDoc ( document, elementId );
    }

/**
 * Get the specified element contained in the specified document.
 */
function getElementInDoc ( doc, elementId )
    {
    var elem = null;
    if ( doc.getElementById )
        {
        elem = doc.getElementById(elementId);
        }
    else if ( doc.all )
        {
        elem = doc.all(elementId);
        }
    return elem;
    }        

/**
 *  Calculate the inside width of the browser window.
 */
function getInsideWindowWidth()
    {
    var insideWidth = 0;
    if ( window.innerWidth )
        {
        insideWidth = window.innerWidth;
        }
    else if ( isIE6CSS() )
        {
        insideWidth = document.body.parentElement.clientWidth;
        }
    else if ( document.body && document.body.clientWidth )
        {
        insideWidth = document.body.clientWidth;
        }
    return insideWidth;
    }

/**
 *  Calculate the inside height of the browser window.
 */
function getInsideWindowHeight()
    {
    var insideHeight = 0;
    if ( window.innerHeight )
        {
        insideHeight = window.innerHeight;
        }
    else if ( isIE6CSS() )
        {
        insideHeight = document.body.parentElement.clientHeight;
        }
    else if ( document.body && document.body.clientHeight )
        {
        insideHeight = document.body.clientHeight;
        }
    return insideHeight;
    }


/**
 *  Retrieve the rendered width of an element.
 */
/*
function getObjectWidth(doc,obj)  
    {
    var elem = getElementInDoc(doc,obj);
    var result = 0;
    if (elem.offsetWidth) 
        {
        result = elem.offsetWidth;
        } 
    else if (elem.clip && elem.clip.width) 
        {
        result = elem.clip.width;
        } 
    else if (elem.style && elem.style.pixelWidth) 
        {
        result = elem.style.pixelWidth;
        }
    return parseInt(result);
    }
*/

/**
 * Retrieve the rendered height of an element.
 */
/*
function getObjectHeight(doc,obj)  
    {
    var elem = getElementInDoc(doc,obj);
    var result = 0;
    if (elem.offsetHeight) 
        {
        result = elem.offsetHeight;
        } 
    else if (elem.clip && elem.clip.height) 
        {
        result = elem.clip.height;
        } 
    else if (elem.style && elem.style.pixelHeight) 
        {
        result = elem.style.pixelHeight;
        }
    return parseInt(result);
    }
*/

/**
 * Set the visibility of an object to visible.
 */
function show ( doc, obj ) 
    {
    var theObj = getElementInDoc(doc,obj);
    if (theObj) 
        {
        theObj.visibility = "visible";
        }
    }

/**
 * Center the specified element.
 */
function centerOnWindow ( doc, obj, objWidth, objHeight )
    {
    var scrollX = 0, scrollY = 0;
    if ( document.body && typeof document.body.scrollTop != "undefined" )
        {
        scrollX += document.body.scrollLeft;
        scrollY += document.body.scrollTop;
        if ( document.body.parentNode &&
             typeof document.body.parentNode.scrollTop != "undefined" )
            {
            scrollX += document.body.parentNode.scrollLeft;
            scrollY += document.body.parentNode.scrollTop;
            }
        else if ( typeof window.pageXOffset != "undefined" )
            {
            scrollX += window.pageXOffset;
            scrollY += window.pageYOffset;
            }
        var x = Math.round((getInsideWindowWidth()/2)-(objWidth/2)) + scrollX;
        var y = Math.round((getInsideWindowHeight()/3)-(objHeight/2)) + scrollY;
        theObj = getElementInDoc ( doc, obj );
        theObj.style.left = x;
        theObj.style.top = y;
        }
    }

/**
 *  Enable or disable all input fields on the page.
 */
function enableInput ( status )
    {
    if ( document.all )
        {
        for ( var i = 0; i < document.all.length; i++ )
            {
            if ( document.all[i].tagName == 'INPUT' )
                {
                document.all[i].disabled = !status;
                }

            // disable anchors, img onclicks, ...
            // how can original values be restored when re-enabled?
            /*
            if ( document.all[i].tagName == 'IMG' )
                {
                document.all[i].style.filter = status ? '' : 'gray()';
                document.all[i].onclick = ';';
                }
            */
            }
        }
    }

    /**
     * Change opacity by adjusting alpha filter.
     */
    /*
    function setOpacity ( opacity, id ) 
        {
        var imgStyle = getElement(id).style;
        imgStyle.opacity = (opacity / 100);
        imgStyle.MozOpacity = (opacity / 100);
        imgStyle.KhtmlOpacity = (opacity / 100);
        imgStyle.filter = "alpha(opacity=" + opacity + ")";
        }                
    */

    /**
     * Fade out the specified image.
     */
    /*
    function fadeOut ( image )
        {
        var link = '<%=storePath%>/Form/User/ProductSelector' +
                   '?productSelectorMode=tableMode' +
                   '&selectorName=' + image;
        // var winProperties = 'resizable=yes,scrollbars=yes,width=350,height=300';
        // var millisec = 1000;
        // var speed = Math.round ( millisec / 100 );
        // var timer = 0;
        // for ( i = 100; i >= 0; i-- )
        //     {
        //     setTimeout("setOpacity(" + i + ",'" + image + "')",(timer * speed));
        //     timer++;
        //     }
        // setTimeout ( "openProductSelector('"+image+"', '"+link+"')", millisec );
        setTimeout ( "openProductSelector('"+image+"', '"+link+"')", 10 );
        //setTimeout ( "window.open('"+link+"','productSelectorWindow','','"+winProperties+"','')", 1000 );
        }
    */
