<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="80%">

<div id="bookmenu">
<table border="1" width='100%' height='1000pt' align='left'>
<tr valign='top'>
<td>
<?php
include("connect.php");
include("billID.php");
$billNo = $index;

echo "<form method='post' name='form' action='billinsert.php' onsubmit='getvalue(counter);' >";
echo "<table border='1' width='100%'>";
echo "<tr>";
echo "<th><font size='4px'>Bill - $billNo</font></th>";
echo "</tr>";

?>

<p align="left">
<a href="JavaScript:void(0);" id=addrow name=addrow type=button value=Add onClick="add()";">
<img src="Picture/add.gif" width="35" height="35" alt="Add Row">Add Item</a>

<a href="JavaScript:void(0);" id=refresh name=refresh type=button value=Cancel onClick="window.location.reload( false );">
<img src="Picture/error.gif" width="35" height="35" alt="Cancel Sales">Cancel Sales</a>
</p>

<tr valign='top'>
<td>
<table id="t1" border="1" width="100%">
<tr bgcolor='black' style='color:white'>
<th width='15%'>ISBN</th>
<th width='40%'>Description</th>
<th width='10%'>Unit Price</th>
<th width='10%'>Discount (%)</th>
<th width='10%'>Quantity</th>
<th width='10%'>Total</th>
<th width='5%'>Del</th>
</tr>
</table>
</td>
</tr>

<script language="JavaScript"> 
var k=0;
function getvalue(cv){
	var e = cv;
	e.value = k + 1;
}

function add(){ 
k++; 
if(k<30){
var row = t1.insertRow(t1.rows.length); 
var col = row.insertCell(0); 
//col.innerHTML = "<td width=15%><div id=ISBN"+k+"><input type=text size=18 readonly></div></td>"; 
col.innerHTML = "<td width=15%><input type=text size=15  id=ISBN"+k+" name=ISBN"+k+" value='' readonly>";
col = row.insertCell(1); 
col.innerHTML = "<td width=40%><input type=text size=40 id=description"+k+" name=description"+k+" value='' onfocus='postValue("+k+")'>";
col = row.insertCell(2);
col.innerHTML = "<td width=10%><input type=text size=6  id=price"+k+" name=price"+k+" value=0 readonly>";
//col.innerHTML = "<td width=10%><div id=price"+k+"></div></td>";
//<input type=text size=6  id=price"+k+" name=price"+k+" value=''></div>' onchange='multiply(quantity"+k+".value, price"+k+".value, subtotal"+k+", total, subtotal"+k+".value, total.value, qtotal, qtotal.value, oldquantity"+k+", oldquantity"+k+".value, discount"+k+".value);'>";
col = row.insertCell(3); 
col.innerHTML = "<td width=10%><input type=text size=8  id=discount"+k+" name=discount"+k+" value=0 onchange='multiply(quantity"+k+".value, price"+k+".value, subtotal"+k+", total, subtotal"+k+".value, total.value, qtotal, qtotal.value, oldquantity"+k+", oldquantity"+k+".value, discount"+k+".value)'>"; 
col = row.insertCell(4); 
col.innerHTML = "<td width=10%><input type=text size=8  id=quantity"+k+" name=quantity"+k+" value='0' onchange='multiply(quantity"+k+".value, price"+k+".value, subtotal"+k+", total, subtotal"+k+".value, total.value, qtotal, qtotal.value, oldquantity"+k+", oldquantity"+k+".value, discount"+k+".value);'>"; 
col = row.insertCell(5); 
col.innerHTML = "<td width=10%><input type=text size=6  type='text' id=subtotal"+k+" name=subtotal"+k+" value=0 readonly><input type='hidden' id=oldquantity"+k+" name=oldquantity"+k+" value=0>";
col = row.insertCell(6); 
col.innerHTML = "<td width=5%><a href='JavaScript:void(0)'; type=button value=Delete onclick='deleteRow(this, quantity"+k+".value, total, subtotal"+k+".value, total.value, qtotal, qtotal.value);'><img src=Picture/delete.gif width=25 height=25 border=0 alt=Delete Last Row></a></td>";

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

}
else{
alert("full");
} 
}
function del(){ 
var c = document.getElementsByName('tt'); 
for(var i=0; i<c.length; i++) 
if(c[i].checked) 
t1.deleteRow(i); 
} 
//onclick='deleteRow(this, quantity"+k+".value, total, subtotal"+k+".value, total.value, qtotal, qtotal.value)
function deleteRow(obj,q,t,l,v,y,h){
	var e = y;
	e.value = parseInt(h) - parseInt(q);
	
	var d = t;
	d.value = ((v-l).toFixed(2));
 
t1.deleteRow(obj.parentElement.parentElement.rowIndex); 
} 

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 postValue(z){
    var p = z;
	
    xmlHttp=GetXmlHttpObject();
    if(xmlHttp==null) {
        
        alert ("Browser does not support HTTP Request");
        return;
    }
    var description0=document.getElementById("description"+p+"");
	//alert(description0.value);
    var url="get.php?";
    var postdate="value="+description0.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("ISBN"+p+"").value = str_text[0];
            document.getElementById("price"+p+"").value = ((+(str_text[1])).toFixed(2));
			document.getElementById("quantity"+p+"").value = str_text[2];
			document.getElementById("subtotal"+p+"").value = ((+(str_text[1])).toFixed(2));
			document.getElementById("total").value = ((+(document.getElementById("total").value) + +(str_text[1])).toFixed(2));
			document.getElementById("qtotal").value = parseInt(document.getElementById("qtotal").value) + parseInt(str_text[2]);
			document.getElementById("oldquantity"+p+"").value = parseInt(str_text[2]);

        }
		}
    } 
}

//onchange='multiply(1quantity"+k+".value, 2price"+k+".value, 3subtotal"+k+", 4total, 
//5subtotal"+k+".value, 6total.value, 6qtotal, 7qtotal.value, 8oldquantity"+k+", 10oldquantity"+k+".value, discount"+k+".value);'>
function multiply(q,p,s,t,l,v,y,a,h,x,d){
if(d==0){
	var c = s;
	c.value=((q*p).toFixed(2));
	
	var d = t;
	d.value = ((v-l+(q*p)).toFixed(2));
}else{
	var c = s;
	c.value=((q*p*((100-d)/100)).toFixed(2));
	
	var tt = t;
	tt.value = ((v-l+(q*p*((100-d)/100))).toFixed(2));
}	
	
	var e = y;
	e.value = parseInt(a)+parseInt(q)-parseInt(x);
	
	var f = h;
	f.value = q;
	
	//alert(x);
}
</SCRIPT> 

</table>
</td>

<td width='20%'>
<?php
echo "<table border='1' width='' align='right'>";

echo "<tr>";
echo "<th style='background-color:green' colspan='2'><font size='3px'>Member Info (Optional)</font></th>";
echo "</tr>";

echo "<tr>";
echo "<th style='background-color:orange'>Member ID: </th>";
echo "<td style='background-color:orange'><input type='text' name='memberName' id='memberName' value='' onfocus='passmember()'></td>";
echo "</tr>";

echo "<tr>";
echo "<th style='background-color:orange'>Name: </th>";
echo "<td style='background-color:orange'><input type='text' name='name' id='name' value='' readonly></td>";
echo "</tr>";

echo "<tr>";
echo "<th style='background-color:orange'>Point: </th>";
echo "<td style='background-color:orange'><input type='text' name='point' id='point' value='' readonly></td>";
echo "</tr>";
?>

<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("name").value = str_text[0];
            document.getElementById("point").value = str_text[1];
        }
		}
    } 
}
</script>
</td>
</tr>

<tr>
<th colspan="2" style="background-color:green"><font size="5px"><input type="hidden" id="counter" name="counter" value="">Bill Total</font></th>
</tr>

<tr>
<th>Item in Bill: </th>
<td><input type='text' name='qtotal' size='9' style="font-size: 20" value='0' readonly></td>
</tr>

<tr>
<th style="background-color:orange">Bill Total: </th>
<td style="background-color:orange"><font size="5px">RM <input type='text' name='total' size='6' style="font-size: 20" value='0.00' readonly></font></td>
</tr>

<tr>
<th style="background-color:green">Cash: </th>
<td style="background-color:green"><font size="5px">RM <input type='text' name='cash' size='6' style="font-size: 20" value='0.00' onchange='changecash(total.value, cash.value, change)'></font></td>
</tr>

<tr>
<th style="background-color:orange">Change: </th>
<td style="background-color:orange"><font size="5px">RM <input type='text' name='change' size='6' style="font-size: 20" value='0.00' readonly></font></td>
</tr>

<tr>
<?php
echo "<td colspan='2'><center><input type='hidden' name='billNo' value='$billNo'><input type='button' name='Submit' value='Submit' onclick='submitcheck(total.value,cash.value,counter)'></center></td>";
?>
</tr>

<script language="JavaScript"> 
function changecash(t,c,ch){
	var e = ch;
	e.value = ((c-t).toFixed(2));
	//alert(x);
}

function submitcheck(t,v,cv){
	if((t==0.00)||(v==0.00)){
	alert("Please check your bill.");
	}else{
	
	var e = cv;
	e.value = k + 1;
	
	this.form.submit();
	}
}

</SCRIPT> 


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