//this function returns an array of the window height and width
function windowSize() {
	var returnValue = Array();
	returnValue['height'] = Math.max(Math.max(Geometry.getWindowY(), Geometry.getViewportHeight()),Math.max(Geometry.getVerticalScroll(), Geometry.getDocumentHeight()));
	returnValue['width'] = Math.max(Math.max(Geometry.getWindowX(), Geometry.getViewportWidth()),Math.max(Geometry.getHorizontalScroll(), Geometry.getDocumentWidth()));

	return returnValue;
}

/**
 * displayBackground - creates a semitransparent background to display over the page
 * There is an onlclick event handler added to the item to hide everything too
 */
function displayBackground() {
	var windowDims			= windowSize();
	var x					= windowDims['width'];
	var y					= windowDims['height'];
	objBody 				= document.body;
	objDiv 					= document.createElement("div");
	objDiv.style.width 		= x + 'px';
	objDiv.style.height 	= y + 'px';
	//This line was added solely for this site and should NOT be used elsewhere
	objDiv.style.marginTop 	= '-' + y + 'px';
	objDiv.className 		= "tempBackground";
	objDiv.style.float		= "left";
	objDiv.style.position	= "absolute";
	objDiv.style.backgroundColor	= "#000";

	if(document.all) {
		objDiv.style.opacity = "0.60";
		objDiv.style.filter = "alpha(opacity=60)";
		objDiv.style.MozOpacity = '0.6';
	}
	else { objDiv.style.opacity = "0.60"; objDiv.style.mozOpacity = "0.6"; }

	objDiv.style.zIndex		= "999";

	objDiv.setAttribute('id','tempBackground');
	objDiv.onclick = function() {removeModalMessage();};
	objBody.appendChild(objDiv);
}

/**
 * displayMessage - creates a message to display
 * @param string strMessage the text message to dislay
 * @param string strId the id of the object to display near
 */
function displayMessage(strMessage,strId) {

	obj = document.getElementById(strId);
	objBg = document.body;
	newObj = document.createElement("div");
	newObjTxt = document.createTextNode(strMessage);
	newObj.appendChild(newObjTxt);
	newObj.className = 'modalMessage';

	//Position the element
	newObj.style.top = findPosY(obj) - 30 + 'px';
	newObj.style.left = findPosX(obj) + (obj.offsetWidth / 2) + 'px';

	newObj.style.color = '#000';
	newObj.style.paddingTop = '8px';
	newObj.style.paddingLeft = '18px';
//	if(document.all) {
//		newObj.filter = 'DXImageTransform.Microsoft.AlphaImageLoader(src=\'img/chick.png\', sizingMethod=\'image\')';
//	}

	//Append the new element to the body
	objBg.appendChild(newObj);

}

/**
 * displayModalMessage - creates a modal message to display to the user
 * @param array errorMessages
 */
function displayModalMessage(errorMessages) {
	if(document.getElementById("modalMessage") == null) {
		//Initialize the background before creating the error popups
		//displayBackground();
		for(var i in errorMessages) {
			displayMessage(errorMessages[i]['message'], errorMessages[i]['fieldName']);
		}
	}
}
/**
 * removeModalMessage - loops through all of the elements attached to the modal background
 * eliminates them, then eliminates the background itself
 */
function removeModalMessage() {
	obj = document.getElementById('tempBackground');
	objBody = document.body;
	var doomedChildren = getElementsByClassName(document, "div", "modalMessage");
	for(var i in doomedChildren) {
		if(doomedChildren[i] && doomedChildren[i].nodeType == 1) {
			objBody.removeChild(doomedChildren[i]);
		}
	}
	objBody.removeChild(obj);
}

/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/
/**
 * getElementsByClassName - returns an array of elements of a certain type and classname
 * @param object oElm The root item to search through
 * @param string strTagName The type of elements to look for
 * @param string strClassName The class name to search for
 * @return array an array of certain type and class name
 */
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}





/**
 * whiteBackgrounds - this function addresses a bug in Firefox. For some
 * reason, random textfield backgrounds are a different color than they
 * should be.
 * @author Cory Collier
 * @param string strId The id of the form element to fix
 */
function whiteBackgrounds(strId) {
	obj = document.getElementById(strId);
	for(var i in obj.elements) {
		objTxt = obj.elements[i];
		if(objTxt.type == 'text' || objTxt.type == 'textarea') {
			objTxt.style.background = '#ffffff';
		}
	}
}

/**
 * findPosX - this function finds the x coordinate of a given object by summing the x coordinates of each parent object
 * @param object obj The DOM element to find the x coordinate of
 * @return int The x coordinate
 */
function findPosX(obj) {
	var curleft = 0;
    if(obj.offsetParent) {
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    } else if(obj.x) {
		curleft += obj.x;
    }
    return curleft;
  }

/**
 * findPosY - this function finds the Y coordinate of a given object by summing the Y coordinates of each parent object
 * @param object obj The DOM element to find the x coordinate of
 * @return int The x coordinate
 */
 function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent) {
				 break;
			}
			obj = obj.offsetParent;
       }
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
 }
