/**
 * Javascript specific for Karby Gård
 * 
 * @author Daniel Thor
 * 2007-05
 */

/**
 * toggleOptional() handles optional fields in forms
 * the fields are constructed with a checkbox and a input-field
 * if the checkbox is checked the input-field in enabeled
 * if the checkbox is unchecked the field is diabled and the contents is removed
 * 
 * @param string id The id of the form field
 */

function toggleOptional(id) {
    obj = document.getElementById(id); // Fetch field object from its id
    obj.disabled = !obj.disabled;      // Toggle disabled
    obj.value = '';                    // Clear the checkbox
    
    return true;
}

/**
 * initToggle() initializes the optional fields
 * if there is content in the field (for instance when editing a post)
 * the checkbox is checked
 * 
 * the checkbox must have the same id as the input field but with cb as prefix
 * i.e. input field id = inputField
 * then checkbox must have id = cbinputField
 * 
 * @param string id The id of the input field
 */

function initToggle(id) {
    // Get input field object by its id
    inputField = document.getElementById(id);
    // Check if there is something in the input field
    if (inputField.value != '') {
        // Check the checkbox since the input field is active
        checkbox = document.getElementById('cb' + id);
        checkbox.checked = true;
    }
    else {
        // Nothing in the input field, disable the field
        inputField.disabled = true;
    }
    
    return true;
}