/*
 * Common JavaScript functions
 *
 */
// {{{ toggleFieldsOffOn()
// Toggle field(s) to enabled/disabled depending on the state of trigger field
function toggleFieldsOffOn(triggerid, rowids) {
    obj_triggerid = document.getElementById(triggerid);

    str_rowids = new String(rowids);
    arr_rowids = str_rowids.split(',');

    arr_rowobjects = new Array();

    // Get each field object by id and stick into array
    i = 0;
    for(i in arr_rowids) {
        arr_rowobjects.push(document.getElementById(arr_rowids[i]));
    }

    // if the trigger is TRUE, set the slave fields to be visible
    if (obj_triggerid.checked  && obj_triggerid.value == 1) {
        for(i in arr_rowobjects) {
            obj_row = arr_rowobjects[i];
            obj_row.className = 'rowon showhide';
            
            for (i in obj_row.childNodes) {
                obj_row.childNodes[i].className = 'rowon showhide';
            }
            
        }
        
    }
    // otherwise hide slave fields 
    else {
        for(i in arr_rowobjects) {
            obj_row = arr_rowobjects[i];
            //obj_row.disabled = true;
            obj_row.value = '';

            arr_inputs = obj_row.getElementsByTagName('INPUT');
            
            // set all the input values to null
            for (i in arr_inputs) {
                arr_inputs[i].value = '';
            }
            
            if (obj_row.tagName == 'SELECT') {
                obj_row.selectedIndex = 0;
            }
            
            obj_row.className = 'rowoff';
            
            for (i in obj_row.childNodes) {
                obj_row.childNodes[i].className = 'rowoff';
            }
            
        }
    }// if/else
    
}// }}} 
// {{{ toggleRowByValue()
// Toggle a row or rows visible/invisible based on a value of a trigger field
function toggleRowByValue(triggerid, rowids, triggervalue) {
    obj_triggerid = document.getElementById(triggerid);

    str_rowids = new String(rowids);
    arr_rowids = str_rowids.split(',');

    arr_rowobjects = new Array();

    // Get each field object by id and stick into array
    i = 0;
    for(i in arr_rowids) {
        arr_rowobjects.push(document.getElementById(arr_rowids[i]));
    }
    
    // if the trigger row value equals our trigger value, set the slave fields to be visible
    if (obj_triggerid.value == triggervalue) {
        for(i in arr_rowobjects) {
            obj_row = arr_rowobjects[i];
            obj_row.className = 'rowon showhide';
            
            for (i in obj_row.childNodes) {
                obj_row.childNodes[i].className = 'rowon showhide';
            }
            
        }
        
    }
    // otherwise hide slave fields 
    else {
        for(i in arr_rowobjects) {
            obj_row = arr_rowobjects[i];
            //obj_row.disabled = true;
            //obj_row.value = '';

            arr_inputs = obj_row.getElementsByTagName('INPUT');
            
            // set all the input values to null
            for (i in arr_inputs) {
                arr_inputs[i].value = '';
            }
            
            if (obj_row.tagName == 'SELECT') {
                obj_row.selectedIndex = 0;
            }
            
            obj_row.className = 'rowoff';
            
            for (i in obj_row.childNodes) {
                obj_row.childNodes[i].className = 'rowoff';
            }
            
        }
    }// if/else
    
}// }}} 
// {{{ toggleRowOnCheck()
// Toggle a row or rows visible/invisible based on a whether box is checked
function toggleRowOnCheck(triggerid, rowids ) {
    obj_triggerid = document.getElementById(triggerid);

    str_rowids = new String(rowids);
    arr_rowids = str_rowids.split(',');

    arr_rowobjects = new Array();

    // Get each field object by id and stick into array
    i = 0;
    for(i in arr_rowids) {
        arr_rowobjects.push(document.getElementById(arr_rowids[i]));
    }
    
    // if the trigger row value equals our trigger value, set the slave fields to be visible
    if (obj_triggerid.checked == true) {
        for(i in arr_rowobjects) {
            obj_row = arr_rowobjects[i];
            obj_row.className = 'rowon showhide';
            
            for (i in obj_row.childNodes) {
                obj_row.childNodes[i].className = 'rowon showhide';
            }
            
        }
        
    }
    // otherwise hide slave fields 
    else {
        for(i in arr_rowobjects) {
            obj_row = arr_rowobjects[i];
            //obj_row.disabled = true;
            //obj_row.value = '';

            arr_inputs = obj_row.getElementsByTagName('INPUT');
            
            // set all the input values to null
            for (i in arr_inputs) {
                arr_inputs[i].value = '';
            }
            
            if (obj_row.tagName == 'SELECT') {
                obj_row.selectedIndex = 0;
            }
            
            obj_row.className = 'rowoff';
            
            for (i in obj_row.childNodes) {
                obj_row.childNodes[i].className = 'rowoff';
            }
            
        }
    }// if/else
    
}// }}} 

