// hasLabel: Map input id -> Input has label (true/false)
if (hasLabel == null) var hasLabel = new Array();
// labelId: Map input id -> label id
if (labelId == null) var labelId = new Array();

// Compacts all text field inputs on a form by moving the label inside the field.
// formElementId - Id of the form element.
// className - Optional. Class name to set on the input label text. If omitted class name of the label is used, if set, else input class is used.
function compactForm(formElementId, className) {
    var form = document.getElementById(formElementId);
    var labels = form.getElementsByTagName("label");
    for ( var i=0; i<labels.length; i++ ) {
        if (labels[i].htmlFor) compactField(labels[i].id, className);
    }
    // Clear labels, after onsubmit function, if defined.
    var old_onsubmit = form.onsubmit;
    form.onsubmit = function() {
        if (old_onsubmit) {
            if (!old_onsubmit()) {
                return false;
            }
        }
        clearLabels(this);
    }
}

// Clear all input values from label text in a form
// This function should be called before submitting the form.
// form - The form element.
function clearLabels(form) {    
    var labels = form.getElementsByTagName("label");
    for ( var i=0; i<labels.length; i++ ) {
        if (!labels[i].htmlFor) continue;
        var input = document.getElementById(labels[i].htmlFor);
        if (hasLabel[input.id]) {
            if (input.nodeName == "SELECT") {
                //empty
            } else {
                input.value = "";
            }
            hasLabel[input.id] = false;
        }
    }
}

// Clears and resets the value of a compact input.
function resetInput(inputId) {
    var input = document.getElementById(inputId);
    if (hasLabel[input.id]) {
        return;
    } else {
        var label = document.getElementById(labelId[input.id]);
        input.className = label.className;
        input.value = getTextContent(label);
        hasLabel[input.id] = true;
    }

}


// Compacts a text field input by setting the label as the value of the field.
// textInputLabelElementId - Id of label element for the input field.
// className - Optional. Class name to set on the input label text. If omitted class name of the label is used, if set, else input class is used.
function compactField(textInputLabelElementId, className) {
    var label = document.getElementById(textInputLabelElementId);
    var input = document.getElementById(label.htmlFor);

    if (input == null) return;
    if (isCheckbox(input)) {
        // Note: A label element is defined for the checkbox, but the checkbox input does not have the label as value, so hasLabel is false
        hasLabel[input.id] = false;
        return;
    }

    // Define variables
    // labelId: Map input id -> label id
    labelId[input.id] = label.id;
    // inputClassName: Map input id -> class name
    if (inputClassName == null) var inputClassName = new Array();
    inputClassName[input.id] = input.className

    // Add input events to handle labels.
    if (input.nodeName == "SELECT") {
        var old_onchange = input.onchange;
        input.onchange = function() {
            if (old_onchange) old_onchange();
            if (hasLabel[this.id]) {
                this.remove(0);
                this.className = inputClassName[this.id];
                hasLabel[this.id] = false;
            }
        }
    } else {
        var old_onfocus = input.onfocus;
        input.onfocus = function() {
            if (old_onfocus) old_onfocus();
            if (hasLabel[this.id]) {
                this.value = "";
                this.className = inputClassName[this.id];
                hasLabel[this.id] = false;
            }
        }
        var old_onblur = input.onblur;
        input.onblur = function() {
            if (old_onblur) old_onblur();
            var label = document.getElementById(labelId[this.id]);
            if (this.value == "") {
                this.className = label.className;
                this.value = getTextContent(label);
                hasLabel[this.id] = true;
            }
        }
    }

    // Compact field
    label.style.display = "none";
    if (className == null) {
        if (label.className == "") label.className = input.className;
    } else {
        label.className = className;
    }
    if (input.nodeName == "SELECT") {
        var labelOption = new Option(getTextContent(label), "");
        labelOption.className = label.className;
        input.options.add(labelOption, 0);
        input.selectedIndex = 0;
        hasLabel[input.id] = true;
    } else {
        hasLabel[input.id] = (input.value == "");
        if (hasLabel[input.id]) {
            input.value = getTextContent(label);
        }
        input.className = label.className;
    }
}

/*
 * Gets a compact text input value given its label id
 *
 * textInputLabelElementId - Id of text input label.
 */
function getTextInputValue(textInputLabelElementId) {
    var input = document.getElementById(document.getElementById(textInputLabelElementId).htmlFor);
    if (hasLabel[input.id]) {
        return "-";
    } else {
        return input.value;
    }
}

/*
 * Gets the value of the checked radio button in a radio button group.
 * If no radio button is checked null is returned.
 *
 * radioGroup - Radio button group.
 */
function getRadioValue(radioGroup) {
    for (var i=0; i < radioGroup.length; i++)
    {
        if (radioGroup[i].checked) return radioGroup[i].value;
    }
    return null;
}

function getCheckboxValue(checkboxLabel) {
    var checkbox = document.getElementById(document.getElementById(checkboxLabel).htmlFor);
    if (checkbox.checked) return checkbox.value;
    return '-';
}

function getSelectedValue(select) {
    return (getTextContent(select[select.selectedIndex]));
}

/*
 * Helper function to get the text content for an element.
 */
function getTextContent(element) {
    
    if(document.all){
        return element.innerText;
    } else{
        return element.textContent;
    }
}

/*
 * Helper function to check if an element is a checkbox input.
 */
function isCheckbox(element) {
    return (typeof(element.attributes["type"])=="object") && (element.attributes["type"].value=="checkbox");
}

