/*
 Contains javascript functions to filter the meta fields in the advanced search form.
 The prototype library must be included before this file.
 Julien Roubieu - 22/02/07
*/


/*
    This functions calls displayChildren on all the meta fields.
    It should be called when the page is loaded to initialize the form fields.
*/
function filterAll()
{
    for (var i=1; i<=5; i++) {
        if ($("meta"+i+"Field") !=  null && $("meta"+(i+1)+"Field") != null) {
            eval("displayChildren(i, meta"+i+"Children);");
        }
    }
}

/*
    Called when a field of the form has changed.
    This function will set its style to relflect if this field is going to be
     used in the search (if it is not empty).
*/
function setUsed(field)
{
    if (field == null) return;
    if (field.value != '') {
        field.parentNode.className = "fieldCell_02_selected";
        getPreviousElement(field.parentNode).className = "fieldNameCell_02_selected";
    }
    else {
        field.parentNode.className = "fieldCell_02";
        getPreviousElement(field.parentNode).className = "fieldNameCell_02";
    }
}


function setUsedCouple(field, coupleFieldId)
{
    var field2 = $(coupleFieldId);
    if (field.value != '' || (field2 && field2.value != '')) {
        field.parentNode.className = "fieldCell_02_selected";
        getPreviousElement(field.parentNode).className = "fieldNameCell_02_selected";
    }
    else {
        field.parentNode.className = "fieldCell_02";
        getPreviousElement(field.parentNode).className = "fieldNameCell_02";
    }
}

function setUsedSelect(field)
{
    if (field.selectedIndex != -1 && field.options[field.selectedIndex].text != '-') {
        field.parentNode.className = "fieldCell_02_selected";
        getPreviousElement(field.parentNode).className = "fieldNameCell_02_selected";
    }
    else {
        field.parentNode.className = "fieldCell_02";
        getPreviousElement(field.parentNode).className = "fieldNameCell_02";
    }
}

/*
    Returns the previous non-text sibling
*/
function getPreviousElement(element)
{
    var p = element.previousSibling;
    while (p.nodeType != 1) {
        p = p.previousSibling;
    }
    return p;
}

/*
    Shows only the children of the selected values of the meta-parentId field.
*/
function displayChildren(parentId, allChildOptions)
{
    // The following test is important, since the "allChildOptions" will be empty
    // if there is no parent-child relationship between the two fields.
    if (allChildOptions.length == 0) {
        return(false);
    }

    var parentSelect = $("meta"+parentId+"Field");
    var childSelect = $("meta"+(parentId+1)+"Field");

    if (childSelect == null) return; // no meta child

    var childState = getState(childSelect);

    emptySelect(childSelect);

    var selectedParentsValues = new Array();
    $A(parentSelect.getElementsByTagName('option')).each (function(opt)
            {
                if (opt.selected) {
                    selectedParentsValues.push(opt.value);
                }
            });

    selectedParentsValues.each (function(parentValue)
    {
       var children = getChildrenForParentValue(parentValue, allChildOptions);
       children.each (function(child)
       {
           if (childState[child.value] != null) {
                // add the same old option
                childSelect.appendChild(childState[child.value]);
           }
           else {
                // add a new option
                childSelect.options[childSelect.options.length] = new Option(child.text, child.value, false);
           }
       });

    });

    // propagate to further children
    var i = parentId +1;
    if ($("meta"+i+"Field") !=  null && $("meta"+(i+1)+"Field") != null) {
        eval("displayChildren(i, meta"+i+"Children);");
    }
}



/*
    Parses the array and returns the children meta values for the given parent value.
    Each object of the returned Array has two properties: value and text.
*/
function getChildrenForParentValue(parentValue, allChildOptions)
{
    var res = new Array();
    var childOptions = allChildOptions[parentValue].split(";");
    if (childOptions.length == 1 && childOptions[0] == "") {
        return res;
    }
    else {
        for (var i=0; i < childOptions.length-1; i++) { // -1 because list is ending with ';'
            var v = childOptions[i].lastIndexOf("=");
            var child = {
                value: childOptions[i].substring(0, v),
                text: childOptions[i].substring(v+1)
            }
            res.push(child);
        }
    }
    return res;
}



/*
    Removes all options of this select element
*/
function emptySelect(selectElement)
{
    $A(selectElement.getElementsByTagName('option')).each(function(opt)
            {
                selectElement.removeChild(opt);
            })
}



/*
    Returns an array of (option.value -> option) of the given "select" element.
*/
function getState(selectElement)
{
    var state = new Array();
    var options = $A(selectElement.getElementsByTagName('option'));
    options.each(function(opt){
        state[opt.value] = opt;
    });
    return state;
}



function displaySimpleSearch()
{
    MyEffect.hide('advancedSearch');
    MyEffect.BlindDown('searchBarInAdvancedSearch');
    clearSearchForm();
    return false;
}

function displayAdvancedSearch()
{
    MyEffect.BlindUp('searchBarInAdvancedSearch');
    MyEffect.BlindDown('advancedSearch');
    return false;
}

/**
    Updates the class name of the cells containing non-empty fields.
*/
function setHighlights()
{
    setUsedSelect($('categoriesField'))
    setUsed($('textField'));
    setUsed($('ownerField'));
    setUsedSelect($('downloadableField'));
    setUsedSelect($('streamableField'));
    setUsedCouple($('expireDateMinField'), 'expireDateMaxField');
    setUsedCouple($('uploadDateMinField'), 'uploadDateMaxField');
    if ($('meta1Field') != null) setUsedSelect($('meta1Field'));
    if ($('meta2Field') != null) setUsedSelect($('meta2Field'));
    if ($('meta3Field') != null) setUsedSelect($('meta3Field'));
    if ($('meta4Field') != null) setUsedSelect($('meta4Field'));
    if ($('meta5Field') != null) setUsedSelect($('meta5Field'));
    if ($('meta6Field') != null) setUsedSelect($('meta6Field'));
    if ($('meta7MinField') != null) setUsedCouple($('meta7MinField'), 'meta7MaxField');
    if ($('meta8MinField') != null) setUsedCouple($('meta8MinField'), 'meta8MaxField');
    if ($('meta9Field') != null) setUsedSelect($('meta9Field'));
    if ($('meta10Field') != null) setUsedSelect($('meta10Field'));
    if ($('meta11MinField') != null) setUsedCouple($('meta11MinField'), 'meta11MaxField');
    if ($('meta12MinField') != null) setUsedCouple($('meta12MinField'), 'meta12MaxField');
    if ($('meta13Field') != null) setUsed($('meta13Field'));
    if ($('meta14Field') != null) setUsed($('meta14Field'));
}

/*
    Clears all fields of advanced search form.
*/
function clearSearchForm()
{
    emptyMultipleSelect($('categoriesField'));
    if ($('textField') != null) $('textField').value = '';
    if ($('ownerField') != null) $('ownerField').value = '';
    if ($('sizeMinField') != null) $('sizeMinField').value = '';
    if ($('sizeMaxField') != null) $('sizeMaxField').value = '';
    if ($('downloadableField') != null) $('downloadableField').options[0].selected = true;
    if ($('streamableField') != null) $('streamableField').options[0].selected = true;
    if ($('expireDateMinField') != null) $('expireDateMinField').value = '';
    if ($('expireDateMaxField') != null) $('expireDateMaxField').value = '';
    if ($('uploadDateMinField') != null) $('uploadDateMinField').value = '';
    if ($('uploadDateMaxField') != null) $('uploadDateMaxField').value = '';
    if ($('meta1Field') != null) emptyMultipleSelect($('meta1Field'));
    if ($('meta2Field') != null) emptyMultipleSelect($('meta2Field'));
    if ($('meta3Field') != null) emptyMultipleSelect($('meta3Field'));
    if ($('meta4Field') != null) emptyMultipleSelect($('meta4Field'));
    if ($('meta5Field') != null) emptyMultipleSelect($('meta5Field'));
    if ($('meta6Field') != null) emptyMultipleSelect($('meta6Field'));
    filterAll();
    if ($('meta7MinField') != null) $('meta7MinField').value = '';
    if ($('meta7MaxField') != null) $('meta7MaxField').value = '';
    if ($('meta8MinField') != null) $('meta8MinField').value = '';
    if ($('meta8MaxField') != null) $('meta8MaxField').value = '';
    if ($('meta9Field') != null) $('meta9Field').options[0].selected = true;
    if ($('meta10Field') != null) $('meta10Field').options[0].selected = true;
    if ($('meta11MinField') != null) $('meta11MinField').value = '';
    if ($('meta11MaxField') != null) $('meta11MaxField').value = '';
    if ($('meta12MinField') != null) $('meta12MinField').value = '';
    if ($('meta12MaxField') != null) $('meta12MaxField').value = '';
    if ($('meta13Field') != null) $('meta13Field').value = '';
    if ($('meta14Field') != null) $('meta14Field').value = '';
    setHighlights();
    return false;
}

function emptyMultipleSelect(selectElement)
{
    $A(selectElement.getElementsByTagName('option')).each(function(opt)
        {
            opt.selected = false;
        })
}

