<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='dbl_prefix_tbl';
$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(isset($_POST['Cancel']))
{
 $s->pageLocation($targetpage);
}

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

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

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="prefix_id='".$_REQUEST['id']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
	$TypeAbbrev=$RData['prefix_code'];
	$Sales_Type=$RData['prefix_name'];
	$id=$RData['id'];
	
}


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

}
 

if($_REQUEST['Save']  && $action=='Edit')
{
	$data['prefix_code']=$_REQUEST['TypeAbbrev'];
	$data['prefix_name']=$_REQUEST['Sales_Type'];
	$cond="prefix_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['TypeAbbrev']))
	{
	$data['prefix_code']=$_REQUEST['TypeAbbrev'];
	$data['prefix_name']=$_REQUEST['Sales_Type'];
	// $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 Prefix Code 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">
	
	</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">Prefix Code <font color="#FF0000">*</font></td>
	<td ><input type="Text" size="20"  name="TypeAbbrev" value="<?php echo $TypeAbbrev; ?>">    </td>
	<td class="tabledata1">Prefix Name <font color="#FF0000">*</font></td>
	<td><input type="Text" name="Sales_Type" value="<?php echo $Sales_Type; ?>" <?php if($_REQUEST['action']=='Edit') { ?> readonly="readonly" <?php } ?>>	</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/Prefix_Master<?php echo $rurl; ?>&amp;action=Addnew"><input type="button" name="Add New" value="Add New" class="search"  onclick="showtabledata()"/></a>
</p>

<?php } ?>

<?php  include('Viewprefix.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;
	}
}



</script>