// misc. form utility functions (i.e. form validation)
// @author Daniel Lichtenberger, UCS

function isEmptyString(input) {
    return input == null || input.length == 0 || trim(input).length == 0;
}

function isInteger(input) {
    return !isEmptyString(input) && parseInt(input) != Number.NaN;
}

function isEmail(input) {
    return !isEmptyString(input) && input.lastIndexOf(".") > 2 && input.indexOf("@") > 0;
}

/* Returns the value of the selected option of the given select element,
   or null if no option is selected. */
function getSelectedOptionValue(selectElement) {
    var option = getSelectedOption(selectElement);
    return option != null ? option.value : null;
}

function getSelectedOption(selectElement) {
    if (selectElement == null || selectElement.selectedIndex < 0 || selectElement.options == null ||
        selectElement.selectedIndex >= selectElement.options.length)
        return null;
    else
        return selectElement.options[selectElement.selectedIndex];
}

function clearSelect(selectElement) {
    while (selectElement.options.length > 0) {
        selectElement.options[selectElement.options.length - 1] = null;
    }
}

/* Selects a given select option by value.
*/
function selectOptionValue(selectElement, value) {
    if (selectElement != null && selectElement.options) {
        for (var i = 0; i < selectElement.options.length; i++) {
            if (selectElement.options[i].value == value) {
                selectElement.selectedIndex = i;
                break;
            }
        }
    }
}

/* Creates options from the given JSON array using the given properties for the option value/label.
   If valueProperty is null, the option index is used as value. */
function appendOptions(selectElement, options, valueProperty, textProperty, selectedValue) {
    for (var i = 0; i < options.length; i++) {
        var option = options[i];
        var opt = document.createElement("option");
        opt.value = valueProperty != null ? option[valueProperty] : i;
        opt.text = option[textProperty];
        opt.selected = opt.value == selectedValue;
        selectElement.options[i] = opt;
    }
}

/* Remove all options of the given select element */
function clearOptions(selectElement) {
    while (selectElement.options.length > 0) {
        selectElement.options[0] = null;
    }
}

/* Cycles through the given array of radio buttons and returns the selected
   element, or null if no button is selected */
function getSelectedRadioButton(radioElements) {
    for (var i = 0; i < radioElements.length; i++) {
        if (radioElements[i].checked) {
            return radioElements[i];
        }
    }
    return null;
}

/* Cycles through the given array of radio buttons and returns the selected
   value, or null if no button is selected */
function getSelectedRadioValue(radioElements) {
    var radio = getSelectedRadioButton(radioElements);
    return radio != null ? radio.value : null;
}

/** Returns the selected value(s) of the given multiselect field in an array */
function getMultiSelectOptionValues(selectElement) {
    if (selectElement == null || selectElement.options.length == 0) {
        return new Array();
    }
    var result = new Array();
    for (var i = 0; i < selectElement.options.length; i++) {
        var option = selectElement.options[i];
        if (option.selected) {
            result.push(option.value);
        }
    }
    return result;
}

/** Insert the given text at the text area's current cursor position */
function insertAtCursorPosition(textArea, text) {
    if (textArea.className.indexOf("htmlEditor") != -1 || textArea.className.indexOf("tiny") != -1  ) {
        // insert into tinyMCE text editor
        tinyMCE.execCommand("mceInsertContent", true, text);
    } else if (document.all) {
        textArea.focus();
        document.selection.createRange().text += text;
    } else {
        textArea.value = textArea.value.substring(0, textArea.selectionStart)
                + text + textArea.value.substring(textArea.selectionStart);
    }
}

/** Populates a mtl:editTranslatedElement tag with the given translations (a map of languageId -> translation) */
function populateTranslatedElementTag(form, name, translations) {
    var i = 0;
    // clear translations
    while (form[name + "[" + i + "].translation"] != null) {
        form[name + "[" + i + "].translation"].value = "";
        i++;
    }
    // update translations
    var rows = i;
    for (var languageId in translations) {
        if (typeof(translations[languageId]) == 'function') {
            continue;
        }
        for (i = 0; i < rows; i++) {
            if (form[name + "[" + i + "].language"].value == languageId) {
                form[name + "[" + i + "].translation"].value = translations[languageId];
            }
        }
    }
}