 <?php
 $targetpage = "index.php?route=DealerDistrbutions/Setup/RoleMaster&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."";
 $table='securityroles';
 
 $limit = 50; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;	
 
if($_REQUEST['Search'])
{
	  $filter=array();
      if($_REQUEST['companyid']!='')
	  $filter[]="companyid='".$_REQUEST['companyid']."'";
	  if($_REQUEST['role_name']!='')
	  $filter[]="role_name like '%".$_REQUEST['role_name']."%'";
 		if(count($filter)>0)
		{
		   $filterval=implode(' and ',$filter );
		    $GetRecord = "Select * from $table where  $filterval";
		}
		else    //and secrolename!='Super Administrator' 
		{
		  $GetRecord = "Select * from $table where companyid='".$_SESSION['ACGCompanyId']."' order by companyid ASC";
		}}
else
{
	   $GetRecord = "Select * from $table where companyid='".$_SESSION['ACGCompanyId']."'";}
 
if(isset($_POST['save']) && $_REQUEST['action']=='add')
{   
 	$moduleid1=array();	
	$getlastid=DB_query("select * from $table order by secroleid desc limit 1",$db) ;
	$rid=DB_fetch_array($getlastid);
	$lastid=$rid['secroleid']+1;
 	$data['secrolename']=$_REQUEST['rolename'];  
	$data['role_name']=$_REQUEST['rolename']; 
	if($_REQUEST['rolepid']=='')
	$data['rolepid']=0; 
	else
	$data['rolepid']=$_REQUEST['rolepid']; 
	
	for($i=0;$i<count($_REQUEST['moduleid']);$i++)
	{
	 if($_REQUEST['moduleid'][$i]!='')
	 {
	 	$moduleid1[]=$_REQUEST['moduleid'][$i];
	 }
	}	
	$data['modules_allowed']=implode(',',array_unique($moduleid1));
	$data['companyid']=$_REQUEST['companyid']; 
	$data['rolestatus']=$_REQUEST['rolestatus'];
	$data['created_date']=date('Y-m-d');
	$data['created_by']=$_SESSION['UserID'];
	//echo "<pre>";
	//print_r($data); exit;
	$row=$s->RecordInsert($table, $data);
	$lastid=$s->GetLastRecord('securityroles', '1=1','secroleid');
	
	foreach($_REQUEST['formid'] as $key => $value)
	{
	    if($value!=''){
			if($_REQUEST['assign'][$key]=='on'){
		$dataRoll['moduleid']=$_REQUEST['moduleid'][$key];
 		$dataRoll['roll_id']=$lastid;
		$dataRoll['frm_moduleid']=$value;
		 
 		if($_REQUEST['add'][$key]!='')
		$dataRoll['PerAdd']=$_REQUEST['add'][$key]; 
		else
		$dataRoll['PerAdd']=0; 
		
		if($_REQUEST['edit'][$key]!='')
		$dataRoll['PerEdit']=$_REQUEST['edit'][$key]; 
		else
		$dataRoll['PerEdit']=0; 
		
		if($_REQUEST['delete'][$key]!='')
		$dataRoll['PerDelete'] =$_REQUEST['delete'][$key]; 
		else
		$dataRoll['PerDelete'] =0; 
		
		if($_REQUEST['view'][$key]!='')
		$dataRoll['PerView'] =$_REQUEST['view'][$key]; 
		else
		$dataRoll['PerView'] =0; 
		
		if($_REQUEST['approve'][$key]!='')
		$dataRoll['PerApprove'] =$_REQUEST['approve'][$key]; 
		else
		$dataRoll['PerApprove'] =0; 
		
		$dataRoll['Permission']=$dataRoll['PerAdd'].','.$dataRoll['PerEdit'].','.$dataRoll['PerDelete'].','.$dataRoll['PerView'].','.$dataRoll['PerApprove']; 
		$dataRoll['created_by']=$_SESSION['UserID'];
		$dataRoll['created_date']=date('Y-m-d');
		$row= $s->RecordInsert('company_profile', $dataRoll);
		if(!$row)
		{
		   die('Error'.DB_error());
		}
		}}
   	}
	echo "<script>location.href='index.php?route=Setup/RoleHierarchy&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
 }
 
if($_REQUEST['action']=='Edit' && $_REQUEST['Id']!='')
{
 	$sql=DB_query("Select * from securityroles where secroleid=".$_REQUEST['Id']."",$db);
	$row=DB_fetch_array($sql);
	extract($row);
	$sql2=DB_query("Select * from company_profile where roll_id=".$_REQUEST['Id']."",$db);
 }

if($_REQUEST['save']!='' && $_REQUEST['action']=='Edit')
{
	$moduleid1=array();	
 	$data['secrolename']=$_REQUEST['rolename'];  
	$data['role_name']=$_REQUEST['rolename']; 
	if($_REQUEST['rolepid']=='')
	$data['rolepid']=0; 
	else
	$data['rolepid']=$_REQUEST['rolepid']; 
	
	for($i=0;$i<count($_REQUEST['moduleid']);$i++)
	{
	 if($_REQUEST['moduleid'][$i]!='')
	 {
	 	$moduleid1[]=$_REQUEST['moduleid'][$i];
	 }
	}	
	$data['modules_allowed']=implode(',',array_unique($moduleid1));
	$data['companyid']=$_REQUEST['companyid']; 
	$data['rolestatus']=$_REQUEST['rolestatus'];
	$data['modified_date']=date('Y-m-d');
	$data['modified_by']=$_SESSION['UserID'];
	$cond="secroleid='".$_REQUEST['Id']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
	
	$delete_company_profile=DB_query("Delete from company_profile where roll_id=".$_REQUEST['Id']."",$db);
	foreach($_REQUEST['formid'] as $key => $value)
	{
	    if($value!=''){
		if($_REQUEST['assign'][$key]=='on'){
		$dataRoll['moduleid']=$_REQUEST['moduleid'][$key];
 		$dataRoll['roll_id']=$_REQUEST['Id'];
		$dataRoll['frm_moduleid']=$value;
		 
 		if($_REQUEST['add'][$key]!='')
		$dataRoll['PerAdd']=$_REQUEST['add'][$key]; 
		else
		$dataRoll['PerAdd']=0; 
		
		if($_REQUEST['edit'][$key]!='')
		$dataRoll['PerEdit']=$_REQUEST['edit'][$key]; 
		else
		$dataRoll['PerEdit']=0; 
		
		if($_REQUEST['delete'][$key]!='')
		$dataRoll['PerDelete'] =$_REQUEST['delete'][$key]; 
		else
		$dataRoll['PerDelete'] =0; 
		
		if($_REQUEST['view'][$key]!='')
		$dataRoll['PerView'] =$_REQUEST['view'][$key]; 
		else
		$dataRoll['PerView'] =0; 
		
		if($_REQUEST['approve'][$key]!='')
		$dataRoll['PerApprove'] =$_REQUEST['approve'][$key]; 
		else
		$dataRoll['PerApprove'] =0; 
		
		$dataRoll['Permission']=$dataRoll['PerAdd'].','.$dataRoll['PerEdit'].','.$dataRoll['PerDelete'].','.$dataRoll['PerView'].','.$dataRoll['PerApprove']; 
		$dataRoll['modified_by']=$_SESSION['UserID'];
		$dataRoll['modified_date']=date('Y-m-d');
		$row= $s->RecordInsert('company_profile', $dataRoll);
		if(!$row)
		{
		   die('Error'.DB_error());
		}
		}}
   	}	
	
	echo "<script>location.href='index.php?route=Setup/RoleMaster&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&action=".$_REQUEST['action']."&Id=".$_REQUEST['Id']."'</script>";
}
 ?>
<div align="right">
<?php if($_REQUEST['action']==''){	?>
<a href="<?php echo $targetpage;?>&action=add">
AddNew</a> <?php } else{?>
<A HREF="javascript:history.go(-1)"> [Go Back]</A> 
<?php } ?></div>
<style type="text/css">
fieldset{ 
   -moz-border-radius-bottomleft: 7px;
   -moz-border-radius-bottomright: 7px;
   -moz-border-radius-topleft: 7px;
   -moz-border-radius-topright: 7px;
   -webkit-border-radius: 7px;
   border-radius: 7px;
   padding:5px;
   margin:0px;
   /*background:#ededed;*/
}
.detailedViewHeader{

	border:1px solid #DDDDDD;
	padding:12px;
	background:  #fff; 
	background:  #dddcdd url(images/inner.gif) bottom repeat-x; 
	color: #000000;	
}
</style>
<link href="css/pagnation.css" rel="stylesheet" type="text/css">
<?php if($_REQUEST['action']=='')
{

if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	include('paging.php');
?>
<h2 align="center">Roles</h2>
<table width="100%" border="0" cellspacing="1" cellpadding="2" style="border:#999999 solid 1px;">
  <tr> <th scope="col"><strong>Company </strong></th>
    <th scope="col"><strong>Role Name</strong></th> 
    <th scope="col"><strong>Under</strong></th>
	 
	<th scope="col"><strong>Status</strong></th>
	      <th scope="col"><strong>Edit</strong></th>
    <th scope="col"><strong>Delete</strong></th>
  </tr>
 
 
   <?php 
  
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='3'>--No Record--</td>";
   
   while($RowData = DB_fetch_array($ResultData))
   {?> 
    <tr bgcolor="#FFFFFF">
   <td><?php  echo  $s->getField('companies','CompanyId',$RowData['companyid'],'CompanyName');
   
    ?>	  </td>
	
   <td><?php echo $RowData['role_name']; ?>	  </td>
    <td><?php //echo $RowData['rolepid'];	 
	   if($RowData['rolepid']!=0){echo  $s->getField('securityroles','secroleid',$RowData['rolepid'],'role_name');}else{echo "Primary";}
	  ?>	  </td>
  
    <td><?php echo ($RowData['rolestatus']=='Y'?'Active':'Inactive'); ?>	  </td>
      <td><div align="center"><a href="<?php echo $targetpage;?>&action=Edit&Id=<?php echo $RowData['secroleid']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td><div align="center"><a href="<?php echo $targetpage;?>&action=del&Id=<?php echo $RowData['secroleid']; ?>" onClick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></div></td>	
  </tr>
  
	<?php $i++;}   
   ?> 
</table>
<?php 	
}
elseif($_REQUEST['action']!=''){

 $sql=DB_query("select * from companies where CompanyId='".$companyid."'",$db);
		            $row1=DB_fetch_array($sql);
		            $moduleallowed=$row1['modules_allowed'];
					if($moduleallowed)
		            $moduledata1=explode(",",$moduleallowed);
					else
					$moduledata1='';
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
<fieldset>
<div style="display:none;">

</div>
<form action="" method="post" name="frm">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
 <tbody><tr>
   <td colspan="6" class="detailedViewHeader" id="abc" align="center" height="30"><b>Add Role</b></td>
 </tr>
	
<tr>
	<td colspan="3">&nbsp;</td>
</tr>

  <tr>
  	<td valign="top" width="265">
	<fieldset>
		<table style="margin-top:2px;" align="center" border="0" cellpadding="0" cellspacing="0" width="190">
      	<tbody><tr>
	        <td>
	        	<table align="center" border="0" cellpadding="5" cellspacing="2" width="80%">
					<tbody>
					<tr>
					<td class="paddfeild">Company</td>				
						<td align="left">
						<!--onchange="CompanyRoles(this.value,'txtCompanyRole','CompanyRole.php')"-->
						<select name="companyid" id='companyid' onchange="showroles(this.value)">
						)
						 >
		   <option value="0" selected="selected">--Select Company--</option>
 		<?php
		  $query=DB_query("select * from companies",$db);
		  while($row=DB_fetch_array($query))
		  {
		  
		  	$p='';
			if($row['CompanyId']==$companyid)
			{
			   $p="selected";
			}

		 ?>
		<option value="<?php echo $row['CompanyId'] ?>" <?php echo $p; ?>><?php echo $row['CompanyName'] ?></option>
		<?php } ?>
		</select>		
						</td>
						
						<?php 
		$pcode= $rolepid;
		function CategoryShow($pcode, $PcateID, $dashVal)
		{
			global $db;
			$dashVal = $dashVal . "-";
			$sqlCate = "select * from securityroles  where  rolepid  = '".$PcateID."'";
			$rsCate  = DB_query($sqlCate,$db);	
			 
			if(DB_num_rows($rsCate)>0)
			{
				while($rowCate = DB_fetch_array($rsCate))
				{
					$sqlCateSel = "select * from securityroles where rolepid =".$rowCate['rolepid']." ";
					$rsCateSel	= DB_query($sqlCateSel,$db);
					$px='';
					 if($rowCate['secroleid']==$pcode)
					 $px="selected";
			?>
					<option value="<?php echo $rowCate['secroleid'];?>" <?php echo $px ?>><?php echo $dashVal;?>
					 <?php echo $rowCate['role_name'];?> </option>
		   <?php			
					   CategoryShow( $pcode,$rowCate['secroleid'], $dashVal);
				}
			}
		} 
		
		$dashVal = "&nbsp;&nbsp;&nbsp;";
		$query=DB_query("select * from securityroles where rolepid=0 and companyid ='".$companyid."'",$db);
		?></tr>
		  			<tr>
		     <td>Under </td>
		     <td><div id='txtCompanyRole'>
			  <select name="rolepid" multiple="multiple" id="rolepid1"  >
			  <option value="0" <?php if($rolepid==0)echo "selected";?>>Primary</option>
            <?php
		if(DB_num_rows($query)>0){
		 
		  while($row=DB_fetch_array($query))
		  {		  
		  	$p='';
			if($row['secroleid']==$rolepid)
			{
			   $p="selected";
			}
		 ?>
            <option value="<?php echo $row['secroleid'] ?>" <?php echo $p; ?> ><?php echo $row['role_name'] ?></option>
            <?php 
		   CategoryShow($rolepid, $row['secroleid'], $dashVal);
		}} ?>
          </select>
		</div></td></tr>
					<tr><td class="paddfeild">Module</td>				
						<td align="left">
						
					<select name="module" class="whitestatus" style="width:150px;" id="module" onchange="callcategory(this.value)">
					<option value="0">Select</option>
					 <?php 
					
					 if(!empty($moduledata1))
					 $query=DB_query("select * from modules where id in ('".$moduledata1."')",$db);
					 else
					 $query=DB_query("select * from modules",$db);
                     while($row=DB_fetch_array($query))
                     {
				     ?>
					<option value="<?php echo $row['id']; ?>"><?php echo $row['modulename']; ?></option>
					<?php } ?>
					</select>
					
						</td>
					</tr>
					<tr>
					<td class="paddfeild">Category</td>				
					<td align="left"><div id="catedata">
					<select name="subcat" class="whitestatus" style="width:150px;" id="subcat">
					<option value="0">Select</option>
					<?php 
					 $query1=DB_query("SELECT * FROM module_form_categories",$db);
                     while($row=DB_fetch_array($query1))
                     {
				     ?>
					<option value="<?php echo $row['id']; ?>"><?php echo $row['cat_desc']; ?></option>
					<?php } ?>
					</select></div>
							
		</td>
					</tr>
				</tbody></table>
					  </td>
		</tr>	
    </tbody></table>	
	</fieldset>
	<fieldset><legend>forms</legend>
	
	<table style="margin-top:2px;" align="center" border="0" cellpadding="0" cellspacing="0" height="170" width="250">
	<tbody><tr><td>
	<div style="height:170px; overflow:auto;">
			<div id="roleassign"></div>
			
		</div></td></tr>  
		<tr>
			<td><div align="right"><input name="button" type="button" class="buttonsview" value=">>" onclick="swapdata()" /></div></td>
		</tr>    		
    </tbody></table>
	
	</fieldset>
	
	</td>
	
    <td width="681" valign="top">
    	<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
    		<tbody><tr>
    			<td>
    				<fieldset>
	<table align="center" border="0" cellpadding="5" cellspacing="2" width="100%">
	<tbody><tr>
		<td class="paddfeild"><font color="red">Role</font></td>
		<td>
		<input name="rolename" id="rolename" style="width:200px;background-color:#FFFFFF;" value="<?php echo $secrolename;?>" type="text">				</td>				
		<td class="paddfeild"><font color="red">Status</font></td>
		<td><input name="rolestatus" type="radio"  value="Y"   <?php  echo ($rolestatus =='Y'?'checked':'')?> id="rolestatus1"/>
		  Active
            <input name="rolestatus" type="radio" value="N" <?php  echo ($rolestatus =='N'?'checked':'')?> id="rolestatus2"/>
Inactive</td>
	</tr>
	</tbody></table>
    				</fieldset>	
    			</td>
    		</tr>
    	</tbody></table>
	<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
		 <tbody><tr>
		<td width="6%" height="25">&nbsp;</td>
		<td class="paddfeildbold" width="30%"><strong>Module</strong></td>
		<td class="paddfeildbold" width="28%"><strong>Form</strong></td>
		<td class="paddfeildbold"><strong> Permission </strong></td>
		</tr>
	  	<tr>
	  <td colspan="4">
		<div style="height:270px; overflow:auto; ">
		<table align="center" border="0" cellpadding="1" cellspacing="0" width="100%">
			
			 
	  <tbody>
		<?php 
		if($_REQUEST['action']=='Edit' && $_REQUEST['Id']!='')
		{
			$k=1;
			while($row2=DB_fetch_array($sql2))
			{
				$addchecked='';
				$editchecked='';
				$viewchecked='';
				$deletechecked='';
				$approvechecked='';
				
				if($row2['PerAdd']==1)
				{$addchecked='checked';}
				else{$addchecked='';}
				
				if($row2['PerEdit']==1)
				{$editchecked='checked';}
				else{$editchecked='';}
				
				if($row2['PerDelete']==1)
				{$deletechecked='checked';}
				else{$deletechecked='';}
				
				if($row2['PerView']==1)
				{$viewchecked='checked';}
				else{$viewchecked='';}
				
				if($row2['PerApprove']==1)
				{$approvechecked='checked';}
				else{$approvechecked='';}
			?>
			<tr bgcolor="#CCCCCC">
		<td><input name="assign[<?php echo $k; ?>]" checked="checked" type="checkbox" id="assign_<?php echo $k; ?>"></td>
		<td><input name="modulename[<?php echo $k; ?>]" style="width:150px;" class="whitebox" readonly="readonly" value="<?php echo $s->getField('modules','id',$row2['moduleid'],'modulename');?>" type="text" id="modulename_<?php echo $k; ?>" />
			<input type="hidden" name="moduleid[<?php echo $k; ?>]" id="moduleid_<?php echo $k; ?>" value="<?php echo $row2['moduleid'];?>" /></td>	
	 	<td><input name="formname[<?php echo $k; ?>]" style="width:150px;" class="whitebox" readonly="readonly" value="<?php echo $s->getField('modules_form','id',$row2['frm_moduleid'],'frm_name');?>" id="formname_<?php echo $k; ?>" type="text" />
	 		<input type="hidden" name="formid[<?php echo $k; ?>]" id="formid_<?php echo $k; ?>" value="<?php echo $row2['frm_moduleid'];?>" /></td>		
		<td>Add
		<input name="add[<?php echo $k; ?>]" type="checkbox" value="1"  id="add_<?php echo $k; ?>" <?php echo $addchecked;?>/>
		&nbsp;Edit  <input name="edit[<?php echo $k; ?>]" type="checkbox" value="1"  id="edit_<?php echo $k; ?>" <?php echo $editchecked;?>/>
		&nbsp;Delete<input name="delete[<?php echo $k; ?>]" type="checkbox" value="1"  id="delete_<?php echo $k; ?>" <?php echo $deletechecked;?>/>	
		&nbsp;View<input name="view[<?php echo $k; ?>]" type="checkbox" value="1"  id="view_<?php echo $k; ?>" <?php echo $viewchecked;?>/>
		&nbsp;Approval<input name="approve[<?php echo $k; ?>]" type="checkbox" value="1"  id="approve_<?php echo $k; ?>" <?php echo $approvechecked;?>/></td>
		</tr>	
		<tr style="height:5px"><td></td></tr>
			<?php 	
			$k++;}
		}
		?>
		<?php 
		$form_count=DB_fetch_array(DB_query("SELECT COUNT(*) as formcount FROM `modules_form`WHERE STATUS='Y'",$db));
		$form_count_no=$form_count['formcount'];
		if($_REQUEST['action']=='Edit' && $_REQUEST['Id']!='')
		{$counter=$k; $limit=$form_count_no-$k+1;}else{$counter=1;  $limit=$form_count_no;}
		//echo $limit;
		for($i=$counter;$i<=1000;$i++)
		{ ?>
		<tr bgcolor="#CCCCCC">
		<td><input name="assign[<?php echo $i; ?>]" checked="checked" type="checkbox" id="assign_<?php echo $i; ?>" onclick="select_all(<?php echo $i?>)"></td>
		<td><input name="modulename[<?php echo $i; ?>]" style="width:150px;" class="whitebox" readonly="readonly" value="" type="text" id="modulename_<?php echo $i; ?>" />
			<input type="hidden" name="moduleid[<?php echo $i; ?>]" id="moduleid_<?php echo $i; ?>" value="" /></td>	
	 	<td><input name="formname[<?php echo $i; ?>]" style="width:150px;" class="whitebox" readonly="readonly" value="" id="formname_<?php echo $i; ?>" type="text" />
	 		<input type="hidden" name="formid[<?php echo $i; ?>]" id="formid_<?php echo $i; ?>" value="" /></td>		
		<td>Add
		<input name="add[<?php echo $i; ?>]" type="checkbox" value="1"  id="add_<?php echo $i; ?>" checked="checked"/>
		&nbsp;Edit  <input name="edit[<?php echo $i; ?>]" type="checkbox" value="1"  id="edit_<?php echo $i; ?>" checked="checked"/>
		&nbsp;Delete<input name="delete[<?php echo $i; ?>]" type="checkbox" value="1"  id="delete_<?php echo $i; ?>" checked="checked"/>	
		&nbsp;View<input name="view[<?php echo $i; ?>]" type="checkbox" value="1"  id="view_<?php echo $i; ?>" checked="checked"/>
		&nbsp;Approval<input name="approve[<?php echo $i; ?>]" type="checkbox" value="1"  id="approve_<?php echo $i; ?>" checked="checked"/></td>
		</tr>
		<tr style="height:5px"><td></td></tr>
		<?php } ?>
				
		
		
	  	 </tbody>
		 
		 </table>
		</div>	</td>
	</tr>
		<tr>
			<td colspan="5">&nbsp;</td>
		</tr>
		<tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td colspan="2" align="right"><input name="save" class="buttonsview" value="Save" accesskey="S" type="submit" onclick="return validate();">
		<input name="Submit2" value="Cancel" class="buttonsview" type="button"></td>
		
  </tr>
</tbody></table>
</td>
  </tr>  
</tbody></table>
</form>
</fieldset>
 </td>
   
  </tr>
  <tr>
    <td>
	
    </td>
  </tr>
</table>
<?php } ?>
<script>
function select_all(i)
{
	if(document.getElementById("assign_"+i).checked==false)
	{
		if(document.getElementById("add_"+i).checked==true)
		{document.getElementById("add_"+i).checked=false}
		if(document.getElementById("edit_"+i).checked==true)
		{document.getElementById("edit_"+i).checked=false}
		if(document.getElementById("delete_"+i).checked==true)
		{document.getElementById("delete_"+i).checked=false}
		if(document.getElementById("view_"+i).checked==true)
		{document.getElementById("view_"+i).checked=false}
		if(document.getElementById("approve_"+i).checked==true)
		{document.getElementById("approve_"+i).checked=false}
	}
	else if(document.getElementById("assign_"+i).checked==true)
	{
		if(document.getElementById("add_"+i).checked==false)
		{document.getElementById("add_"+i).checked=true}
		if(document.getElementById("edit_"+i).checked==false)
		{document.getElementById("edit_"+i).checked=true}
		if(document.getElementById("delete_"+i).checked==false)
		{document.getElementById("delete_"+i).checked=true}
		if(document.getElementById("view_"+i).checked==false)
		{document.getElementById("view_"+i).checked=true}
		if(document.getElementById("approve_"+i).checked==false)
		{document.getElementById("approve_"+i).checked=true}
	}
}
function callcategory(str)
{
 if(str.length==0)
	{
	alert("Must Select Module name");
	return;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("catedata").innerHTML=xmlhttp.responseText;

	
	}
	}
	xmlhttp.open("GET","modulecategory.php?str="+str,true);
	xmlhttp.send();




}


function showtotalrecords(str)
{
   if(str.length==0)
	{
	
	return;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("roleassign").innerHTML=xmlhttp.responseText;

	
	}
	}
	var module=document.getElementById("module").value;
	
	xmlhttp.open("GET","roleassign.php?str="+str+"&module="+module,true);
	xmlhttp.send();

}

function CompanyRoles(str,hiddiv, url)
{

	//document.getElementById("txtfiles").style.display  = "none";;
  if (str=="")
  {
  document.getElementById(hiddiv).innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(hiddiv).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",url+"?q="+str,true);
xmlhttp.send();
}



function swapdata()
{
myCheckboxArray = document.frm.elements["frmnamerole[]"];
var t=myCheckboxArray.length;
//alert(t);
if(t!=undefined)
{
	for(var i=1;i<=t;i++)
	{
		if(document.getElementById("frmname_"+i).checked==true)
		{
			for(var k=1;k<=1000;k++)
			{
				if(document.getElementById("formid_"+k).value==document.getElementById("frmname_"+i).value)
				{
					alert(document.getElementById("form_name_"+i).value+" already exists in the list");
					break;
				}
				else
				{
					if(document.getElementById("modulename_"+k).value=='' )
					{
						var module_id = document.getElementById("module");
     					var module_name = module_id.options[module_id.selectedIndex].text;
						var frm_name=document.getElementById("form_name_"+i).value;
						//alert(module_name); alert(frm_name);
						document.getElementById("modulename_"+k).value=module_name;
						document.getElementById("formname_"+k).value=frm_name;	
						document.getElementById("moduleid_"+k).value=document.getElementById("module").value;
						document.getElementById("formid_"+k).value=document.getElementById("frmname_"+i).value;			
						break;
					}
					else
					{
						continue;
					}
				} 
			}
		}
	}

}
else
{
		if(document.getElementById("frmname_1").checked==true)
		{
			for(var k=1;k<=1000;k++)
			{
				if(document.getElementById("formid_"+k).value==document.getElementById("frmname_1").value)
				{
					alert(document.getElementById("form_name_1").value+" already exists in the list");
					break;
				}
				else
				{
					if(document.getElementById("modulename_"+k).value=='' )
					{
						var module_id = document.getElementById("module");
     					var module_name = module_id.options[module_id.selectedIndex].text;
						var frm_name=document.getElementById("form_name_1").value;
						//alert(module_name); alert(frm_name);
						document.getElementById("modulename_"+k).value=module_name;
						document.getElementById("formname_"+k).value=frm_name;	
						document.getElementById("moduleid_"+k).value=document.getElementById("module").value;
						document.getElementById("formid_"+k).value=document.getElementById("frmname_1").value;			
						break;
					}
					else
					{
						continue;
					}
				} 
			}
		}
	}
}

function validate()
{
	if(document.getElementById("companyid").value=='0')
	{
		alert('Please assign a company');
		document.getElementById("companyid").focus();
		return false;
	}
	if(document.getElementById("companyid").value!='0')
	{
		if(document.getElementById("rolepid").value=='')
		{alert('Under which role do you want to create this role?');
		document.getElementById("rolepid").focus();
		return false;}
	}
	if(document.getElementById("rolename").value=='')
	{
		alert('Please enter name of the role');
		document.getElementById("rolename").focus();
		return false;
	}
	if(document.getElementById("rolestatus1").checked==false && document.getElementById("rolestatus2").checked==false)
	{
		alert('Please choose status of the role');
		document.getElementById("rolestatus1").focus();
		return false;
	}
	if(document.getElementById("modulename_1").value=='')
	{
		alert('Please assign access permission to the role');
		return false;
	}
}



function FProfileMaster(str)
{

   myCheckboxArray = document.frm.elements["frmnamerole[]"];
	var t=myCheckboxArray.length;
	
	if(document.getElementById('assigned_site').checked==true)
	{	
	
	if(t==undefined)	
	document.getElementById('frmname_'+1).checked=true;
	for (var i = 1; i <=t; i++)
	{
	document.getElementById('frmname_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)	
	document.getElementById('frmname_'+1).checked=false;
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('frmname_'+i).checked=false;	
	}
	}
	

	
}

function showroles(str)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	alert(xmlhttp.responseText);
	var data=xmlhttp.responseText.split("__");
	document.getElementById("rolepid1").innerHTML=data[0];
	document.getElementById("module").innerHTML=data[1];
	 }
  }
xmlhttp.open("GET","dealer_role2.php?q="+str,true);
xmlhttp.send();



}

</script>
