/* * hiding_library.js
 * a simple set of functions for hiding parts of web pages
 * Copyright 2006 Orbro, LLC
 * www.Orbro.com
 */

//hideSection
//the first argument is the id name of the section to hide
//it checks to see if getElementById is supported
// if it is, the function sets the display to none
function hideSection(idname) {
	
	
	//give up if document.getElementById is not supported
	 if(!document.getElementById)
	 {
	
	 	return true;
 	 }

	if (!document.getElementById(idname)) {
		return true;
	}
	
	document.getElementById(idname).style.display = 'none';

	return true;
}


//showSection
//the first argument is the id name of the section to show
//it checks to see if getElementById is supported
// if it is, the function sets the display to block
function showSection(idname) {
	

	
	//give up if document.getElementById is not supported
	 if(!document.getElementById)
	 {
	 	return true;
 	 }

	if (!document.getElementById(idname)) {
		return true;
	}
	
	document.getElementById(idname).style.display = 'block';

	return true;
}




//toggleSection
//the first argument is the id name of the section to toggle
//it checks to see if getElementById is supported
// if it is, the function sets the display to block
function toggleSection(idname) {
	
	
	//give up if document.getElementById is not supported
	 if(!document.getElementById)
	 {
	 	return true;
 	 }
	
	if (document.getElementById(idname).style.display == 'none') {
		document.getElementById(idname).style.display = 'block';
	} else {

		document.getElementById(idname).style.display = 'none';
	}

	return true;
}


function conditionalHideSection(formElementID, value, idname) {
        var element;
        if (document.getElementById) {
                element = document.getElementById(formElementID);
                if ((value == element.value) && (isEnabled(element))) {
                        hideSection(idname);
                } else {
                        showSection(idname);
                }
        }
}

/* This function will check whether the given form element is
   in fact enabled.  We were finding that it was giving a value even
   if the field wasn't checked... */
function isEnabled(form_element) {
        
        switch (form_element.type) {
        case "radio":
        case "checkbox":
		
		
                return form_element.checked;
                break;

        case "select-one":
        case "select-multiple":
                /* we just need to iterate over the options array and check that 
                   at least one is selected */
                for (j = 0; j < form_element.options.length; j++) {
                        if (form_element.options[j].selected) {
                                return true;
                        }
                }
                break;

        /* this we assume will be on, as we're acting on submission of the form */
        case "submit":
                return true;
                break;

        /* these are always off */
        case "button":
        case "reset":
                return false;
                break;

        /* these are always on if not null */
        case "file":
        case "password":
        case "hidden":
        case "text":
        case "textarea":
                return (form_element.value != "");
                break;

        default:
                return false;
        }

        return false;
}

function conditionalShowSection(formElementID, value, idname) {
		
		var element;
        if (document.getElementById) {
                element = document.getElementById(formElementID);
                if ((isEnabled(element)) && (value == element.value) ) {
                        showSection(idname);
                } else {
                        hideSection(idname);
                }
        } else {
                hideSection(idname);
        }
}
