<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$rproduct_clientid='';
$tripclientid='';$rcnt='';$client_main='';
$tyrenum_veh='';$rclient_code='';$rrate='';$rclient_name='';$rclient_id='';$rtown_id='';$rpackname='';$billtype='';
$cclient_name='';
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();
$rate='';
if(isset($_REQUEST['rateid'])){
	$rrateid=mysql_escape_string($_REQUEST['rateid']);
	}
if(isset($_REQUEST['del'])){
	$del=$_REQUEST['del'];
	$id=mysql_escape_string($_REQUEST['del']);
	}
	if(isset($_REQUEST['client_main']) && $_REQUEST['client_main']!=''){
		 $client_main=mysql_escape_string($_REQUEST['client_main']);
		 }
	//-------------------------delete billtype---------------//
	 $rbilltype='';
		   $page='';$editroutetown='';$routedel='';$client_subid='';
		   if(isset($_REQUEST['page'])){
	$page=$_REQUEST['page'];
	}
	//-----------------------------------------------------------------//
            $tablename='cf_rate';
   			$sqlObj->tablename=$tablename;
		    $pdate=date("Y-m-d");
		    if(isset($_POST['save'])){		
			if(isset($_POST['client_code'])){
			$client_code=addslashes($_POST['client_code']);
			if(isset($_POST['client_subid'])){
			$client_subid=addslashes($_POST['client_subid']);
			}
			//getting client code
			$s=mysql_query("select client_id,client_name from cf_client where client_sub_id='".$client_subid."'");
			$o=mysql_fetch_object($s);
			$client_name=$o->client_name;
			$sclient_id=$o->client_id;
			}
			if(isset($_POST['packname'])){
			$packname=addslashes($_POST['packname']);
			}	
			if(isset($_POST['billtype'])){
			$billtype=addslashes($_POST['billtype']);
			}
			if($billtype!='Pack Billing')
			$packname='';
			//-------------count takes---for rate and town--------------------------------------//
			if(isset($_POST['rcnt']))
			{
				$rcnt=addslashes($_POST['rcnt']);
				$numsql="select id from cf_rate where client_code='".$client_subid."'";
				$numqry=mysql_query($numsql);
				if(mysql_num_rows($numqry)>0){
				mysql_query("delete from cf_rate where client_code='".$client_subid."'");
				}
				for($i=1;$i<$rcnt;$i++){
				if(isset($_POST['town_id'.$i])){
				$town_id=addslashes($_POST['town_id'.$i]);
				}
				if(isset($_POST['townname'.$i])){
				$townname=addslashes($_POST['townname'.$i]);
				}
				if(isset($_POST['rate'.$i])){
				$rate=addslashes($_POST['rate'.$i]);
				}
				
				//insert the values
				if($client_subid!='')
					{
						$fieldname='(client_code,client_name,billtype,rate,date,client_id,town_name,town_id,packname)';
						$fieldvalue='"'.$client_subid.'","'.$client_name.'","'.$billtype.'","'.$rate.'","'.$pdate.'","'.$sclient_id.'","'.$townname.'","'.$town_id.'","'.$packname.'"';
						//if($rrateid=="")
						//{	echo 'f'.$rrateid;
						
						
							echo $qsy= "insert into ".$tablename.$fieldname." values(".$fieldvalue.")";echo '<br>';
							$CreateClient 	= mysql_query($qsy);
							$lastid = mysql_insert_id();
						//}
						/*else
						{
							$updata="client_code = '".$client_code."',client_name='".$clientname."',billtype='".$billtype."',rate='".$rate."',client_id='".$sclient_id."',town_name='".$townname."',town_id='".$town_id."',packname='".$packname."'";
							$qsy= "update ".$tablename." set ".$updata." where id='".$rrateid."'";
							$CreateClient 	= mysql_query($qsy);
							$lastid = $rrateid;
						}	*/				
					 }
			 //-----------------------------------------//
				}
			}
			//------------------------------------------------------------------//	
			/*if(isset($_POST['clientname'])){
			$clientname=addslashes($_POST['clientname']);
			}	
			if($client_code!='')
			{
				$sqlObj->fieldname='(client_code,client_name,billtype,rate,date,client_id,town_name,town_id,packname)';
				$sqlObj->fieldvalue='"'.$client_code.'","'.$clientname.'","'.$billtype.'","'.$rate.'","'.$pdate.'","'.$sclient_id.'","'.$townname.'","'.$town_id.'","'.$packname.'"';
				if($rrateid=="")
				{
				 	$qsy= "insert into ".$tablename.$sqlObj->fieldname." values(".$sqlObj->fieldvalue.")";
				 	$CreateClient 	= mysql_query($qsy);
				 	$lastid = mysql_insert_id();
				}
				else
				{
					$updata="client_code = '".$client_code."',client_name='".$clientname."',billtype='".$billtype."',rate='".$rate."',client_id='".$sclient_id."',town_name='".$townname."',town_id='".$town_id."',packname='".$packname."'";
					$qsy= "update ".$tablename." set ".$updata." where id='".$rrateid."'";
					$CreateClient 	= mysql_query($qsy);
					$lastid = $rrateid;
				}					
			 }
			*/
			
			if(isset($_REQUEST['page'])){
			$page=$_REQUEST['page'];
			}
			if(isset($_REQUEST['client_name'])){
			$cclient_name=$_REQUEST['client_name'];
			}exit;
			if($_POST['save']=="Add More"){
				  header("location:add-rate.php?client_main=".$client_main."&client_name=".$cclient_name."&page=$page&msg=You Have Successfully Saved");
			}else{
				  header("location:rate-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 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;
	   // document.getElementById("txtHinttwn").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","clientajax_rate.php?str="+str,true);
xmlhttp.send();
}
*/

</script>

	<script type='text/javascript'>
	
function validationrate()
{
if(document.prodfrmname.client_subid.value=='')
	{
		alert("Enter Client Code");
		document.prodfrmname.client_subid.focus();
		return false;
	}
if(document.prodfrmname.rate.value=='')
	{
		alert("Enter rate");
		document.prodfrmname.rate.focus();
		return false;
	}
if(document.prodfrmname.townname.value=='')
	{
		alert("Enter Townname");
		document.prodfrmname.townname.focus();
		return false;
	}
	if(document.prodfrmname.clientname.value=='')
	{
		alert("Enter Client Name");
		document.prodfrmname.clientname.focus();
		return false;
	}
	if(document.prodfrmname.billtype.value=='')
	{
		alert("Enter Billtype");
		document.prodfrmname.billtype.focus();
		return false;
	}
	
}
function showtownvalued(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
  document.getElementById("txtHint").innerHTML='<img src="images/loading.gif" border="0">';
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;
    }
  }
xmlhttp.open("GET","clientajax_rate_town.php?str="+str,true);
xmlhttp.send();
}
//--client code dropdown
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_ratecode.php?str="+str,true);
xmlhttp.send();
}
</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 Rate</h1>
   <form name="prodfrmname" 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" style="min-width:600px;border-right:0px; "> -->
           <div class="form1"> 
           <div>  
		    <?php
		  
	if(isset($_REQUEST['rateid'])){
	$rrateid=mysql_escape_string($_REQUEST['rateid']);
	$rqry="SELECT *  FROM cf_rate  where id='".$rrateid."'";
	$sqry=mysql_query($rqry);
	if($sqry && mysql_num_rows($sqry)>0)
	{
		while($sobj=mysql_fetch_object($sqry))
		{
			$rbilltype=$sobj->billtype;
			$rclient_code=$sobj->client_code;
			$rclient_name=$sobj->client_name;
			$rtown_name=$sobj->town_name;
			$rtown_id	=$sobj->town_id	;
			$rrate=$sobj->rate;
			$rclient_id=$sobj->client_id;
			$rpackname=$sobj->packname;
			}
		}
	}		
			if(isset($client_code) && $client_code!=''){
			$rclient_code=$client_code;
			}
			
			if(isset($clientname) && $clientname!=''){
			$rclient_name=$clientname;
			}	
			
			if(isset($billtype) && $billtype!=''){
			$rbilltype=$billtype;
			}
			if(isset($rate) && $rate!=''){
			$rrate=$rate;
			}
			if(isset($packname) && $packname!=''){
			$rpackname=$packname;
			}	
?>  
<script>
function showtownvalue(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;
    }
  }
xmlhttp.open("GET","clientajax_rate_town.php?str="+str,true);
xmlhttp.send();
}
 function selectbilltype(val){
			 if(val=="Volume Billing"){
			 document.getElementById("rateper").innerHTML="kilogram";
			 document.getElementById("pckname").style.display="none";
			 }
			 if(val=="KM  Billing"){
			 document.getElementById("rateper").innerHTML="KM";
			 document.getElementById("pckname").style.display="none";
			 }
			  if(val=="Pack Billing"){
			  if(document.getElementById("rateper")){
			 document.getElementById("rateper").innerHTML="Pack";
			 }
			  document.getElementById("pckname").style.display="block";
			 }
			 }
</script>
<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']==$rproduct_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)"> -->
				<select class="input1 input3" name="client_subid" onChange="showtownvalue(this.value)">
				<? if(isset($rproduct_client_code) && $rproduct_client_code!=''){?>
                     <option value="">select client</option>
                         <?php 
                            $sq_client="select * from cf_client where client_id='".$rproduct_clientid."'";
                            $arr=mysql_query($sq_client);
                            while($resclient=mysql_fetch_array($arr))
                            {
                        ?>
                            <option value="<?=$resclient['client_sub_id']?>" <? if($resclient['client_sub_id']==$rproduct_client_code) {?>selected<? }?> ><?=$resclient['client_code']?></option>
                        <?php
                                }
								}else{?>
								<option value="">--Select Code--</option>
								<? }
                     ?>
    		 </select>
     	   </div>
<!-- <p id="divshw" class="input1">Client<span style="color:#FF3300">*</span></p>
<select class="input1 input3" name="clientids" id="clientids" onChange="showtownvalue(this.value)">
     <option value="">Select Client</option>
      <?php// //
 /*  $sq_client="select * from cf_client_parent";
    $arrclient=mysql_query($sq_client);
    while($resc=mysql_fetch_array($arrclient))
    {*/
    ?>      
          <option value="<?//=$resc['client_id']?>" <?// if($resc['client_id']==$tripclientid) {?>selected<?// }?>><?//=//$resc['client_name']?></option>
               <?php//
   // }
  ?>
     </select>
	 </div> -->
  <div>
            &nbsp;
            </div>
     	   
          <div> 
			
      			<p class="input1">Bill Type<span style="color:#FF3300">*</span></p>
				<select name="billtype" id="billtype" class="input1 input3" onChange="selectbilltype(this.value)">
				<option value="KM  Billing" <? if($rbilltype=="KM  Billing"){?> selected="selected" <? }?>>KM  Billing</option>
				<option value="Volume Billing" <? if($rbilltype=="Volume Billing"){?> selected="selected" <? }?>>Volume Billing</option>
				<option value="Pack Billing" <? if($rbilltype=="Pack Billing"){?> selected="selected" <? }?>>Pack Billing</option>
				</select>
     	   </div>  
		   <div>
            &nbsp;
            </div>
			
			<div id="pckname" style="display:none;">
			<p class="input1">Pack Name</p>
           <input name="packname" class="input1 input3"  type="text" value="<?=$rpackname?>">  
            </div>            
          <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['rateid'])){
	  ?><input name="rateid" type="hidden" value="<?=$rrateid?>">  
	 
      <input name="save" type="submit" class="input1" value="Edit" onClick="return validationrate();"/>
      <?php }else { ?>   
     <input name="save" type="submit" class="input1" value="Save" onClick="return validationrate();"/>
	  <input name="save" type="submit" class="input1" value="Add More" onClick="return validationroute();" style="margin-left:20px;"/>
     <?php } ?>

       <!--   <input type="button" class="input1" value="Cancel" > -->
        </div>
        </td>
          </tr>
        </table></div>
			
			</div>
        </td>
            <td valign="top">
			<div id="txtHint" style="float:left;">
			<?php  if($rclient_name!=''){?>
			
			<? 
			 $rqry="SELECT a.*  FROM cf_route as b LEFT JOIN cf_route_town as a  ON a.route_id = b.route_id and b.route_client_code='".$rclient_code."'";
			$rsql=mysql_query($rqry);
			?>
			 <div> 
			 <p class="input1">Town Name<span style="color:#FF3300">*</span></p>
<select name="townname" class="input1 input3">
<? while($robj=mysql_fetch_object($rsql))
{?>
<option value="<?=$robj->route_town_id?>" <?php if($robj->route_town_id==$rtown_id){?> selected="selected" <? }?>><?=$robj->town?></option>
<? 
}?>
</select>
 </div> 
 <? }else{?>
 <!--  <div> 
			 <p class="input1">Town Name<span style="color:#FF3300">*</span></p>
<select  class="input1 input3">
<option value="">--Select Town--</option>
</select>
 </div>  -->
 <? }?></div> 
		
            
            </td>
          </tr>
        </table>
	<div id="div2">
	</div>
	
       

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


		






