<?php  
include('includes/Class_www_users.php');
include('CRM/functions.php');
$deptid=$s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');
$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$lowerlist=  UserDeptList($LowerDeptList);
if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist).",".$_SESSION['UserID'];
else
$lowerlist="'".$_SESSION['UserID']."'";


$lowerlist;
$table='www_users';
$fullacces=8;
$_REQUEST['fullacces']=$fullacces;
$daction=$_REQUEST['daction'];
$query = "SELECT COUNT(*) as num FROM www_users";
$total_pages =DB_fetch_array(DB_query($query,$db));
$total_pages = $total_pages['num'];
 	/* Setup vars for query. */
$targetpage = "index.php?route=Setup/CompanyUsers&Application=".$_REQUEST['Application']."&modid=".$_REQUEST['modid'].""; 	//your file name  (the name of this file)
	$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 no page var is given, set start to 0
	/* Setup page vars for display. */
	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');
	$_REQUEST['fullacces']=8;
//*************************Search********************************************************************
if($_REQUEST['Search'])
{
	  $filter=array();
      if($_REQUEST['userid']!='')
	  $filter[]="userid='".$_REQUEST['userid']."'";
	  if($_REQUEST['companyid']!='')
	  $filter[]="companyid='".$_REQUEST['companyid']."'";
 		if(count($filter)>0)
		{
		   $filterval=implode(' and ',$filter );
		    $GetRecord = "Select * from $table where   $filterval LIMIT $start, $limit";
		}
		else
		{
		  $GetRecord = "Select * from $table where companyid='".$_SESSION['ACGCompanyId']."' LIMIT $start, $limit";
		}
}
else
{
$GetRecord = "Select * from $table  where companyid='".$_SESSION['ACGCompanyId']."'  LIMIT $start, $limit ";
}  
//**************************************************************************************************  
 
 
 function CategoryShow($companyid, $PcateID, $dashVal,$seclect)
		{
			global $db;
			$dashVal = $dashVal . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			    $s=new db_query();
   		 

		   	$sqlCate = "select * from deptmaster_hierarchy  where  parentid  = '".$PcateID."' and companyid='".$companyid."'";
		 
			$rsCate  = DB_query($sqlCate,$db);	
 			if(DB_num_rows($rsCate)>0)
			{
				while($rowCate = DB_fetch_array($rsCate))
				{
				$px='';
				 if($rowCate['id']==$seclect)
				 $px="selected"; ?>
 
 <option value="<?php echo $rowCate['id'];?>" <?php echo $px ?>><?php echo $dashVal;?>
 <?php echo "-".$rowCate['deptname'];?> </option>
  					
				 
 				 
				
					 
					<?php 
					  CategoryShow($companyid,$rowCate['id'], $dashVal);
				}
			}
		} 
		
	 
		
		$dashVal = "&nbsp;&nbsp;&nbsp;";
 
 
 
$msgErr=array();
$fstatus=true;
//*********************************GET DATA FROM DATA BASE*****************************
if($_REQUEST['action']=='Edit'  && $_REQUEST['Id']!='' && !$_REQUEST['Save'])
{
	
 	$condition="id='".$_REQUEST['Id']."'";
	//echo "select * from $table where $condition ";
	$query=DB_query("select * from $table where $condition ",$db);
	$RData=DB_fetch_array($query);	
	$userid=$RData['userid'];
	$companyid=$RData['companyid'];
   	$mst_site=$RData['mst_site'];
	$mst_dpt_heirarchy=$RData['mst_dpt_heirarchy'];
	$designation=$RData['designation'];
	$department=$RData['department'];
 	$assigned_sites=$RData['assigned_sites'];
	$dashboard=$RData['dashboard_assign'];
	$parentid=$RData['parentid'];
 	$phone=trim($RData['phone']);
  	$realname=trim($RData['realname']);
	$email=trim($RData['email']);
	$fullaccess=$RData['fullaccess'];
 	$rolepid=$RData['rolepid']; 
	
 
	$get_sql1=DB_query("Select * from empmaster where  mst_www_userid ='".$_REQUEST['Id']."' ",$db);
	$get_row1=DB_fetch_array($get_sql1);
    $emp_code=$get_row1['emp_code'];
	$emp_shift=$get_row1['employee_type'];
	$doj=$get_row1['doj'];
  
}
else
{
	$userid=trim($_REQUEST['userid']); 
	$password=trim($_REQUEST['password']); 
	$rolepid=trim($_REQUEST['rolepid']); 
	$dashboard=$RData['dashboard_assign'];
	$realname=trim($_REQUEST['realname']);
	$heirarchy=trim($_REQUEST['heirarchy']);
	$assigned_sites=$_REQUEST['assigned_sites'];
	$mst_site=$_REQUEST['mst_site'];
	$fullacces=trim($_REQUEST['fullacces']);
	$designation=trim($_REQUEST['designation']);
	$department=trim($_REQUEST['department']);
	$phone=trim($_REQUEST['phone']);
	$email=trim($_REQUEST['email']);
	$companyid=trim($_REQUEST['companyid']); 
	$fullaccess=$_REQUEST['fullaccess'];
	$parentid=$_REQUEST['parentid'];
	$emp_code=$_REQUEST['emp_code'];
	$emp_shift=$_REQUEST['employee_type'];
	$doj=$_REQUEST['doj'];
}
 
if($_REQUEST['Save'] )
{
	if($companyid=='')
	{
	   $msgErr[]='Please Select Company';
	   $fstatus=false;
	}
	
	/*if($rolepid=='')
	{
	   $msgErr[]='Please Select Roles';
	   $fstatus=false;
	}
	*/
}

//**********************DELETE****************************************************
if($_REQUEST['action']=='del')
{
			$condition="id='".$_REQUEST['Id']."'";
			$s->DeleteRecord($table,$condition);
			$s->pageLocation($targetpage.'&daction=Deleted');
}
 
 
//*************************Update DATA FROM DATA BASE***************************************
if($_REQUEST['Save'] && $_REQUEST['action']=='add')
{
	$get_last_id="Select id from www_users order by id DESC LIMIT 1";
	$last_id_result=DB_query($get_last_id,$db);
	$last_id=DB_fetch_array($last_id_result);
	$ID=$last_id['id'];
	$nextId=$ID+1;
	$user_input=$_REQUEST['password'];
	$data_userid['id']=$nextId;
  	$data_userid['userid']=$_REQUEST['userid'];
	$data_userid['parentid']=$_REQUEST['www_users'];
	$data_userid['mst_dpt_heirarchy']=$_REQUEST['heirarchy'];
	$data_userid['password']= CryptPass($_POST['password']);
 	$data_userid['email']=$_REQUEST['email'];
	
		
	if($_REQUEST['assigned_sites']!='0' && $_REQUEST['assigned_sites']!='')
	{
		$data_userid['assigned_sites']=implode(",",$_REQUEST['assigned_sites']);
	 
	}
	else
	{
		$data_userid['assigned_sites']=$_REQUEST['assigned_sites'];
	 
	}
	$data_userid['mst_site']=implode(",",$_REQUEST['mst_site']);
	
	
	/*if(count($_REQUEST['dashboard'])>1)
	{
		$data_userid['dashboard_assign']=implode(",",$_REQUEST['dashboard']);
	 
	}
	else
	{
		$data_userid['dashboard_assign']=$_REQUEST['dashboard'];
	 
	}*/
	
	$data_userid['dashboard_assign']=$_REQUEST['dashboard'];
    $data_userid['department']=$_REQUEST['department'];
    $data_userid['designation']=$_REQUEST['designation'];
	$data_userid['fullaccess']=8;
	$data_userid['phone']=$_REQUEST['phone'];
	$data_userid['theme']='professional';
	$data_userid['language']='en_US';
	$data_userid['Type']='user';
  	$data_userid['modulesallowed']=implode(',',$_REQUEST['modulesallowed']);
  	$data_userid['realname']=$_REQUEST['realname'];
	$data_userid['rolepid']=$rolepid; 
  	$data_userid['companyid']=$companyid; 
	
	$site1=explode(",",$data_userid['mst_site']);
	$site2=$site1[0];
	
	
	
	
	//echo "<pre>";
	//print_r($data_userid);
	//exit;
 	$row_insert= $s->RecordInsert($table, $data_userid );
	$lastid=$s->GetLastRecord($table,  "1=1",'id');	
	$data_mst_emp['emp_site']=$site2;
	$data_mst_emp['emp_name']=$_REQUEST['realname'];
	$data_mst_emp['department']=$_REQUEST['department'];
	$data_mst_emp['designation']=$_REQUEST['designation'];
	$data_mst_emp['CompanyId']=$companyid;
	$data_mst_emp['employment_type']='Permanent';
	$data_mst_emp['created_by']=$_SESSION['UserID'];
	$data_mst_emp['mst_www_userid']= $lastid;
	$data_mst_emp['emp_code']=$_REQUEST['emp_code'];
	$data_mst_emp['employee_type']=$_REQUEST['emp_shift'];
	$data_mst_emp['doj']=FormatDateForSQL($_REQUEST['doj']);
	$data_mst_emp['emp_weekly_off']=7;
  	$row_insert= $s->RecordInsert('empmaster', $data_mst_emp ); 

	
  	echo "<script>location.href='".$targetpage."'</script>";	 
 
 }
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit' && $fstatus )
{
 
	//$data['realname']=$_REQUEST['realname'];
	if($_POST['password']!=''){
	$data['password']= CryptPass($_POST['password']);
	}
	$data['email']=$_REQUEST['email'];
	if($_REQUEST['assigned_sites']!='0' && $_REQUEST['assigned_sites']!='')
	{
		$data['assigned_sites']=implode(",",$_REQUEST['assigned_sites']);
	 
	}
	else
	{
		$data['assigned_sites']=$_REQUEST['assigned_sites'];
	 
	}
	$data['mst_site']=implode(",",$_REQUEST['mst_site']);
	
	$data['mst_site']=$data['mst_site'];
	$data['dashboard_assign']=$_REQUEST['dashboard'];
	$data['department']=$_REQUEST['department'];
    $data['designation']=$_REQUEST['designation'];
	$data['mst_dpt_heirarchy']=$_REQUEST['heirarchy'];
 	$data['fullaccess']=8;
	$data['phone']=$_REQUEST['phone'];
 	$data['modulesallowed']=implode(',',$_REQUEST['modulesallowed']);
	$data['theme']='professional';
	$data['language']='en_US';
	$data['Type']='user';
 	 $data['rolepid']=$rolepid; 
	 $data['companyid']=$_SESSION['ACGCompanyId']; 	 
	 $data['parentid']=$_REQUEST['www_users'];
	//print_r($data);
	//exit;	
		 
 	$cond="id='".$_REQUEST['Id']."'";
	$row_update= $s->RecordEdit($table, $data, $cond);
 	 if(!$row_update)
	{
	   die('Error'.DB_error());
	}  
 	/*$data_mst_emp['emp_site']=$_REQUEST['mst_site'];
	$data_mst_emp['emp_name']=$_REQUEST['realname'];
	$data_mst_emp['department']=$_REQUEST['department'];
	$data_mst_emp['designation']=$_REQUEST['designation'];
	//$data_mst_emp['CompanyId']=$companyid;
	$data_mst_emp['employment_type']='Permanent';
	$data_mst_emp['created_by']=$_SESSION['UserID'];
	$data_mst_emp['created_by']=$_SESSION['UserID'];
	$data_mst_emp['emp_code']=$_REQUEST['emp_code'];
	$data_mst_emp['employee_type']=$_REQUEST['emp_shift'];
	$data_mst_emp['doj']=FormatDateForSQL($_REQUEST['doj']);
	$data_mst_emp['emp_weekly_off']=7;
	
 	//
	$cond="mst_www_userid='".$_REQUEST['Id']."'";
	$chkemp=$s->GetFieldValue('empmaster',$cond ,'id');
	if($chkemp==0){
		$data_mst_emp['mst_www_userid']=$_REQUEST['Id'];
 	  	$row_insert= $s->RecordInsert('empmaster', $data_mst_emp ); 		
	}else{

  	$row_insert= $s->RecordEdit('empmaster',$data_mst_emp ,$cond  ); 	
	 }*/
	 
 $s->pageLocation($targetpage.'&daction=Updated'); 
}

		?>
<script language="javascript" >
 
function FProfileMaster()
{
	myCheckboxArray = document.myform.elements["assigned_sites[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('assigned_site').checked==true)
	{	
	if(t==undefined)
	document.getElementById('assigned_sites_'+0).checked=true;		
	for (var i = 0; i <= t; i++)
	{
	document.getElementById('assigned_sites_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('assigned_sites_'+0).checked=false;		
	
	for (var i = 0; i <= t; i++)
	{
	document.getElementById('assigned_sites_'+i).checked=false;	
	}
	}
}




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 getZones(str)
{
	var cnt=document.getElementById("cnt").value;
	var arr=Array();
	for(var i=0; i<cnt; i++)
	{
	if(document.getElementById("mst_site_"+i).checked==true)
	{
	arr.push(document.getElementById("mst_site_"+i).value);
	}
	}
	//alert(arr);
	
	if(str=="")
	{
	document.getElementById('hid-zone-div').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("hid-zone-div").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","ZonePrefix.php?arr="+arr,true);
	xmlhttp.send();
}




function GetCompanyDetals(val)
{
	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("heirarchy").innerHTML=data[0];
	//document.getElementById("empshift").innerHTML=data[1];
	document.getElementById("DepartData").innerHTML=data[2];
	document.getElementById("ROLES").innerHTML=data[3];
	document.getElementById("BranchCode").innerHTML=data[4];
	}
	}
	xmlhttp.open("GET","BranchPrefix.php?type="+'Companyusers'+"&q="+val,true);
	xmlhttp.send();
}


function GetDesginationUser(val)
{
	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("hid-dept-div").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","Getdesginationfordept.php?deptId="+val,true);
	xmlhttp.send();
}


</script> 
<link href="css/pagnation.css" rel="stylesheet" type="text/css">
<script language="javascript" src="js/function-ajax.js"></script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Company Users</h1>
<?php if($_REQUEST['action']==''){	?>
<p align="left"><a href="<?php echo $targetpage;?>&action=add"><input type="button" value="AddNew" class="search" /></a></p><?php }else{?>
<p align="right"><a href="javascript:history.go(-1)"> [Go Back]</a></p>
<?php } ?>
<?php
//**************************Display Mess********************************************     
   
     if(isset($daction))
	 { ?>
	 <div align="left" class="success" >Record <?php echo $_REQUEST['daction']; ?> Successfully </div>
	 <?php 
	 }
	 
	 
//**************************Error Section*********************************************     
	
	 
if(isset($msgErr)&&$msgErr!='' && count($msgErr)>0 )
{?>
    <tr>
     <td colspan="3" ><div class="error">
  <?php 
	for($kp=0;$kp<count($msgErr);$kp++)
	 {
		echo  $msgErr[$kp].'<br>';
	 }
  ?></div></td>
  </tr>
  
   <?php } 
 //**************************Error Section*********************************************     
 ?>
 
 
<?php  if($_REQUEST['action']==''){?>	
<form action="" method="post" name="myform">
<table width="50%" border="0" cellspacing="0" cellpadding="3" align="center">
<tr class="row1">
    <td><strong>Company  :</strong> </td>     
	<td><select name="companyid" onchange="CompanyRoles(this.value,'txtCompanyRole','CompanyRole.php')" style="width:200px;">
		<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']==$_REQUEST['companyid'])
		{
		$p="selected";
		}
		
		?>
		<option value="<?php echo $row['CompanyId'] ?>" <?php echo $p; ?>><?php echo $row['CompanyName'] ?></option>
		<?php } ?>
		</select>
      </td>
</tr>
<tr class="row1">
	<td colspan="2" style="text-align:center;"><input name="Search" class="search"  value="Search" type="submit"></td>
</tr>
</table>
</form>

<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr class="row1"> 
	<td class="tabledata"><strong>Company </strong></td>
	<td class="tabledata"><strong>UserId</strong></td>
	<td class="tabledata"><strong>Department</strong></td>
	<td class="tabledata"><strong>Role</strong></td>
	<td class="tabledata"><strong>Name</strong></td>
	<td class="tabledata"><strong>Email</strong></td>
	<td class="tabledata"><strong>Status</strong></td>
	<td class="tabledata"><strong>Edit</strong></td>
	<td class="tabledata"><strong>Delete</strong></td>
</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 class="row1"> 
   <td><?php  echo  $s->getField('companies','CompanyId',$RowData['companyid'],'CompanyName');
   
    ?>	  </td>
   <td><?php echo $RowData['userid']; ?>	  </td>
      <td><?php echo  $s->getField('deptmaster','id',$RowData['department'],'deptname'); ?>	-
	  <?php echo  $s->getField('desigmaster','id',$RowData['designation'],'designame');?>	    </td>

   <td><?php  echo  $s->getField('securityroles','secroleid',$RowData['rolepid'],'role_name');?></td>
   <td><?php echo $RowData['realname']; ?>	  </td>
   
  <td><?php echo $RowData['email']; ?>	  </td>




   
    <td><?php echo ($RowData['blocked']=='0'?'Active':'Inactive'); ?>	  </td>
      <td><div align="center"><a href="<?php echo $targetpage;?>&action=Edit&Id=<?php echo $RowData['id']; ?>"><img src="images/icon-edit.png"  width="15" height="15"/></a></div></td>
	<td><?php if($_SESSION['frmDelete']==1 && $_SESSION['UserID']!=$RowData['id']) { ?> 
		<div align="center"><a href="<?php echo $targetpage;?>&action=del&Id=<?php echo $RowData['id']; ?>" onClick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete3.gif" width="15" height="15" /></a>
		
		</div><?php } else { echo "No Permissions"; }?></td>	
  </tr>

<?php $i++;}  ?> 
<tr class="row1"> 
	<td>&nbsp;</td>
	<td><?=$pagination?></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
</tr>
</table>

<?php } else{ ?>
<form action="" method="post" name="myform" >
<table width="50%" border="0" cellspacing="0" cellpadding="3" align="center">
<tr class="row1">
	<td class="tabledata1"><strong>User Id</strong></strong></td>
	<td><input name="fullacces" type="hidden" value="8" />
	<input name="userid" id="userid" type="text" value="<?php echo $userid;?>"  style="width:200px;"/></td>
</tr>
<tr class="row1">
	<td><strong>Password:</strong></td>
	<td><input type='password' name='password' id="password" style="width:200px;" maxlength=20 value='<?php echo $password;?>'></td>
</tr>
<tr class="row1">	 
		 <td><strong>Name:</strong></td>
		 <td><input type='text ' name='realname' id="realname" style="width:200px;" value='<?php echo $realname ;?>' onblur="ShowDiv(this.value,'hid-EmployeeID','EmpCodePrefix.php')"></td>
</tr>
<!--<tr class="row1">
	   <td class="tabledata1"><strong>Employee Code</strong></strong></td>
	   <td><div id="hid-EmployeeID"><input type="text" name="emp_code" id="emp_code" value="<?php echo $emp_code;?>" size="30" tabindex="4" style="width:200px;"/></div></td>
</tr>-->
<tr class="row1">
		<td class="tabledata1"><strong>Company Name</strong></td>
		<td class="tabledata1" colspan="2"><select name="companyid" id='companyid' onchange="GetCompanyDetals(this.value)" style="width:200px;">
 						<option value="0">--</option>
					<?php //CompanyId='".$_SESSION['ACGCompanyId']."'
					  $query=DB_query("select * from companies where 1=1",$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>
</tr>
<tr class="row1">
		<td class="tabledata1"><strong>Heirarchy</strong></td>
		<td>
			<div id="herar" style="height:135px; width:200px; border:1px solid; background-color:#FFFFFF">
			<select name="heirarchy" id="heirarchy"  multiple="multiple" style="height:135px; width:200px;">
			<?php if($_REQUEST['action']=='Edit'){ ?>
			
			<?php echo $mst_dpt_heirarchy;
			$dashVal = "&nbsp;&nbsp;&nbsp;";
			$query_comprole=DB_query("select * from deptmaster_hierarchy where parentid=0 and companyid='".$_SESSION['ACGCompanyId']."'",$db);
			while($row_comprole=DB_fetch_array($query_comprole))
			{
			$sel='';
			if($mst_dpt_heirarchy==$row_comprole['id'])
			$sel='selected';
			?>
			<option value="<?php echo $row_comprole['id']; ?>"<?php echo $sel; ?>><?php echo $row_comprole['deptname']; ?></option>
			<?php CategoryShow($row_comprole['CompanyId'], $row_comprole['id'], $dashVal,$mst_dpt_heirarchy);}	?>
			<?php } ?>
			</select>
			</div>
		</td>
</tr>
<?php //if($_REQUEST['action']!='Edit'){ ?>
<!--<tr class="row1">
		<td class="tabledata1"><strong>Employee Shift</strong></td>
		<td id="empshift"><select name="" id="" style="width:200px;">
				<option value="">Select</option>
			</select>
		</td>
</tr>
<tr class="row1">
		<td class="tabledata1"><strong>Date Of Joining</strong></td>
		<td><input type="text" name="doj" id="doj" style="width:200px;" class="datepicker" /></td>
</tr>-->
<?php //}?>
<tr class="row1">
		<td class="tabledata1"><strong>Department</strong></td>
		<td id="DepartData">
		<select name="department" id="department" onchange='GetDesginationUser(this.value)'  style="width:200px"  >
		<option value="000">---Select---</option>
		<?php $qdepart=DB_query("select * from deptmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
		while($rsdepart=DB_fetch_array($qdepart)){?>
	<option value="<?php echo $rsdepart['id'];?>"<?php echo($department==$rsdepart['id']?'selected':'')?>><?php echo $rsdepart['deptname'];?></option>
	<?php }?>
	</select>
		</td>
</tr>
<tr class="row1">
		<td class="tabledata1"><strong>Desgination</strong></td>
		<td id="hid-dept-div"><select name="designation" id="designation" style="width:200px;"  > 
	<option value="000">---Select---</option>
	<?php $qdesig=DB_query("select id,designame from desigmaster where  CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
		while ($rsdesig=DB_fetch_array($qdesig)){
	?>
	<option value="<?php echo $rsdesig['id'];?>" <?php echo ($designation==$rsdesig['id']?'selected':'')?>><?php echo $rsdesig['designame'];?></option>
	<?php }?>
	</select>
		</td>
</tr>
<tr class="row1">
		<td class="tabledata1"><strong>Roles</strong></td>
		<td>
			<div id="ROLES" style="height:150px; width:200px; border:1px solid; background-color:#FFFFFF">
			<?php if($_REQUEST['action']=='Edit'){ ?>
			<?php 
		$pcode= $rolepid;
		function CategoryShow1($pcode, $PcateID, $dashVal)
		{
			global $db;
			$dashVal = $dashVal . "&nbsp;&nbsp;&nbsp;&nbsp;";
			$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			
					   CategoryShow1( $pcode,$rowCate['secroleid'], $dashVal);
				}
			}
		} 
		
		$dashVal = "&nbsp;&nbsp;&nbsp;";
		$query=DB_query("select * from securityroles where rolepid=0 and companyid ='".$_SESSION['ACGCompanyId']."'",$db);
		?>
		   <strong>(Select only one Role)</strong>
		  <select name="rolepid" multiple="multiple" style="height:135px; width:200px;">
            <?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 
		   CategoryShow1($rolepid, $row['secroleid'], $dashVal);
		}} ?>
          </select>
		  <?php } ?>
			</div>
		</td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Company Branch</strong></td>
	<td>
	<div id="BranchCode" style="overflow:auto; height:150px; width:200px; border:1px solid; background-color:#FFFFFF" >
	<?php if($_REQUEST['action']=='Edit'){ ?>
	<?php 
	if($mst_site!='0'){
	$sites=explode(",",$mst_site);}
	else
	{$sites=$mst_site;}
	?>
	<?php $c3=0;
	//echo "select * from locations where mst_companyid='".$companyid."'";
	$query=DB_query("select * from locations where mst_companyid='".$companyid."'",$db);
	while($row=DB_Fetch_array($query))
	{
	if($sites!='0')
	{
	$c='';
	for($s=0;$s<count($sites);$s++)
	{
	if($sites[$s]==$row['loccode'])
	$c="checked";
	}
	}
	?>
	<input type="checkbox" name="mst_site[<?php echo $c3;?>]" id="mst_site_<?php echo $c3;?>" 
	<?php echo $c;?> value="<?php echo $row['loccode'];?>" 
	onclick="getZones(this.value)"/><!--ShowDiv(this.value,'hid-zone-div','ZonePrefix.php?type=Companyusers')-->
	<?php echo $row['loccode']." - ".$row['locationname'];?><br />
	
	
	<?php
	$c3++; } ?>
	<input type="hidden" name="" id="cnt" value="<?php echo $c3;?>" />
	</div>
	<?php  }?>
	</td>
</tr>

<tr class="row1">
	<td class="tabledata1"><strong>Zone</strong></td>
	<td> 
	<div id="hid-zone-div" style="overflow:auto; height:150px; width:200px; border:1px solid; background-color:#FFFFFF" >
	<?php if($_REQUEST['action']=='Edit'){ ?>
	<?php  
	if($mst_site!='')	{
	if($assigned_sites!='0'){
	$sites=explode(",",$assigned_sites);}
	else
	{$sites=$assigned_sites;}
	
	$c3=0;$j=1;
	$site1=implode("','",explode(",",$mst_site));
	$query=DB_query("select * from zone_master where site in('$site1') and  status='Y'",$db);
	?>
	<input type="checkbox" name="assigned" id="assigned_site"  value="0" onclick="FProfileMaster()">All<br />
	<?php 
	while($row=DB_fetch_array($query))
	{
	if($sites!='0')
	{
	$c='';
	for($s=0;$s<count($sites);$s++)
	{
	if($sites[$s]==$row['id'])
	$c="checked";
	}
	}
	?>
	
	<?php 
	?>
	<input type="checkbox" name="assigned_sites[]" id="assigned_sites_<?php echo $c3;?>" <?php echo $c;?> value="<?php echo $row['id'];?>" 
	/>
	<?php echo $j." - ".$row['ZoneTitle'];?><br />
	
	<?php  $c3++; $j++;} ?>
	<?php } ?>
	<?php }?>
	</div>
	
	
	
	</td>
</tr>
<tr class="row1">
	<td class="tabledata1">DashBoard </strong></td>
	<td><div style="overflow:auto; height:150px; width:200px; border:1px solid; background-color:#FFFFFF" >	
	<?php
	$module1=explode(",",$dashboard); 
	$sqldashboard=DB_query("select * from dashboard_master",$db);
	while($sqldash=DB_fetch_array($sqldashboard))
	{
		$p1='';
 
if(in_array($sqldash['id'], $module1))
$p1='checked';
	?>	
	<input type="radio" name="dashboard" value="<?php echo $sqldash['id'];?>" <?php echo $p1?>>	<?php echo $sqldash['dashboard_name']."<br>"; ?>
	<?php	 } ?>		
</div></td>
</tr>



<tr class="row1">
	<td class="tabledata1" colspan="2">
			<table width="100%" border="0" cellspacing="0" cellpadding="3">
		 	<tr>
			<td valign="top"> 
			<fieldset>
			<legend> <strong>User Contact Details</strong></legend>
			<table width="100%" border="0" cellspacing="0" cellpadding="3">
			<tr class="row1">
				<td class="tabledata1"><strong>Contact Number:</strong></td>
				<td><input name="phone" type="text" style="width:200px;" value="<?php  echo $phone;?>" />&nbsp;</td>
			</tr>
			<tr class="row1">
				<td class="tabledata1"><strong>Email Address:&nbsp;</strong></td>
			    <td><input name="email" type="text" style="width:200px;" value="<?php echo $email ?>" /></td>
			</tr>
			<tr class="row1">
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			</tr>
			</table>
			</fieldset></td>
			<td valign="top"> 
			<fieldset>
			<legend> <strong>Security&nbsp; Setting </strong></legend>
			<?php $ModuleList = array('Orders','Receivables', 'Payables','Purchasing', 'Inventory', 'Manufacturing', 'General Ledger', 'Setup');?>
			<table width="100%" border="0" cellspacing="0" cellpadding="3">
			<?php 
			foreach($ModuleList as $ModuleName){
			echo '<tr><td>'  . $ModuleName . ' ' . _('options') . ": </td><td><select name='modulesallowed[]'>";
			echo '<option selected value=1>' . _('Yes');
			echo '<option value=0>' . _('No');
			}
			echo '</select></td></tr>';?>
			</table>
			</fieldset></td>
		  </tr>
		</table>
	</td>
</tr>
<tr class="row1">
		    <td colspan="2"><div align="center"><input type="submit" onclick="return validate()" class="search" name="Save" value="Save" /></div></td>
</tr>
</table>
<div id='hid-site-div'>

</div> 
<div id='hid-zone-div'  >

</div>

</form>
<?php } ?>
</div>
</div>

<script type="text/javascript" language="javascript">
function validate()
{
	if(document.getElementById("userid").value=='')
	{
		alert("Please Fill The UserId");
		document.getElementById("userid").focus();
		return false;
	}
	if(document.getElementById("password").value=='')
	{
		alert("Please Fill The Password");
		document.getElementById("password").focus();
		return false;
	}
	if(document.getElementById("realname").value=='')
	{
		alert("Please Fill The Name");
		document.getElementById("realname").focus();
		return false;
	}
	if(document.getElementById("companyid").value=='0')
	{
		alert("Please Select The Company");
		document.getElementById("companyid").focus();
		return false;
	}
	if(document.getElementById("department").value=='000')
	{
		alert("Please Select The Department");
		document.getElementById("department").focus();
		return false;
	}
	if(document.getElementById("designation").value=='000' || document.getElementById("designation").value=='0')
	{
		alert("Please Select The Desgination");
		document.getElementById("designation").focus();
		return false;
	}
	if(document.getElementById("rolepid").value=='')
	{
		alert("Please Select The Roles");
		document.getElementById("rolepid").focus();
		return false;
	}
	var countbranch=document.getElementById("cnt").value;
	var chec=0;
	for(var i=0; i<countbranch;i++)
	{
		if(document.getElementById("mst_site_"+i).checked==true)
		{
			chec=1;
		}
	}
	if(chec==0)
	{
		alert("Please Check At Least One Branch");
		return false;
	}
	
	var countzone=document.getElementById("countzone").value;
	var chkzone=0;
	for(var j=0; j<countzone;j++)
	{
		if(document.getElementById("assigned_sites_"+j).checked==true)
		{
			chkzone=1;
		}
		
	}
	if(chkzone==0)
	{
		alert("Please Check At Least One Zone");
		return false;
	}
	
}
</script>