/* products */
function refreshProductSelector()
{
	$("#product_selector").submit();	
}

/* samples */
function orderSample(description, part_no)
{
	// send request off
	$.post("ajax/order_sample.php", {
  		// request
		description: description,
		part_no: part_no
	}, function() {
		// callback
		window.location.href = "/samples-quotation";	
		return true;
	});
}

function updateSample(index, field, value)
{
	// send request off
	$.post("ajax/update_sample.php", {
  		// request
		index: index,
		field: field,
		value: value
	}, function() {
		// callback
		refreshSamplesTable();
		return true;
	});
}

function removeSample(index)
{
	// send request off
	$.post("ajax/remove_sample.php", {
  		// request
		index: index
	}, function() {
		// callback
		refreshSamplesTable();
		return true;
	});
}

function refreshSamplesTable()
{
	// populate the form fields with the existing address
	$.get("ajax/samples_list.php", function(data){
		// update div container to show table
		$("#samplesTable").html(data);
	});
}

function openCustomerPortal()
{
	alert("Coming soon.  Please try back later");
}

function clearProductSearchBox()
{
	$("#productSearchBox").val("");	
}

function fetchVideo(filename, title, description)
{
	// fetch in requested video HTML/javascriptT
	$.ajax({
		url:		"ajax/fetch_video.php?filename=" + filename + "&title=" + title + "&description=" + description,
		dataType: 	"html",
		success: 	function(data){
		// update div container and execute any javascript
			$("#productVideosViewer").html(data);
		}
	});
	
	return true;
}

// initialise any scripts upon document load
$(function() {

	// autocomplete search
	$( "#productSearchBox" ).autocomplete({
		minLength: 1,
		delay: 350,
		source: "ajax/search_results.php",
		select: function( event, ui ) {
			// clear search box
			clearProductSearchBox();
			// stop autocomplete from re-filling search box
			event.preventDefault();
			// redirect to appropriate page if necessary
			if (ui.item.href != "Too many results" && ui.item.href != "No results")
			{
				window.location.href = ui.item.href;
			}
		}
	})
	.data( "autocomplete" )._renderItem = function( ul, item ) {
		// show feedback if too many or no results
		if (item.href == "Too many results")
		{
			html = "<a href=\"#\"><span><strong>" + item.label + "</strong></span></a>";
		}
		else if (item.href == "No results")
		{
			html = "<a href=\"#\"><span><strong>" + item.label + "</strong></span></a>";	
		}
		else
		{
			// show actual link to new page link which will be redirected to if chosen
			html = "<a href='" + item.href +"'><img src='img/search/" + item.icon + "' /><span><strong>" + item.label + "</strong></span></a>";
		}
		
		return $( "<li></li>" )
		.data( "item.autocomplete", item )
			.append( html )
			.appendTo( ul );
	};
	
	// toggle for showing any generated errors - press SHIFT * on numpad
	$(document).keypress(function(event){
		if (event.which == 42 && event.shiftKey == true)
		{
			$("#hidden_errors").toggle();
		}
	});
});
