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



<center><h1><?php  echo $page_title;?></h1></center>
 <?php  $table='transport_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['TransportName']))
	{
	   $msgErr[]='Please Enter Zone Title';
	   $status=false;
	}
	if($_REQUEST['CompanyId']=='0')
	{
		$msgErr[]='Please Enter Company Name';
	     $status=false;
		
	}
	if($_REQUEST['Site']=='0')
	{
		$msgErr[]='Please Enter Branch';
	     $status=false;
		
	}
	
	 
	 
	 
		
} 
function CheckTransportName($TransportName)
{
	global $db;
	$SQL="Select TransportName from transport_master where TransportName='".$TransportName."'";
	$result=DB_query($SQL,$db);
	$resultrow=DB_fetch_array($result);
	if(isset($resultrow['TransportName'])){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'];
	  $TransportName=$RData['TransportName'];
	 $Transportcode=$RData['Transportcode'];
	  $status=$RData['status'];
  $id=$RData['id'];
			 
}

 

if($_REQUEST['Save']  && $action=='Edit')
{
 $data['CompanyId']=$_REQUEST['CompanyId'];
  $data['Site']=$_REQUEST['Site'];
  $data['TransportName']=$_REQUEST['TransportName'];
   $data['status']=$_REQUEST['status'];
   $data['Transportcode']=$_REQUEST['Transportcode'];
   $cond="id='".$_REQUEST['id']."'";
 	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
 	
	
  $s->pageLocation($targetpage);
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status)
{
	
	if(CheckTransportName($_REQUEST['TransportName']))
	{
   $data['CompanyId']=$_REQUEST['CompanyId'];
   $data['Site']=$_REQUEST['Site'];
    $data['TransportName']=$_REQUEST['TransportName'];
	$data['Transportcode']=$_REQUEST['Transportcode'];
	$data['dealerid']=trim($_SESSION['dealerid']);
   $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);
 	}
	else
		 {
		 	prnMsg(_('This Transporter Already Exist'));
		
	}
 
}

if($action=='Del')
{
	$SQLDel=DB_query("Delete From transport_master where id='".$_REQUEST['id']."'",$db);
	$s->pageLocation($targetpage);
}

?>


 <form action="" method="post">
<table width="50%" border="0" cellspacing="3" cellpadding="3">
   <tr>
   <td colspan="2"> <div align="right"> <!--<a href="index.php?route=Setup/TransportMaster<?php echo $rurl; ?>&action=Addnew">Add New</a>--></div></td>
   </tr>
  <?php if(isset($daction))
  {?>
    <tr>
  
    <td colspan="2" ><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="2" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
   
   
  <tr>
    <td width="35%">Company Name </td>
    <td width="65%"><select name="CompanyId" onchange="getSite(this.value)">
    	
<!--    	<option value="0">--Select--</option>-->
			<?php
			$CompanyName=DB_query("Select CompanyId,CompanyName 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>
  </tr>
  <tr>
    <td>Branch</td>
    <td>
    	<select id="SiteName" name="Site">
<!--    	<option value="0">--Select--</option>-->
			<?php
			$SiteName=DB_query("Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')",$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>
  	<td>Transport Name</td>
  	<td><input type="text" name="TransportName" id="TransportName" value="<?php echo $TransportName  ?>"></td>
  	</tr>
	 <tr>
  	<td>Transport Code</td>
  	<td><input type="text" name="Transportcode" id="Transportcode" value="<?php echo $Transportcode  ?>"></td>
  	</tr>
   <tr>
    <td>Status</td>
    <td> <input name="status" type="radio" value="Y" <?php  echo ($status=='Y'?'checked':'')?>>
	Active
	<input name="status" type="radio" value="N"<?php  echo ($status=='N'?'checked':'')?>>Inactive
	
     
    
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td> 
	
      <input type="submit" name="Save" value="Save">
    
    </td>
  </tr>
</table>
</form>
 


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