<script type="text/javascript">
    function selectallaction(checkid,ids)
    {
        var idArray;
        var lengthArray=0;
        var i=0;
    
        if(ids!="")
        {
            idArray=ids.split(",");

            lengthArray=idArray.length;
		
            if(document.getElementById(checkid).checked==true)
            {
                for(i=0;i<lengthArray;i++)
                {
                    $("#chkaccess-"+idArray[i]).attr("checked","checked");
                }
            }
            else
            {
                for(i=0;i<lengthArray;i++)
                {
                    $("#chkaccess-"+idArray[i]).attr("checked",false);
                }
                $("#selectall").attr("checked",false);
            }
        }
    }

    function unselectallaction(checkid,ids,unceckid)
    {
        var idArray;
        var lengthArray=0;
        var i=0;
    
        if(ids!="")
        {
            idArray=ids.split(",");
            lengthArray=idArray.length;
            if(document.getElementById(checkid).checked==false)
            {
                for(i=0;i<lengthArray;i++)
                {
                    $("#chkaccess-"+idArray[i]).attr("checked",false);
                }
                $("#selectall").attr("checked",false);
            }
            if(unceckid!="")
            {
                $("#"+unceckid).attr("checked",false);
            }
        }
    }

    function selectfirstaction(checkid,ids)
    {
        var idArray;
        var lengthArray=0;
        var i=0;
    
        if(ids!="")
        {
            idArray=ids.split(",");
            lengthArray=idArray.length;
            if(document.getElementById(checkid).checked==true)
            {
                for(i=0;i<lengthArray;i++)
                {
                    $("#chkaccess-"+idArray[i]).attr("checked","checked");
                }
            }
        }
    }

    function unselectall(checkid)
    {
        if(document.getElementById(checkid).checked==false)
        {
            $("input[name='chkaccess[]']").each(function() {
                this.checked = false;
            });
            $("input[name='chkmodule[]']").each(function() {
                this.checked = false;
            });
            $("input[name='chkaction[]']").each(function() {
                this.checked = false;
            });
        }

    }

    function selelectallchk()
    { 
        if(document.getElementById('selectall').checked==true)
        {
            $("input[name='chkaccess[]']").each(function() {
                this.checked = true;
            });
            $("input[name='chkmodule[]']").each(function() {
                this.checked = true;
            });
            $("input[name='chkaction[]']").each(function() {
                this.checked = true;
            });
        }
        else
        {
            $("input[name='chkaccess[]']").each(function() {
                this.checked = false;
            });
            $("input[name='chkmodule[]']").each(function() {
                this.checked = false;
            });
            $("input[name='chkaction[]']").each(function() {
                this.checked = false;
            });
	
        }
	
        $(".case").click(function(){
 
            if($(".case").length == $(".case:checked").length) {
                $("#selectall").attr("checked", "checked");
            } else {
                $("#selectall").removeAttr("checked");
            }
 
        });
    }

    function validate()
    {
        var count =0;
        $("input[name='chkaccess[]']").each(function() {
	
            if(this.checked==true)
            {
                count ++;
            }
        });
        if(count==0)
        {
            alert("Please select access rights for modules.");
            return false;
        }
        else
        {
            document.frmcontact.submit();
        }
    }
</script>
<?php if ($_REQUEST['action'] != '') $action = ucwords($_REQUEST['action']); ?>
<div class="breadcrumbs"><a href="index.php">Home </a>&nbsp;>&nbsp; <a href="index.php?module=modcontrol">Manage Assign Module Action</a>&nbsp; >&nbsp; <span class="current"><?= $action ?>Assign Module Access</span>  </div>
<div id="dialog-modal" title="groups"></div>
<div class="add-new-user-title">
    <h2><img alt="add-new-listing" src="<?= GLOBAL_ADMIN_IMAGES_PATH ?>/add-new-user-icon.png"><?= $action ?> Assign Module Action  </h2>
</div>

<?= $messagebox ?>
<div class="web-booking-main">
    <div class="clear"></div>
    <!-- new access -->
    <form  name="frmcontact"  id="frmcontact" action="<?= $frmaction ?>" method="post" onSubmit="return validate();" enctype="multipart/form-data" >
        <input type="hidden" name="h_save" id="h_save" value="T" />
        <input type="hidden" name="action" id="action" value="add" />
        <div class="grid-list-subtitle-bg" style="margin-top:10px;" >
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="4%" align="center" valign="middle" class="grid-td-left"><input class="checkallcheckbox" name="selectall" id="selectall" type="checkbox" onclick="selelectallchk();"/></td>
                    <td width="20%" align="left" valign="middle" class="grid-td-top">Module Name</td>
                    <?php
                    foreach ($AllAction as $Action) {
                        if ($Action['actionId'] == 1) {
                            $onclick = "selectallaction('chkaction-" . $Action[actionId] . "',$('#hidactions-" . $Action[actionId] . "').val());unselectall('chkaction-" . $Action[actionId] . "');";
                        } else {
                            $onclick = "selectallaction('chkaction-" . $Action[actionId] . "',$('#hidactions-" . $Action[actionId] . "').val());selectfirstaction('chkaction-" . $Action[actionId] . "',$('#hidactions-1').val());";
                        }
                        ?> 
                        <td width="10%" align="center" valign="middle" class="grid-td-top">
                            <input class="checkallcheckbox" name="chkaction[]" id="chkaction-<?php echo $Action['actionId'] ?>" type="checkbox" onclick="<?php echo $onclick; ?>"  />&nbsp;<?php echo $Action['actionName'] ?></td>
                    <?php } ?>
                </tr>
            </table>
        </div>
        <div class="information-white-bg" id="dvpersonalinformation" >
            <div class="grid-list-inner-table-text">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" >
            <?php  $i=0; 
            foreach ($AllModule as $Module) { 
                if($Module['chrcustom']=='Y' && $i==0) { ?>
                    <tr>
                        <td width="100%" align="left" colspan="9"  valign="middle" class="grid-td-top">
                        <div class="grid-list-subtitle-bg" style="padding: 0px 0 0px 0px;" >
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td width="100%" align="left"  valign="middle" class="grid-td-left">Custom Modules</td>
                                </tr>
                            </table>
                        </div>
                        </td>
                    </tr>
                <?php  $i++ ; } ?>
                        <tr id="gtr-<?= $row['int_glcode'] ?>"  style="background-color: transparent;"  class="<?= $clsname ?>" onmouseover="this.style.backgroundColor='#f6f6f6'" onmouseout="this.style.backgroundColor='transparent'" >
                            <td width="4%" align="center" valign="middle" class="grid-td-left"><input name="chkmodule[]" id="chkmodule-<?php echo $Module['moduleId'] . '-' . $Module['chrcustom']; ?>" type="checkbox" value="<?php echo $row['int_glcode'] ?>" onclick="selectallaction('chkmodule-<?php echo $Module['moduleId'] . '-' . $Module['chrcustom']; ?>',$('#hidmodules-<?php echo $Module['moduleId'] . '-' . $Module['chrcustom']; ?>').val())"/></td>
                            <td width="20%" align="left" valign="middle" class="grid-td-top"><?php echo $Module['chr_pro_modulename'] ?></td>         
                            <?php
                            foreach ($AllAction as $Action) {

  
                                if ($Action['actionId'] == 1) {
                                    $onclick = "unselectallaction('chkaccess-" . $Module[moduleId] . "-" . $Action[actionId] . "-" . $Module[chrcustom] . "',$('#hidmodules-" . $Module[moduleId] . "-" . $Module[chrcustom] . "').val(),'chkmodule-" . $Module['moduleId'] . "-" . $Module['chrcustom'] . "')";
                                } else {
                                    $onclick = "javascript:$('#chkaccess-" . $Module['moduleId'] . "-1-" . $Module['chrcustom'] . "').attr('checked',true);";
                                }

                                if ($actionmodule[$Module['moduleId']][$Action['actionId']][$Module['chrcustom']] == $Module['moduleId'] . '-' . $Action['actionId'] . '-' . $Module['chrcustom']) {
                                    $checked = "checked";
                                } else {
                                    $checked = "";
                                } ?>
                                <td width="10%" align="center" valign="middle" class="grid-td-top">
                                    <input <?php echo $checked ?> class="checkallcheckbox"  name="chkaccess[]" id="chkaccess-<?php echo $Module['moduleId'] . '-' . $Action['actionId'] . '-' . $Module['chrcustom']; ?>" type="checkbox" value="<?php echo $Module['moduleId'] . '-' . $Action['actionId'] . '-' . $Module['chrcustom']; ?>" onclick="<?php echo $onclick; ?>"/>&nbsp;</td>
                                <?php
                                $modulewiseids .= $Module['moduleId'] . '-' . $Action['actionId'] . '-' . $Module['chrcustom'] . ',';
                                $actionwiseids[$Action['actionName']] .= $Module['moduleId'] . '-' . $Action['actionId'] . '-' . $Module['chrcustom'] . ',';
                            }
                            ?>   <input type="hidden" name="hidmodules[]" id="hidmodules-<?php echo $Module['moduleId'] . '-' . $Module['chrcustom']; ?>" value="<?php echo $modulewiseids; ?>">    </tr>
                        <?php
                                $modulewiseids = "";
                            }
                            foreach ($AllAction as $Action) {
                                ?> 
                        <input type="hidden" name="hidactions[]" id="hidactions-<?php echo $Action['actionId']; ?>" value="<?php echo $actionwiseids[$Action['actionName']]; ?>">
                    <?php } ?>
                    <input type="hidden" name="ptrecords" id="ptrecords" value="<?= $rowcount ?>">
                </table>
            </div>
        </div>

        <div class="add-new-user-button" style="padding-bottom: 30px;">
            <input width="162" hspace="5" height="25" border="0" type="image" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('btnsaveandc','','<?= GLOBAL_ADMIN_IMAGES_PATH ?>/save-keep-editing-button-hover.jpg',1)" src="<?= GLOBAL_ADMIN_IMAGES_PATH ?>/save-keep-editing-button.jpg" alt="save-keep-editing" id="btnsaveandc" name="btnsaveandc" value="1">
            <input width="81" hspace="5" height="25" border="0" type="image" src="<?= GLOBAL_ADMIN_IMAGES_PATH ?>/cancel-button.jpg" alt="cancel" name="Image35" id="Image35" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image35','','<?= GLOBAL_ADMIN_IMAGES_PATH ?>/cancel-button-hover.jpg',1)" onclick="window.location.reload();return false;">
        </div>

    </form>
    <div class="clear"></div>