function insertOption(to, Text, Value)
{
    var newOption = new Option(Text, Value);
    var Index = to.length;
    to.options[Index] = newOption;
}

function removeOption(from, Index)
{
    var Length = from.length;
    if(Length > 0)
    {
        from.options[Index] = null;
    }
}

function removeAll(from)
{
    var Length = from.length;
    var i;

    // Find the selected Options in reverse order and delete them from the 'from' Select.
    for( i = Length-1; i >= 0; i--)
    {
        removeOption(from, i);
    }
}

function moveOptions(from, to)
{
    var Length = from.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;
    var i;

    // Find the selected Options in reverse order and delete them from the 'from' Select.
    for( i = Length-1; i >= 0; i--)
    {
        if(from.options[i].selected)
        {
            selectedText[selectedCount] = from.options[i].text;
            selectedValues[selectedCount] = from.options[i].value;
            removeOption(from, i);
            selectedCount++;
        }
    }

    // Add the selected text/values in reverse order. This will add the Options to the 'to' Select
    // in the same order as they were in the 'from' Select.
    for(i=selectedCount-1; i>=0; i--)
    {
        insertOption(to, selectedText[i], selectedValues[i]);
    }
}

function selectOptions(from)
{
    var Length = from.length;

    for( i = Length-1; i >= 0; i--)
    {
        from.options[i].selected = true;
    }
}

function addOption(formElement, Position, Text, Value)
{
    var selOption = new Option(Text, Value);
    eval(formElement.options[Position] = selOption);
}

function newRollSelect(first, second)
{
    switch (first.selectedIndex) {
        case 7: second.disabled = false;
                addOption(second, 0, "-- Select Subroll --","0");
                addOption(second, 1, "Licensed Professional Counselor (LPC)", "1");
                addOption(second, 2, "Licensed Clinical Addiction Specialist (LCAS)", "2");
                addOption(second, 3, "Certified Clinical Supervisor (CCS)", "3");
                addOption(second, 4, "Licensed Marriage and Family Counselor", "4");
                addOption(second, 5, "Certified Substance Abuse Counselor (CSAC)", "5");
                addOption(second, 6, "Licensed Clinical Social Worker", "6");
                addOption(second, 7, "Licensed Psychologist","7");
                addOption(second, 8, "Licensed Psychological Associate","8");
                addOption(second, 9, "Certified Clinical Supervisor", "9");
                addOption(second, 10, "Certified Substance Abuse Prevention Consultant", "10");
                addOption(second, 11, "Registered Nurse", "11");
                addOption(second, 12, "Clinical Nurse Specialist", "12");
                addOption(second, 13, "Nurse Practitioner", "13");
                addOption(second, 14, "Physician's", "14");
                addOption(second, 15, "Assistant", "15");
                addOption(second, 16, "Psychiatrist", "16");
                addOption(second, 17, "Qualified Professional", "17");
                addOption(second, 18, "Paraprofessional", "18");
                addOption(second, 19, "Associate Professional", "19");
                break;
        default: while (second.length) second.remove(0);
                second.disabled = true;
                addOption(second, 0, "-- Select Subroll --", "0");
                break;

    }
}

function ajaxChainer(scriptFile)
{
    var script = document.createElement("script");
    script.src = scriptFile; //'generameunnumero.php';
    script.setAttribute('language', 'javascript');
    script.setAttribute('type', 'text/javascript');
    document.body.appendChild(script);
}

function enableOnSelect(elementOnChange, elementToEnable, item)
{
    if(elementOnChange.selectedIndex == item)
    {
        elementToEnable.disabled = false;
    }
    else{
        elementToEnable.disabled = true;
        elementToEnable.selectedIndex = 0;
    }
}

function disableField(field)
{
    field.disabled = true;
}


function checkThem(master)
{
    var Items = document.form_coordinator.elements;

    if(master.checked == true)
    {
        for (i=0; i < Items.length; i++)
        {
            if(Items[i].type == "checkbox")
                Items[i].checked=1;
        }
    }
    else
    {
        for (i=0; i < Items.length; i++)
        {
            if(Items[i].type == "checkbox")
                Items[i].checked=0;
        }
    }
}

function isEmpty(id)
{
    id = document.getElementById(id);
    var isempt = 1;

    if(id.length > 0)
        isempt = 0;

    return isempt;
}

function returnNoEmpty(activity, report)
{
    if(!isEmpty(activity))
        return activity;
    else if(!isEmpty(report))
        return report;
    else
        return 0;
}
