<script type="text/javascript">
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();	


}
</script>



<center><h1><?php  echo $page_title;?></h1></center>
 <?php  $table='subzone_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=".$_REQUEST['route'].$url;

function CheckZoneTitle($Zonetitle)
{
	global $db;
	$SQL="Select subzone from subzone_master where subzone='".$Zonetitle."'";
	$result=DB_query($SQL,$db);
	$resultrow=DB_fetch_array($result);
	if(isset($resultrow['subzone'])){return false;}else{return true; }
	
	
}

 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['CompanyId'];
	  $Site=$RData['Site'];
	   $Zone=$RData['zone'];
	   $subZone=$RData['subzone'];
	 
	  $status=$RData['status'];
  $id=$RData['id'];
			 
}
  
//************************Edit*********************************************** 


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

if($_REQUEST['Save']  && $action=='Edit')
{
 $data['CompanyId']=$_REQUEST['CompanyId'];
  $data['Site']=$_REQUEST['Site'];
   $data['zone']=$_REQUEST['areacode'];
   $data['subzone']=$_REQUEST['subZoneTitle'];
   $data['status']=$_REQUEST['status'];
   $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'])
{
	
	if(CheckZoneTitle($_REQUEST['subzone']))
	{
   $data['CompanyId']=$_REQUEST['CompanyId'];
   $data['Site']=$_REQUEST['Site'];
   $data['zone']=$_REQUEST['areacode'];
   $data['subzone']=$_REQUEST['subZoneTitle'];
   $data['status']=$_REQUEST['status'];
// $data['Predefined']=$_REQUEST['Predefined'];
	$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');
 	}
	else
		 {
		 	prnMsg(_('This SubZone Already Exist'));
		
	}
 
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><a href="index.php?route=Setup/subzonemaster<?php echo $rurl; ?>&amp;action=Addnew">
  <input type="button" name="Add New" value="Add New" class="search" />
</a></h1>

 <form action="" method="post">
   <center>
  <?php if($_REQUEST['action']=='Addnew' || $_REQUEST['action']=='Edit' and $_REQUEST['daction']=='') { ?>
<table width="50%" 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 class="tabledata1">Company Name </td>
    <td width="32%"><select name="CompanyId" onchange="getSite(this.value)">
    	<!--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 class="tabledata1">Branch</td>
    <td width="17%"><div id="SiteName">
    	<select name="Site">
    		
    		
    	
    	<option value="0">--Select--</option>
			<?php
			$SiteName=DB_query("Select * from locations ",$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>
		</div></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Zone Title</td>
    <td><div id="zoneid" >
	
	<select name="areacode" id="areacode">
      <option value="">Select</option>
      <?php 
		$sql=DB_query("select * from zone_master ",$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>
    <td class="tabledata1">Status</td>
    <td><input name="status" type="radio" value="Y" <?php  echo ($status=='Y'?'checked':'')?> />
<strong>Active</strong>
  <input name="status" type="radio" value="N"<?php  echo ($status=='N'?'checked':'')?> />
  <strong>Inactive</strong> </td>
  </tr> 
	 <tr class="row1">
  	<td class="tabledata1">Sub Zone Title</td>
  	<td colspan="3"><input type="text" name="subZoneTitle" id="subZoneTitle" value="<?php echo $subZone  ?>" size="80"></td>
  	</tr>
   
  <tr class="row1">
    <td>&nbsp;</td>
    <td colspan="3"> 
	
      <input type="submit" name="Save" value="Save" class="search" >    </td>
  </tr>
</table>
<?php } ?>
</center>
</form>
 


<?php  include('ViewsubZoneList.php') ?> 
