function LookUpAndShowAvailability( LUStyle, LUSize, LUColour, AvailabilityMessage ) {
//	$("#unit-status").text( 'Checking stock availability...' );

	$( '#unit-status' ).text( '' );
	$( '#NotAvailAndDiscontinued' ).val( '' );
	$.ajax({
		type: 'POST',
		url: 'http://www.groundeffect.co.nz/fmapi/stocklevel.php',
		data: {
			style: LUStyle,
			size: ( LUSize == '-' ? '' : LUSize ),
			colour: ( LUColour == '-' ? '' : LUColour )
		},
		dataType: 'json',
		timeout: 15000,   // 15 seconds
		success: function( response ) {
			if ( response.found > 0 ) { // We found at least one matching product record.
				// Check that the selected style and size haven't changed while the stock level was being checked:
				// we'll only update the page if they are still the same, i.e. the values for which we were checking.
				var StyleMatch  = SelectedStyle == LUStyle;
				var SizeMatch   = ( SizeCodes[SelectedSize] == LUSize ) || ( ( SizeCodes[SelectedSize] == '-' ) && ( LUSize == '' ) );
				var ColourMatch = ( SelectedColour == LUColour ) || ( ( SelectedColour == '-' ) && ( LUColour == '' ) );
				if ( StyleMatch && SizeMatch && ColourMatch ) {
					 // We select one of two messages, depending on whether it will be displayed on the product detail page or the shopping cart page.
					$( '#unit-status' ).text( urldecode( response[ AvailabilityMessage ] ) );
					// Make a note of whether or not this combination has been sold out and discontinued.
					$( '#NotAvailAndDiscontinued' ).val( response.wwwNotAvailAndDiscontinued ? response.wwwNotAvailAndDiscontinued : '' );
				} else if ( SelectedSize == '' || SelectedColour == '' ) {
					 // Selected size or colour has changed, and one of them is now blank: clear the message and sold-out flag in this case.
					$("#unit-status").text( '' );
					$( '#NotAvailAndDiscontinued' ).val( '' );
				}
			}
		},
		error: function ( XMLHttpRequest, textStatus, errorThrown ) {
			if ( ( ! XMLHttpRequest ) || XMLHttpRequest.readyState == 0 || XMLHttpRequest.status == 0 ) {
				// Not actually an error: user probably navigated away.
			} else {
				// We do nothing here in the case of timeouts or other errors for this lookup, as the info is not super-critical.
				// alert( 'Error (LU&SA) : ' + textStatus );
			}
		}
	});
	
}

function UpdateAvailableSizes( LUStyle, LUColour ) {
	// Start by quickly marking all sizes as available before determining which ones aren't.
	$( '#Size option' ).each( function( i ) { $(this).removeClass( 'disabledOption' ).addClass( 'enabledOption' ); });
	// Now, if we have a specified colour, look up its availability in different sizes.
	if ( ( LUColour != '' ) && ( LUColour != '-' ) ) {
		$.ajax({
			type: 'POST',
			url: 'http://www.groundeffect.co.nz/products/size_availability.php',
			data: { 'style': LUStyle, 'colour': LUColour },
			dataType: 'json',
			success: function( response ) {
				if ( SelectedStyle == LUStyle && SelectedColour == LUColour ) {
					// Update size list to 'grey out' those that are unavailable.
					$( '#Size option' ).each( function( i ) {
						// var ThisSize = SizeCodes[$( this ).attr( 'value' )];   // This lookup translates (e.g.) "WS" to "S".
						var ThisSize = $( this ).attr( 'value' );
						if ( ( ThisSize == '-' ) || ( $.inArray( ThisSize, response.sizes ) >= 0 ) ) {
							// "-" is the value used for "N/A"
							// $( this ).attr( 'disabled', '' );
							$(this).removeClass( 'disabledOption' ).addClass( 'enabledOption' );
							// Nasty hack to force a style refresh in Safari.
							$( '#size-container' ).width( $( '#size-container' ).width() + 1 );
							$( '#size-container' ).width( $( '#size-container' ).width() - 1 );
						} else {
							//$( this ).attr( 'disabled', 'disabled' );
							$(this).removeClass( 'enabledOption' ).addClass( 'disabledOption' );
							// Nasty hack to force a style refresh in Safari.
							$( '#size-container' ).width( $( '#size-container' ).width() + 1 );
							$( '#size-container' ).width( $( '#size-container' ).width() - 1 );
							// Handle the case where the user has managed to select an unavailable combination before we made it un-selectable.
							if ( $( this ).attr( 'selected' )  ) {
								$( this ).attr( 'selected', '' );
								SelectedSize = '';
								UpdateAvailableColours( '<?= $style ?>', SelectedSize );
								ReportInvalidSizeColourCombination();
							}
						}
					});
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				if ( ( ! XMLHttpRequest ) || XMLHttpRequest.readyState == 0 || XMLHttpRequest.status == 0 ) return;  // Not actually an error: user probably navigated away.
				else alert( 'Error (UAS): ' + textStatus );
			}
		});
	}
}

function UpdateAvailableColours( LUStyle, LUSizeName ) {
//	var LUSize = SizeCodes[LUSizeName];   // This lookup translates (e.g.) "WS" to "S".
	var LUSize = LUSizeName;   // This lookup translates (e.g.) "WS" to "S".
	// Start by quickly marking all colours as available before determining which ones aren't.
	$( '#Colour option' ).each( function( i ) { $(this).removeClass( 'disabledOption' ).addClass( 'enabledOption' ); });
	// Now, if we have a specified size, look up its availability in different colours.
	if ( ( LUSize != '' ) && ( LUSize != '-' ) ) {
		$.ajax({
			type: 'POST',
			url: 'http://www.groundeffect.co.nz/products/colour_availability.php',
			data: { style: LUStyle, size: LUSize },
			dataType: 'json',
			success: function( response ) {
				if ( SelectedStyle == LUStyle && SelectedSize == LUSize ) {
					// Update colour list to 'grey out' those that are unavailable.
					$( '#Colour option' ).each( function( i ) {
						var ThisColour = $( this ).attr( 'value' );
						if ( ( ThisColour == '-' ) || ( $.inArray( ThisColour, response.colours ) >= 0 ) ) {
							// "-" is the value used for "N/A"
							// $( this ).attr( 'disabled', '' );
							$(this).removeClass( 'disabledOption' ).addClass( 'enabledOption' );
						} else {
							//$( this ).attr( 'disabled', 'disabled' );
							$(this).removeClass( 'enabledOption' ).addClass( 'disabledOption' );
							// Handle the case where the user has managed to select an unavailable combination before we made it un-selectable.
							if ( $( this ).attr( 'selected' )  ) {
								$( this ).attr( 'selected', '' );
								SelectedColour = '';
								UpdateAvailableSizes( '<?= $style ?>', SelectedColour );
								ReportInvalidSizeColourCombination();
							}
						}
					});
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				if ( ( ! XMLHttpRequest ) || XMLHttpRequest.readyState == 0 || XMLHttpRequest.status == 0 ) return;  // Not actually an error: user probably navigated away.
				else alert( 'Error (UAC): ' + textStatus );
			}
		});
	}
}

function urldecode( str ) {
	return unescape( str.replace( /\+/g, ' ' ) );
}

function ReportInvalidSizeColourCombination() {
	SimpleMessage( 'Sorry, not available in this size/colour combination.', 'Availability' );
}
