<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","getSite.php?Company="+str,true);

	xmlhttp.send();	

} 

</script>



<?php  

$table='zone_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;



if($_REQUEST['Save'])

{

	if(empty($_REQUEST['ZoneTitle']))

	{

	$msgErr[]='Please Enter Zone Title';

	$status=false;

	}

	if($_REQUEST['CompanyId']=='0')

	{

	$msgErr[]='Please Enter Company Name';

	$status=false;

	

	}

	if($_REQUEST['Site']=='')

	{

	$msgErr[]='Please Enter Branch';

	$status=false;

	

	}

}



if(isset($_POST['Cancel']))

{

 $s->pageLocation($targetpage);

}



 

function CheckZoneTitle($Zonetitle)

{

	global $db;

	$SQL="Select ZoneTitle from zone_master where ZoneTitle='".$Zonetitle."'";

	$result=DB_query($SQL,$db);

	$resultrow=DB_fetch_array($result);

	if(isset($resultrow['ZoneTitle'])){return false;}else{return true; }

}



 

  

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



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'];

	$ZoneTitle=$RData['ZoneTitle'];

	$zonecode=$RData['zonecode'];

	$status=$RData['status'];

	$id=$RData['id'];

	

}





if( $action=='Delete')

{

	  $sql="delete from  zone_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['ZoneTitle']=$_REQUEST['ZoneTitle'];

	$data['zonecode']=$_REQUEST['zonecode'];

	$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'] &&  $status && $action=='Addnew')

{

	if(CheckZoneTitle($_REQUEST['ZoneTitle']))

	{

	$data['CompanyId']=$_REQUEST['CompanyId'];

	$data['Site']=$_REQUEST['Site'];

	$data['ZoneTitle']=$_REQUEST['ZoneTitle'];

	$data['zonecode']=$_REQUEST['zonecode'];

	$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 Zone Already Exist'));

	}

}

?>

<link rel="stylesheet" href="css/new_mxerp.css">

<div class="body_contain">

<div class="tab_div">

<h1><?php  echo $page_title;?></h1>

<p align="left">



 <?php if($_REQUEST['action']=='Addnew' || $_REQUEST['action']=='Edit' and $_REQUEST['daction']=='') { ?>

 <form action="" method="post">



<table width="100%" border="0" cellspacing="3" cellpadding="3">

   <tr>

   <td colspan="4"> </td>

   </tr>

  <?php if(isset($daction))

  {?>

    <tr>

  

    <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>

  

    <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<font color="#FF0000">*</font></td>

	<td ><select name="CompanyId" id="CompanyId" onchange="getSite(this.value)">

	

	<option value="0">--Select--</option>

	<?php

	$CompanyName=DB_query("Select * from companies where 1=1",$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">Unit<font color="#FF0000">*</font></td>

	<td><select name="Site" id="SiteName">

	<option value="">--Select--</option>

	<option value="0">All</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>	</td>

</tr>

<tr class="row1">

<?php if($_REQUEST['action']=='Edit'){ 

	$s1='';

	} else { $s1="onkeyup=ShowDiv(this.value,'zonecode1','zonecodedata.php')"; } ?>

	<td class="tabledata1">Area<font color="#FF0000">*</font></td>

	<td><input type="text" name="ZoneTitle" id="ZoneTitle" value="<?php echo $ZoneTitle  ?>" style="width:150px;" <?php echo $s1; ?> /></td>

    <td> Area Code </td>

    <td><div id="zonecode1"><input type="text" name="zonecode" id="zonecode" style="width:150px;" value="<?php echo $zonecode; ?>" /></div></td>

</tr>

<tr class="row1" >

	<td class="tabledata1">Status<font color="#FF0000">*</font></td>

	<td><input name="status" type="radio" value="Y" <?php  echo ($status=='Y'?'checked':'')?> />

	Active</td>

	<td class="tabledata1" colspan="2"><input name="status" type="radio" value="N"<?php  echo ($status=='N'?'checked':'')?> />

	Inactive</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 { ?>

<a href="index.php?route=DealerDistrbutions/Setup/ZoneMaster<?php echo $rurl; ?>&amp;action=Addnew"><input type="button" name="Add New" value="Add New" class="search"  onclick="showtabledata()"/></a>

</p>



<?php } ?>



<?php  include('ViewZoneList.php'); ?> 

</div>

</div>

<script language="javascript">

function showtabledata()

{



document.getElementById("showadd").style.display='block';



}



</script>

<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("ZoneTitle").value=='')

	{

		alert("Please Fill The Zone Title First");

		document.getElementById("ZoneTitle").focus();

		return false;

	}

}





function abc()

{

window.alert("Area Used You Can Not Delete");

}





</script>