<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$page='';
$rroute_client_code='';
$rroute_client_name='';
$rroutedistance='';
$tyrenum_veh='';
$cclient_name='';
$rtownid='';$client_main='';
if($page=='')
$page=1;
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();
$routedistance='';
if(isset($_REQUEST['client_main']) && $_REQUEST['client_main']!=''){
		 $client_main=mysql_escape_string($_REQUEST['client_main']);
		 }
if(isset($_REQUEST['routeid'])){
	$rrouteid=mysql_escape_string($_REQUEST['routeid']);
	}
if(isset($_REQUEST['del'])){
	$del=$_REQUEST['del'];
	$id=mysql_escape_string($_REQUEST['del']);
	}
	//-------------------------delete routename---------------//
	 $rroute_name='';
		   $page='';$editroutetown='';$routedel='';
		   if(isset($_REQUEST['page'])){
	$page=$_REQUEST['page'];
	}
	if(isset($_REQUEST['routedel'])){
	$routedel=$_REQUEST['routedel'];
	mysql_query("delete from cf_route_town where route_town_id='".$routedel."'");
	}
	//-----------------------------------------------------------------//
			$tablename='cf_route';
            $routedate=date("Y-m-d");
			if(isset($_POST['client_code'])){
			$client_code=addslashes($_POST['client_code']);
			//getting client code
			$s=mysql_query("select client_id from cf_client where client_sub_id='".$client_code."'");
			$o=mysql_fetch_object($s);
			$sclient_id=$o->client_id;
			}
			
			if(isset($_POST['clientname'])){
			$clientname=addslashes($_POST['clientname']);
			}	
			
			if(isset($_POST['routename'])){
			$routename=addslashes($_POST['routename']);
			}
			if(isset($_POST['routedistance'])){
			$routedistance=addslashes($_POST['routedistance']);
			}	
			if(isset($_POST['save'])){			
			$sqlObj->tablename=$tablename;
			if($client_code!=''){
			$sqlObj->fieldname='(route_client_code,route_client_name,route_name,routedistance,date,client_id)';
			$sqlObj->fieldvalue='"'.$client_code.'","'.$clientname.'","'.$routename.'","'.$routedistance.'","'.$routedate.'","'.$sclient_id.'"';
			if($rrouteid==""){
			 $qsy= "insert into ".$tablename.$sqlObj->fieldname." values(".$sqlObj->fieldvalue.")";
			 $CreateClient 	= mysql_query($qsy);
			$lastid = mysql_insert_id();
			}else{
			$updata="route_client_code = '".$client_code."',route_client_name='".$clientname."',route_name='".$routename."',routedistance='".$routedistance."',client_id='".$sclient_id."'";
			$qsy= "update ".$tablename." set ".$updata." where route_id='".$rrouteid."'";
			$CreateClient 	= mysql_query($qsy);
			$lastid = $rrouteid;
			}					
			@mysql_query("delete from cf_route_town where route_id='".$rrouteid."'");
			for($i=0;$i<count($_POST['routetown']);$i++)
			{
				if($_POST['routetown'][$i]!='')
				{
				$sql = mysql_query("insert into cf_route_town(route_id,town) values ('".$lastid."','".addslashes(trim($_POST['routetown'][$i]))."')");
				}	
			}
			
			//exit;
			
			
			
			}
			if(isset($_REQUEST['page'])){
			$page=$_REQUEST['page'];
			}
			if(isset($_REQUEST['client_name'])){
			$cclient_name=$_REQUEST['client_name'];
			}
			if($_POST['save']=="Add More"){
				  header("location:add-route.php?client_name=".$cclient_name."&page=$page&msg=You Have Successfully Saved");
			}else{
				  header("location:route_list.php?client_main=".$client_main."&client_name=".$cclient_name."&page=$page&msg=You Have Successfully Saved");
			}
			
		}//save button end

			
			

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Supply Chain Management</title>
<link href="css/styles.css" rel="stylesheet"  />
<link type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type="text/javascript" src="scripts_date/jquery.js"></script>
<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script src="js/addmoretown.js"></script>



<script type="text/javascript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
function showClientcode(str){
var xmlhttp;    
if (str=="")
  {
  document.getElementById("txtcodeshow").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	   document.getElementById("txtcodeshow").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","clientajax_code.php?str="+str,true);
xmlhttp.send();
}
/*
function showClient(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
	   showdist(str);
    }
  }
xmlhttp.open("GET","clientajax_route.php?str="+str,true);
xmlhttp.send();
}
*/
function showdist(str){
var xmlhttp;    
if (str=="")
  {
  document.getElementById("add_town").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	   document.getElementById("add_town").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","towns_selected.php?str="+str,true);
xmlhttp.send();
}

</script>

<script src="js/jquery-1.7.2.min.js"></script>	
	<script type='text/javascript'>
	 $('a.addimgs').live('click',
            function(){ 
             $(this).closest('span').clone(true).appendTo('#add_img').find('input').val('');
		   $(this).text('Remove').removeClass('addimgs').addClass('rimg');
            });
        $('a.rimg').live('click',
            function(){
                $(this)
                    .closest('span')
                    .remove();
            });
	
	
function del_route(rroute_town_id,page,rrouteid,ab){
document.routefrmname.action="add-route.php?routedel="+rroute_town_id+"&page="+page+"&routeid="+rrouteid+"&ab="+ab;
document.routefrmname.submit();
}
function validationroute()
{
if(document.routefrmname.client_code.value=='')
	{
		alert("Enter Client Code");
		document.routefrmname.client_code.focus();
		return false;
	}
	if(document.routefrmname.clientname.value=='')
	{
		alert("Enter Client Name");
		document.routefrmname.clientname.focus();
		return false;
	}
	if(document.routefrmname.routename.value=='')
	{
		alert("Enter Route Name");
		document.routefrmname.routename.focus();
		return false;
	}
}
</script>
<style>
.lableentrytext{
font-size: 12px;
color: #505050;
clear: both;
margin-bottom: 5px;
text-transform: capitalize;
}
.addimgs{
font-size: 12px;
color: #505050;
clear: both;
margin-bottom: 5px;
text-transform: capitalize;
cursor:hand;
cursor:pointer;
}
.addimgs a:hover{
color: #000;
}
.rimg{
font-size: 12px;
color: #505050;
clear: both;
margin-bottom: 5px;
text-transform: capitalize;
cursor:hand;
cursor:pointer;
}
.rimg a:hover{
color: #000;
}

</style>
</head>

<body>
<div class="wrapper">
  <div>
<?php 
	include("header.php");
?>
 
</div>
 
 
 <div class="tabbedArea">
  
  <div class="tabBody">
   <h1>Add Route</h1>
   <form name="routefrmname" method="post" action="">
   <?php
	$div=1;
	?>
<input type="hidden" id="divcnt" value="<?=$div?>" name="cnt"/>
   <div class="addEmp" style="margin-bottom:0px; border-bottom:0px solid;width:1000px;">
        <table width="70%" border="0" cellspacing="0" cellpadding="10">
          <tr>
            <td valign="top">
            <div class="form1">
            
           <div>  
		    <?php
		  
	if(isset($_REQUEST['routeid'])){
	$ruteid=mysql_escape_string($_REQUEST['routeid']);
	$rqry="SELECT a.*,b.*  FROM cf_route as a LEFT JOIN cf_route_town as b  ON a.route_id = b.route_id where a.route_id='".$ruteid."'";
	//$rqry=" select a.*,b.* from cf_route as ,cf_route_town as b";
	$sqry=mysql_query($rqry);
	if($page=='')
	$page=1;
	if($sqry && mysql_num_rows($sqry)>0)
	{
		while($sobj=mysql_fetch_object($sqry))
		{
			$rroute_name=$sobj->route_name;
			$rroute_clientid=$sobj->client_id;
			$rroute_client_code=$sobj->route_client_code;
			$rroute_client_name=$sobj->route_client_name;
			$rroutedistance=$sobj->routedistance;
			$rroute_town_id=$sobj->route_town_id;
			$rtown=$sobj->town;
			$rtownid.="'".$sobj->town."',";
			}
		}
	}	
	
			if(isset($_REQUEST['clientname']) && $_REQUEST['clientname']!=''){
			$rroute_clientid=mysql_escape_string($_REQUEST['clientname']);
			}
			if(isset($_REQUEST['client_code']) && $_REQUEST['client_code']!=''){
			$rroute_client_code=mysql_escape_string($_REQUEST['client_code']);
			}
			
			if(isset($clientname) && $clientname!=''){
			$rroute_client_name=$clientname;
			}	
			
			if(isset($routename) && $routename!=''){
			$rroute_name=$routename;
			}
			if(isset($routedistance) && $routedistance!=''){
			$rroutedistance=$routedistance;
			}	
?>  <!-- first client selects -->
<p id="divshw" class="input1">Client<span style="color:#FF3300">*</span></p>
                <select class="input1 input3" name="clientname" onChange="showClientcode(this.value)">
                     <option value="">select client</option>
                         <?php 
                            $sq_client1="select * from cf_client_parent";
                            $arr1=mysql_query($sq_client1);
                            while($resclient1=mysql_fetch_array($arr1))
                            {
                        ?>
                            <option value="<?=$resclient1['client_id']?>" <? if($resclient1['client_id']==$rroute_clientid) {?>selected<? }?> ><?=$resclient1['client_name']?></option>
                        <?php
                                }
                     ?>
    		 </select>
     	   </div>
<!-- client select end -->
<!-- next clientcode selects -->
<div id="txtcodeshow">
      			<p id="divshw" class="input1">Client Code<span style="color:#FF3300">*</span></p>
                <select class="input1 input3" name="client_code" onChange="showdist(this.value)">
				<? if(isset($rroute_client_code) && $rroute_client_code!=''){?>
                     <option value="">select client</option>
                         <?php 
                            $sq_client="select * from cf_client where client_id='".$rroute_clientid."'";
                            $arr=mysql_query($sq_client);
                            while($resclient=mysql_fetch_array($arr))
                            {
                        ?>
                            <option value="<?=$resclient['client_sub_id']?>" <? if($resclient['client_sub_id']==$rroute_client_code) {?>selected<? }?> ><?=$resclient['client_code']?></option>
                        <?php
                                }
								}else{?>
								<option value="">--Select Code--</option>
								<? }
                     ?>
    		 </select>
     	   </div>
           <div>     
      			<p class="input1">Route Name<span style="color:#FF3300">*</span></p><input type="text" name="routename" id="routename" class="input1 input3" value="<?=$rroute_name?>" >
     	   </div>  
		    <div>     
      			<p class="input1">Distance in Kms<!--<span style="color:#FF3300">*</span> --></p><input type="text" name="routedistance" id="routedistance" class="input1 input3" value="<?=$rroutedistance?>" >
     	   </div>            
            
          </div>
        </td>
            <td valign="top">
            <div class="form1">
           <?=$editroutetown?>
           <div class="controls" id="add_town"><span style="display:block;"><p class="input1">Route Town<span style="color:#FF3300">*</span></p>
		   <p>
		   <? if($rroute_client_code!=''){?>
		   <select name="routetown[]" class="input1 input3" multiple="multiple" style="height:200px;" >
		   <?
		   //------------district dispay---------------//
			$qtown=mysql_query("select 	district_id,district from cf_district where dist_clientcode='".$rroute_client_code."'");
			 while($stown=mysql_fetch_object($qtown)){
			?>
				<option value="<?=$stown->district_id?>" <? if(strstr($rtownid, "'".$stown->district_id."'")){?> selected="selected"   <? }?>><?=$stown->district?></option>
				<? 
				}
				//-------------------------------------//	
				?>
				</select>
				<? }else{?>
		   <select><option value=""><? if($editroutetown!=''){echo $editroutetown;}else{?>Choose Client Code to Get Town<? }?></option></select>
		   <? }?>
		   </p>
		 </span>
		</div>
             <div>
            &nbsp;
            </div>
            </div></td>
          </tr>
        </table>
	<div id="div2">
	</div>
	<table width="70%" border="0" cellspacing="0" cellpadding="10">
       <tr>
         <td colspan="2"><div class="searchField1"><input name="client_main" type="hidden" value="<?=$client_main?>">  
         <input name="client_name" type="hidden"  <? if(isset($_REQUEST['client_name']) && $_REQUEST['client_name']!=''){?>value="<?=$_REQUEST['client_name']?>" <? }?>/>  
           <?php
	 	if(isset($_REQUEST['routeid'])){
	  ?><input name="routeid" type="hidden" value="<?=$rrouteid?>">    
      <input name="save" type="submit" class="input1" value="Edit" onClick="return validationroute();"/>
      <?php }else { ?> <span style="padding-right:20px;">
     <input name="save" type="submit" class="input1" value="Save" onClick="return validationroute();"/></span> 
	 <span style="margin-left:20px;"> &nbsp; &nbsp; &nbsp;
	  <input name="save" type="submit" class="input1" value="Add More" onClick="return validationroute();" style="margin-left:20px;"/></span>  
     <?php } ?>

         <input type="button" class="input1" value="Cancel" >
        </div>
        </td>
          </tr>
        </table>
       

<div class="mand"><p>Fields marked <span style="color:#FF3300">*</span> are Mandatory</p></div>
</div>
   </form>
   </div>
 </div>
 <div class="footer"><p>Powered by <a href="http://www.aadhocc.co.uk/" target="_blank">Aadhocc</a></p></div>
 
</div>
<div class="quicLInksCover">
  <?php 
	include("leftlink.php");
?>

</div>
</body>
</html>


		






