$(document).ready(
	function() {
		var updateNeeded = false; // This will be set false as side effect of clicking update cart button 
			
		// Every time the page (document) is loaded and ready we check feedback from the PHP-building of the page.
		//	Highlight any order lines whose requested quantity was greater than what was available in stock
		var highLights = $('span.madeAvailable').length;
		if (highLights>0) {
			$('#messages').html('The <span class="titleRef">Qty.</span> of each highlighted item has been reduced to the amount we have available in stock.<br>Click the <span class="titleRef">Update cart</span> button to clear this message, or go directly to the checkout.').show();
		} 
		else { $('#messages').hide();
		} // end if else
		
		// set up event handlers		
				
		// Reject non-numeric input in quantity text input fields
		$('td.quantity input')
		.keypress(
			function (event)
				{	if (event.which && (event.which < 48 || event.which > 57)) 
					{event.preventDefault();}
				} // end anonymous function
			)	// end keypress parameters
		.blur(
			function () {						
				$("#messages").html("Quantity changed. Don't forget to update the cart before going to the checkout!.").show();			
				$("#ckoButton").attr('disabled','disabled');
				updateNeeded = true;
    		}
    	);
    	
    	// check there are no outstanding quatity updates need doing
    	// automatically triggering form submit on blur didn't seem to work
		// tell viewer we're waiting on PayPal
		$("#ppform").submit(
			function() {
				if (!updateNeeded) {
					$("#messages").html("Encrypted transaction details sent to PayPal.<br>Waiting for PayPal server ...").show();
					return true;
				}
				else {
					return false;
				}
			} // end anon function for paypal submit
		); // end submit parameters
				
	} // end anon function for ready
); // end ready

