function initOdr () {
    handleDeliveryType();
}

function handleDeliveryType () {
    var theForm = getForm();

    var deliveryTypes = theForm.deliveryType;
    var recipientTypes = theForm.recipientType;

    // Företag/Kontor/Lantbruk
    var nonPrivateChoiceId = getNonPrivateRecipient(recipientTypes);
    var nonPrivateChoice = document.getElementById(nonPrivateChoiceId);
    var nonPrivateRowId = nonPrivateChoiceId + "Row";
    var nonPrivateRow  =  document.getElementById(nonPrivateRowId);

    // Läns- och kommunbokning
    var countyReservationChoice = document.getElementById("countyReservation");
    var countyReservationRow = document.getElementById("countyReservationRow");

    // Inlämning
    var dropOffTypeAll = document.getElementById("dropofftypeall");
    var dropOffTypeCompany = document.getElementById("dropofftypecompany");

    var deliveryTypeSelected = false;
    var odrExclusiveId = "";

    for (i = 0; i < deliveryTypes.length; i++) {
        if (deliveryTypes[i].checked) {
            if (deliveryTypes[i].value == TYPE_ODR_STANDARD) {
                // Standard
                nonPrivateChoice.checked = false;
                countyReservationChoice.checked = false;
                nonPrivateRow.style.display = "none";
                countyReservationRow.style.display = "none";

                removeOptions('dropoffType');
                addOption('dropoffType', 'Välj inlämning', '');
                addOption('dropoffType', 'Företagscenter', 'Företagscenter');
                addOption('dropoffType', 'ODR-central', 'ODR-central');
                addOption('dropoffType', 'Postterminal', 'Postterminal');
                //dropOffTypeAll.style.display = "block";
                //dropOffTypeCompany.style.display = "none";
            } else if (deliveryTypes[i].value == TYPE_ODR_EXCLUSIVE) {
                // Exklusive
                nonPrivateRow.style.display = "block";
                countyReservationRow.style.display = "block";
                odrExclusiveId = deliveryTypes[i].id;

                removeOptions('dropoffType');
                addOption('dropoffType', 'Välj inlämning', '');
                addOption('dropoffType', 'Företagscenter', 'Företagscenter');
                addOption('dropoffType', 'ODR-central', 'ODR-central');
                addOption('dropoffType', 'Postterminal', 'Postterminal');
                //dropOffTypeAll.style.display = "block";
                //dropOffTypeCompany.style.display = "none";

            } else if (deliveryTypes[i].value == TYPE_ODR_LOCAL) {
                // Local
                nonPrivateRow.style.display = "block";
                countyReservationRow.style.display = "block";
                countyReservationRow.style.display = "none";
                odrExclusiveId = deliveryTypes[i].id;

                countyReservationRow.style.display = "none";
                removeOptions('dropoffType');
                addOption('dropoffType', 'Företagscenter', 'Företagscenter');
                //dropOffTypeAll.style.display = "none";
                //dropOffTypeCompany.style.display = "block";
            }
            deliveryTypeSelected = true;
        } else if (deliveryTypes[i].value == TYPE_ODR_EXCLUSIVE) {
            // Get id for ODR Standard to use in default selection
            odrExclusiveId = deliveryTypes[i].id;
        }
    }

    // Default - if no deliverytype is selected
    if (!deliveryTypeSelected) {
      document.getElementById(odrExclusiveId).checked = true;
    }
}
function addOption(selectbox,text,value )
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    document.getElementById(selectbox).options.add(optn);
}
function removeOptions(selectbox)
{
    var i;
    for(i=document.getElementById(selectbox).options.length-1;i>=0;i--)
    {
        document.getElementById(selectbox).remove(i);
    }
}

function getNonPrivateRecipient(recipientTypes) {

    for (var i = 0; i < recipientTypes.length; i++) {
        if (recipientTypes[i].value == RECIPIENT_ODR_NON_PRIVATE) {
            return recipientTypes[i].id;
        }
    }
}

function handleCountyReservation() {
    var theForm = getForm();

    if (theForm.blockReservation) {

        theForm.blockReservation.checked = false;

        if (theForm.countyReservation.checked) {
            theForm.blockReservation.disabled = true;
        } else {
            theForm.blockReservation.disabled = false;
        }
    }
}

function handleBlockReservation() {
    var theForm = getForm();

    if (theForm.countyReservation) {
        theForm.countyReservation.checked = false;

        if (theForm.blockReservation.checked) {
            theForm.countyReservation.disabled = true;
        } else {
            theForm.countyReservation.disabled = false;
        }
    }
}

