$(document).ready(function(){
	
	// if the flbar div is visible we want to show the bar
	if($('#flbar').is(':visible')){
		$('#topbar').show();
	}
	else{
		$('#topbar').hide();
	}
	
	// when the product form is submitted (when the submit button is clicked)
	$("form[name=insertProduct] input[name=submit]").click(function() {

		// get the form data
		var formData = $('form[name=insertProduct]').serialize();
		
		// pass an additional param to let the action page know we're submitting via ajax
		var formData = formData + '&ajax_request=1';
		
		// what container to fill with the results
		var resultID = '.insertResults';
					
		// make the ajax call
		$.ajax({
		type: "POST",
		url: "/_actions/cart/act_insertCartLine.cfm",
		data: formData,
		cache: false,
		beforeSend: function(){
			// show a loading message
			$(resultID).empty().addClass('message').append("<blink>Loading...</blink>");
		},
		success: function(data) {
				
				// append the message
				$(resultID).empty().addClass('message').append(data);
				
				// get info for the float bar
				$.ajax({
				type: "POST",
				url: "/shared/floatbar.cfm",
				cache: false,
				beforeSend: function(){
					$('#topbar').slideUp('fast');
				},					
				success: function(data) {
					// append the data to the results container
					$('#topbar').empty().append(data).slideDown();
				}
			});

		},
		error: function(msg){
			// append the error to the results container
			$(resultID).empty().addClass('message').append("Error: Please ensure all form fields are completed.");
		}
		});
		
		return false;
		
	});		
	
});