<link href="css/colorbox1.css" rel="stylesheet" type="text/css" media="screen">
<link href="css/cal.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/jquery2.js"></script>
<script type="text/javascript" src="js/jquery_002.js"></script>
<?php
 $targetpage = "index.php?route=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')
{   
 	//echo "manvi";
	echo "<pre>";
	print_r($_REQUEST);
    exit;
	$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']=1; 
		else
		$dataRoll['PerAdd']=0; 
		
		if($_REQUEST['edit'][$key]!='')
		$dataRoll['PerEdit']=1; 
		else
		$dataRoll['PerEdit']=0; 
		
		if($_REQUEST['delete'][$key]!='')
		$dataRoll['PerDelete'] =1; 
		else
		$dataRoll['PerDelete'] =0; 
		
		if($_REQUEST['view'][$key]!='')
		$dataRoll['PerView'] =1; 
		else
		$dataRoll['PerView'] =0; 
		
		if($_REQUEST['approve'][$key]!='')
		$dataRoll['PerApprove'] =1; 
		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')
{
	//echo "manvi";
	//echo "<pre>";
	//print_r($_REQUEST);
	//exit;
	$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']=1; 
		else
		$dataRoll['PerAdd']=0; 
		
		if($_REQUEST['edit'][$key]!='')
		$dataRoll['PerEdit']=1; 
		else
		$dataRoll['PerEdit']=0; 
		
		if($_REQUEST['delete'][$key]!='')
		$dataRoll['PerDelete'] =1; 
		else
		$dataRoll['PerDelete'] =0; 
		
		if($_REQUEST['view'][$key]!='')
		$dataRoll['PerView'] =1; 
		else
		$dataRoll['PerView'] =0; 
		
		if($_REQUEST['approve'][$key]!='')
		$dataRoll['PerApprove'] =1; 
		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 rel="stylesheet" href="css/new_mxerp.css">
<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']!=''){?>
<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>
  <?php
   
   function CategoryShow($pcode, $PcateID, $dashVal)
		{
			global $db;
			$dashVal = $dashVal . "&nbsp;&nbsp;&nbsp;";
			echo $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);
				}
			}
		} 
   ?>
	
<tr>
	<td colspan="3">&nbsp;</td>
</tr>
 <?php  $sql=DB_query("select * from companies",$db); while($res=DB_fetch_assoc($sql)){ ?>
<tr>
   <td colspan="3"  id="abc" align="center" height="20px">
      
       <div style="width: 100%;">
           <div style="width: 38%; float: left;"><input type="checkbox"><?php echo $res['CompanyName'] ?></div>
           <div style="width: 59%; float: left;"><select name="rolepid"  id="rolepid" style="width:200px;">
		<option value="0">Primary</option>
 		<?php
		  $query=DB_query("select * from securityroles where rolepid=0 and companyid='".$res['CompanyId']."'",$db);
		  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>
       </div>
       
   </td>
  
 </tr>
 <?php } ?>
 <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">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 
					 $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="250" width="250">
	<tbody><tr><td>
	<div style="height:250px; 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 class="paddfeildbold">&nbsp;</td></tr>
		 <tr><td class="paddfeildbold"><input name="button" type="button" class="search" accesskey="A" onclick="addRow('dataTable')" value="Add Rows" /></td></tr>
		 <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%">
			
			 
		<?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 class="tabledata">
		<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; ?>" /></td>
		<td><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" /></td>
	 	<td><input type="hidden" name="formid[<?php echo $k; ?>]" id="formid_<?php echo $k; ?>" value="<?php echo $row2['frm_moduleid'];?>" /></td>		
		<td><input name="add[<?php echo $k; ?>]" type="checkbox" id="add_<?php echo $k; ?>" <?php echo $addchecked;?>/>Add</td>
		<td><input name="edit[<?php echo $k; ?>]" type="checkbox" id="edit_<?php echo $k; ?>" <?php echo $editchecked;?>/>Edit</td>
		<td><input name="delete[<?php echo $k; ?>]" type="checkbox" id="delete_<?php echo $k; ?>" <?php echo $deletechecked;?>/>Delete</td>
		<td><input name="view[<?php echo $k; ?>]" type="checkbox" id="view_<?php echo $k; ?>" <?php echo $viewchecked;?>/>View</td>
		<td><input name="approve[<?php echo $k; ?>]" type="checkbox" id="approve_<?php echo $k; ?>" <?php echo $approvechecked;?>/>Approval</td>
		</tr>
		<tr style="height:5px"><td></td></tr>
			<?php 	
			$k++;}
		}
		?>
		<input type="hidden" name="" id="hidformcount" value="<?php echo $k;?>" />
		<?php 
		if($_REQUEST['action']=='Edit' && $_REQUEST['Id']!='')
		{$counter=$k;}else{$counter=1;}
		//echo $form_count_no."<br />";
		//echo "k-".$k."<br />";
		//echo $limit."<br />";
		//for($i=$counter;$i<=$limit;$i++)
		//{ ?>
		<tbody id="dataTable">
		<tr class="tabledata">
		<td class="tabledata"><input name="assign[]" checked="checked" type="checkbox" id="assign_<?php echo $counter;?>" onclick="select_all(<?php echo $counter;?>)"></td>
		<td><input name="modulename[]" style="width:150px;" class="whitebox" readonly="readonly" type="text" id="modulename_<?php echo $counter;?>" /></td>
		<td><input type="hidden" name="moduleid[]" id="moduleid_<?php echo $counter;?>" /></td>	
	 	<td><input name="formname[]" style="width:150px;" class="whitebox" readonly="readonly" id="formname_<?php echo $counter;?>" type="text" /></td>
		<td><input type="hidden" name="formid[]" id="formid_<?php echo $counter;?>" /></td>		
		<td><input name="add[]" type="checkbox"   id="add_<?php echo $counter;?>" checked />Add</td>
		<td><input name="edit[]" type="checkbox" id="edit_<?php echo $counter;?>" checked />Edit</td>
		<td><input name="delete[]" type="checkbox" id="delete_<?php echo $counter;?>" checked />Delete</td>	
		<td><input name="view[]" type="checkbox" id="view_<?php echo $counter;?>" checked />View</td>
		<td><input name="approve[]" type="checkbox" id="approve_<?php echo $counter;?>" checked />Approval</td>
		</tr>
		</tbody>
		<?php //} ?>
		 
		 </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" rel='day_view' value="Save" accesskey="S" type="submit" onclick="return validate();" class="search">
		<input name="Submit2" value="Cancel" type="button" class="search"></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()
{
    // alert($("input[rel='day_view']").val());
        /*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)
    {
	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();
}

function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
			var newcell = row.insertCell(i);
			newcell.className='tabledata';
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
			   //alert(newcell.childNodes[0].id);
			   
			   var brk_newcellchildNodesid=newcell.childNodes[0].id.split('_');
			   //alert(brk_newcellchildNodesid[1]);
			   
			   var rowCountNew=parseFloat(brk_newcellchildNodesid[1])+(rowCount);
			   
				switch(newcell.childNodes[0].type) {
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;
					newcell.childNodes[0].id = brk_newcellchildNodesid[0]+'_'+rowCountNew;
					
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = brk_newcellchildNodesid[0]+'_'+rowCountNew;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = brk_newcellchildNodesid[0]+'_'+rowCountNew;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = brk_newcellchildNodesid[0]+'_'+rowCountNew;
				newcell.childNodes[0].checked=true;

				break;

				case "select-one":
				
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = brk_newcellchildNodesid[0]+'_'+rowCountNew;
				break;	

			}
			
			

		}

	}

</script>

