// Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
//
// ************************
// layer utility routines *
// ************************

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if (document.getElementById && document.getElementById(objectId)) {
        // W3C DOM
        return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
        // MSIE 4 DOM
        return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
        // NN 4 DOM
        return getObjNN4(document,objectId);
    } else {
        return false;
    }
} // getStyleObject

function getObject(objectId) {
    // cross-browser function to get an object given its id
    if (document.getElementById && document.getElementById(objectId)) {
        // W3C DOM
        return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
        // MSIE 4 DOM
        return document.all(objectId)
    } else if (document.layers && document.layers[objectId]) {
        // NN 4 DOM
        return getObjNN4(document,objectId);
    } else {
        return false;
    }
} // getObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if (styleObject) {
        styleObject.visibility = newVisibility;
        return true;
    } else {
        // we couldn't find the object, so we can't change its visibility
        return false;
    }
} // changeObjectVisibility

function changeObjectBackground(objectId, newBackground) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if (styleObject) {
        styleObject.background = newBackground;
        return true;
    } else {
        // we couldn't find the object, so we can't change its background
    return false;
    }
} // changeObjectBackground

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if (styleObject) {
        styleObject.left = newXCoordinate + 'px';
        styleObject.top = newYCoordinate + 'px';
        return true;
    } else {
        // we couldn't find the object, so we can't very well move it
        return false;
    }
} // moveObject

function setLyr(obj,lyr)
{
    var newX = findPosX(obj);
    var newY = findPosY(obj);
    var x = new getObject(lyr);
    x.style.top = newY + 'px';
    x.style.left = newX + 'px';
    return x;
}

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x){
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

function getWidth(obj)
{
    var width = 0;
    if (obj.offsetWidth)
    {
        width = obj.offsetWidth
    }
    else {
        width = parseInt(getStyleProperty(obj, 'width'));
    }
    return width;
}

function getHeight(obj)
{
    var height = 0;
    if (obj.offsetHeight)
    {
        height = obj.offsetHeight;
    }
    else {
        height = parseInt(getStyleProperty(ob, 'height'));
    }
    return height;
}

function getStyleProperty(obj, property)
{
    var y = null;
    if (obj.currentStyle) {
        var y = obj.currentStyle[property];
    }
    else if (window.getComputedStyle) {
        var y = document.defaultView.getComputedStyle(obj, null).getPropertyValue(property);
    }
    return y;
}

function append(s, a) {
    return s + a;
}

function remove(s, t) {
    i = s.indexOf(t);
    r = "";
    if (i == -1) {
        return s;
    }
    r = s.substring(0,i) + s.substring(i + t.length, s.length + 1);
    alert(r);
    return r;
}

function removeAll(s, t) {
    i = s.indexOf(t);
    r = "";
    if (i == -1) {                                                              
        return s;
    }
    r += s.substring(0,i) + remove(s.substring(i + t.length), t);
    return r;
}
 
