/**
 * Utility methods and functions for page display requirements
 *
 * @author Michael Sweeney 
 *
 * showSection(sourceId, targetId) replaces innerhtml with new content
 * 
 */

/**
 * Replaces innerHTML of specified target with specified source
 *
 * @param string sourceId - DOM id value of source element
 * @param string targetId - DOM id value of target element
 */
function showSection(sourceId, targetId) {
    
	sourceEl = document.getElementById(sourceId);
	targetEl = document.getElementById(targetId);
    
	targetEl.innerHTML = sourceEl.innerHTML;
    
    //checkColumnHeight(sourceEl.offsetHeight);
}

/**
 * Make sure the contents column is as high as the sidebar nav column so that
 * footer is not overdrawn. 
 */
function checkColumnHeight(colHeight) {
    
    if(document.getElementById('leftBar')) {
        leftBar = document.getElementById('leftBar');
        centerCol = document.getElementById('centerCol');
        
        if(colHeight < leftBar.offsetHeight) {
            centerCol.style.height = leftBar.offsetHeight + 'px';
        }
        else {
            centerCol.style.height = 'auto';
        }
    }
}

/**
 * Utility function to add behaviors to the window.onload event
 * Usage note: don't quote the function name paramter for this.
 */
function addLoadEvent(efunc) {
    var exOnload = window.onload;   // save the existing onload object
    
    if(typeof window.onload != 'function') {
        window.onload = efunc;     // if no existing function add the new one
    }
    else {
        window.onload = function() {
            exOnload();            // replace the existing function
            efunc();               // and add the new one
        }
    }
}


/**
 * Sets form input fields to have a highlighted background color on focus.
 * Invoke with addLoadEvent in the head to apply to entire page.
 */
function setInputHighlight() {
    var inputs = document.getElementsByTagName('input');
    var textareas = document.getElementsByTagName('textarea');
    
    for(var i = 0; i < inputs.length; i++) {
        if(inputs[i].type == 'text' ) {
            inputs[i].onfocus = function() {
                this.style.backgroundColor = '#f2f9b8';
                if(this.value.lastIndexOf('Enter ') != -1) {
                    this.value = "";
                }
            }
            
            inputs[i].onblur = function() {
                this.style.backgroundColor = '#ffffff';
            }
        }
    }
    
    for(var j = 0; j < textareas.length; j++) {
        textareas[j].onfocus = function() {
            this.style.backgroundColor = '#f2f9b8';
        }
        
        textareas[j].onblur = function() {
            this.style.backgroundColor = '#ffffff';
        }
        
    }
}
