<script type="text/javascript">

function getdelearSite1(str)
{

if(str.length==0)
{
	document.getElementById("SiteName").innerHTML="";
	return;
	
}
if(window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	  
		document.getElementById("SiteName").innerHTML=xmlhttp.responseText;
		
	}
	
}
xmlhttp.open("GET","getdealerSiteassign.php?Company="+str,true);
xmlhttp.send();	





}




function getSite(str)
{
	
if(str.length==0)
{
	document.getElementById("SiteName").innerHTML="";
	return;
	
}
if(window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	  	
		document.getElementById("SiteName").innerHTML=xmlhttp.responseText;
		
	}
	
}
xmlhttp.open("GET","getSite1.php?Company="+str,true);
xmlhttp.send();	
	
	
} 
function showzone(str)
{

if(str.length==0)
{
	document.getElementById("SiteName").innerHTML="";
	return;
	
}
if(window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	  	
		document.getElementById("zoneid").innerHTML=xmlhttp.responseText;
		
	}
	
}
xmlhttp.open("GET","getSite1.php?Companyusers="+str,true);
xmlhttp.send();	


}


function showsubzone(str)
{

if(str.length==0)
{
	document.getElementById("subzonedata").innerHTML="";
	return;
	
}
if(window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	  	
		document.getElementById("subzonedata").innerHTML=xmlhttp.responseText;
		
	}
	
}
xmlhttp.open("GET","getSitesubzone.php?Companyusers="+str,true);
xmlhttp.send();	





}





</script>



<center><h1><?php  echo $page_title;?></h1></center>
 <?php  $table='dbl_engineer_master';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;
 $modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$url="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=DealerDistrbutions/Setup/engineer_master".$url;



 if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="id='".$_REQUEST['id']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
  	 $CompanyId=$RData['company'];
	  $Site=$RData['Site'];
	   $Zone=$RData['zone'];
	   $dealer=$RData['dealer'];
	   $subareacode=$RData['subzone'];
	   $engineer_code=$RData['engineer_code'];
	   $engineer_name=$RData['engineer_name'];
	
  $id=$RData['id'];
			 
}
  
//************************Edit*********************************************** 


if( $action=='Delete')
{
	  $sql="delete from  dbl_engineer_master where id='".$_REQUEST['id']."'";
	  $Res=DB_query($sql,$db);
	  prnMsg(_('Deleted Successfully'));
	  echo "<script>location.href='index.php?route=DealerDistrbutions/Setup/engineer_master&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
 

if($_REQUEST['Save']  && $action=='Edit')
{
   $data['company']=$_REQUEST['CompanyId'];
   $data['Site']=$_REQUEST['Site'];
   $data['zone']=$_REQUEST['areacode'];
   $data['dealer']=$_REQUEST['dealer'];
   $data['subzone']=$_REQUEST['subareacode'];
   $data['engineer_code']=$_REQUEST['engineer_code'];
   $data['engineer_name']=$_REQUEST['engineer_name'];
   $cond="id='".$_REQUEST['id']."'";
 	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
 	
	
  $s->pageLocation($targetpage.'&action=Addnew&daction=Updated');
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] && $action=='Addnew')
{
	
	
   $data['company']=$_REQUEST['CompanyId'];
   $data['Site']=$_REQUEST['Site'];
   $data['zone']=$_REQUEST['areacode'];
    $data['dealer']=$_REQUEST['dealer'];
   $data['subzone']=$_REQUEST['subareacode'];
   $data['engineer_code']=$_REQUEST['engineer_code'];
   $data['engineer_name']=$_REQUEST['engineer_name'];
	$insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	
	if(!$row)
	{
	   die('Error'.DB_error());
	   
	}
	//prnMsg(_('Record inserted'),'Success');
	 $s->pageLocation($targetpage.'&action=Addnew&daction=Inserted');
 	
 
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>View Engineer Master</h1>
 <form action="" method="post">
 <center><table width="100%" border="1" cellspacing="3" cellpadding="3">
   <tr class="row1">
   <td colspan="4"> </td>
   </tr>
  <?php if(isset($daction))
  {?>
    <tr class="row1">
  
    <td colspan="4" ><div class="success">
	<?php 
	 if($_REQUEST['daction']!=1 && $_REQUEST['daction']!=2)
	 echo "Record ".$_REQUEST['daction']." Successfully";
	 elseif($_REQUEST['daction']==1)
	 	 echo "Can't Delete This Record because AccountName Attatch with LedgerDayBook table ";
	 elseif($_REQUEST['daction']==2)		 
		 echo "Can't Delete This Record. First Delete it's Parent Record   ";
		 ?>
	</div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr class="row1">
  
    <td colspan="4" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>  
   <?php }?>   
  <tr class="row1">
    <td width="17%" class="tabledata1">Company<font color="#FF0000"><strong>*</strong></font></td>
    <td width="33%"><select name="CompanyId" onchange="getSite(this.value)" id="CompanyId" style="width:100%">
    	<!--where CompanyId='".$_SESSION['ACGCompanyId']."'-->
    	<option value="0">--Select--</option>
			<?php
			$CompanyName=DB_query("Select * from companies",$db);
			while($CompanyRow=DB_fetch_array($CompanyName))
			{ ?>
			<option value="<?php echo $CompanyRow['CompanyId']?>" <?php echo($CompanyId==$CompanyRow['CompanyId']?'selected':'');?>><?php echo $CompanyRow['CompanyName']?></option>	
				
		<?php	}
			
			   
			?>
		</select></td>
		<td width="22%" class="tabledata1">Associate</td>
		 <td width="28%">
    	<select name="dealer" id="dealer" style="width:100%" onchange="getdelearSite1(this.value)"  >
    	<option value="">--Select--</option>
		
			<?php
			$SiteName=DB_query("Select * from dbl_dealer where company='".$_SESSION['ACGCompanyId']."' ",$db);
			while($SiteRow=DB_fetch_array($SiteName))
			{ ?>
			<option value="<?php echo $SiteRow['id']?>"  <?php echo($dealer==$SiteRow['id']?'selected':'');?>  ><?php echo $SiteRow['dealer_name']?></option>	
				
		<?php	}?>
		</select>
	</td>
    
  </tr>
  <tr class="row1">
  <td width="22%" class="tabledata1">Unit<font color="#FF0000"><strong>*</strong></font></td>
    <td width="28%">
    	<select name="Site" id="SiteName" style="width:100%"  onchange="showzone(this.value)">
    	<option value="">--Select--</option>
		<option value="0">All</option>
			<?php
			$SiteName=DB_query("Select * from locations where mst_companyid='".$_SESSION['ACGCompanyId']."'",$db);
			while($SiteRow=DB_fetch_array($SiteName))
			{ ?>
			<option value="<?php echo $SiteRow['loccode']?>"  <?php echo($Site==$SiteRow['loccode']?'selected':'');?>  ><?php echo $SiteRow['locationname']?></option>	
				
		<?php	}?>
		</select></td>
	
 
    <td class="tabledata1">Area </td>
    <td><div id="zoneid" >
	
	<select name="areacode" id="areacode" style="width:100%" onchange="showsubzone(this.value)">
      <option value="">Select</option>
      <?php 
		$sql=DB_query("select * from zone_master where Site='".$Site."' ",$db);
		while($row1=DB_fetch_array($sql))
		{
		?>
      <option value="<?php echo $row1['id']; ?>" <?php echo($Zone==$row1['id']?'selected':'');?> ><?php echo $row1['ZoneTitle']; ?></option>
      <?php } ?>
    </select></div></td>
	</tr>
	 <tr class="row1">
    <td class="tabledata1">Sub Area </td>
    <td><div id="subzonedata">
	<select name="subareacode" style="width:100%;">
     	<option value="0">--Select--</option>
		<?php
		$ZoneName=DB_query("SELECT * FROM subzone_master WHERE zone='".$Zone."' ",$db);
		while($ZoneRow=DB_fetch_array($ZoneName)){?>
		<option value="<?php echo $ZoneRow['id']?>"<?php echo($subareacode==$ZoneRow['id']?'selected':'');?>><?php echo $ZoneRow['subzone']?></option>	
		<?php }?>
	</select></div></td>
  </tr> 
	 <tr class="row1">
  	<td class="tabledata1">Engineer Code<font color="#FF0000"><strong>*</strong></font></td>
  	<td><input type="text" name="engineer_code" id="engineer_code" style="width:100%" value="<?php if(isset($engineer_code)) { echo $engineer_code; }?>" /></td>
	 <td>Engineer Name<font color="#FF0000"><strong>*</strong></font></td>
	 <td><input type="text" name="engineer_name" id="engineer_name" style="width:100%" value="<?php if(isset($engineer_name)) { echo $engineer_name; }?>"  /></td>
	 </tr>
   
  <tr class="row1">
    <td colspan="4" align="center">	
      <input type="submit" name="Save" value="Save" onclick="return validate();" class="search" >    </td>
    </tr>
</table>
 </center>
</form>
<script language="javascript" type="text/javascript">
function validate()
{

	if(document.getElementById("CompanyId").value=='0')
	{
		alert("Please Select The Company First");
		document.getElementById("CompanyId").focus();
		return false;
	}
	if(document.getElementById("SiteName").value=='')
	{
		alert("Please Select The Area First");
		document.getElementById("SiteName").focus();
		return false;
	}
	if(document.getElementById("engineer_code").value=='')
	{
		alert("Please Fill The Engineer Code First");
		document.getElementById("engineer_code").focus();
		return false;
	}
	if(document.getElementById("engineer_name").value=='')
	{
		alert("Please Fill The Engineer Name  First");
		document.getElementById("engineer_name").focus();
		return false;
	}
}

function showzone(str)
{

if(str.length==0)
{
	document.getElementById("SiteName").innerHTML="";
	return;
	
}
if(window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{	  	
	document.getElementById("zoneid").innerHTML=xmlhttp.responseText;
	}
	
}
xmlhttp.open("GET","getSite1.php?Companyusers="+str,true);
xmlhttp.send();	


}





</script>

 
