/*
v 1.53, 
Made by Mattias Henell, Large Medium AB, mhenell@largemedium.com
*/
var cStart = "";
var cEnd = "";
var os = navigator.userAgent;

if (isNAV4) {
	cStart = "layers['";
	cEnd = "']";
	cStyle = "";
} else if (isNAV6) {
	cStart = "getElementById('";
	cEnd = "')";
	cStyle = ".style";
} else if (isOPERA) {
	cStart = "all.";
	cEnd = "";
	cStyle = ".style";
} else {
	cStart = "all.";
	cEnd = "";
	cStyle = ".style";
}
		
//Convert Object name String or object reference into a valid object reference
function getObject(obj, nestedLayer){
	var theObj
	if (typeof obj == "string") {
		if(nestedLayer && isNAV4) { 
			theObj = eval("document.layers."+nestedLayer+".document."+obj)
		} else {
			theObj = eval("document."+cStart+obj+cEnd);
			//if (DEBUG && !theObj) alert("getObject(not found: '"+obj+"')");
			// get the style portion for theObj, if theObj exists
			if (theObj && (cStyle != "")) {
				theObj = eval("document."+cStart+obj+cEnd+cStyle);
			}
		}
	} else {
		theObj = obj;
	}
	return theObj
}

//positioning an object at a specific pixel coordinate
function shiftTo(obj, x, y, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	theObj.left = x;
	theObj.top = y;
}

//Moving an object by x and / or y pixels
function shiftBy(obj, deltaX, deltaY, nestedLayer) {
//alert('move '+obj+'('+deltaX+','+deltaY+')');
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	deltaX = parseInt(deltaX) + parseInt(getObjectLeft(obj))
	deltaY = parseInt(deltaY) + parseInt(getObjectTop(obj))
	theObj.left = deltaX
	theObj.top = deltaY
}

//Setting the z-order of an object
function setZIndex(obj, zOrder, nestedLayer){
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	theObj.zIndex = zOrder
}

//setting the visibility of an object
function setVisibility(obj, visibility, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	if (theObj) theObj.visibility = visibility;
	else debug("setVisibility('"+obj+"'): object not found", 'ERROR');
}

//Retrieving the x-cordinate of a positionable object
function getObjectLeft(obj, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	return parseInt(theObj.left)
}

//Retrieving the y-cordinate of a positionable object
function getObjectTop(obj, nestedLayer){
	nestedLayer= nestedLayer
	var theObj = getObject(obj, nestedLayer)
	return parseInt(theObj.top)
}

//Changing content of a layer
function reWriteLayer(obj, contentString, nestedLayer){
	if (isNAV6) document.getElementById(obj).innerHTML = contentString
	else if (isIE) document.all[obj].innerHTML=contentString
	else if(isNAV4) {
			theObj = getObject(obj,nestedLayer)
			theObj.document.open();
			theObj.document.write(contentString);
			theObj.document.close();
	}
	else if (isOPERA) return false
}

//clipping a layer
function clipLayer(obj, lengthTop, lenghtRight, lengthBottom, lengthLeft, nestedLayer){
	nestedLayer = nestedLayer
	var theObj = getObject(obj)
	
	if(!isNAV4) {
		var clipString = "rect(" +lengthTop +"," +lenghtRight +"," +lengthBottom +","+ lengthLeft +")"
		theObj.clip = clipString;
		}
	else {
		theObj.clip.top = lengthTop
		theObj.clip.right = lenghtRight
		theObj.clip.bottom = lengthBottom
		theObj.clip.left = lengthLeft
	}
}

//Changing a picture within a layer
function setImage(obj, imgName, imgSrc, nestedLayer) {
	var img;
	if (isNAV4) {
		var myObj = getObject(obj, nestedLayer)
		img = myObj.document.images[imgName];
	} else {
		img = document.images[imgName];
	}
	if (img) img.src = imgSrc;
	else debug("setImage(image not found: '"+imgName+"')", 'ERROR');
}

// LAF: Swaps a layer with the currently visible layer
// limited 1 per page, this could be changed be creating a group
var currentLayer;
function setLayer(layer) {
	if (currentLayer) setVisibility(currentLayer,'hidden');
	setVisibility(layer, 'visible');
	currentLayer = layer;
}

// LAF: submits a form, could be in a layer
function submitForm(name, layerName, nestedLayer) {
	var form = getForm(name, layerName, nestedLayer);
	if (form) form.submit();
	else debug("submitForm(form not found: '"+name+"')", 'ERROR');
}

function getForm(name, layerName, nestedLayer) {
	var form;
	if (isNAV4 && layerName) {
		var myObj = getObject(layerName, nestedLayer)
		form = myObj.document.forms[name];
	} else {
		form = document.forms[name];
	}
	return form;
}

//changing background color in a div
function changeBgColor(obj, color, nestedLayer) {
		obj = getObject(obj,nestedLayer)
		
		if(isNAV4) {
			obj.document.bgColor = color
			}
		
		else {
			obj.backgroundColor = color
		}
}
