function getTopPos(oElement){
	var iTopPos = oElement.offsetTop;
	var oParent = oElement.offsetParent;
	while(oParent){
		iTopPos += oParent.offsetTop;
		oParent = oParent.offsetParent;
	}
	return iTopPos;
}


function getLeftPos(oElement){
	var iLeftPos = oElement.offsetLeft;
	var oParent = oElement.offsetParent;
	while(oParent){
		iLeftPos += oParent.offsetLeft;
		oParent = oParent.offsetParent;
	}
	return iLeftPos;
}


// This function will show all properties for the object passed in...
function displayObjProperties(o) {
	var result = "";
	count = 0;
	for (var i in o) {
		result += o + "." + i + "=" + o[i] + "\n";
		count++;
		if (count == 20) {
			alert(result);
			result = "";
			count = 0;
		}
	}
	alert(result);
}

function Coordinates(o){
	this.left = 0;
	this.top = 0;
	this.right = 0;		
	this.bottom = 0;
	this.object = o;

	this.overLaps = hasOverLap;
	this.overLappedObjects = getOverLappedObjects;
	this.hideOverLappedDropDowns = hideDropDowns;
	this.showOverLappedDropDowns = showDropDowns;
	this.isCursorOver = _isCursorOver;
	this.overLapsTop = _topOverlap;
	this.overLapsLeft = _leftOverlap;
	this.overLapsRight = _rightOverlap;
	this.overLapsBottom = _bottomOverlap;
	this.Refresh = _refreshValues;
	
	this.relativeLeft = _getRelativeLeft;
	this.relativeTop = _getRelativeTop;

	if(o){
		this.left = getLeftPos(o);
		this.top = getTopPos(o);
		this.right = this.left + o.clientWidth;		
		this.bottom = this.top + o.clientHeight;
	}
}

function _getRelativeLeft(){
	var iLeftPos = oElement.style.posLeft;
	var oParent = oElement.offsetParent;
	while(oParent){
		iLeftPos += oParent.style.posLeft;
		oParent = oParent.offsetParent;
	}
	return iLeftPos;
}

function _getRelativeTop(){
	var iTopPos = oElement.style.posTop;
	var oParent = oElement.offsetParent;
	while(oParent){
		iTopPos += oParent.offsetTop;
		oParent = oParent.style.posTop;
	}
	return iTopPos;
}

function _refreshValues(){
	if(this.object){
		this.left = getLeftPos(this.object);
		this.top = getTopPos(this.object);
		this.right = this.left + this.object.clientWidth;		
		this.bottom = this.top + this.object.clientHeight;
	}
}

function _isCursorOver(){
	var iX = window.event.x;
	var iY = window.event.y;
	
	// We're inbetween the coordinates horizontaly
	if(iX > this.left+1 && iX < this.right-1){
		if(iY > this.top+1 && iY < this.bottom-1){
			return true;
		}
	}
	return false;
}

function hasOverLap(coordinateObject){
	var blnReturn = false;

	if(coordinateObject){
		blnReturn = testForOverLap(this.top,this.left,this.right,this.bottom,coordinateObject.top,coordinateObject.left,coordinateObject.right,coordinateObject.bottom);
	}

	return blnReturn;	
}

function getOverLappedObjects(){
	var arrReturn = new Array();
	var sStatus = "";

	var arrElements = document.getElementsByTagName("SELECT");
	for(var i=0;i<arrElements.length;i++){	
		if((arrElements[i].id != this.object.id) || arrElements[i].name != this.object.name){
			if(this.overLaps(new Coordinates(arrElements[i]))){
				arrReturn[arrReturn.length] = arrElements[i];
			}
		}
	}
	
	var arrObjElements = document.getElementsByTagName("OBJECT");
	for(var i=0;i<arrObjElements.length;i++){	
		if((arrObjElements[i].id != this.object.id) || arrObjElements[i].name != this.object.name){
			if(this.overLaps(new Coordinates(arrObjElements[i]))){
				arrReturn[arrReturn.length] = arrObjElements[i];
			}
		}
	}
	
	var arrEmbdElements = document.getElementsByTagName("EMBED");
	for(var i=0;i<arrEmbdElements.length;i++){	
		if((arrEmbdElements[i].id != this.object.id) || arrEmbdElements[i].name != this.object.name){
			if(this.overLaps(new Coordinates(arrEmbdElements[i]))){
				arrReturn[arrReturn.length] = arrEmbdElements[i];
			}
		}
	}
	return arrReturn;	
}

function testForOverLap(sourceTop,sourceLeft,sourceRight,sourceBottom,testTop,testLeft,testRight,testBottom){
	var blnReturn = false;
	
	//Test the left/top
	if(testForPointCollision(sourceLeft,sourceTop,testTop,testLeft,testRight,testBottom)){
		blnReturn = true;
	}
	
	//Test the right/top
	if(testForPointCollision(sourceRight,sourceTop,testTop,testLeft,testRight,testBottom)){
		blnReturn = true;
	}
	
	//Test the left/bottom
	if(testForPointCollision(sourceLeft,sourceBottom,testTop,testLeft,testRight,testBottom)){
		blnReturn = true;
	}
	
	//Test the right/bottom
	if(testForPointCollision(sourceRight,sourceBottom,testTop,testLeft,testRight,testBottom)){
		blnReturn = true;
	}
	
	if(!blnReturn){
		//No point collisions found, look for linear collisions..
		
		//left to right
		if(testForLinearCollision(sourceLeft,sourceRight,testLeft,testRight)){
			//top and bottom
			if(testForLinearCollision(sourceTop,sourceBottom,testTop,testBottom)){
				blnReturn = true;
			}
		}		
	}
	
	return blnReturn;
}


function testForPointCollision(x,y,top,left,right,bottom){
	//Need to know if this point is inside this box..
	//X is side to side
	//Y is up and down
	var blnReturn = false;
	
	if(x >= left && x <= right){
		//is with in the left and right boundries..
		if(y >= top && y <= bottom){
			//Is within the top and bottom boundries
			blnReturn = true;
		}
	}

	return blnReturn;
}

function testForLinearCollision(sourceStart,sourceStop,targetStart,targetStop){
	var blnReturn = false;
	
	//Begins before or at the target start and ends before or at the target stop...
	if(sourceStart <= targetStart && (sourceStop > targetStart && sourceStop <= targetStop)){
		blnReturn = true;
	}
	
	//Begins after or at the target start and ends before or at the target stop...
	if(sourceStart >= targetStart && (sourceStop > targetStart && sourceStop <= targetStop)){
		blnReturn = true;
	}
	
	//Begins after or at the target start and ends after or at the target stop...
	if((sourceStart >= targetStart && sourceStart < targetStop ) && (sourceStop > targetStart && sourceStop >= targetStop)){
		blnReturn = true;
	}
	
	//Begins before the target start and ends after the target stop...
	if(sourceStart < targetStart && sourceStop > targetStop){
		blnReturn = true;
	}
	
	return blnReturn;
}

function _topOverlap(coordinateObject){
	var blnReturn = false;

	if(coordinateObject){
		blnReturn = testForLinearCollision(this.top,this.bottom,coordinateObject.top,coordinateObject.bottom);
	}

	return blnReturn;
}

function _leftOverlap(coordinateObject){
	var blnReturn = false;

	if(coordinateObject){
		blnReturn = testForLinearCollision(this.left,this.right,coordinateObject.left,coordinateObject.right);
	}

	return blnReturn;
}

function _rightOverlap(coordinateObject){
	var blnReturn = false;

	if(coordinateObject){
		blnReturn = testForLinearCollision(this.right,this.left,coordinateObject.right,coordinateObject.left);
	}

	return blnReturn;
}

function _bottomOverlap(coordinateObject){
	var blnReturn = false;

	if(coordinateObject){
		blnReturn = testForLinearCollision(this.bottom,this.top,coordinateObject.bottom,coordinateObject.top);
	}

	return blnReturn;
}

function hideDropDowns(){
	var oHiddenFields = new hiddenFields(this.object.id);
	oHiddenFields.fields = hideDropDownListsInElementArray(this.overLappedObjects());
		
	window.hiddenBoxes[window.hiddenBoxes.length] = oHiddenFields;
}

function showDropDowns(){
	
	//Show any hidden fields
    for(var iChild=0;iChild<window.hiddenBoxes.length;iChild++){
		if(window.hiddenBoxes[iChild].name == this.object.id){
			var arrFields = window.hiddenBoxes[iChild].fields;
			showDropDownListsInElementArray(arrFields);
		}
    }	
}

function hideDropDownListsInElementArray(arrElements){
	var arrHiddenElements = new Array();
	if(arrElements){
		for(var i=0;i<arrElements.length;i++){
			if(arrElements[i]){
				if(arrElements[i].tagName && (arrElements[i].tagName.toLowerCase() == "select" || arrElements[i].tagName.toLowerCase() == "object" || arrElements[i].tagName.toLowerCase() == "embed")){
					if(arrElements[i].style.visibility != "hidden"){
						arrElements[i].style.visibility = "hidden";
						arrElements[i].DynaHide = true;
						arrHiddenElements[arrHiddenElements.length] = arrElements[i];
					}
					else{
						arrHiddenElements[arrHiddenElements.length] = arrElements[i];
					}
				}
			}
		}
	}
	return arrHiddenElements;
}

function showDropDownListsInElementArray(arrElements){
	if(arrElements){
		for(var i=0;i<arrElements.length;i++){
			if(arrElements[i].tagName && (arrElements[i].tagName.toLowerCase() == "select" || arrElements[i].tagName.toLowerCase() == "object" || arrElements[i].tagName.toLowerCase() == "embed")){			
				if(arrElements[i].DynaHide == true){
					arrElements[i].style.visibility = "visible";
					arrElements[i].DynaHide = false;
				}
			}
		}
	}
}

if(!window.hiddenBoxes) window.hiddenBoxes = new Array();
function hiddenFields(name){
	this.name = name;
	this.fields = new Array();
}