<script language="javascript">
	var asInitVals = new Array();
	
	$(document).ready(function() {
		$("#submit_div").hide();
		$("#tabs").tabs( {
			"show": function(event, ui) {
				var oTable = $('div.dataTables_scrollBody>table.display', ui.panel).dataTable();
				var tab1 = $("#tabs-1").is(":visible");
				
				if(tab1)
					$("#tab_id").val("1");
				
				if ( oTable.length > 0 ) {
					oTable.fnAdjustColumnSizing();
				}
			}
		} );
	} );
        
	function frm_validation()
	{
		if(navigator.appName != "Microsoft Internet Explorer")
			frmname = document.survey;
		
		var tab1 = $("#tabs-1").is(":visible");
		
		if(tab1)
			$("#tab_id").val("1");
		
		if(tab1)
		{
			if(frmname.usr_id.value == "")
			{
				alert("Please Provide User Name");
				frmname.usr_id.focus();
				return false;
			}
		}
	}
	
	function frm_edit_val(frmval)
	{
		$("#id").val(frmval["sub_mnu_id"]);
		$("#min_mnu_id").val(frmval["min_mnu_id_fk"]);
		$("#sub_name").val(frmval["sub_mnu_name"]);
		$("#sub_title").val(frmval["sub_mnu_desc"]);
		$("#sub_pref").val(frmval["sub_mnu_preference"]);
		
		if(frmval['status'] != 0)
			$("#status").attr("checked", true);
		else
			$("#status").attr("checked", false);
		
		close_frame();
		$("#delete_img").show();
	}
	
	function specimen_data()
	{
		usr_id = $("#usr_id").val();
		mnu_id = $("#min_mnu_id").val();
		
		var url = "";
		var loc = window.location;
		var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
		url = loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
		
		$.ajax({
			type: "POST",
			data: "page="+getUrlVars()["page"]+"&mode=edit&usr_id="+usr_id+"&mnu_id="+mnu_id,
			url: url+"index.php",
			error: function(msg){
				alert( msg.status );
			},
			success: function(msg){
				n = msg.indexOf("No menu items to display");
					// alert(msg+" "+n);
				if(msg != '' && n < 1)
					$("#submit_div").show();
				else
					$("#submit_div").hide();
				$('#dataTable').html(msg);
				$("#dataTable").show();
				
				$("#dataTable table").css("border", 0);
				
				// alert(msg);
				// close_frame();
				/*$('#tabs-1').html(msg);
				$("#delete_img").show();*/
			}
		});
	}
	
	function checkbox_count(ctrlname)
	{
		var checkboxes = 0;
		$('input[type=checkbox]').each(function(index, val)
		{
			if ($(this).attr('id') == ctrlname) {
				checkboxes++;
			}
		});
		return checkboxes;
	}
	
	function checkbox_chk_count(ctrlname)
	{
		var checkboxes = 0;
		$('input[type=checkbox]:checked').each(function(index, val)
		{
			if ($(this).attr('id') == ctrlname) {
				checkboxes++;
			}
		});
		return checkboxes;
	}
	
	function SetChecked(val, ctrlname, frmname)
	{
		ptr = frmname;
		len = ptr.elements.length;
		var i=0;
		for(i=0; i<len; i++)
		{
			if(ptr.elements[i].name == ctrlname)
			{
				ptr.elements[i].checked = val;
				SetChecked_Sub(val, ptr.elements[i].id, ptr.elements[i].value);
			}
		}
	}
	
	function SetChecked_Sub(chk_val, ctrlname, ctrl_val)
	{
		mn_val = ctrl_val;
		mn_chk = chk_val;

		if(!isNaN(ctrlname))
		{
			if(!chk_val)
			{
				$('input[type=checkbox]').each(function(index, val)
				{
					if (($(this).attr('id') == ctrlname+"_add" || $(this).attr('id') == ctrlname+"_edit" || $(this).attr('id') == ctrlname+"_delete") && mn_val == $(this).attr('value')) {
						$(this).removeAttr("checked");
					}
				});
			}
			else
			{
				$('input[type=checkbox]').each(function(index, val)
				{
					if (($(this).attr('id') == ctrlname+"_add" || $(this).attr('id') == ctrlname+"_edit" || $(this).attr('id') == ctrlname+"_delete") && mn_val == $(this).attr('value')) {
						$(this).attr("checked", true);
					}
				});
			}

			var checkboxes = checkbox_count(ctrlname);
			var checked = checkbox_chk_count(ctrlname);

			if(checkboxes == checked)
				$("#min_mnu_"+ctrlname).attr("checked", true);
			else
				$("#min_mnu_"+ctrlname).removeAttr("checked");


			ctrlname1 = ctrlname+"_add";
			var checkboxes = checkbox_count(ctrlname1);
			var checked = checkbox_chk_count(ctrlname1);

			if(checkboxes == checked)
				$("#min_mnu_"+ctrlname1).attr("checked", true);
			else
				$("#min_mnu_"+ctrlname1).removeAttr("checked");

			ctrlname2 = ctrlname+"_edit";
			var checkboxes = checkbox_count(ctrlname2);
			var checked = checkbox_chk_count(ctrlname2);

			if(checkboxes == checked)
				$("#min_mnu_"+ctrlname2).attr("checked", true);
			else
				$("#min_mnu_"+ctrlname2).removeAttr("checked");

			ctrlname3 = ctrlname+"_delete";
			var checkboxes = checkbox_count(ctrlname3);
			var checked = checkbox_chk_count(ctrlname3);

			if(checkboxes == checked)
				$("#min_mnu_"+ctrlname3).attr("checked", true);
			else
				$("#min_mnu_"+ctrlname3).removeAttr("checked");

		}
		else
		{
			in_ctrl = ctrlname.substr(0, ctrlname.indexOf("_"));
			ctrl_ext = ctrlname.substr(ctrlname.indexOf("_")+1);

			switch(ctrl_ext)
			{
				case 'add':
					ctrlname1 = ctrlname;
					ctrlname2 = in_ctrl+"_edit";
					ctrlname3 = in_ctrl+"_delete";
					break;

				case 'edit':
					ctrlname1 = in_ctrl+"_add";
					ctrlname2 = ctrlname;
					ctrlname3 = in_ctrl+"_delete";
					break;

				case 'delete':
					ctrlname1 = in_ctrl+"_add";
					ctrlname2 = in_ctrl+"_edit";
					ctrlname3 = ctrlname;
					break;
			}

			ctrl1_chk = true;
			ctrl2_chk = true;
			ctrl3_chk = true;

			$('input[type=checkbox]').each(function(index, val)
			{
				if ($(this).attr('id') == in_ctrl && mn_val == $(this).attr('value'))
				{
					if(!$(this).attr("checked") && chk_val)
					{
						$(this).attr("checked", true);
					}
				}
				else
				{
					if (mn_val == $(this).attr('value'))
					{
						if($(this).attr('id') == ctrlname1)
							ctrl1_chk = $(this).attr("checked");
						if($(this).attr('id') == ctrlname2)
							ctrl2_chk = $(this).attr("checked");
						if($(this).attr('id') == ctrlname3)
							ctrl3_chk = $(this).attr("checked");
					}
				}
			});

			// alert(ctrl1_chk+" && "+ctrl2_chk+" && "+ctrl3_chk);

			if(!ctrl1_chk && !ctrl2_chk && !ctrl3_chk)
			{
				$('input[type=checkbox]').each(function(index, val)
				{
					if ($(this).attr('id') == in_ctrl && mn_val == $(this).attr('value'))
						$(this).removeAttr("checked");
				});
			}

			var checkboxes = checkbox_count(in_ctrl);
			var checked = checkbox_chk_count(in_ctrl);

			if(checkboxes == checked)
				$("#min_mnu_"+in_ctrl).attr("checked", true);
			else
				$("#min_mnu_"+in_ctrl).removeAttr("checked");

			var checkboxes = checkbox_count(ctrlname1);
			var checked = checkbox_chk_count(ctrlname1);

			if(checkboxes == checked)
				$("#min_mnu_"+ctrlname1).attr("checked", true);
			else
				$("#min_mnu_"+ctrlname1).removeAttr("checked");

			var checkboxes = checkbox_count(ctrlname2);
			var checked = checkbox_chk_count(ctrlname2);

			if(checkboxes == checked)
				$("#min_mnu_"+ctrlname2).attr("checked", true);
			else
				$("#min_mnu_"+ctrlname2).removeAttr("checked");

			var checkboxes = checkbox_count(ctrlname3);
			var checked = checkbox_chk_count(ctrlname3);

			if(checkboxes == checked)
				$("#min_mnu_"+ctrlname3).attr("checked", true);
			else
				$("#min_mnu_"+ctrlname3).removeAttr("checked");
		}
	}
	
	function show_div(val)
	{
		if(val != '')
		{
			$("#mnu_div").show();
			specimen_data();
			// $("#submit_div").show();
		}
		else
		{
			$("#mnu_div").hide();
			$("#dataTable").html('');
			$("#submit_div").hide();
		}
	}
</script>
<div style="width: 95%; margin: 0 auto; padding: 10px;">
    <div id="frm_display">
    <div id="tabs">
        <ul>
            <li class="active"><a href="#tabs-1">&nbsp;User Privilages&nbsp;</a></li>
        </ul>

        <form class="form" name="survey" id="survey" method="post" action="<?php echo $action; ?>" onsubmit="return frm_validation();">
            <div id="tabs-1">
                <div class="questionrow">
                    <div class="question">User Name</div>
                    <div class="answer">
                        <input type="hidden" name="tab_id" id="tab_id" value="" />
                        <input type="hidden" name="id" id="id" value="" />
                        <?php
                        $common->dropdown('usr_id', 'users_name', 'lime_users', '', 'users_name', 'usr_id', $usr_id, 'onchange="return show_div(this.value);"');
                        ?>
                    </div>
                </div>
                <div class="clear_form">
                </div>
                <div class="questionrow" id="mnu_div" style="display: none;">
                    <div class="question">Main Menu Name</div>
                    <div class="answer">
                        <?php
                        $common->dropdown('min_mnu_id', 'min_mnu_desc', 'custom_main_menu', '', 'min_mnu_desc', 'min_mnu_id', $min_mnu_id, 'onchange="return specimen_data();"');
                        ?>
                        <script>
                            var len = $("#min_mnu_id").length;
                            $("#min_mnu_id option").each(function(i){
                                txt = $(this).html();
                                if(txt == 'Select')
                                    $(this).html('All');
                            });
                        </script>
                    </div>
                </div>
                <div class="clear_form">
                </div>
                <div class="questionrow" id="dataTable" style="display: none;">
                </div>
                <div class="clear_form">
                </div>
                <div class="questionrow">
                    <div style="padding: 5px;" align="center" id="submit_div">
                        <input type="submit" name="Submit" value="Submit" />
                    </div>
                </div>
            </div>
        </form>
            </div>
    </div>
</div>