﻿// JScript File

function ToggleAllOptionals(obj, ctlName) {
    var id_array = document.getElementById(ctlName).value.split(",");

    for (i = 0; i < id_array.length; i++) {
        if (obj.checked) {
            if (!document.getElementById(id_array[i]).checked) document.getElementById(id_array[i]).click();
        }
        else { document.getElementById(id_array[i]).click(); }
    }
}

function ToggleAllOptionalRows(ctl) {
    var i;
    var ctlId;
    var hidCtlId;
    var chkCtlID;
    var idx;

    for (i = 0; i < document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].type == "checkbox") {
            if (document.forms[0].elements[i].value == ctl.value && document.forms[0].elements[i].name != ctl.name && !document.forms[0].elements[i].disabled) {

                chkCtlID = document.forms[0].elements[i].id;
                idx = chkCtlID.indexOf("OptionalItemCode");
                ctlId = chkCtlID.substring(0, idx);
                hidCtlId = ctlId + "OptionItemCodesSelectionCtl";

                if (ctl.checked) {
                    document.forms[0].elements[i].checked = true;
                    AddRemoveOptionals(chkCtlID, hidCtlId)
                }
                else {
                    document.forms[0].elements[i].checked = false;
                    AddRemoveOptionals(chkCtlID, hidCtlId)
                }
            }
        }
    }
}

function AddRemoveOptionals(chkCtlID, hidCtlID) {
    var chkCtl;
    var hidCtl;
    var idx;

    chkCtl = document.getElementById(chkCtlID);
    hidCtl = document.getElementById(hidCtlID);

    //if user has selected an item, then check to see if it is already added
    //if the hidden item codes selection control already has value, then concatenate the
    //selected value to the control, else just set the value to the control 

    if (chkCtl.checked) {
        if (hidCtl.value.indexOf(chkCtl.value) == -1) {
            if (hidCtl.value == "")
                hidCtl.value = chkCtl.value;
            else
                hidCtl.value += "," + chkCtl.value;
        }
    }
    else {
        idx = hidCtl.value.indexOf(chkCtl.value);
        len = chkCtl.value.length;

        hidCtl.value = hidCtl.value.replace(chkCtl.value, "");

        //Check to see if the user is trying to remove a code which is present in the first or middle of selected codes
        //if the user is removing an item at the start, then remove the comma which follows the item code
        //if the user is removing an item in the middle, then remove the comma which is before the item code
        if (hidCtl.value.indexOf(",") == 0)
            hidCtl.value = hidCtl.value.substring(0);

        if (hidCtl.value.indexOf(",") == hidCtl.value.length - 1)
            hidCtl.value = hidCtl.value.substring(0, hidCtl.value.length - 1);
    }
}

function LoadOptionalsPopUp(url) {
    var diasettings = "";
    var querystring = "";

    diasettings = "dialogWidth:1000px;dialogHeight:560px;center:yes;resizable:yes;status:yes;toolbar:yes;menubar:yes;scrollbars=no";

    if (window.showModalDialog)
        validAdd = window.showModalDialog(url + querystring, self, diasettings);
    else
        validAdd = window.open(url + querystring, 'self', 'height=560,width=740,screenX=550,screenY=600,modal=1,dialog=1,menubar=0,location=0,resizable=1,scrollbars=1,status=0,toolbar=0,directories=0');
} 

