<?php 
$login=mysql_connect('localhost', 'wauhaha_karaoke', 'cross123','wauhaha_karaoke');
	
mysql_selectDB("wauhaha_karaoke",$login);

$id = $_GET['id'];
$sd = $_POST['sd'];
$sh = $_POST['sh'];
$eh = $_POST['eh'];
$hours = $_POST['hours'];
$re = $_POST['re'];
$date = $_POST['date'];
$result = mysql_query("select * from leave2 where id='$id' ");
$row = mysql_fetch_array($result);
$lr = $row['l'];
$lr2 = $lr - $hours;
if($hours <=0){
			 echo "<script language='javascript'>alert('Time End cannot earlier than Time Start.')</script>";
			echo "<script language='javascript'>window.location='aplleave1.php?id=$id'</script>";die;
}
if($re ==""){
		    echo "<script language='javascript'>alert('Please Insert Completely.')</script>";
			echo "<script language='javascript'>window.location='aplleave1.php?id=$id'</script>";die;
}else{

$query = mysql_query("Insert Into lreason(id,sd,ed,sh,eh,h,r,Date,lr)VALUES('$id','$sd','$sd','$sh','$eh','$hours','$re','$date','$lr')");
$query2 = mysql_query("Update leave2 Set l='$lr2' where id='$id' ");
if(($query)&&($query2)){

			echo "<script language='javascript'>alert('Apply Success.')</script>";
			echo "<script language='javascript'>window.location='leave.php?id=$id'</script>";

}else 		echo "<script language='javascript'>alert('Apply Fail.')</script>";
			echo "<script language='javascript'>window.location='leave.php?id=$id'</script>";


}
	

?>