// The main javascript file

/**
 * handleException
 * Common exception handling function to display the message via an alert box
 * @access public
 * @return void
 * @access Steven Mapes
 **/
function handleException(e){
    alert("An exception occurred in the script. Error name: "+e.name+". Message: "+e.message);
}

/**
 * Dynamically redefine the value of a syle
 * @return void
 * @author Steven Mapes
 **/
function setStyleById(elementID, styleParam, stlyeValue) {
    try {
        var n = document.getElementById(elementID);
        n.style[styleParam] = stlyeValue;
    }
    catch(err) {
        handleMoonError(err.message());
    }
}

/**
 *
 * @access public
 * @return void
 **/
function setCurrentElementStyle(anElement,styleParam, stlyeValue){
    try {
        anElement.style[styleParam] = stlyeValue;
    }
    catch(err) {
        handleMoonError(err.message());
    }
}

/**
* Dynamically Toggle the CSS class used against an element based on the ID of that element
* @author Steven Mapes <steve@stevenmapes.com>
* @return void
* @access public
**/
function toggleDisplay(anElement,aClassName) {
	try {
    	if (checkElementExists(anElement)) {
    	   n = document.getElementById(anElement);
    	   n.className = aClassName;
    	}
	}
	catch(err) {
		handleMoonError(err);
	}
}

/**
 *
 * @access public
 * @return void
 **/
function setStatus(statusMsg) {
    try {
	    window.status = statusMsg;
	    return true;
	}
	catch(err) {
	   handleMoonError(err.message());
	}
}

/**
 * checkElementExists()
 * Checks to see if a DOM element exists with the given ID
 * @param string el
 * @access public
 * @return void
 **/
function checkElementExists(el){
    var response = false;
    if (document.getElementById(el)) {
        response = true;
    }
    return response;
}

/**
 * statusHelp
 *
 * @param string statusHelp
 * @access public
 * @return void
 **/
function statusHelp(el){
    if (helpArray[el]) {
        document.getElementById('infoBar').innerHTML = helpArray[el];
    }
}

/**
 * showHide()
 * Toggles the visibility of DOM objects to hide most, show one
 * @param string pM The prefix to match
 * @param string sID  The ID of the DOM item to show
 * @param string tag  The type of tag to scan through
 * @return void
 * @access public
 * @author Steven Mapes
 **/
function showHide(pM,sID,tag) {
    try {
        var inputs = document.getElementsByTagName(tag);
        if (inputs.length > 0) {
            for(i=0;i<inputs.length;i++) {
                var prefix = inputs[i].id.substring(0,pM.length);
                if (prefix == pM) {
                    if (sID == inputs[i].id) {
                        // Show it
                        setStyleById(sID,'display','block');
                    } else {                
                        // Hide It
                        setStyleById(inputs[i].id,'display','none');
                    }
                }
            }
        }
    } catch (e) {
        handleException(e);
    }
}
