
/* - ++resource++resources/sequence.js - */
function createInputElement(id, name, type, value) {
   var element = null;
   try {
       // Internet Explorer needs special way of creating this element
       element = document.createElement('<input id="'+id+'" name="'+name+'" type="'+type+'" value="'+value+'"/>');
   } catch (e) {}
   if (!element || element.nodeName != 'INPUT') {
       element = document.createElement('input');
       element.id = id;
       element.name = name;
       element.type = type;
       element.value = value;
   }
   return element;
}

function createTextareaElement(id, name) {
   var element = null;
   try {
       // Internet Explorer needs special way of creating this element
       element = document.createElement('<textarea id="'+id+'" name="'+name+'" style="display:none"/>');
   } catch (e) {}
   if (!element || element.nodeName != 'TEXTAREA') {
       element = document.createElement('textarea');
       element.id = id;
       element.name = name;
       element.style.display = 'none';
       }
   return element;
}

function sequenceAddItem(field_id)
{
    var input = document.getElementById(field_id+'.input');
    if (input.value.length > 0)
    {
        var found = false;
        var select = document.getElementById(field_id+'.select');
        for (var j in select.options)
        {
            if (!found && select.options[j].value == input.value)
            {
                found = true;
            }
        }
        if (!found)
        {
            var item = new Option(input.value, input.value);
            select.options[select.length] = item;

            var div = document.getElementById(field_id);
            var new_input = createInputElement(field_id+'.'+input.value+'.regions', field_id+'.regions:list', 'hidden', input.value);
            div.insertBefore(new_input, select);

            div = document.getElementById(field_id + '.locations');
            var new_textarea = createTextareaElement(field_id+'.'+input.value+'.textarea', field_id+'.'+input.value+'.textarea');

            div.appendChild(new_textarea);

            var nodes = div.childNodes;
            for (var x in nodes)
            {
                if (nodes[x].nodeName == 'TEXTAREA')
                {
                    if (nodes[x].id == field_id+'.'+select.value+'.textarea')
                    {
                        nodes[x].style.display= '';
                    }
                    else
                    {
                        nodes[x].style.display= 'none';
                    }
                }
            }
        }
    }
    input.value = "";
}

function sequenceRemoveItem(field_id) {
    var select = document.getElementById(field_id+'.select');
    var value = select.value;
    if (select.selectedIndex >= 0) {
        select.remove(select.selectedIndex);
    }
    var div = document.getElementById(field_id);
    var elem = document.getElementById(field_id+'.'+value+'.regions');

    div.removeChild(elem);

    div = document.getElementById(field_id + '.locations');
    elem = document.getElementById(field_id+'.'+value+'.textarea');

    div.removeChild(elem);

    var nodes = div.childNodes;
    for (var x in nodes)
    {
        if (nodes[x].nodeName == 'TEXTAREA')
        {
            if (nodes[x].id == field_id+'.'+select.value+'.textarea')
            {
                nodes[x].style.display= '';
            }
            else
            {
                nodes[x].style.display= 'none';
            }
        }
    }

}

function sequenceChangeItem(field_id) {
    var select = document.getElementById(field_id+'.select');
    var div = document.getElementById(field_id + '.locations');
    var nodes = div.childNodes;
    for (var x in nodes)
    {
        if (nodes[x].nodeName == 'TEXTAREA')
        {
            if (nodes[x].id == field_id+'.'+select.value+'.textarea')
            {
                nodes[x].style.display= '';
            }
            else
            {
                nodes[x].style.display= 'none';
            }
        }
    }
}

function createNamedElement(name) {
   var element = null;
   try {
       // Internet Explorer needs special way of creating this element
       element = document.createElement('<input name="'+name+'">');
   } catch (e) {}
   if (!element || element.nodeName != 'INPUT') {
       element = document.createElement('input');
       element.name = name;
   }
   return element;
}

function rebuildValues(field_id) {
    var counter = 0;
    found = document.getElementById('value.'+counter+'.'+field_id);

    // hack
    if (found == null) {
        found = document.getElementById('value.None.'+field_id);
    }
    //

    while (found != null) {
        found.parentNode.removeChild(found);
        counter++;
        found = document.getElementById('value.'+counter+'.'+field_id);

        // hack
        if (found == null) {
            found = document.getElementById('value.None.'+field_id);
        }
        //
    }

    var select = document.getElementById('select.'+field_id);
    var source = document.getElementById(field_id);
    var sourceParent = source.parentNode;
    for (var x = 0; x < select.options.length; x++) {
        copy = createNamedElement(field_id);
        copy.setAttribute('type', 'hidden');
        copy.setAttribute('id', 'value.'+x+'.'+field_id);
        copy.setAttribute('value', select.options[x].value);
        sourceParent.insertBefore(copy, source);
    }
}

function dynamicAddItem(field_id) {
    var input = document.getElementById('input.'+field_id);
    if (input.value.length > 0) {
        var select = document.getElementById('select.'+field_id);
        var item = new Option(input.value, input.value);
        select.options[select.length] = item;
        rebuildValues(field_id);
    }
    input.value = '';
}

function dynamicRemoveItem(field_id) {
    var select = document.getElementById('select.'+field_id);
    if (select.selectedIndex >= 0) {
        select.remove(select.selectedIndex);
        rebuildValues(field_id);
    }
}

function sequenceMoveItem(field_id, direction) {
    var select = document.getElementById('select.'+field_id);
    var old_position = select.selectedIndex;
    var new_position = old_position + direction;
    if (new_position < 0 || new_position >= select.length )
        return;

    var array = new Array();
    for (var i=0; i<select.options.length; i++)
        array.push(new Option(select.options[i].text,
                              select.options[i].value));

    for (var i=0; i<array.length; i++) {
        if (i==old_position) select.options[i] = array[new_position];
        else if (i==new_position) select.options[i] = array[old_position];
        else select.options[i] = array[i];
    }

    rebuildValues(field_id);
    select.options[new_position].selected = true;
}

