<html>
<head>
<link href="bookstaff.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
</head>
<title>
Parent Window
</title>

</head>
<body>

<table border='1' width='100%'>
<tr>
<td width="70%">

<div id="bookmenu">
<table border="1" width='100%' align='left'>
<?php
include("connect.php");
echo "<form method='post' name='form' action='giftinsert.php'>";
?>

<script language="JavaScript">

$().ready(function() {
	$("#memberName").autocomplete("memberauto.php", {
		width: 260,
		matchContains: true,
		//mustMatch: true,
		//minChars: 0,
		//multiple: true,
		//highlight: false,
		//multipleSeparator: ",",
		selectFirst: false
	});
});

var xmlHttp;
function GetXmlHttpObject(){
    
    var xmlHttp=null;
    try {
        
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     } catch (e) {
         
     //Internet Explorer
        try {
        
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
        
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    return xmlHttp;
}

function passmember(){
	
    xmlHttp=GetXmlHttpObject();
    if(xmlHttp==null) {
        
        alert ("Browser does not support HTTP Request");
        return;
    }
    var member=document.getElementById("memberName");
	//alert(description0.value);
    var url="getmember.php?";
    var postdate="value="+member.value;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    xmlHttp.send(postdate);

function stateChanged() { 
    
    if (xmlHttp.readyState=="4" || xmlHttp.readyState=="200"){
    
        var str_text=xmlHttp.responseText;
        if (str_text != ""){
            //alert("没有这个名字的数据");
			//}
        //else {
            
            str_text = str_text.split('|');
			
            document.getElementById("point").value = str_text[1];
        }
		}
    } 
}
</script>

<?php
$result = mysql_query("SELECT * FROM gift");
$num_results = mysql_num_rows($result);

echo "<tr>";
echo "<th>Gift Image</th>";
echo "<th>Gift Description</th>";
echo "</tr>";

for($i=0; $i<$num_results; $i++)
{
	$row = mysql_fetch_array($result);
	echo "<tr>";
	echo "<td rowspan='3' width='30%'>";
	echo "<img src=/bookstore/bookstaff/".$row["giftimage"]." border='0' width='200' height='200'></a>";
	echo "</td>";
	echo "<td width='70%'>Gift: <input type='hidden' name='itemName' value='".$row["itemName"]."'><b>".$row["giftname"]."</a></b></td>";
	echo "</tr>";
	
	echo "<tr>";	
	echo "<td>Redeem Points: <input type='hidden' name='redeempoint' value='".$row["redeempoint"]."'>".$row["redeempoint"]." Points</td>";
	echo "</tr>";
	
	echo "<tr>";
	//if($row['giftquantity']!=0){
	echo "<td align='right'><input type='button' value='Redeem' onclick='calculate(redeempoint.value, point.value, giftpoint, pointbalance)'></td>";
	//}else{
	//	echo "<td align='right'>Out of Stock!</td>";
	//}
	echo "</tr>";
}
?>
</table>
</td>

<script language="Javascript">
function calculate(a,b,c,d){
var f = c;
c.value = a;

var e = d;
e.value = +(b) - +(a);
}

function submitcheck(a,b,c){
	if((a=="")||(b==0)||(c<0)){
	alert("Please check redeem information.");
	}else{
	this.form.submit();
	}
}
</script>

<td width="30%" valign="top">
<?php
echo "<table width='100%'>";

echo "<tr>";
echo "<th colspan='2'>Member Data</th>";
echo "</tr>";

echo "<tr>";
echo "<td>Member Name:</td>";
echo "<td><input type='text' name='memberName' id='memberName' value='' onfocus='passmember()'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Current Point:</td>";
echo "<td><input type='text' name='point' id='point' value='0' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Gift Point:</td>";
echo "<td><input type='text' name='giftpoint' id='giftpoint' value='0' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Point Balance:</td>";
echo "<td><input type='text' name='pointbalance' id='pointbalance' value='0' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='2' align='center'><input type='button' name='Submit' value='Submit' onclick='submitcheck(point.value,giftpoint.value,pointbalance.value)'></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
?>
</td>

</tr>
</table>
</form>
</div>
</body>
</html>