<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)
	{
	var data=xmlhttp.responseText.split("__");
	//alert(data);
	document.getElementById("areacode").innerHTML=data[1];	
	document.getElementById("SiteName").innerHTML=data[0];
	}
	}
	xmlhttp.open("GET","getSite1.php?Company="+str,true);
	xmlhttp.send();	
} 



function showzone(str)
{
	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>


 <?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'];
	    $subzonecode=$RData['subzonecode'];
	   $subZone=$RData['subzone'];
	 
	  $status=$RData['status'];
  $id=$RData['id'];
			 
}
if(isset($_POST['Cancel']))
{
 $s->pageLocation($targetpage);
}

  
//************************Edit*********************************************** 


if( $action=='Delete')
{
	  $sql="delete from  subzone_master where id='".$_REQUEST['id']."'";
	  $Res=DB_query($sql,$db);
	  prnMsg(_('Deleted Successfully'));
	  $s->pageLocation($targetpage);

}
 

if($_REQUEST['Save']  && $action=='Edit')
{
 $data['CompanyId']=$_REQUEST['CompanyId'];
  $data['Site']=$_REQUEST['Site'];
   $data['zone']=$_REQUEST['areacode'];
   $data['subzone']=$_REQUEST['subZoneTitle'];
   $data['subzonecode']=$_REQUEST['subzonecode'];
   $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'] && $action!='Edit')
{
	
	if(CheckZoneTitle($_REQUEST['subzone']))
	{
   $data['CompanyId']=$_REQUEST['CompanyId'];
   $data['Site']=$_REQUEST['Site'];
   $data['zone']=$_REQUEST['areacode'];
   $data['subzonecode']=$_REQUEST['subzonecode'];
   $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><?php echo  $page_title?></h1>
  <?php if($_REQUEST['action']=='Addnew' || $_REQUEST['action']=='Edit' and $_REQUEST['daction']=='') { ?>


 <form action="" method="post">
<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="23%" class="tabledata1">Company Name<font color="#FF0000">*</font></td>
    <td width="28%"><select name="CompanyId" id="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 width="17%" class="tabledata1">Unit<font color="#FF0000">*</font></td>
    <td width="30%">
    	<select name="Site" id="SiteName" onchange="showzone(this.value)">
    	<option value="">--Select--</option>
		</select></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Area<font color="#FF0000">*</font></td>
    <td id="zoneid">
	<select name="areacode" id="areacode">
      <option value="">Select</option>
    </select></td>
    <td class="tabledata1">Status<font color="#FF0000">*</font></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 Area<font color="#FF0000">*</font></td>
	<?php if($_REQUEST['action']=='Edit'){ 
	$s1='';
	} else { $s1="onkeyup=ShowDiv(this.value,'zonecode2','subzonecodedata.php')"; } ?>
  	<td ><input type="text" name="subZoneTitle" id="subZoneTitle" value="<?php echo $subZone  ?>" <?php echo $s1; ?> size="30" ></td>
	<td>Sub Area Code </td>
  	<td><div id="zonecode2"><input type="text" name="subzonecode" id="subzonecode" style="width:150px;" value="<?php echo $subzonecode  ?>" /></div></td>
  	<td width="2%"></td>
    </tr>
   <tr class="row1">
	<td colspan="4" style="text-align:center;">
		<input type="submit" name="Save" value="Save" onclick="return validate();" class="search" >  
	  <input type="submit" name="Cancel" value="Cancel" class="search" />
	</td>
</tr>
 
</table>
</form>
<?php }  else { ?>
<p align="left">
<a href="index.php?route=DealerDistrbutions/Setup/subzonemaster<?php echo $rurl; ?>&action=Addnew">
   <input type="button" name="Add New" value="Add New" class="search" /></a>
</p>
<?php } ?>

 


<?php  include('ViewsubZoneList.php') ?> 
<script type="text/javascript" language="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 Branch First");
		document.getElementById("SiteName").focus();
		return false;
	}
	if(document.getElementById("areacode").value=='0')
	{
		alert("Please Select The Zone First");
		document.getElementById("areacode").focus();
		return false;
	}
	if(document.getElementById("subZoneTitle").value=='')
	{
		alert("Please Fill The Sub Zone Title First");
		document.getElementById("subZoneTitle").focus();
		return false;
	}
}


function abc()
{
window.alert("Sub Area Used You Can Not Delete");
}
</script>