<?php
include("protect.php");
include("dbconnect.php");
extract($_REQUEST);
$uname=$_SESSION['uname'];
$msg="";
$rdate=date("d-m-Y");
$q1=mysql_query("select * from register where account='$uname'");
$r1=mysql_fetch_array($q1);
$amt=$r1['deposit'];
$q2=mysql_query("select * from admin where username='admin'");
$r2=mysql_fetch_array($q2);
$atm=$r2['amount'];
if(isset($btn))
{

	if($atm>$amount)
	{
		if($amt>=$amount)
		{
		mysql_query("update register set deposit=deposit-$amount where account='$uname'");
		mysql_query("update admin set amount=amount-$amount where username='admin'");
		$mq = mysql_query("select max(id) as maxid from event");
			$mr=mysql_fetch_array($mq);
			$id = $mr['maxid']+1;
			$n=mysql_query("insert into event(id,account,event,transfer_to,amount,rdate) values($id,'$uname','Debit','','$amount','$rdate')");
				
				?>
				<script language="javascript">
				alert("Receive Your Amount...");
				window.location.href="userhome.php";
				</script>
				<?php
				
	
		}
		else
		{
		$msg="Insufficient balance in your Account!!";
		}
	
	}
	else
	{
	$msg="Amount not Available!!!";
	}

}



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php include("title.php"); ?></title>

<link href="style.css" rel="stylesheet" type="text/css" />
<script language="javascript">
            function validate()
            {
			  
                if (document.form1.amount.value == "")
                {
                    alert("Enter the Amount");
                    document.form1.amount.focus();
                    return false;
                }
				
               
                return true;
            }
        </script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  
  <div class="sd"><!--<a href="index.php">Home</a>-->
    <div align="center"></div>
  </div>
  <p align="center" class="txt">Welcome <?php echo $r1['name']; ?></p>
  <table width="72%" height="285" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#9AA1B3">
    <tr>
      <td align="center" scope="col"><h2>Enter the Amount</h2></td>
    </tr>
    <tr>
      <td align="center" scope="col">
        Rs. 
        <input name="amount" type="text" class="inp" />
        <input name="btn" type="submit" class="butt1" value="Click here >>" onclick="return validate()" />
      </td>
    </tr>
  </table>
  <h2 align="center" class="msg"><?php echo $msg; ?></h2>
  <p>&nbsp;</p>
  <p align="center" class="sd"><a href="userhome.php">Back</a></p>
</form>
</body>
</html>
