﻿//
// JScript file to deal with Wagamama assign names functionality
//
    
//
// Uncheck a check box for a given Id
//
function ClearCheckBox(chkBoxId,keyEvent) {

    if (keyEvent.charCode == 0 && keyEvent.keyCode < 32) {
        return true;        
    }

    var chkBox = document.getElementById(chkBoxId);
    if (chkBox) {
        chkBox.checked = false;
    }
    return true;
}


//
// De-select a drop-down for a given Id
//
function ClearDropDown(dropDownId, keyEvent) {

    if (keyEvent.charCode == 0 && keyEvent.keyCode < 32) {
        return true;
    }
    var dropDown = document.getElementById(dropDownId);
    if (dropDown) {
        dropDown.selectedIndex = 0;
    }
    return true;
}
//
// Clear a text box if a checkbox is checked
//
function ClearTextBoxFromCheckBox(chkbox, txtBoxId, keyEvent) {

    if (chkbox.checked) {
        var txtBox = document.getElementById(txtBoxId);
        if (txtBox) {
            txtBox.value = '';
        }
    }
    
    return true;
}


//
// Clear a text box if a drop-down is selected
//
function ClearTextBoxFromDropDown(dropDown, txtBoxId, keyEvent) {

    if (dropDown.selectedIndex > 0) {
        var txtBox = document.getElementById(txtBoxId);
        if (txtBox) {
            txtBox.value = '';
        }
    }

    return true;
}

function ToggleModifierPanel(parentContainerId, sourceContainerId, targetContainerId, controlId, quantityId) {

    var ElemToHide = document.getElementById(controlId);

    var SourceContainer = document.getElementById(sourceContainerId);
    var TargetContainer = document.getElementById(targetContainerId);
    var ParentContainer = document.getElementById(parentContainerId);

    if (ElemToHide && ElemToHide.style.visibility != 'hidden') {
        //Copy the target code back to the source container
        if (SourceContainer && TargetContainer && ParentContainer) {
            var OriginalContainer = TargetContainer.childNodes[1];
            ParentContainer.appendChild(OriginalContainer);
        }
        $('.overlay').remove();
        ElemToHide.style.visibility = 'hidden';


    }
    else if (ElemToHide && ElemToHide.style.visibility != 'visible') {
        //Copy the source html from the source
        //container at the top of the page
        if (SourceContainer && TargetContainer) {

            TargetContainer.appendChild(SourceContainer);
 
        }

        var overlay = $("<div class='overlay'></div>");
        overlay.css({
            position: "absolute",
            "z-index": 5000,
            width: "100%",
            height: ($("body").height()),
            top: 0,
            left: 0,
            "background": "#000000",
            "opacity": 0.5,
            overflow: "hidden",
            "cursor": "pointer"
        });


        //Switch on the first n div's that hold the people selector
        //Where n is defined by the quantity id control
        var QuantityControl = document.getElementById(quantityId);
        if (QuantityControl) {
            var Quantity = Number(QuantityControl[QuantityControl.selectedIndex].value);

            $('div.AssignName, div.AssignNameAlternate', $(ElemToHide)).each(
                function(intIndex) {

                    if (intIndex < Quantity) {
                        $(this)[0].style.display = "";      //Make this one visible
                    }
                    else {
                        $(this)[0].style.display = "none";  //Hide this one
                    }
                }
            );

        }

        $("body").prepend(overlay);


        overlay.click(function() {
            var elementToHid = document.getElementById(controlId);
            elementToHid.style.visibility = 'hidden';
            $(this).remove();
            return false;
        });


        var sc = $(this.body).scrollTop();
        ElemToHide.style.top = sc + 200 + "px";
        ElemToHide.style.visibility = 'visible';
        
    }

 
}

