var selectedList;
var availableList;
function createListObjects(){
    availableList = document.getElementById("availableOptions");
    selectedList = document.getElementById("selectedOptions");
}

function delAttribute(){
    createListObjects();
    var selIndex = selectedList.selectedIndex;
    availableList = document.getElementById("availableOptions");
    if (availableList.disabled == false){
        if (selIndex < 0) 
            return;
        option = selectedList.options.item(selIndex)
        availableList.appendChild(option)
        selectNone(selectedList, availableList);
        setSize(availableList, selectedList);
        hidden = document.getElementById("users[" + option.value + "]");
        div = document.getElementById("allowed_users_div");
        div.removeChild(hidden);
    }
}

function addAttribute(){
    createListObjects();
    var addIndex = availableList.selectedIndex;
    if (addIndex < 0) 
        return;
    option = availableList.options.item(addIndex)
    if (validate_attribute(option)){
        return;
    }
    selectedList.appendChild(option);
    selectNone(selectedList, availableList);
    setSize(selectedList, availableList);
    div = document.getElementById("allowed_users_div");
    hidden = document.createElement("input");
    hidden.type = "hidden";
    hidden.value = option.value;
    hidden.name = "users[" + option.value + "]";
    hidden.id = hidden.name;
    div.appendChild(hidden);
}

function validate_attribute(option1){
    for (var i=0;i<selectedList.options.length;i++){
        var option2 = selectedList.options[i]
        if (option2.value == option1.value){
            return true
        }
    }
    return false
}


function delAll(){
    createListObjects();
    div = document.getElementById("allowed_users_div");
    availableList = document.getElementById("availableOptions");
    if (availableList.disabled == false){
        var len = selectedList.length - 1;
        for (i = len; i >= 0; i--) {
            option = selectedList.item(i);
            availableList.appendChild(option);
            hidden = document.getElementById("users[" + option.value + "]");
            div.removeChild(hidden);
        }
        selectNone(selectedList, availableList);
        setSize(selectedList, availableList);
    }
}

function addAll(){
    createListObjects();
    availableList = document.getElementById("availableOptions");
    if (availableList.disabled == false){
        div = document.getElementById("allowed_users_div");
        var len = availableList.length - 1;
        for (i = len; i >= 0; i--) {
            option = availableList.item(i);
            if (validate_attribute(option)){
                return;
            }
            selectedList.appendChild(option);
            hidden = document.createElement("input");
            hidden.type = "hidden";
            hidden.value = option.value;
            hidden.name = "users[" + option.value + "]";
            hidden.id = hidden.name;
            div.appendChild(hidden);
        }
        selectNone(selectedList, availableList);
        setSize(selectedList, availableList);
    }
}

function selectNone(list1, list2){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}

function setSize(list1, list2){
    list1.size = 10;//getSize(list1);
    list2.size = 10;//getSize(list2);
}

function getSize(list){
    /* Mozilla ignores whitespace, IE doesn't - count the elements in the list */
    var len = list.childNodes.length;
    var nsLen = 0;
    //nodeType returns 1 for elements
    for (i = 0; i < len; i++) {
        if (list.childNodes.item(i).nodeType == 1) 
            nsLen++;
    }
    if (nsLen < 2) 
        return 2;
    else 
        return nsLen;
}

function showSelected(){
    var optionList = document.getElementById("selectedOptions").options;
    var data = '';
    var len = optionList.length;
    for (i = 0; i < len; i++) {
        if (i > 0) 
            data += ',';
        data += optionList.item(i).value;
    }
    alert(data);
}



function add_standard(){
    standards_list = document.getElementById("standards_selection");
    var addIndex = standards_list.selectedIndex;
    if (addIndex < 0) 
        return;
    ul = document.getElementById("standards_ul");
    option = standards_list.options.item(addIndex);
    standards_list.removeChild(option);
    li = document.createElement('li')
    li.setAttribute("id", option.value);
    li.appendChild(document.createTextNode(option.text));
    li.appendChild(document.createTextNode("  "));
    a = document.createElement('a');
    a.href = '#';
    a.setAttribute("onclick","remove_standard('" + option.value  + "')");
    a.appendChild(document.createTextNode("remove"));
    li.appendChild(a);
    ul.appendChild(li);
	
    //add hidden field to form
    hidden = document.createElement("input");
    hidden.type = "hidden";
    hidden.value = option.value;
    hidden.name = "standards[" + option.value + "]";
    hidden.id = hidden.name;
    div = document.getElementById("hidden_standards");
    div.appendChild(hidden);
}

function remove_standard(id){
	
    li = document.getElementById(id);
    ul = document.getElementById("standards_ul");
    ul.removeChild(li);
	
    standards_list = document.getElementById("standards_selection");
    option = document.createElement("option");
    option.setAttribute("value", id);
    option.appendChild(document.createTextNode(li.childNodes[0].nodeValue));
    standards_list.appendChild(option);
	
    //remove hidden field to form
    div = document.getElementById("hidden_standards");
    hidden = document.getElementById("standards[" + id + "]");
    div.removeChild(hidden);
}
