<?php
include('header.php');
include('config.php');
$lender_name='';
?>

<?php 



?>
<table align='center' width="100%" style="text-align:center;">
		<tr>
		    <th>Lender</th>
			<th>Period</th>
			<th>Upfront Commisson</th>
			<th>Trail Commission</th>
		</tr>
		
<?php
		$query = "SELECT l1 . * , l2.lender_name
						FROM  `tbllenderdetails` l1
						INNER JOIN tbllenders l2 
						ON l1.lenderid = l2.id "
						."WHERE lenderid='".$_REQUEST['id']."' "; ;
		
		$result = mysql_query($query);
		$rows = mysql_num_rows($result);
		$tr=0;
		$count=1;
		if ($rows > '0') 
		{	while($row=mysql_fetch_array($result))
			{
				$lender_name = $row['lender_name'];
				if($tr==0)
				{
					
					echo "<tr class='ListColor'>";
					$tr=1;
				}
				else if($tr==1)
				{
					
					echo "<tr>";
					$tr=0;
				}
		?>
		      <td><?php echo $lender_name ?></td>
		      <td><?php  if($row['Period']=='5'){?> Year 5 Onwards <?php } else { echo 'Year '. $row['Period']; }?></td>
		      <td><?php if($row['Period']=='1'){ ?> <input type="text" id='txtfront<?php echo $count?>' value='<?php echo $row['front'];?>' /> <?php } else { ?> <input type="text" id='txtfront<?php echo $count?>' value='<?php echo $row['front'];  ?>' readonly /> (readonly) <?php } ?> </td>
		      
		      <td><input type="text" id="txttrail<?php echo $count?>" value='<?php echo $row['Trail'];?>' /></td>
		<?php 		
		    $count = $count+1;
			}
		}

		?>
		<table>
		<tr>
		<td align="center">
		<input type="button" id="update" value="Update Records" onclick="Updateformdata();"/>
		</td>
		</tr>
		</table>
		<script type="text/javascript">
		function Updateformdata()
		{
			var Result=0;
			for(i=1;i<6;i++)
			{
			  var front = 	$('#txtfront'+ i).val();
			  var trail = 	$('#txttrail'+ i).val();
			var dataString = 'id=<?php echo $_REQUEST["id"] ?>&trail=' + trail + '&front=' + front + '&period='+ i;
			
			$.ajax({
        	    type: 'POST',
        	    data: dataString,
        	    url: 'http://fintrack.com.au/loancal/updatecommission.php?id=<?php echo $_REQUEST["id"] ?>',
        	    async: false,
        	    success: function(data){
            	   Result=1; 
        	    }
			});
			}

			if(Result==1)
			{
				alert('Updated successfully.');
				window.location="lenders.php";
			}
		}
		
		</script>
           <script src="http://code.jquery.com/jquery-1.9.1.js"></script>