// when the page loads we create the gateway and send off
// a request for the authors as well as a request for the cart
// content. SRS has no notion of queued requests. If we sent 
// both requests to the same gateway simultaneously, one
// request would be abandoned. We'll work around this issue by
// setting up a second gateway and send one request to each 
// gateway. 
function body_onLoad() {
	objGateway = new gateway("serverpages/bookstore.cfm?");
	objSecondGateway = new gateway("serverpages/bookstore.cfm?");
	updateCart();
	objSecondGateway.setListener('authorsPacket_onReceive').setArguments( {type:'authors'} ).request();
	
	// we disable these buttons using script rather than the disabled attribute. 
	// This is because  of buggy button support in Opera 7. Buttons in O7 
	// can't be enabled or disabled via script. With this workaround, the
	// buttons in O7 remain enabled all the time.
	var button = document.getElementById('browseAddToCartButton');
	button.disabled = true;
	button = document.getElementById('searchAddToCartButton');
	button.disabled = true;
}



// this function loads the "author" select box with a query record 
// set. Here we are using an SRS function to make the job simpler. 
// The function attributes are: field ID, data, value column, 
// display column, prompt option, prompt value.
function authorsPacket_onReceive(packet) {
	var select = document.getElementById('selectAuthor');
	srsLoadSelect(select, packet, 'id', 'sortname', '&gt; Author &lt;', 0);
}



function selectAuthor_onChange(field) {
	var authorId = field.options[field.selectedIndex].value;
	objGateway.setListener('booksOfAuthorPacket_onReceive').setArguments( {type:'booksByAuthor',authorId:authorId} ).request();
}



// this function loads the "book" select box with a query record 
// set.
function booksOfAuthorPacket_onReceive(packet) {
	var select = document.getElementById('selectBook');
	srsLoadSelect(select, packet, 'id', 'title', '&gt; Title &lt;', 0);
	selectBook_onChange(select);
}



function selectBook_onChange(field) {
	var button = document.getElementById('browseAddToCartButton');
	var bookId = field.options[field.selectedIndex].value;
	button.disabled = ( bookId == 0 );
} 



function searchBox_onKeyUp(field) {
	objGateway.setListener('searchResultsPacket_onReceive').setArguments( {type:'search',key:field.value} ).request();
}



function browseAddToCartButton_onClick() {
	var select = document.getElementById('selectBook');
	var bookId = select.options[select.selectedIndex].value;
	addToCart(bookId);
}



function addToCart(bookId) {
	if ( bookId != 0 )
		objGateway.setListener('updateCart').setArguments( {type:'addToCart',bookId:bookId} ).request();
}



function updateCart() {
	// objGateway.setListener('cartPacket_onReceive').setArguments( {type:'cart'} ).request()
	// When this function is fired in response to the addToCart() method, the
	// line above throws an error in Opera 7. We work around the error by adding
	// this slight delay:
	setTimeout("objGateway.setListener('cartPacket_onReceive').setArguments( {type:'cart'} ).request()", 10);
}



// this function generates an HTML table of cart items.
function cartPacket_onReceive(packet) {
	var theTable = document.getElementById('tableCart');
	var tbody = document.createElement("tbody");
	var tr, td;
	
	if ( packet.id.length == 0 ) {
		tr = document.createElement("tr");
		td = document.createElement("td");
		td.innerHTML = "Your cart is empty.";
		tr.appendChild(td);
		td = document.createElement("td");
		td.innerHTML = "&nbsp;";
		tr.appendChild(td);
		td = document.createElement("td");
		td.innerHTML = "&nbsp;";
		tr.appendChild(td);
		tbody.appendChild(tr);
	}
	else		
		for ( var i = 0 ; i < packet.id.length; i++ ) {
			tr = document.createElement("tr");
			td = document.createElement("td");
			td.innerHTML = packet.quantity[i];
			tr.appendChild(td);
			td = document.createElement("td");
			td.innerHTML = packet.authorname[i];
			tr.appendChild(td);
			td = document.createElement("td");
			td.innerHTML = packet.title[i];
			tr.appendChild(td);
			tbody.appendChild(tr);
		}

	theTable.replaceChild(tbody, theTable.childNodes[1]); 

	var checkOutButton = document.getElementById('checkOutButton');
	checkOutButton.disabled = ( packet.id.length == 0 );
	var emptyCartButton = document.getElementById('emptyCartButton');
	emptyCartButton.disabled = ( packet.id.length == 0 );
}



// Here, we are loading a record set into a select box. 
// We would normally use the SRS helper function srsLoadSelect()
// to do this. However, in this case we want to group the 
// results in the select by author using the HTML optgroup
// element. So we'll write the code in full. 
function searchResultsPacket_onReceive(packet) {
	var select = document.getElementById('selectSearchResults');
	var j = select.childNodes.length;
	// remove existing nodes
	for ( var i = 0; i < j; i++ )
		select.removeChild(select.childNodes[0]);
	// if no results then disable the field
	if ( packet.id.length == 0) {
		var optgroup = document.createElement("optgroup"); 
		optgroup.label = "[No matches]";
		select.appendChild(optgroup);
		select.disabled = true;	
	}
	else {	
		// loop through all the results, starting a
		// new optgroup each time the author changes
		var currentAuthorId = packet.authorid[0];
		var optgroup = document.createElement("optgroup"); 
		optgroup.label = packet.sortname[0];
		for ( var i = 0 ; i < packet.id.length; i++ ) {
			if ( packet.authorid[i] != currentAuthorId ) {
				select.appendChild(optgroup);
				optgroup = document.createElement("optgroup"); 
				optgroup.label = packet.sortname[i];
				currentAuthorId = packet.authorid[i];
			}
			var option = document.createElement("option"); 
			option.innerHTML = packet.title[i];
			option.value = packet.id[i];
			optgroup.appendChild(option);
		}
		select.appendChild(optgroup);
		select.disabled = false;
	}
}



function selectSearchResults_onChange(field) {
	var button = document.getElementById('searchAddToCartButton');
	var bookId = field.options[field.selectedIndex].value;
	button.disabled = ( bookId == 0 );
}



function selectSearchResults_onDblClick(field) {
	var button = document.getElementById('searchAddToCartButton');
	var bookId = field.options[field.selectedIndex].value;
	button.disabled = ( bookId == 0 );
	if ( bookId != 0 ) 
		addToCart(bookId);
}



function searchAddToCartButton_onClick() {
	var select = document.getElementById('selectSearchResults');
	var bookId = select.options[select.selectedIndex].value;
	addToCart(bookId);
}



function emptyCartButton_onClick() {
	if ( confirm('Are you sure you want to empty your cart?') )
		objGateway.setListener('updateCart').setArguments( {type:'emptyCart'} ).request();
}
 
 
 
function checkOutButton_onClick() {
	alert('Since this is not a real Russian bookstore,\nyou can leave but you can never check out!');
}

