/**
 * Contains a set of global helper functions that assist in dealing with browser
 * incompatability issues.
 *
 * <p>Each of the contained functions represents a browser neutral way to deal with
 * browser specific DOM issues.</p>
 *
 * <p>Dependancies:</p>
 * <ul>
 *   <li>utils.js</li>
 * </ul>
 */


/* ***************************************************************************
 * ACCESS FUNCTIONS
 * Functions for obtaining information from the DOM.
 *************************************************************************** */
 
function getElement(reference) {
	return (typeof reference == 'string') ? document.getElementById(reference) : reference;
}

/**
 * Returns the currently applied DOM style object for a given DOM object.
 * <p>Compatability Tested for:</p>
 * <ul>
 * <li>Firefox 1.5.0.6 on Microsoft Windows XP</li>
 * <li>Microsoft IE 6.0.2900.2180.xpsp.050622-1524</li>
 * </ul>
 * @param DOM element
 * @return DOM Style element containing current/computeed style properties.
 * @since 1.0
 */
function getStyle(elem) {
	if (elem.currentStyle !== undefined) {
		return elem.currentStyle;
	} else {
		return document.defaultView.getComputedStyle(elem, null);
	}
}

function getWindowOffset(ref,isObj) {
	if(!isObj){	
		ref = getElement(ref);
	}
	var pos = new com.rig.Utils.Position();
	while (ref) {
		pos.top += ref.offsetTop;
		pos.left += ref.offsetLeft;
		ref = ref.offsetParent;
	}
	
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
		pos.left += document.body.leftMargin;
		pos.top += document.body.topMargin;
	}
	return pos;
}

function getAbsolutePos(ref, parent) {
	ref = getElement(ref);
	var pos = new com.rig.Utils.Position();
	while (ref) {
		pos.top += ref.offsetTop;
		pos.left += ref.offsetLeft;
		ref = ref.offsetParent;
		if (ref !== null) {
            var position = getStyle(ref).position;
			if (position == 'relative') {
				// Fix IE Bug that adds 10px
				if (ref.currentStyle != undefined) {
					pos.left -= 10;
				}
				ref = null;
			} else if (position == 'absolute') {
                ref = null;
            }
		}
	}
    if(parent != undefined){
        ppos = getAbsolutePos(parent);
        pos.top += ppos.top;
        pos.left += ppos.left;
    }
	return pos;
}

function resolutionSize() {
		  var myWidth = 0, myHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		  }
		  var temp = {};
		  
		  temp.width = myWidth;
		  temp.height = myHeight;
		 
		 return temp;
	}

function pageSize() {
		  var myWidth = 0, myHeight = 0;
		  if( typeof( window.offsetWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.offsetWidth;
			myHeight = window.offsetHeight;
		  } else if( document.documentElement && ( document.documentElement.scrollWidth || document.documentElement.scrollHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.scrollWidth;
			myHeight = document.documentElement.scrollHeight;
		  } else if( document.body && ( document.body.offsetWidth || document.body.offsetHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.offsetWidth;
			myHeight = document.body.offsetHeight;
		  }
		  var temp = {};
		  
		  temp.width = myWidth;
		  temp.height = myHeight;
		 
		 return temp;
	}

/* ***************************************************************************
 * ACTION FUNCTIONS
 * Functions for modifying the DOM.
 *************************************************************************** */

/**
 * Returns true if a given DOM element is visible on the page.  This function
 * only works off the visiblilty properties and not any layering information.
 * If an element is hidden by another element on the page but would otherwise
 * be visible this function would still return true.
 * <p>Compatability Tested for:</p>
 * <ul>
 * <li>Firefox 1.5.0.6 on Microsoft Windows XP</li>
 * <li>Microsoft IE 6.0.2900.2180.xpsp.050622-1524</li>
 * </ul>
 * @param DOM element or id of a DOM element
 * @return <code>true</code> if the element is visible on the page otherwise false.
 * @since 1.0
 */
function isElementVisible(ref) {
	var elem = getElement(ref);
	var style = getStyle(elem);
	/*alert("style = "+ style);
	alert("style.visibility = "+ style.visibility);
	alert("style.display = "+ style.display);*/
	if (style) {
		if (style.display){
			if (style.display == 'none'){
				return false;
			} 
			else {
				return true;
			}
		}	
		else{	
			if (style.visibility) {
				if (style.visibility == 'show') {
					return true;
				} else {
					return false;
				}
			}
		}
	} else {
		if (elem.visibility == 'show') {
			return true;
		} else {
			return false;
		}
	}
}

/**
 * Makes a given DOM object visible on the page. This function only works by
 * modifying the visiblilty properties and not any layering information.
 * The element may still not be visible after calling this function if it is
 * hidden behind other page elements.
 * <p>Compatability Tested for:</p>
 * <ul>
 * <li>Firefox 1.5.0.6 on Microsoft Windows XP</li>
 * <li>Microsoft IE 6.0.2900.2180.xpsp.050622-1524</li>
 * </ul>
 * @param DOM element or id of a DOM element
 * @since 1.0
 */
function showElement(ref) {
	var elem = getElement(ref);
	var style = getStyle(elem);
	alert("show style = "+ style);
	alert("show style.visibility = "+ style.visibility);
	alert("show style.display = "+ style.display);
	
	if (style) {
		if (style.display) {
			style.display = 'block';
		} else {
			style.visibility = 'show';
		}
	} else {
		elem.visibility = 'show';
	}
}

/**
 * Makes a given DOM object invisible on the page. This function only works by
 * modifying the visiblilty properties and not any layering information.
 * <p>Compatability Tested for:</p>
 * <ul>
 * <li>Firefox 1.5.0.6 on Microsoft Windows XP</li>
 * <li>Microsoft IE 6.0.2900.2180.xpsp.050622-1524</li>
 * </ul>
 * @param DOM element or id of a DOM element
 * @since 1.0
 */
function hideElement(ref) {
	var elem = getElement(ref);
	var style = getStyle(elem);
	alert("hide style = "+ style);
	alert("hide style.visibility = "+ style.visibility);
	alert("hide style.display = "+ style.display);
	
	if (style) {
		if (style.display) {
			style.display = 'none';
		} else {
			style.visibility = 'hidden';
		}
	} else {
		elem.visibility = 'hidden';
	}
}

/**
 * Toggles a given DOM objects visibility on the page. This function only works 
 * by modifying the visiblilty properties and not any layering information.
 * <p>Compatability Tested for:</p>
 * <ul>
 * <li>Firefox 1.5.0.6 on Microsoft Windows XP</li>
 * <li>Microsoft IE 6.0.2900.2180.xpsp.050622-1524</li>
 * </ul>
 * @param DOM element or id of a DOM element
 * @since 1.0
 */
function toggleVisibility(ref) {
	var elem = getElement(ref);
    if (isElementVisible(elem)) {
		hideElement(elem);
	} else {
		showElement(elem);
	}
}

/**
 * Convenience function to toggle the visibility of two DOM elements.
 * <p>Compatability Tested for:</p>
 * <ul>
 * <li>Firefox 1.5.0.6 on Microsoft Windows XP</li>
 * <li>Microsoft IE 6.0.2900.2180.xpsp.050622-1524</li>
 * </ul>
 * @param DOM element or id of a DOM element
 * @see #toggleVisibility(ref)
 * @since 1.0
 */
function swapVisibilities(ref1, ref2) {
	var elem1 = ( typeof ref1 == 'string' ) ? document.getElementById(ref1) : ref1;
	var elem2 = ( typeof ref2 == 'string' ) ? document.getElementById(ref2) : ref2;
	toggleVisibility(elem1);
	toggleVisibility(elem2);
}


WriteLayer = function(ID,parentID,sText) { 
	if (document.layers) { 
		var oLayer; 
		if(parentID){ 
			oLayer = eval('document.' + parentID + '.document.' + ID + '.document'); 
		}else{ 
			oLayer = document.layers[ID].document; 
		} 
		
		oLayer.open(); 
		oLayer.write(sText); 
		oLayer.close(); 

	} else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") { 
		document.getElementById(ID).innerHTML = sText; 

	} else if (document.all) {
		document.all[ID].innerHTML = sText;
	}
} 

/**
 *
 */
function addEvent(target, event, listener, useCapture) {
	if (typeof target.addEventListener != 'undefined') {
		target.addEventListener(event, listener, useCapture);
	} else if (typeof target.attachEvent != 'undefined') {
		target.attachEvent('on'+event, listener);
	} else {
		target['on'+event] = listener;
	}
}

function getOuterHTML(elem) {
	if (elem.outerHTML != undefined) return elem.outerHTML;
	if (document.createElement == undefined) return undefined;
	var div =  document.createElement('div');
	div.appendChild(elem);
	var result = div.innerHTML;
	div.removeChild(elem);
	return result;
}

function removeInputValue(elem) {
	var elemObj = (typeof(elem) == 'string') ? document.getElementById(elem) : elem;
	switch (elemObj.tagName.toUpperCase()) {
		case 'TEXTAREA':
            elemObj.value = '';
            break;
		case 'INPUT': 
			if (elemObj.type == 'radio' || elemObj.type == 'checkbox') {
                elemObj.checked = false;
            }else if(elemObj.type == 'text'){
                elemObj.value = '';
            }
            if(elemObj.validation) elemObj.validation.onChange();
            break;
		case 'SELECT':
            var options = elemObj.getElementsByTagName('OPTION');
            options[0].selected = true;
            if(elemObj.validation) elemObj.validation.onChange();
            break;
		default :
            var inputs = elemObj.getElementsByTagName('INPUT');
            for(var i=0; i<inputs.length; i++){
                removeInputValue(inputs[i]);
            }
            var selects = elemObj.getElementsByTagName('SELECT');
            for(var s=0; s<selects.length; s++){
                removeInputValue(selects[i]);
            }
            var textareas = elemObj.getElementsByTagName('TEXTAREA');
            for(var t=0; t<textareas.length; t++){
                removeInputValue(textareas[t]);
            }
    }
}
