/* /system/residentportal/200607/js_client/library.js
 *
 * Created: 06/02/2006
 * Author: Ryan Sullivan
 * Common JavaScript functionality
 */ 

// ResidentPortal object
var ResidentPortal = Object;

	/*
	 * findElement(tagID)
	 *
	 * Returns the element in the HTML document that corresponds to the given ID.
	 * Will generate a warning (and return null) if the element is not found.
	 */
	ResidentPortal.findElement = function(tagID)
	{
		var oElem = null;
		if (document.getElementById) oElem = document.getElementById(tagID);
		else if (document.all) oElem = document.all[tagID];
		else if (document.layers) oElem = document.layers[tagID];
		else if (/[^\w\_]/g.test(tagID) == false)
			eval('if (document.' + tagID + ') oElem = document.' + tagID + ';');
		
		if (oElem == null) debugLog.warn('Element with ID "' + tagID + '" was not found.');
		
		return oElem;
	}
	
	/*
	 * breakOut()
	 *
	 * This is used to break resident portal out of frames.
	 */
	ResidentPortal.breakOut = function()
	{
		if (top != self) top.location.href = self.location.href;
	}

	/*
	 * getBoxModel(el)
	 *
	 * Returns an array of TRBL dimensions for any element, with
	 * cross-browser support.
	 */
	ResidentPortal.getBoxModel = function(el)
	{
		var box;
		var boxdim = [-1, -1, -1, -1];
		if (el.getBoundingClientRect) // IE
		{
			box = el.getBoundingClientRect();
			boxdim[0] = box.top;
			boxdim[1] = box.right;
			boxdim[2] = box.bottom;
			boxdim[3] = box.left;
		}
		else if (document.getBoxObjectFor) // Mozilla, Netscape, etc.
		{
			box = document.getBoxObjectFor(el);
			boxdim[0] = box.y;
			boxdim[1] = box.x + box.width;
			boxdim[2] = box.y + box.height;
			boxdim[3] = box.x;
		}
		else // Safari, Opera, others
		{
			boxdim[0] = el.offsetTop;
			boxdim[3] = el.offsetLeft;
	
			var parent = el.offsetParent;
			if (parent != el)
			{
				while (parent)
				{
					boxdim[3] += parent.offsetLeft;
					boxdim[0] += parent.offsetTop;
					parent = parent.offsetParent;
				}
			}
	
			// opera & (safari absolute) incorrectly account for body offsetTop
			var ua = navigator.userAgent.toLowerCase();
			if (ua.indexOf('opera') != -1 ||
				( ua.indexOf('safari') != -1 && el.style.position == 'absolute' ))
			{
				boxdim[0] -= document.body.offsetTop;
			}
			
			boxdim[1] = boxdim[3] + el.offsetWidth;
			boxdim[2] = boxdim[0] + el.offsetHeight;
		}
		
		return boxdim[0] == -1 ? null : boxdim;
	}

	/*
	 * fixSize()
	 *
	 * Makes sure the window is large enough to accommodate its contents.
	 * Great for making pop-up dialogs without scrollbars.
	 *
	 * Note that for this to work, the browser must NOT be in quirks mode.
	 */
	ResidentPortal.fixSize = function() {
	    var dsize = ResidentPortal.getBoxModel(document.body);
	    if (dsize) {
	        // guesstimates to allow for window decorations
	        var w = (dsize[1] - dsize[3]) + 20;
	        var h = (dsize[2] - dsize[0]) + 120;

	        // make sure we don't blow up past the user's screen.. that would be rude
	        var offSet = 32;        // also rude to park in top left corner, under remote desktop title bar
	        var maxWid = screen.width - offSet;
	        var maxHt = screen.height - offSet;

	        if (w > maxWid) w = maxWid;
	        if (h > maxHt) h = maxHt;

	        if (self.resizeTo && self.moveTo) {
	            self.moveTo(offSet, offSet);
	            self.resizeTo(w, h);
	        }
	    }
	}
	
	/*
	 * tagFields(fields)
	 *
	 * Takes a comma-delimited list of field names and tags them with the "err" CSS class.
	 * This is used for marking bad form fields.
	 */
	ResidentPortal.tagFields = function(fields)
	{
		if (fields == '') return;
		var arrFields = fields.split(',');
		for (var i = 0; i < arrFields.length; i++)
		{
			var oElem = ResidentPortal.findElement(arrFields[i]);
			if (oElem != null)
			{
				if (oElem.nodeName == 'INPUT')
					oElem.className += ' err';
				else if (oElem.nodeName == 'SELECT' || oElem.nodeName == 'OPTION')
				{
					// fancy stuff to wrap it in a DIV
					var oParent = oElem.parentNode;
					if (oParent && oElem.nodeName == 'OPTION') oParent = oElem.parentNode;
					if (oParent)
					{
						var oDIV = document.createElement('DIV');
						oDIV.className = 'err';
						oDIV.style.display = 'inline';
						oDIV.appendChild(oElem.cloneNode(true));
						oParent.replaceChild(oDIV, oElem);
					}
				}
				else
				{
					debugLog.warn('tagFields(): The field ' + arrFields[i] + ' is a tag of type ' + oElem.nodeName);
				}
			}
		}
	}
	
	ResidentPortal.debugBarLoaded = false;
	
	/*
	 * debugBarLoad()
	 *
	 * When RP_DEBUG is set to True in residentportal_inc.asp, this will automatically be loaded
	 * to display helpful debugging information on the Resident Portal screens.
	 */
	ResidentPortal.debugBarLoad = function()
	{
		if (ResidentPortal.debugBarLoaded || top != self) return;
		
		debugLog.trace('debugBarLoad(): Loading the debug bar ...');
		
		var debugBar = document.createElement('DIV');
		debugBar.id = 'ResidentPortal_debugBar';
		debugBar.style.position = 'absolute';
		debugBar.style.top = '0';
		debugBar.style.left = '0';
		debugBar.style.width = '100%';
		debugBar.style.height = '31px';
		debugBar.style.border = '1px solid #000000';
		debugBar.style.backgroundColor = '#c0c0c0';
		debugBar.style.zIndex = 65535;
		
		var xml_rqst;
		if (window.ActiveXObject) xml_rqst = new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) xml_rqst = new XMLHttpRequest();
		else
		{
			debugLog.fatal('debugBarLoad(): This browser does not support XMLHttpRequest. Aborting.');
			return;
		}
		
		xml_rqst.open('GET', '/system/residentportal/200607/debug_bar.asp', false);
		xml_rqst.send('');
		if (xml_rqst.status == 200)
		{
			debugBar.innerHTML = xml_rqst.responseText;
		}
		else
		{
			debugLog.fatal('debugBarLoad(): Unable to load debug_bar.asp. Status: ' + xml_rqst.status);
			debugBar.innerHTML = 'Server brought back status: ' + xml_rqst.status;
		}
		
		document.body.appendChild(debugBar);
		ResidentPortal.debugBarLoaded = true;
	}
	
	/*
	 * hideDebugBar()
	 *
	 * This can be used to hide the debug bar if it is getting in the way.
	 */
	ResidentPortal.hideDebugBar = function()
	{
		if (!ResidentPortal.debugBarLoaded) return;
		var debugBar = ResidentPortal.findElement('ResidentPortal_debugBar');
		if (debugBar)
		{
			debugBar.style.display = 'none';
		}
	}
